diff --git a/.gitignore b/.gitignore index 7cb2e66..cf16bff 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ crashlytics-build.properties /Assets/Plugins/RootMotion/PuppetMaster/_Integration /VMDRecords +/umamusume diff --git a/Assets/Plugins/I18N.meta b/Assets/Plugins/I18N.meta new file mode 100644 index 0000000..096540d --- /dev/null +++ b/Assets/Plugins/I18N.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 497e7785cb8817945ad032b3ad69da45 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/I18N.CJK.dll b/Assets/Plugins/I18N/I18N.CJK.dll similarity index 100% rename from Assets/Plugins/I18N.CJK.dll rename to Assets/Plugins/I18N/I18N.CJK.dll diff --git a/Assets/Plugins/I18N.CJK.dll.meta b/Assets/Plugins/I18N/I18N.CJK.dll.meta similarity index 100% rename from Assets/Plugins/I18N.CJK.dll.meta rename to Assets/Plugins/I18N/I18N.CJK.dll.meta diff --git a/Assets/Plugins/I18N.dll b/Assets/Plugins/I18N/I18N.dll similarity index 100% rename from Assets/Plugins/I18N.dll rename to Assets/Plugins/I18N/I18N.dll diff --git a/Assets/Plugins/I18N.dll.meta b/Assets/Plugins/I18N/I18N.dll.meta similarity index 100% rename from Assets/Plugins/I18N.dll.meta rename to Assets/Plugins/I18N/I18N.dll.meta diff --git a/Assets/Plugins/LZ4.meta b/Assets/Plugins/LZ4.meta new file mode 100644 index 0000000..dd3180e --- /dev/null +++ b/Assets/Plugins/LZ4.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dd64793f28865c14b8f230db22e5c0ab +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/LZ4/K4os.Compression.LZ4.Streams.dll b/Assets/Plugins/LZ4/K4os.Compression.LZ4.Streams.dll new file mode 100644 index 0000000..f34454e Binary files /dev/null and b/Assets/Plugins/LZ4/K4os.Compression.LZ4.Streams.dll differ diff --git a/Assets/Plugins/LZ4/K4os.Compression.LZ4.Streams.dll.meta b/Assets/Plugins/LZ4/K4os.Compression.LZ4.Streams.dll.meta new file mode 100644 index 0000000..1d994ce --- /dev/null +++ b/Assets/Plugins/LZ4/K4os.Compression.LZ4.Streams.dll.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: 8fa90c95ae974744d86e11e1cd793c66 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/LZ4/K4os.Compression.LZ4.dll b/Assets/Plugins/LZ4/K4os.Compression.LZ4.dll new file mode 100644 index 0000000..d9bfc85 Binary files /dev/null and b/Assets/Plugins/LZ4/K4os.Compression.LZ4.dll differ diff --git a/Assets/Plugins/LZ4/K4os.Compression.LZ4.dll.meta b/Assets/Plugins/LZ4/K4os.Compression.LZ4.dll.meta new file mode 100644 index 0000000..f833930 --- /dev/null +++ b/Assets/Plugins/LZ4/K4os.Compression.LZ4.dll.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: 38a6e9a942b6b8948a1a92be95c5b15c +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/LZ4/K4os.Hash.xxHash.dll b/Assets/Plugins/LZ4/K4os.Hash.xxHash.dll new file mode 100644 index 0000000..cd0af6d Binary files /dev/null and b/Assets/Plugins/LZ4/K4os.Hash.xxHash.dll differ diff --git a/Assets/Plugins/LZ4/K4os.Hash.xxHash.dll.meta b/Assets/Plugins/LZ4/K4os.Hash.xxHash.dll.meta new file mode 100644 index 0000000..338b10e --- /dev/null +++ b/Assets/Plugins/LZ4/K4os.Hash.xxHash.dll.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: 607fe1db3678bac46bad918b3532e867 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/LZ4/System.Buffers.dll b/Assets/Plugins/LZ4/System.Buffers.dll new file mode 100644 index 0000000..c0970c0 Binary files /dev/null and b/Assets/Plugins/LZ4/System.Buffers.dll differ diff --git a/Assets/Plugins/LZ4/System.Buffers.dll.meta b/Assets/Plugins/LZ4/System.Buffers.dll.meta new file mode 100644 index 0000000..268bc98 --- /dev/null +++ b/Assets/Plugins/LZ4/System.Buffers.dll.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: a7682400267a5244abeb01b25144548f +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/LZ4/System.Memory.dll b/Assets/Plugins/LZ4/System.Memory.dll new file mode 100644 index 0000000..1e6aef8 Binary files /dev/null and b/Assets/Plugins/LZ4/System.Memory.dll differ diff --git a/Assets/Plugins/LZ4/System.Memory.dll.meta b/Assets/Plugins/LZ4/System.Memory.dll.meta new file mode 100644 index 0000000..e041789 --- /dev/null +++ b/Assets/Plugins/LZ4/System.Memory.dll.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: 4f44e1b4f3db0674dae250f26d7e82c9 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/LZ4/System.Runtime.CompilerServices.Unsafe.dll b/Assets/Plugins/LZ4/System.Runtime.CompilerServices.Unsafe.dll new file mode 100644 index 0000000..491a80a Binary files /dev/null and b/Assets/Plugins/LZ4/System.Runtime.CompilerServices.Unsafe.dll differ diff --git a/Assets/Plugins/LZ4/System.Runtime.CompilerServices.Unsafe.dll.meta b/Assets/Plugins/LZ4/System.Runtime.CompilerServices.Unsafe.dll.meta new file mode 100644 index 0000000..1fd62fd --- /dev/null +++ b/Assets/Plugins/LZ4/System.Runtime.CompilerServices.Unsafe.dll.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: fd120d11247acd240b59ca0703afade4 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + Windows Store Apps: WindowsStoreApps + second: + enabled: 0 + settings: + CPU: AnyCPU + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Sqlite.meta b/Assets/Plugins/Sqlite.meta new file mode 100644 index 0000000..fe8ef5e --- /dev/null +++ b/Assets/Plugins/Sqlite.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d3385e4d710e579459941d2fa931686b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Mono.Data.Sqlite.dll b/Assets/Plugins/Sqlite/Mono.Data.Sqlite.dll similarity index 100% rename from Assets/Plugins/Mono.Data.Sqlite.dll rename to Assets/Plugins/Sqlite/Mono.Data.Sqlite.dll diff --git a/Assets/Plugins/Mono.Data.Sqlite.dll.meta b/Assets/Plugins/Sqlite/Mono.Data.Sqlite.dll.meta similarity index 100% rename from Assets/Plugins/Mono.Data.Sqlite.dll.meta rename to Assets/Plugins/Sqlite/Mono.Data.Sqlite.dll.meta diff --git a/Assets/Plugins/Mono.Data.dll b/Assets/Plugins/Sqlite/Mono.Data.dll similarity index 100% rename from Assets/Plugins/Mono.Data.dll rename to Assets/Plugins/Sqlite/Mono.Data.dll diff --git a/Assets/Plugins/Mono.Data.dll.meta b/Assets/Plugins/Sqlite/Mono.Data.dll.meta similarity index 100% rename from Assets/Plugins/Mono.Data.dll.meta rename to Assets/Plugins/Sqlite/Mono.Data.dll.meta diff --git a/Assets/Plugins/sqlite3.def b/Assets/Plugins/Sqlite/sqlite3.def similarity index 100% rename from Assets/Plugins/sqlite3.def rename to Assets/Plugins/Sqlite/sqlite3.def diff --git a/Assets/Plugins/sqlite3.def.meta b/Assets/Plugins/Sqlite/sqlite3.def.meta similarity index 100% rename from Assets/Plugins/sqlite3.def.meta rename to Assets/Plugins/Sqlite/sqlite3.def.meta diff --git a/Assets/Plugins/sqlite3.dll b/Assets/Plugins/Sqlite/sqlite3.dll similarity index 100% rename from Assets/Plugins/sqlite3.dll rename to Assets/Plugins/Sqlite/sqlite3.dll diff --git a/Assets/Plugins/sqlite3.dll.meta b/Assets/Plugins/Sqlite/sqlite3.dll.meta similarity index 100% rename from Assets/Plugins/sqlite3.dll.meta rename to Assets/Plugins/Sqlite/sqlite3.dll.meta diff --git a/Assets/Scenes/Version2.unity b/Assets/Scenes/Version2.unity index 119b79b..6b4467d 100644 --- a/Assets/Scenes/Version2.unity +++ b/Assets/Scenes/Version2.unity @@ -1682,13 +1682,14 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 844924682} + - {fileID: 2073237536} m_Father: {fileID: 1704906816} m_RootOrder: 17 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 141.5, y: -410} - m_SizeDelta: {x: 283, y: 50} + m_AnchoredPosition: {x: 141.5, y: -1071.7834} + m_SizeDelta: {x: 283, y: 100} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &93905315 MonoBehaviour: @@ -17114,7 +17115,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 283, y: -75} + m_AnchoredPosition: {x: 283, y: -25} m_SizeDelta: {x: 283, y: 50} m_Pivot: {x: 1, y: 0.5} --- !u!114 &844924683 @@ -29470,6 +29471,139 @@ MonoBehaviour: m_isRichTextEditingAllowed: 0 m_LineLimit: 0 m_InputValidator: {fileID: 0} +--- !u!1 &1516377149 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1516377150} + - component: {fileID: 1516377153} + - component: {fileID: 1516377152} + - component: {fileID: 1516377151} + m_Layer: 5 + m_Name: Button + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1516377150 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1516377149} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 1618706696} + m_Father: {fileID: 2073237536} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1516377151 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1516377149} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_WrapAround: 0 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_SelectedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_SelectedTrigger: Selected + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 1516377152} + m_OnClick: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1564959567} + m_TargetAssemblyTypeName: UmaViewerUI, umamusume + m_MethodName: UpdateGameDB + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 1 + m_CallState: 2 +--- !u!114 &1516377152 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1516377149} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &1516377153 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1516377149} + m_CullTransparentMesh: 1 --- !u!224 &1525688469 stripped RectTransform: m_CorrespondingSourceObject: {fileID: 22448498, guid: 916ee089a0d7b63419075f91e1c657ec, @@ -31082,6 +31216,85 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1618706695 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1618706696} + - component: {fileID: 1618706698} + - component: {fileID: 1618706697} + m_Layer: 5 + m_Name: Text + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1618706696 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1618706695} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 1516377150} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1618706697 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1618706695} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_FontData: + m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_FontSize: 25 + m_FontStyle: 0 + m_BestFit: 0 + m_MinSize: 1 + m_MaxSize: 40 + m_Alignment: 4 + m_AlignByGeometry: 0 + m_RichText: 1 + m_HorizontalOverflow: 0 + m_VerticalOverflow: 0 + m_LineSpacing: 1 + m_Text: Update Database +--- !u!222 &1618706698 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1618706695} + m_CullTransparentMesh: 1 --- !u!1 &1622519936 GameObject: m_ObjectHideFlags: 0 @@ -39893,6 +40106,82 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2062531603} m_CullTransparentMesh: 1 +--- !u!1 &2073237535 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2073237536} + - component: {fileID: 2073237538} + - component: {fileID: 2073237537} + m_Layer: 5 + m_Name: UpdateDatabase + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &2073237536 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2073237535} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 1516377150} + m_Father: {fileID: 93905314} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 283, y: -75} + m_SizeDelta: {x: 283, y: 50} + m_Pivot: {x: 1, y: 0.5} +--- !u!114 &2073237537 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2073237535} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0, g: 0, b: 0, a: 0.19607843} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &2073237538 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2073237535} + m_CullTransparentMesh: 1 --- !u!1 &2073676005 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/BaseNcoding.meta b/Assets/Scripts/BaseNcoding.meta new file mode 100644 index 0000000..1cf72b5 --- /dev/null +++ b/Assets/Scripts/BaseNcoding.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8efe8ce6fc7c3d84db812df72e79c7ce +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/Base.cs b/Assets/Scripts/BaseNcoding/Base.cs new file mode 100644 index 0000000..8dd9dfc --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base.cs @@ -0,0 +1,184 @@ +using System; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace BaseNcoding +{ + public abstract class Base + { + public uint CharsCount { get; } + + public double BitsPerChars => (double)BlockBitsCount / BlockCharsCount; + + public int BlockBitsCount { get; protected set; } + + public int BlockCharsCount { get; protected set; } + + public string Alphabet { get; } + + public char Special { get; } + + public abstract bool HasSpecial { get; } + + public Encoding Encoding { get; set; } + + public bool Parallel { get; set; } + + protected readonly int[] InvAlphabet; + + public Base(uint charsCount, string alphabet, char special, Encoding encoding = null, bool parallel = false) + { + if (alphabet.Length != charsCount) + throw new ArgumentException($"Base string should contain {charsCount} chars"); + + for (int i = 0; i < charsCount; i++) + for (int j = i + 1; j < charsCount; j++) + if (alphabet[i] == alphabet[j]) + throw new ArgumentException("Base string should contain distinct chars"); + + if (alphabet.Contains(special)) + throw new ArgumentException("Base string should not contain special char"); + + CharsCount = charsCount; + Alphabet = alphabet; + Special = special; + int bitsPerChar = LogBase2(charsCount); + BlockBitsCount = Lcm(bitsPerChar, 8); + BlockCharsCount = BlockBitsCount / bitsPerChar; + + InvAlphabet = new int[Alphabet.Max() + 1]; + + for (int i = 0; i < InvAlphabet.Length; i++) + InvAlphabet[i] = -1; + + for (int i = 0; i < charsCount; i++) + InvAlphabet[Alphabet[i]] = i; + + Encoding = encoding ?? Encoding.UTF8; + Parallel = parallel; + } + + public virtual string EncodeString(string data) + { + return Encode(Encoding.GetBytes(data)); + } + + public abstract string Encode(byte[] data); + + public string DecodeToString(string data) + { + return Encoding.GetString(Decode(Regex.Replace(data, @"\r\n?|\n", ""))); + } + + public abstract byte[] Decode(string data); + + /// + /// From: http://stackoverflow.com/a/600306/1046374 + /// + /// + /// + public static bool IsPowerOf2(uint x) + { + uint xint = x; + if (x - xint != 0) + return false; + + return (xint & (xint - 1)) == 0; + } + + /// + /// From: http://stackoverflow.com/a/13569863/1046374 + /// + public static int Lcm(int a, int b) + { + int num1, num2; + if (a > b) + { + num1 = a; + num2 = b; + } + else + { + num1 = b; + num2 = a; + } + + for (int i = 1; i <= num2; i++) + { + int mult = num1 * i; + if (mult % num2 == 0) + return mult; + } + + return num2; + } + + private static int LogBase2(uint x) + { + int r = 0; + while ((x >>= 1) != 0) + r++; + return r; + } + + private static int LogBaseN(uint x, uint n) + { + int r = 0; + while ((x /= n) != 0) + r++; + return r; + } + + public static int GetOptimalBitsCount2(uint charsCount, out uint charsCountInBits, + uint maxBitsCount = 64, bool base2BitsCount = false) + { + int result = 0; + charsCountInBits = 0; + int n1 = LogBase2(charsCount); + double charsCountLog = Math.Log(2, charsCount); + double maxRatio = 0; + + for (int n = n1; n <= maxBitsCount; n++) + { + if (base2BitsCount && n % 8 != 0) + continue; + + uint l1 = (uint)Math.Ceiling(n * charsCountLog); + double ratio = (double)n / l1; + if (ratio > maxRatio) + { + maxRatio = ratio; + result = n; + charsCountInBits = l1; + } + } + + return result; + } + + protected static int GetOptimalBitsCount(uint charsCount, out uint charsCountInBits, + uint maxBitsCount = 64, uint radix = 2) + { + int result = 0; + charsCountInBits = 0; + int n0 = LogBaseN(charsCount, radix); + double charsCountLog = Math.Log(radix, charsCount); + double maxRatio = 0; + + for (int n = n0; n <= maxBitsCount; n++) + { + uint k = (uint)Math.Ceiling(n * charsCountLog); + double ratio = (double)n / k; + if (ratio > maxRatio) + { + maxRatio = ratio; + result = n; + charsCountInBits = k; + } + } + + return result; + } + } +} diff --git a/Assets/Scripts/BaseNcoding/Base.cs.meta b/Assets/Scripts/BaseNcoding/Base.cs.meta new file mode 100644 index 0000000..904a5d4 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dddac6b0e9d7d75489cf3110fcc3a611 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/Base256.cs b/Assets/Scripts/BaseNcoding/Base256.cs new file mode 100644 index 0000000..8a90ef2 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base256.cs @@ -0,0 +1,41 @@ +using System.Text; + +namespace BaseNcoding +{ + public class Base256 : Base + { + public const string DefaultAlphabet = "!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁł"; + public const char DefaultSpecial = (char)0; + + public override bool HasSpecial => false; + + public Base256(string alphabet = DefaultAlphabet, char special = DefaultSpecial, + Encoding textEncoding = null) + : base(256, alphabet, special, textEncoding) + { + } + + public override string Encode(byte[] data) + { + var result = new char[data.Length]; + + for (int i = 0; i < data.Length; i++) + result[i] = Alphabet[data[i]]; + + return new string(result); + } + + public override byte[] Decode(string data) + { + unchecked + { + byte[] result = new byte[data.Length]; + + for (int i = 0; i < data.Length; i++) + result[i] = (byte)InvAlphabet[data[i]]; + + return result; + } + } + } +} diff --git a/Assets/Scripts/BaseNcoding/Base256.cs.meta b/Assets/Scripts/BaseNcoding/Base256.cs.meta new file mode 100644 index 0000000..5d61cf6 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base256.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b84ecd8bc32acb7448d64c9bb045948d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/Base32.cs b/Assets/Scripts/BaseNcoding/Base32.cs new file mode 100644 index 0000000..33b70ff --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base32.cs @@ -0,0 +1,203 @@ +using System.Text; + +namespace BaseNcoding +{ + public class Base32 : Base + { + public const string DefaultAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + public const char DefaultSpecial = '>'; + + public override bool HasSpecial => true; + + public Base32(string alphabet = DefaultAlphabet, char special = DefaultSpecial, Encoding textEncoding = null) + : base(32, alphabet, special, textEncoding) + { + } + + public override string Encode(byte[] data) + { + int dataLength = data.Length; + StringBuilder result = new StringBuilder((dataLength + 4) / 5 * 8); + + byte x1, x2; + int i; + + int length5 = (dataLength / 5) * 5; + for (i = 0; i < length5; i += 5) + { + x1 = data[i]; + result.Append(Alphabet[x1 >> 3]); + + x2 = data[i + 1]; + result.Append(Alphabet[((x1 << 2) & 0x1C) | (x2 >> 6)]); + result.Append(Alphabet[(x2 >> 1) & 0x1F]); + + x1 = data[i + 2]; + result.Append(Alphabet[((x2 << 4) & 0x10) | (x1 >> 4)]); + + x2 = data[i + 3]; + result.Append(Alphabet[((x1 << 1) & 0x1E) | (x2 >> 7)]); + result.Append(Alphabet[(x2 >> 2) & 0x1F]); + + x1 = data[i + 4]; + result.Append(Alphabet[((x2 << 3) & 0x18) | (x1 >> 5)]); + result.Append(Alphabet[x1 & 0x1F]); + } + + switch (dataLength - length5) + { + case 1: + x1 = data[i]; + result.Append(Alphabet[x1 >> 3]); + result.Append(Alphabet[(x1 << 2) & 0x1C]); + + result.Append(Special, 6); + break; + case 2: + x1 = data[i]; + result.Append(Alphabet[x1 >> 3]); + x2 = data[i + 1]; + result.Append(Alphabet[((x1 << 2) & 0x1C) | (x2 >> 6)]); + result.Append(Alphabet[(x2 >> 1) & 0x1F]); + result.Append(Alphabet[(x2 << 4) & 0x10]); + + result.Append(Special, 4); + break; + case 3: + x1 = data[i]; + result.Append(Alphabet[x1 >> 3]); + x2 = data[i + 1]; + result.Append(Alphabet[((x1 << 2) & 0x1C) | (x2 >> 6)]); + result.Append(Alphabet[(x2 >> 1) & 0x1F]); + x1 = data[i + 2]; + result.Append(Alphabet[((x2 << 4) & 0x10) | (x1 >> 4)]); + result.Append(Alphabet[(x1 << 1) & 0x1E]); + + result.Append(Special, 3); + break; + case 4: + x1 = data[i]; + result.Append(Alphabet[x1 >> 3]); + x2 = data[i + 1]; + result.Append(Alphabet[((x1 << 2) & 0x1C) | (x2 >> 6)]); + result.Append(Alphabet[(x2 >> 1) & 0x1F]); + x1 = data[i + 2]; + result.Append(Alphabet[((x2 << 4) & 0x10) | (x1 >> 4)]); + x2 = data[i + 3]; + result.Append(Alphabet[((x1 << 1) & 0x1E) | (x2 >> 7)]); + result.Append(Alphabet[(x2 >> 2) & 0x1F]); + result.Append(Alphabet[(x2 << 3) & 0x18]); + + result.Append(Special); + break; + } + + return result.ToString(); + } + + public override byte[] Decode(string data) + { + unchecked + { + if (string.IsNullOrEmpty(data)) + return new byte[0]; + + int additionalBytes = 0, diff = 0, tempLen = 0; + + + int lastSpecialInd = data.Length; + while (data[lastSpecialInd - 1] == Special) + lastSpecialInd--; + int tailLength = data.Length - lastSpecialInd; + + switch (tailLength) + { + case 6: + additionalBytes = 4; + break; + case 4: + additionalBytes = 3; + break; + case 3: + additionalBytes = 2; + break; + case 1: + additionalBytes = 1; + break; + } + + diff = tailLength - additionalBytes; + tailLength = additionalBytes; + tempLen = data.Length - diff; + + byte[] result = new byte[(tempLen + 7) / 8 * 5 - tailLength]; + int length5 = result.Length / 5 * 5; + int x1, x2, x3, x4, x5, x6, x7, x8; + + int i, srcInd = 0; + for (i = 0; i < length5; i += 5) + { + x1 = InvAlphabet[data[srcInd++]]; + x2 = InvAlphabet[data[srcInd++]]; + x3 = InvAlphabet[data[srcInd++]]; + x4 = InvAlphabet[data[srcInd++]]; + x5 = InvAlphabet[data[srcInd++]]; + x6 = InvAlphabet[data[srcInd++]]; + x7 = InvAlphabet[data[srcInd++]]; + x8 = InvAlphabet[data[srcInd++]]; + + result[i] = (byte)((x1 << 3) | ((x2 >> 2) & 0x07)); + result[i + 1] = (byte)((x2 << 6) | ((x3 << 1) & 0x3E) | ((x4 >> 4) & 0x01)); + result[i + 2] = (byte)((x4 << 4) | ((x5 >> 1) & 0xF)); + result[i + 3] = (byte)((x5 << 7) | ((x6 << 2) & 0x7C) | ((x7 >> 3) & 0x03)); + result[i + 4] = (byte)((x7 << 5) | (x8 & 0x1F)); + } + + switch (tailLength) + { + case 4: + x1 = InvAlphabet[data[srcInd++]]; + x2 = InvAlphabet[data[srcInd++]]; + result[i] = (byte)((x1 << 3) | ((x2 >> 2) & 0x07)); + break; + case 3: + x1 = InvAlphabet[data[srcInd++]]; + x2 = InvAlphabet[data[srcInd++]]; + x3 = InvAlphabet[data[srcInd++]]; + x4 = InvAlphabet[data[srcInd++]]; + + result[i] = (byte)((x1 << 3) | ((x2 >> 2) & 0x07)); + result[i + 1] = (byte)((x2 << 6) | ((x3 << 1) & 0x3E) | ((x4 >> 4) & 0x01)); + break; + case 2: + x1 = InvAlphabet[data[srcInd++]]; + x2 = InvAlphabet[data[srcInd++]]; + x3 = InvAlphabet[data[srcInd++]]; + x4 = InvAlphabet[data[srcInd++]]; + x5 = InvAlphabet[data[srcInd++]]; + + result[i] = (byte)((x1 << 3) | ((x2 >> 2) & 0x07)); + result[i + 1] = (byte)((x2 << 6) | ((x3 << 1) & 0x3E) | ((x4 >> 4) & 0x01)); + result[i + 2] = (byte)((x4 << 4) | ((x5 >> 1) & 0xF)); + break; + case 1: + x1 = InvAlphabet[data[srcInd++]]; + x2 = InvAlphabet[data[srcInd++]]; + x3 = InvAlphabet[data[srcInd++]]; + x4 = InvAlphabet[data[srcInd++]]; + x5 = InvAlphabet[data[srcInd++]]; + x6 = InvAlphabet[data[srcInd++]]; + x7 = InvAlphabet[data[srcInd++]]; + + result[i] = (byte)((x1 << 3) | ((x2 >> 2) & 0x07)); + result[i + 1] = (byte)((x2 << 6) | ((x3 << 1) & 0x3E) | ((x4 >> 4) & 0x01)); + result[i + 2] = (byte)((x4 << 4) | ((x5 >> 1) & 0xF)); + result[i + 3] = (byte)((x5 << 7) | ((x6 << 2) & 0x7C) | ((x7 >> 3) & 0x03)); + break; + } + + return result; + } + } + } +} diff --git a/Assets/Scripts/BaseNcoding/Base32.cs.meta b/Assets/Scripts/BaseNcoding/Base32.cs.meta new file mode 100644 index 0000000..b799a44 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base32.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 126c83fbe81b32444ade8272d29789b1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/Base64.cs b/Assets/Scripts/BaseNcoding/Base64.cs new file mode 100644 index 0000000..302be4f --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base64.cs @@ -0,0 +1,170 @@ +using System; +using System.Text; + +namespace BaseNcoding +{ + public class Base64 : Base + { + public const string DefaultAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + public const char DefaultSpecial = '='; + + public override bool HasSpecial => true; + + public Base64(string alphabet = DefaultAlphabet, char special = DefaultSpecial, + Encoding textEncoding = null, bool parallel = false) + : base(64, alphabet, special, textEncoding, parallel) + { + } + + public override string Encode(byte[] data) + { + int resultLength = (data.Length + 2) / 3 * 4; + char[] result = new char[resultLength]; + + int length3 = data.Length / 3; + if (!Parallel) + { + EncodeBlock(data, result, 0, length3); + } + else + { + int processorCount = Math.Min(length3, Environment.ProcessorCount); + System.Threading.Tasks.Parallel.For(0, processorCount, i => + { + int beginInd = i * length3 / processorCount; + int endInd = (i + 1) * length3 / processorCount; + EncodeBlock(data, result, beginInd, endInd); + }); + } + + int ind; + int x1, x2; + int srcInd, dstInd; + switch (data.Length - length3 * 3) + { + case 1: + ind = length3; + srcInd = ind * 3; + dstInd = ind * 4; + x1 = data[srcInd]; + result[dstInd] = Alphabet[x1 >> 2]; + result[dstInd + 1] = Alphabet[(x1 << 4) & 0x30]; + result[dstInd + 2] = Special; + result[dstInd + 3] = Special; + break; + case 2: + ind = length3; + srcInd = ind * 3; + dstInd = ind * 4; + x1 = data[srcInd]; + x2 = data[srcInd + 1]; + result[dstInd] = Alphabet[x1 >> 2]; + result[dstInd + 1] = Alphabet[((x1 << 4) & 0x30) | (x2 >> 4)]; + result[dstInd + 2] = Alphabet[(x2 << 2) & 0x3C]; + result[dstInd + 3] = Special; + break; + } + + return new string(result); + } + + public override byte[] Decode(string data) + { + unchecked + { + if (string.IsNullOrEmpty(data)) + return new byte[0]; + + int lastSpecialInd = data.Length; + while (data[lastSpecialInd - 1] == Special) + lastSpecialInd--; + int tailLength = data.Length - lastSpecialInd; + + int resultLength = (data.Length + 3) / 4 * 3 - tailLength; + byte[] result = new byte[resultLength]; + + int length4 = (data.Length - tailLength) / 4; + if (!Parallel) + { + DecodeBlock(data, result, 0, length4); + } + else + { + int processorCount = Math.Min(length4, Environment.ProcessorCount); + System.Threading.Tasks.Parallel.For(0, processorCount, i => + { + int beginInd = i * length4 / processorCount; + int endInd = (i + 1) * length4 / processorCount; + DecodeBlock(data, result, beginInd, endInd); + }); + } + + int ind; + int x1, x2, x3; + int srcInd, dstInd; + switch (tailLength) + { + case 2: + ind = length4; + srcInd = ind * 4; + dstInd = ind * 3; + x1 = InvAlphabet[data[srcInd]]; + x2 = InvAlphabet[data[srcInd + 1]]; + result[dstInd] = (byte)((x1 << 2) | ((x2 >> 4) & 0x3)); + break; + case 1: + ind = length4; + srcInd = ind * 4; + dstInd = ind * 3; + x1 = InvAlphabet[data[srcInd]]; + x2 = InvAlphabet[data[srcInd + 1]]; + x3 = InvAlphabet[data[srcInd + 2]]; + result[dstInd] = (byte)((x1 << 2) | ((x2 >> 4) & 0x3)); + result[dstInd + 1] = (byte)((x2 << 4) | ((x3 >> 2) & 0xF)); + break; + } + + return result; + } + } + + private void EncodeBlock(byte[] src, char[] dst, int beginInd, int endInd) + { + for (int ind = beginInd; ind < endInd; ind++) + { + int srcInd = ind * 3; + int dstInd = ind * 4; + + byte x1 = src[srcInd]; + byte x2 = src[srcInd + 1]; + byte x3 = src[srcInd + 2]; + + dst[dstInd] = Alphabet[x1 >> 2]; + dst[dstInd + 1] = Alphabet[((x1 << 4) & 0x30) | (x2 >> 4)]; + dst[dstInd + 2] = Alphabet[((x2 << 2) & 0x3C) | (x3 >> 6)]; + dst[dstInd + 3] = Alphabet[x3 & 0x3F]; + } + } + + private void DecodeBlock(string src, byte[] dst, int beginInd, int endInd) + { + unchecked + { + for (int ind = beginInd; ind < endInd; ind++) + { + int srcInd = ind * 4; + int dstInd = ind * 3; + + int x1 = InvAlphabet[src[srcInd]]; + int x2 = InvAlphabet[src[srcInd + 1]]; + int x3 = InvAlphabet[src[srcInd + 2]]; + int x4 = InvAlphabet[src[srcInd + 3]]; + + dst[dstInd] = (byte)((x1 << 2) | ((x2 >> 4) & 0x3)); + dst[dstInd + 1] = (byte)((x2 << 4) | ((x3 >> 2) & 0xF)); + dst[dstInd + 2] = (byte)((x3 << 6) | (x4 & 0x3F)); + } + } + } + } +} diff --git a/Assets/Scripts/BaseNcoding/Base64.cs.meta b/Assets/Scripts/BaseNcoding/Base64.cs.meta new file mode 100644 index 0000000..a866f2e --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base64.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b591d6746a6fd9b45a0174c0c22d292b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/Base85.cs b/Assets/Scripts/BaseNcoding/Base85.cs new file mode 100644 index 0000000..ce385ae --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base85.cs @@ -0,0 +1,181 @@ +using System; +using System.IO; +using System.Text; + +namespace BaseNcoding +{ + public class Base85 : Base + { + public const string DefaultAlphabet = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu"; + public const char DefaultSpecial = (char)0; + private static readonly uint[] Pow85 = { 85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1 }; + + public const string Prefix = "<~"; + public const string Postfix = "~>"; + + public override bool HasSpecial => false; + + public bool PrefixPostfix { get; set; } + + public Base85(string alphabet = DefaultAlphabet, char special = DefaultSpecial, bool prefixPostfix = false, Encoding textEncoding = null) + : base(85, alphabet, special, textEncoding) + { + PrefixPostfix = prefixPostfix; + BlockBitsCount = 32; + BlockCharsCount = 5; + } + + public override string Encode(byte[] data) + { + unchecked + { + byte[] encodedBlock = new byte[5]; + int decodedBlockLength = 4; + int resultLength = data.Length * (encodedBlock.Length / decodedBlockLength); + if (PrefixPostfix) + resultLength += Prefix.Length + Postfix.Length; + char[] result = new char[resultLength]; + int resultInd = 0; + + if (PrefixPostfix) + CopyString(Prefix, result, ref resultInd); + + int count = 0; + uint tuple = 0; + foreach (byte b in data) + { + if (count >= decodedBlockLength - 1) + { + tuple |= b; + if (tuple == 0) + result[resultInd++] = 'z'; + else + EncodeBlock(encodedBlock.Length, result, ref resultInd, encodedBlock, tuple); + tuple = 0; + count = 0; + } + else + { + tuple |= (uint)(b << (24 - count * 8)); + count++; + } + } + + if (count > 0) + EncodeBlock(count + 1, result, ref resultInd, encodedBlock, tuple); + + if (PrefixPostfix) + CopyString(Postfix, result, ref resultInd); + + return new string(result); + } + } + + public override byte[] Decode(string data) + { + unchecked + { + string dataWithoutPrefixPostfix = data; + if (PrefixPostfix) + { + if (!dataWithoutPrefixPostfix.StartsWith(Prefix) || !dataWithoutPrefixPostfix.EndsWith(Postfix)) + { + throw new Exception("ASCII85 encoded data should begin with '" + Prefix + "' and end with '" + Postfix + "'"); + } + dataWithoutPrefixPostfix = dataWithoutPrefixPostfix.Substring(Prefix.Length, dataWithoutPrefixPostfix.Length - Prefix.Length - Postfix.Length); + } + + MemoryStream ms = new MemoryStream(); + int count = 0; + + uint tuple = 0; + int encodedBlockLength = 5; + byte[] decodedBlock = new byte[4]; + foreach (char c in dataWithoutPrefixPostfix) + { + bool processChar; + switch (c) + { + case 'z': + if (count != 0) + { + throw new Exception("The character 'z' is invalid inside an ASCII85 block."); + } + decodedBlock[0] = 0; + decodedBlock[1] = 0; + decodedBlock[2] = 0; + decodedBlock[3] = 0; + ms.Write(decodedBlock, 0, decodedBlock.Length); + processChar = false; + break; + default: + processChar = true; + break; + } + + if (processChar) + { + tuple += (uint)InvAlphabet[c] * Pow85[count]; + count++; + if (count == encodedBlockLength) + { + DecodeBlock(decodedBlock.Length, decodedBlock, tuple); + ms.Write(decodedBlock, 0, decodedBlock.Length); + tuple = 0; + count = 0; + } + } + } + + if (count != 0) + { + if (count == 1) + { + throw new Exception("The last block of ASCII85 data cannot be a single byte."); + } + count--; + tuple += Pow85[count]; + DecodeBlock(count, decodedBlock, tuple); + for (int i = 0; i < count; i++) + { + ms.WriteByte(decodedBlock[i]); + } + } + + return ms.ToArray(); + } + } + + private static void CopyString(string source, char[] dest, ref int destInd) + { + for (int i = 0; i < source.Length; i++) + dest[destInd++] = source[i]; + } + + private void EncodeBlock(int count, char[] result, ref int resultInd, byte[] encodedBlock, uint tuple) + { + unchecked + { + for (int i = encodedBlock.Length - 1; i >= 0; i--) + { + uint quotient = tuple % 85; + uint remainder = tuple - quotient * 85; + tuple = quotient; + encodedBlock[i] = (byte)remainder; + } + + for (int i = 0; i < count; i++) + result[resultInd++] = Alphabet[encodedBlock[i]]; + } + } + + private void DecodeBlock(int bytes, byte[] decodedBlock, uint tuple) + { + unchecked + { + for (int i = 0; i < bytes; i++) + decodedBlock[i] = (byte)(tuple >> 24 - i * 8); + } + } + } +} diff --git a/Assets/Scripts/BaseNcoding/Base85.cs.meta b/Assets/Scripts/BaseNcoding/Base85.cs.meta new file mode 100644 index 0000000..a3cfa13 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base85.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bb00dc9d3e90a49439d967614c88ded8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/Base91.cs b/Assets/Scripts/BaseNcoding/Base91.cs new file mode 100644 index 0000000..e1d1a8d --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base91.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BaseNcoding +{ + public class Base91 : Base + { + public const string DefaultAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~\""; + public const char DefaultSpecial = (char)0; + + public override bool HasSpecial => false; + + public Base91(string alphabet = DefaultAlphabet, char special = DefaultSpecial, Encoding textEncoding = null) + : base(91, alphabet, special, textEncoding) + { + BlockBitsCount = 13; + BlockCharsCount = 2; + } + + public override string Encode(byte[] data) + { + StringBuilder result = new StringBuilder(data.Length); + + int ebq = 0, en = 0; + for (int i = 0; i < data.Length; ++i) + { + ebq |= (data[i] & 255) << en; + en += 8; + if (en > 13) + { + int ev = ebq & 8191; + + if (ev > 88) + { + ebq >>= 13; + en -= 13; + } + else + { + ev = ebq & 16383; + ebq >>= 14; + en -= 14; + } + + int quotient = Math.DivRem(ev, 91, out int remainder); + result.Append(Alphabet[remainder]); + result.Append(Alphabet[quotient]); + } + } + + if (en > 0) + { + int quotient = Math.DivRem(ebq, 91, out int remainder); + result.Append(Alphabet[remainder]); + if (en > 7 || ebq > 90) + result.Append(Alphabet[quotient]); + } + + return result.ToString(); + } + + public override byte[] Decode(string data) + { + unchecked + { + int dbq = 0, dn = 0, dv = -1; + + List result = new List(data.Length); + for (int i = 0; i < data.Length; ++i) + { + if (InvAlphabet[data[i]] == -1) + continue; + if (dv == -1) + dv = InvAlphabet[data[i]]; + else + { + dv += InvAlphabet[data[i]] * 91; + dbq |= dv << dn; + dn += (dv & 8191) > 88 ? 13 : 14; + do + { + result.Add((byte)dbq); + dbq >>= 8; + dn -= 8; + } + while (dn > 7); + dv = -1; + } + } + + if (dv != -1) + result.Add((byte)(dbq | dv << dn)); + + return result.ToArray(); + } + } + } +} diff --git a/Assets/Scripts/BaseNcoding/Base91.cs.meta b/Assets/Scripts/BaseNcoding/Base91.cs.meta new file mode 100644 index 0000000..b9ab053 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/Base91.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 445180477b0318d48b58549426fd8ba9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/BaseBigN.cs b/Assets/Scripts/BaseNcoding/BaseBigN.cs new file mode 100644 index 0000000..a862647 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/BaseBigN.cs @@ -0,0 +1,261 @@ +using System; +using System.Numerics; +using System.Text; + +namespace BaseNcoding +{ + public class BaseBigN : Base + { + private BigInteger[] _powN; + + public bool ReverseOrder { get; } + + public uint BlockMaxBitsCount { get; } + + public bool MaxCompression { get; } + + public override bool HasSpecial => false; + + public BaseBigN(string alphabet, uint blockMaxBitsCount = 64, + Encoding encoding = null, bool reverseOrder = false, bool parallel = false, + bool maxCompression = false) + : base((uint)alphabet.Length, alphabet, '\0', encoding, parallel) + { + BlockMaxBitsCount = blockMaxBitsCount; + BlockBitsCount = GetOptimalBitsCount(CharsCount, out var charsCountInBits, BlockMaxBitsCount, 2); + BlockCharsCount = (int)charsCountInBits; + + PreparePowN(BlockCharsCount); + + ReverseOrder = reverseOrder; + MaxCompression = maxCompression; + } + + public override string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return ""; + + int blockBitsCount, blockCharsCount; + if (!MaxCompression) + { + blockBitsCount = BlockBitsCount; + blockCharsCount = BlockCharsCount; + } + else + { + blockBitsCount = data.Length * 8; + blockCharsCount = (int)Math.Ceiling(blockBitsCount * Math.Log(2, Alphabet.Length)); + PreparePowN(blockCharsCount); + } + + int data8 = data.Length * 8; + int mainBitsLength = data8 / blockBitsCount * blockBitsCount; + int tailBitsLength = data8 - mainBitsLength; + int mainCharsCount = mainBitsLength * blockCharsCount / blockBitsCount; + int tailCharsCount = (tailBitsLength * blockCharsCount + blockBitsCount - 1) / blockBitsCount; + int totalCharsCount = mainCharsCount + tailCharsCount; + int iterationCount = mainCharsCount / blockCharsCount; + + var result = new char[totalCharsCount]; + + if (!Parallel) + EncodeBlock(data, result, 0, iterationCount, blockBitsCount, blockCharsCount); + else + { + int processorCount = Math.Min(iterationCount, Environment.ProcessorCount); + System.Threading.Tasks.Parallel.For(0, processorCount, i => + { + int beginInd = i * iterationCount / processorCount; + int endInd = (i + 1) * iterationCount / processorCount; + EncodeBlock(data, result, beginInd, endInd, blockBitsCount, blockCharsCount); + }); + } + + if (tailBitsLength != 0) + { + BigInteger bits = GetBitsN(data, mainBitsLength, tailBitsLength); + BitsToChars(result, mainCharsCount, tailCharsCount, bits); + } + + return new string(result); + } + + public override byte[] Decode(string data) + { + if (string.IsNullOrEmpty(data)) + return new byte[0]; + + int blockBitsCount, blockCharsCount; + if (!MaxCompression) + { + blockBitsCount = BlockBitsCount; + blockCharsCount = BlockCharsCount; + } + else + { + blockCharsCount = data.Length; + blockBitsCount = (int)(blockCharsCount / Math.Log(2, Alphabet.Length)) / 8 * 8; + PreparePowN(blockCharsCount); + } + + int totalBitsLength = ((data.Length - 1) * blockBitsCount / blockCharsCount + 8) / 8 * 8; + int mainBitsLength = totalBitsLength / blockBitsCount * blockBitsCount; + int tailBitsLength = totalBitsLength - mainBitsLength; + int mainCharsCount = mainBitsLength * blockCharsCount / blockBitsCount; + int tailCharsCount = (tailBitsLength * blockCharsCount + blockBitsCount - 1) / blockBitsCount; + BigInteger tailBits = CharsToBits(data, mainCharsCount, tailCharsCount); + if (tailBits >> tailBitsLength != 0) + { + totalBitsLength += 8; + mainBitsLength = totalBitsLength / blockBitsCount * blockBitsCount; + tailBitsLength = totalBitsLength - mainBitsLength; + mainCharsCount = mainBitsLength * blockCharsCount / blockBitsCount; + tailCharsCount = (tailBitsLength * blockCharsCount + blockBitsCount - 1) / blockBitsCount; + } + int iterationCount = mainCharsCount / blockCharsCount; + + byte[] result = new byte[totalBitsLength / 8]; + + if (!Parallel) + { + DecodeBlock(data, result, 0, iterationCount, blockBitsCount, blockCharsCount); + } + else + { + int processorCount = Math.Min(iterationCount, Environment.ProcessorCount); + System.Threading.Tasks.Parallel.For(0, processorCount, i => + { + int beginInd = i * iterationCount / processorCount; + int endInd = (i + 1) * iterationCount / processorCount; + DecodeBlock(data, result, beginInd, endInd, blockBitsCount, blockCharsCount); + }); + } + + if (tailCharsCount != 0) + { + BigInteger bits = CharsToBits(data, mainCharsCount, tailCharsCount); + AddBitsN(result, bits, mainBitsLength, tailBitsLength); + } + + return result; + } + + private void EncodeBlock(byte[] src, char[] dst, int beginInd, int endInd, int blockBitsCount, int blockCharsCount) + { + for (int ind = beginInd; ind < endInd; ind++) + { + int charInd = ind * blockCharsCount; + int bitInd = ind * blockBitsCount; + BigInteger bits = GetBitsN(src, bitInd, blockBitsCount); + BitsToChars(dst, charInd, blockCharsCount, bits); + } + } + + private void DecodeBlock(string src, byte[] dst, int beginInd, int endInd, int blockBitsCount, int blockCharsCount) + { + for (int ind = beginInd; ind < endInd; ind++) + { + int charInd = ind * blockCharsCount; + int bitInd = ind * blockBitsCount; + BigInteger bits = CharsToBits(src, charInd, blockCharsCount); + AddBitsN(dst, bits, bitInd, blockBitsCount); + } + } + + private static BigInteger GetBitsN(byte[] data, int bitPos, int bitsCount) + { + BigInteger result = 0; + + int currentBytePos = Math.DivRem(bitPos, 8, out int currentBitInBytePos); + int shift = 8 - currentBitInBytePos; + int xLength = Math.Min(bitsCount, shift); + if (xLength != 0) + { + result = new BigInteger((data[currentBytePos] >> shift - xLength) & ((1 << shift) - 1)) << bitsCount - xLength; + + currentBytePos += Math.DivRem(currentBitInBytePos + xLength, 8, out currentBitInBytePos); + + int x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + + while (x2Length > 0) + { + xLength += x2Length; + result |= (BigInteger)(data[currentBytePos] >> 8 - x2Length) << bitsCount - xLength; + + currentBytePos += Math.DivRem(currentBitInBytePos + x2Length, 8, out currentBitInBytePos); + + x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + } + } + + return result; + } + + private static void AddBitsN(byte[] data, BigInteger value, int bitPos, int bitsCount) + { + int currentBytePos = Math.DivRem(bitPos, 8, out int currentBitInBytePos); + + int shift = 8 - currentBitInBytePos; + int xLength = Math.Min(bitsCount, shift); + if (xLength != 0) + { + data[currentBytePos] |= (byte) ((byte) (value >> bitsCount - shift) & ((1 << shift) - 1)); + + currentBytePos += Math.DivRem(currentBitInBytePos + xLength, 8, out currentBitInBytePos); + + int x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + + while (x2Length > 0) + { + xLength += x2Length; + var bitsCountMinusXLength = bitsCount - xLength; + data[currentBytePos] |= (byte)((value >> (bitsCountMinusXLength - 8 + x2Length)) & 0xFF); + + currentBytePos += Math.DivRem(currentBitInBytePos + x2Length, 8, out currentBitInBytePos); + + x2Length = bitsCountMinusXLength; + if (x2Length > 8) + x2Length = 8; + } + } + } + + private void BitsToChars(char[] chars, int ind, int count, BigInteger block) + { + BigInteger quotient = block; + for (int i = 0; i < count; i++) + { + quotient = BigInteger.DivRem(quotient, CharsCount, out BigInteger remainder); + chars[ind + (!ReverseOrder ? i : count - 1 - i)] = Alphabet[(int)remainder]; + } + } + + private BigInteger CharsToBits(string data, int ind, int count) + { + BigInteger result = 0; + for (int i = 0; i < count; i++) + result += InvAlphabet[data[ind + (!ReverseOrder ? i : count - 1 - i)]] * _powN[_powN.Length - 1 - i]; + return result; + } + + private void PreparePowN(int blockCharsCount) + { + _powN = new BigInteger[blockCharsCount]; + BigInteger pow = 1; + for (int i = 0; i < blockCharsCount - 1; i++) + { + _powN[blockCharsCount - 1 - i] = pow; + pow *= CharsCount; + } + if (blockCharsCount > 0) + _powN[0] = pow; + } + } +} diff --git a/Assets/Scripts/BaseNcoding/BaseBigN.cs.meta b/Assets/Scripts/BaseNcoding/BaseBigN.cs.meta new file mode 100644 index 0000000..e383bc8 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/BaseBigN.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0a06acee1a2bf3d4aae0b2a2b83cb5d4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/BaseN.cs b/Assets/Scripts/BaseNcoding/BaseN.cs new file mode 100644 index 0000000..6a1c011 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/BaseN.cs @@ -0,0 +1,228 @@ +using System; +using System.Text; + +namespace BaseNcoding +{ + public class BaseN : Base + { + private readonly ulong[] _powN; + + public uint BlockMaxBitsCount { get; } + + public override bool HasSpecial => false; + + public bool ReverseOrder { get; } + + public BaseN(string alphabet, uint blockMaxBitsCount = 32, + Encoding encoding = null, bool reverseOrder = false, bool parallel = false) + : base((uint)alphabet.Length, alphabet, '\0', encoding, parallel) + { + BlockMaxBitsCount = blockMaxBitsCount; + BlockBitsCount = GetOptimalBitsCount(CharsCount, out var charsCountInBits, blockMaxBitsCount); + BlockCharsCount = (int)charsCountInBits; + _powN = new ulong[BlockCharsCount]; + ulong pow = 1; + for (int i = 0; i < BlockCharsCount - 1; i++) + { + _powN[BlockCharsCount - 1 - i] = pow; + pow *= CharsCount; + } + _powN[0] = pow; + ReverseOrder = reverseOrder; + } + + public override string Encode(byte[] data) + { + if (data == null || data.Length == 0) + return ""; + + int mainBitsLength = data.Length * 8 / BlockBitsCount * BlockBitsCount; + int tailBitsLength = data.Length * 8 - mainBitsLength; + int mainCharsCount = mainBitsLength * BlockCharsCount / BlockBitsCount; + int tailCharsCount = (tailBitsLength * BlockCharsCount + BlockBitsCount - 1) / BlockBitsCount; + int totalCharsCount = mainCharsCount + tailCharsCount; + int iterationCount = mainCharsCount / BlockCharsCount; + + var result = new char[totalCharsCount]; + + if (!Parallel) + { + EncodeBlock(data, result, 0, iterationCount); + } + else + { + int processorCount = Math.Min(iterationCount, Environment.ProcessorCount); + System.Threading.Tasks.Parallel.For(0, processorCount, i => + { + int beginInd = i * iterationCount / processorCount; + int endInd = (i + 1) * iterationCount / processorCount; + EncodeBlock(data, result, beginInd, endInd); + }); + } + + if (tailBitsLength != 0) + { + ulong bits = GetBits64(data, mainBitsLength, tailBitsLength); + BitsToChars(result, mainCharsCount, tailCharsCount, bits); + } + + return new string(result); + } + + public override byte[] Decode(string data) + { + if (string.IsNullOrEmpty(data)) + return new byte[0]; + + int totalBitsLength = ((data.Length - 1) * BlockBitsCount / BlockCharsCount + 8) / 8 * 8; + int mainBitsLength = totalBitsLength / BlockBitsCount * BlockBitsCount; + int tailBitsLength = totalBitsLength - mainBitsLength; + int mainCharsCount = mainBitsLength * BlockCharsCount / BlockBitsCount; + int tailCharsCount = (tailBitsLength * BlockCharsCount + BlockBitsCount - 1) / BlockBitsCount; + ulong tailBits = CharsToBits(data, mainCharsCount, tailCharsCount); + if (tailBits >> tailBitsLength != 0) + { + totalBitsLength += 8; + mainBitsLength = totalBitsLength / BlockBitsCount * BlockBitsCount; + tailBitsLength = totalBitsLength - mainBitsLength; + mainCharsCount = mainBitsLength * BlockCharsCount / BlockBitsCount; + tailCharsCount = (tailBitsLength * BlockCharsCount + BlockBitsCount - 1) / BlockBitsCount; + } + int iterationCount = mainCharsCount / BlockCharsCount; + + byte[] result = new byte[totalBitsLength / 8]; + + if (!Parallel) + { + DecodeBlock(data, result, 0, iterationCount); + } + else + { + int processorCount = Math.Min(iterationCount, Environment.ProcessorCount); + System.Threading.Tasks.Parallel.For(0, processorCount, i => + { + int beginInd = i * iterationCount / processorCount; + int endInd = (i + 1) * iterationCount / processorCount; + DecodeBlock(data, result, beginInd, endInd); + }); + } + + if (tailCharsCount != 0) + { + ulong bits = CharsToBits(data, mainCharsCount, tailCharsCount); + AddBits64(result, bits, mainBitsLength, tailBitsLength); + } + + return result; + } + + private void EncodeBlock(byte[] src, char[] dst, int beginInd, int endInd) + { + for (int ind = beginInd; ind < endInd; ind++) + { + int charInd = ind * BlockCharsCount; + int bitInd = ind * BlockBitsCount; + ulong bits = GetBits64(src, bitInd, BlockBitsCount); + BitsToChars(dst, charInd, BlockCharsCount, bits); + } + } + + private void DecodeBlock(string src, byte[] dst, int beginInd, int endInd) + { + for (int ind = beginInd; ind < endInd; ind++) + { + int charInd = ind * BlockCharsCount; + int bitInd = ind * BlockBitsCount; + ulong bits = CharsToBits(src, charInd, BlockCharsCount); + AddBits64(dst, bits, bitInd, BlockBitsCount); + } + } + + private static ulong GetBits64(byte[] data, int bitPos, int bitsCount) + { + ulong result = 0; + + int currentBytePos = Math.DivRem(bitPos, 8, out int currentBitInBytePos); + + int xLength = Math.Min(bitsCount, 8 - currentBitInBytePos); + if (xLength != 0) + { + result = ((ulong)data[currentBytePos] << 56 + currentBitInBytePos) >> 64 - xLength << bitsCount - xLength; + + currentBytePos += Math.DivRem(currentBitInBytePos + xLength, 8, out currentBitInBytePos); + + int x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + + while (x2Length > 0) + { + xLength += x2Length; + result |= (ulong)data[currentBytePos] >> 8 - x2Length << bitsCount - xLength; + + currentBytePos += Math.DivRem(currentBitInBytePos + x2Length, 8, out currentBitInBytePos); + + x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + } + } + + return result; + } + + private static void AddBits64(byte[] data, ulong value, int bitPos, int bitsCount) + { + unchecked + { + int currentBytePos = Math.DivRem(bitPos, 8, out int currentBitInBytePos); + + int xLength = Math.Min(bitsCount, 8 - currentBitInBytePos); + if (xLength != 0) + { + byte x1 = (byte)(value << 64 - bitsCount >> 56 + currentBitInBytePos); + data[currentBytePos] |= x1; + + currentBytePos += Math.DivRem(currentBitInBytePos + xLength, 8, out currentBitInBytePos); + + int x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + + while (x2Length > 0) + { + xLength += x2Length; + byte x2 = (byte)(value >> bitsCount - xLength << 8 - x2Length); + data[currentBytePos] |= x2; + + currentBytePos += Math.DivRem(currentBitInBytePos + x2Length, 8, out currentBitInBytePos); + + x2Length = bitsCount - xLength; + if (x2Length > 8) + x2Length = 8; + } + } + } + } + + private void BitsToChars(char[] chars, int ind, int count, ulong block) + { + ulong result = block; + for (int i = 0; i < count; i++) + { + ulong quotient = result / CharsCount; + ulong remainder = result - quotient * CharsCount; + result = quotient; + chars[ind + (!ReverseOrder ? i : count - 1 - i)] = Alphabet[(int)remainder]; + } + } + + private ulong CharsToBits(string data, int ind, int count) + { + ulong result = 0; + for (int i = 0; i < count; i++) + result += (ulong)InvAlphabet[data[ind + (!ReverseOrder ? i : count - 1 - i)]] * _powN[BlockCharsCount - 1 - i]; + return result; + } + } +} diff --git a/Assets/Scripts/BaseNcoding/BaseN.cs.meta b/Assets/Scripts/BaseNcoding/BaseN.cs.meta new file mode 100644 index 0000000..4b0cada --- /dev/null +++ b/Assets/Scripts/BaseNcoding/BaseN.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8d996e73fb18bfd45be33725dcf13786 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/BaseNcoding/StringGenerator.cs b/Assets/Scripts/BaseNcoding/StringGenerator.cs new file mode 100644 index 0000000..339a396 --- /dev/null +++ b/Assets/Scripts/BaseNcoding/StringGenerator.cs @@ -0,0 +1,48 @@ +using System; +using System.Text; + +namespace BaseNcoding +{ + public static class StringGenerator + { + private const string LettersAndDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + public static string GetAlphabet(int charsCount) + { + var result = new StringBuilder(charsCount); + int i = 0; + int count = 0; + do + { + char c = (char)i; + if (!char.IsControl(c) && !char.IsWhiteSpace(c)) + { + result.Append(c); + count++; + } + i++; + } + while (count < charsCount); + + return result.ToString(); + } + + public static string GetRandom(int size, bool onlyLettersAndDigits) + { + Random r = new Random(); + if (onlyLettersAndDigits) + { + var result = new char[size]; + for (int i = 0; i < size; i++) + result[i] = LettersAndDigits[r.Next(LettersAndDigits.Length)]; + return new string(result); + } + + var data = new byte[size]; + for (int i = 0; i < size; i++) + data[i] = (byte) r.Next(32, 127); + var encoding = new ASCIIEncoding(); + return encoding.GetString(data); + } + } +} diff --git a/Assets/Scripts/BaseNcoding/StringGenerator.cs.meta b/Assets/Scripts/BaseNcoding/StringGenerator.cs.meta new file mode 100644 index 0000000..df242cc --- /dev/null +++ b/Assets/Scripts/BaseNcoding/StringGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b72d6d0bcd1a9e846ad04cc26014bbcd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Manifest.meta b/Assets/Scripts/Manifest.meta new file mode 100644 index 0000000..e6efa53 --- /dev/null +++ b/Assets/Scripts/Manifest.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b5ad1e27f0d722e44bbd45017d9aaec8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Manifest/BSVReader.cs b/Assets/Scripts/Manifest/BSVReader.cs new file mode 100644 index 0000000..e0c417b --- /dev/null +++ b/Assets/Scripts/Manifest/BSVReader.cs @@ -0,0 +1,231 @@ +using System; +using System.IO; +using UnityEngine; + +public class BSVReader +{ + public enum ReadMode + { + Memory = 0, + Stream = 1 + } + + private enum Phase : byte + { + Header = 0, + Body = 1, + End = 2 + } + + private enum HeaderPhaseApriori : byte + { + Start = 0, + RowCount = 1, + MaxRowSize = 2 + } + + public enum Format : byte + { + Apriori = 0, + AnonymousSchema = 1 + } + + public static ulong ReadVLQ(byte[] buf, ref int offset, int max_bytes = 8) + { + ulong n = 0; + int count = 0; + while (count < max_bytes) + { + count++; + var val = (buf[offset] & 127Lu); + n |= val; + var isEnd = ((buf[offset] & 128) == 0); + offset++; + if (isEnd) + { + return n; + } + else + { + n = n << 7; + } + } + return n; + } + public static ulong ReadUNum(byte[] buf, ref int offset, int bytes) + { + var arraySegment = new ArraySegment(buf, offset, bytes); + offset += bytes; + ulong n = 0; + for(int i = arraySegment.Offset; i < arraySegment.Offset+arraySegment.Count; i++) + { + n = n << 8; + n |= arraySegment.Array[i]; + } + return n; + } + public static ArraySegment ReadText(byte[] buf, ref int offset) + { + int start = offset; + while (offset < buf.Length &&buf[offset] != 0) + { + offset++; + } + var arraySegment = new ArraySegment(buf, start, offset - start); + offset++; + return arraySegment; + } + public static ArraySegment ReadFixedBlob(byte[] buf, ref int offset, int bytes) + { + var arraySegment = new ArraySegment(buf, offset, bytes); + offset += bytes; + return arraySegment; + } + public static ArraySegment ReadFixedText(byte[] buf, ref int offset, int bytes) + { + var arraySegment = new ArraySegment(buf, offset, bytes); + offset += bytes; + return arraySegment; + } + public static ArraySegment ReadBlob(byte[] buf, ref int offset) + { + ulong size = ReadVLQ(buf, ref offset); + var arraySegment = new ArraySegment(buf, offset, (int)size); + offset += (int)size; + return arraySegment; + } + + public static T[] GetArray(ArraySegment array) + { + T[] subArray = new T[array.Count]; + Buffer.BlockCopy(array.Array, array.Offset, subArray, 0, array.Count); + return subArray; + } + + internal static IBSVReader Init(string path, bool isCompressed,ReadMode mode = ReadMode.Memory) + { + try + { + byte[] input = isCompressed? LZ4Util.DecompressFromFile(path): File.ReadAllBytes(path); + + if (mode == ReadMode.Stream) + { + return new AprioriBSVReader(input, 2); + } + else + { + return new AnonymousSchemaBSVReader(input, 2); + } + } + catch (Exception e) + { + Debug.LogError("Read Manifest File Error :" + e); + return null; + } + } + + public interface IBSVReader : IDisposable + { + public abstract ulong GetRowCount(); + public abstract Format GetFormat(); + + public abstract bool ReadLine(ILineParser parser, ref T dat); + } + + public interface ILineParser + { + public abstract void Parse(byte[] buf, ref int offset, ref T dat); + } + + internal abstract class BaseBSVReader : IBSVReader, IDisposable + { + protected readonly byte[] buf; + protected int offset; + protected ulong rowCount; + protected ulong maxRowSize; + protected ulong row; + protected readonly Format format; + public ulong GetRowCount() { return rowCount; } + public Format GetFormat() { return format; } + public BaseBSVReader(Format format, byte[] buf, int offset) + { + this.format = format; + this.buf = buf; + this.offset = offset; + } + public bool ReadLine(ILineParser parser, ref T dat) + { + if (offset < buf.Length) + { + parser.Parse(buf, ref offset, ref dat); + return true; + } + else return false; + } + public abstract void Dispose(); + } + + internal class AnonymousSchemaBSVReader : BaseBSVReader + { + private readonly ushort headerSize; + private readonly uint schemaCount; + private readonly uint schemaVersion; + private readonly Schema[] schemas; + + internal uint GetSchemaCount() { return schemaCount; } + + internal uint GetSchemaVersion() { return schemaVersion; } + + public AnonymousSchemaBSVReader(byte[] buf, int offset) : base(Format.AnonymousSchema, buf, offset) + { + headerSize = (ushort)ReadUNum(buf, ref this.offset, 2); + rowCount = ReadVLQ(buf, ref this.offset); + maxRowSize = ReadVLQ(buf, ref this.offset); + schemaVersion = (uint)ReadVLQ(buf, ref this.offset); + schemaCount = (uint)ReadVLQ(buf, ref this.offset); + if (schemaCount > 0) + { + schemas = new Schema[schemaCount]; + for (int i = 0; i < schemaCount; i++) + { + Schema.Type type = (Schema.Type)ReadUNum(buf, ref this.offset, 1); + ulong? fixSize = null; + if (type == Schema.Type.UNumFixed || type == Schema.Type.TextFixed || type == Schema.Type.BlobFixed) + { + fixSize = ReadVLQ(buf, ref this.offset); + } + schemas[i] = new Schema(type, fixSize); + } + } + } + + public override void Dispose() { } + } + + public class Schema + { + public enum Type : byte + { + UShort = 16, + UInt = 17, + ULong = 18, + UNum = 32, + UNumFixed = 33, + Blob = 48, + BlobFixed = 49, + Text = 64, + TextFixed = 65 + } + public readonly Type type; + public readonly ulong? fixedSize; + + public Schema(Type type, ulong? fixedSize) { } + } + + internal class AprioriBSVReader : BaseBSVReader + { + public AprioriBSVReader(byte[] buf, int offset) : base(Format.Apriori, buf, offset) { } + public override void Dispose() { } + } +} + diff --git a/Assets/Scripts/Manifest/BSVReader.cs.meta b/Assets/Scripts/Manifest/BSVReader.cs.meta new file mode 100644 index 0000000..f1e1744 --- /dev/null +++ b/Assets/Scripts/Manifest/BSVReader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4a15e32c966280446ba094c5db043677 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Manifest/LZ4Util.cs b/Assets/Scripts/Manifest/LZ4Util.cs new file mode 100644 index 0000000..bd0f08e --- /dev/null +++ b/Assets/Scripts/Manifest/LZ4Util.cs @@ -0,0 +1,54 @@ +using K4os.Compression.LZ4; +using K4os.Compression.LZ4.Streams; +using System; +using System.IO; +using System.Runtime.InteropServices; +using UnityEngine; + +public static class LZ4Util +{ + [DllImport("libnative.dll")] + public static extern int LZ4_decompress_safe_ext(IntPtr src, IntPtr dst, int compressedSize, int dstCapacity); + + public static byte[] DecompressFromFile(string path) + { + var decoder = LZ4Stream.Decode(File.OpenRead(path)); + var target = new MemoryStream(); + decoder.CopyTo(target); + return target.ToArray(); + } + + public static byte[] DecompressFromBytesSafe(byte[] bytes)//WIP + { + IntPtr data = Marshal.AllocHGlobal(bytes.Length); + Marshal.Copy(bytes, 0, data, bytes.Length); + + int dataLength = 0; + dataLength = ((dataLength| bytes[3])<<8); + dataLength = ((dataLength| bytes[2])<<8); + dataLength = ((dataLength| bytes[1])<<8); + dataLength = (dataLength| bytes[0]); + + IntPtr result = Marshal.AllocHGlobal(bytes.Length+ dataLength); + + var output = new byte[bytes.Length + dataLength]; + var outlength = LZ4_decompress_safe_ext(data, result, bytes.Length, output.Length); + + Marshal.Copy(result, output, 0,outlength); + + Array.Resize(ref output, outlength); + Marshal.FreeHGlobal(data); + Marshal.FreeHGlobal(result); + return output; + } + + public static byte[] CompressFromBytes(byte[] bytes) + { + var source = new MemoryStream(bytes); + var decoder = LZ4Stream.Encode(source); + var target = new MemoryStream(); + decoder.CopyTo(target); + return target.ToArray(); + } +} + diff --git a/Assets/Scripts/Manifest/LZ4Util.cs.meta b/Assets/Scripts/Manifest/LZ4Util.cs.meta new file mode 100644 index 0000000..567276f --- /dev/null +++ b/Assets/Scripts/Manifest/LZ4Util.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ad28139c90f43ce4eba7c018a861ca37 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Manifest/ManifestCategory.cs b/Assets/Scripts/Manifest/ManifestCategory.cs new file mode 100644 index 0000000..32fc3f7 --- /dev/null +++ b/Assets/Scripts/Manifest/ManifestCategory.cs @@ -0,0 +1,17 @@ + +public static class ManifestCategory +{ + public const int APP_DEFINED_KIND_BASE = 10; + public const string ASSET_MANIFEST = "manifest"; + public const string PLATFORM_MANIFEST = "manifest2"; + public const string ROOT_MANIFEST = "manifest3"; + + public enum Kind : int + { + Default = 0, + AssetManifest = 1, + PlatformManifest = 2, + RootManifest = 3 + } +} + diff --git a/Assets/Scripts/Manifest/ManifestCategory.cs.meta b/Assets/Scripts/Manifest/ManifestCategory.cs.meta new file mode 100644 index 0000000..a45c1b6 --- /dev/null +++ b/Assets/Scripts/Manifest/ManifestCategory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: be0b7cdd8f20ce14b8d0e1d129684d19 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Manifest/ManifestDB.cs b/Assets/Scripts/Manifest/ManifestDB.cs new file mode 100644 index 0000000..4da06b6 --- /dev/null +++ b/Assets/Scripts/Manifest/ManifestDB.cs @@ -0,0 +1,318 @@ +using Mono.Data.Sqlite; +using Newtonsoft.Json.Linq; +using System; +using System.Collections; +using System.IO; +using System.Text; +using UnityEngine; +using UnityEngine.Networking; +using static BSVReader; +using static ManifestCategory; + +public class ManifestDB +{ + public SqliteConnection MetaDB; + public static string DBPath; + public const string DefaultDBPath = "umamusume/meta"; + public Action callback; + public ManifestDB(string DBPath = DefaultDBPath) + { + ManifestDB.DBPath = DBPath; + if (File.Exists(DBPath)) + { + MetaDB = new SqliteConnection($@"Data Source={DBPath}"); + MetaDB.Open(); + } + else + { + SqliteConnection.CreateFile(DBPath); + MetaDB = new SqliteConnection($@"Data Source={DBPath}"); + MetaDB.Open(); + SqliteCommand cmd = new SqliteCommand( + "CREATE TABLE `a` ( `i` INTEGER PRIMARY KEY,`n` TEXT NOT NULL,`d` TEXT,`g` INTEGER(4) NOT NULL,`l` INTEGER(8) NOT NULL,`c` INTEGER(8) NOT NULL,`h` TEXT NOT NULL,`m` TEXT NOT NULL,`k` INTEGER(1) NOT NULL,`s` INTEGER(1) NOT NULL,`p` INTEGER(4) NOT NULL DEFAULT 0)"); + cmd.Connection = MetaDB; + cmd.ExecuteNonQuery(); + cmd.CommandText = "CREATE INDEX `a0` ON `a`(`n`, `s`)"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "CREATE INDEX `a2` ON `a`(`m`, `s`)"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "CREATE INDEX `a3` ON `a`(`g`, 's')"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "CREATE INDEX `a4` ON `a`(`p`)"; + cmd.ExecuteNonQuery(); + cmd.CommandText = "CREATE INDEX `a1` ON `a`(`s`)"; + cmd.ExecuteNonQuery(); + } + } + + public IEnumerator UpdateResourceVersion(Action callback) + { + this.callback = callback; + callback?.Invoke("Checking Resource Version"); + yield return UpdateMetaDB(); + yield return UpdateMasterDB(); + callback?.Invoke($"Done"); + } + + //umamusume uses the manifest file to generate meta database. + //it needs to get the root manifest file first + //then read the root manifest to get other manifest file information + //recursively read the manifest file to obtain all resource files information + //finally, insert all data into the meta database + public IEnumerator UpdateMetaDB() + { + string rootHash = null; + callback?.Invoke("Getting resource version"); + yield return UmaViewerDownload.DownloadText("https://www.tracenacademy.com/api/MetaC/root", txt => + { + if (string.IsNullOrEmpty(txt)) return; + var item = JObject.Parse(txt); + if ((string)item["n"] == "//root") + { + rootHash = (string)item["h"]; + } + }); + + if (rootHash == null) + { + Debug.LogError("Get RootHash Error"); + yield break; + } + + ManifestEntry rootEntry = new ManifestEntry(Encoding.UTF8.GetBytes("root"), null, 0, 0, 0, 0, "", 3); + rootEntry.hname = rootHash; + yield return UpdateMetaDB(rootEntry); + } + + public IEnumerator UpdateMetaDB(ulong size, ulong checksum) + { + ManifestEntry rootEntry = new ManifestEntry(Encoding.UTF8.GetBytes("root"), null, 0, 0, size, checksum, "", 3); + rootEntry.hname = rootEntry.CalHameString(); + yield return UpdateMetaDB(rootEntry); + } + + public IEnumerator UpdateMetaDB(ManifestEntry rootEntry) + { + if (MetaDB == null || MetaDB.State == System.Data.ConnectionState.Closed) + { + Debug.LogError("Open Meta Database Error"); + yield break; + } + + SqliteCommand command = new SqliteCommand(MetaDB); + + //Insert root + int index = 1; + UpdateManifestEntry(index, "manifest3", rootEntry, ref command); + + //Insert platform + callback?.Invoke("Reading Platform Manifest"); + ManifestEntry[] platformEntrys = null; + yield return GetManifest(rootEntry.hname, Kind.PlatformManifest, entrys => { platformEntrys = entrys; }); + if (platformEntrys == null) + { + Debug.LogError("Read PlatformManifest Error"); + MetaDB.Close(); + yield break; + } + ManifestEntry platformEntry = GetLocalPlatformEntry(platformEntrys); + index++; + UpdateManifestEntry(index, "manifest2", platformEntry, ref command); + + //Insert AssetManifest + ManifestEntry[] assetEntrys = null; + yield return GetManifest(platformEntry.hname, Kind.AssetManifest, entrys => { assetEntrys = entrys; }); + if (assetEntrys == null) + { + Debug.LogError("Read AssetManifest Error"); + MetaDB.Close(); + yield break; + } + using (SqliteTransaction tran = MetaDB.BeginTransaction()) + { + for (int i = 0; i < assetEntrys.Length; i++) + { + index++; + callback?.Invoke($"Reading Assets Manifest : {assetEntrys[i].tname} ({i + 1}/{assetEntrys.Length})"); + UpdateManifestEntry(index, "manifest", assetEntrys[i], ref command); + } + tran.Commit(); + } + + //Insert Asset + foreach (var assetEntry in assetEntrys) + { + ManifestEntry[] assets = null; + yield return GetManifest(assetEntry.hname, Kind.Default, entrys => { assets = entrys; }); + if (assets == null) + { + Debug.LogError("Read AssetManifest Error"); + continue; + } + using SqliteTransaction tran = MetaDB.BeginTransaction(); + for (int i = 0; i < assets.Length; i++) + { + index++; + callback?.Invoke($"Reading AssetBundles Manifest : {assetEntry.tname} ({i + 1}/{assets.Length})"); + UpdateManifestEntry(index, assetEntry.tname, assets[i], ref command); + } + tran.Commit(); + } + } + + public IEnumerator UpdateMasterDB() + { + if (MetaDB == null || MetaDB.State == System.Data.ConnectionState.Closed) + { + Debug.LogError("Open Meta Database Error"); + yield break; + } + + callback?.Invoke($"Checking master.mdb"); + SqliteCommand command = new SqliteCommand("SELECT h FROM a WHERE n LIKE 'master.mdb.lz4'", MetaDB); + var reader = command.ExecuteReader(); + + if (reader.Read()) + { + var hash = reader.GetString(0); + var path = GetManifestPath(hash); + if (!File.Exists(path)) + { + callback?.Invoke($"Downloading master.mdb"); + UnityWebRequest www = UnityWebRequest.Get(UmaViewerDownload.GetGenericRequestUrl(hash)); + yield return www.SendWebRequest(); + if (www.result == UnityWebRequest.Result.Success) + { + File.WriteAllBytes(path, www.downloadHandler.data); + } + else + { + Debug.LogError("Download Master.mdb Failed :" + www.error); + yield break; + } + } + + callback?.Invoke($"Decompress master.mdb"); + var masterPath = $"{Path.GetDirectoryName(DBPath)}/master/master.mdb"; + Directory.CreateDirectory(Path.GetDirectoryName(masterPath)); + File.WriteAllBytes(masterPath, LZ4Util.DecompressFromFile(path)); + } + } + + private void UpdateManifestEntry(int index, string type, ManifestEntry entry, ref SqliteCommand command) + { + command.CommandText = "INSERT OR REPLACE INTO a (i,n,d,g,l,c,h,m,k,s,p) VALUES (@id,@name,@deps,@group,@length,@check,@hash,@m,@k,@s,@p)"; + command.Parameters.Clear(); + command.Parameters.Add(new SqliteParameter("@id", index)); + command.Parameters.Add(new SqliteParameter("@name", (type.Contains("manifest") ? "//" : "") + entry.tname)); + command.Parameters.Add(new SqliteParameter("@deps", entry.tdeps)); + command.Parameters.Add(new SqliteParameter("@group", entry.group)); + command.Parameters.Add(new SqliteParameter("@length", (long)entry.size)); + command.Parameters.Add(new SqliteParameter("@check", (long)entry.checksum)); + command.Parameters.Add(new SqliteParameter("@hash", entry.hname)); + command.Parameters.Add(new SqliteParameter("@m", type)); + command.Parameters.Add(new SqliteParameter("@k", (byte)entry.kind)); + command.Parameters.Add(new SqliteParameter("@s", 1)); + command.Parameters.Add(new SqliteParameter("@p", entry.priority)); + command.ExecuteNonQuery(); + } + + public static ManifestEntry[] GetManifest(string path, bool isCompressed, Kind kind) + { + IBSVReader reader = Init(path, isCompressed); + if (reader == null) return null; + + ulong count = reader.GetRowCount(); + ManifestEntry[] manifests = new ManifestEntry[count]; + if (kind == Kind.Default) + { + for (ulong i = 0; i < count; i++) + { + manifests[i].kind = (int)kind; + if (!reader.ReadLine(ManifestEntry.GetBsvParser(ManifestEntry.Format.Full, reader), ref manifests[i])) + { + Debug.LogError("ReadLineError"); + return null; + } + } + } + else + { + for (ulong i = 0; i < count; i++) + { + manifests[i].kind = (int)kind; + if (!reader.ReadLine(ManifestEntry.GetBsvParser(ManifestEntry.Format.Simplified, reader), ref manifests[i])) + { + Debug.LogError("ReadLineError"); + return null; + } + } + } + return manifests; + + } + + public IEnumerator GetManifest(string hash, Kind kind, Action callback) + { + var path = GetManifestPath(hash); + if (!File.Exists(path)) + { + this.callback?.Invoke($"Downloading Manifest :{hash}"); + yield return DownloadManifest(hash); + } + if (File.Exists(path)) + { + callback.Invoke(GetManifest(path, true, kind)); + } + } + + public IEnumerator DownloadManifest(string hash) + { + UnityWebRequest www = UnityWebRequest.Get(UmaViewerDownload.GetManifestRequestUrl(hash)); + www.timeout = 15; + yield return www.SendWebRequest(); + if (www.result == UnityWebRequest.Result.Success) + { + File.WriteAllBytes(GetManifestPath(hash), www.downloadHandler.data); + } + else + { + Debug.LogError("Download Manifest Failed :" + www.error); + } + } + + public string GetManifestPath(string hash, bool createPath = true) + { + string path = $"{Path.GetDirectoryName(DBPath)}/dat/{hash.Substring(0, 2)}"; + if (createPath) + { + Directory.CreateDirectory(path); + } + return path + $"/{hash}"; + } + + public static ManifestEntry GetLocalPlatformEntry(ManifestEntry[] platformEntrys) + { + foreach (var entry in platformEntrys) + { + if (Encoding.UTF8.GetString(entry.name) == "Windows" && Application.platform == RuntimePlatform.WindowsPlayer) + { + return entry; + } + else if (Encoding.UTF8.GetString(entry.name) == "IOS" && Application.platform == RuntimePlatform.IPhonePlayer) + { + return entry; + } + else if (Encoding.UTF8.GetString(entry.name) == "Android" && Application.platform == RuntimePlatform.Android) + { + return entry; + } + else + { + return entry; + } + } + return platformEntrys[0]; + } +} + diff --git a/Assets/Scripts/Manifest/ManifestDB.cs.meta b/Assets/Scripts/Manifest/ManifestDB.cs.meta new file mode 100644 index 0000000..4621d41 --- /dev/null +++ b/Assets/Scripts/Manifest/ManifestDB.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1c5745921b997134aa8682f304140a8a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Manifest/ManifestEntry.cs b/Assets/Scripts/Manifest/ManifestEntry.cs new file mode 100644 index 0000000..6ef37f4 --- /dev/null +++ b/Assets/Scripts/Manifest/ManifestEntry.cs @@ -0,0 +1,154 @@ +using BaseNcoding; +using System; +using System.Security.Cryptography; +using System.Text; +using UnityEngine; +using static BSVReader; + +public struct ManifestEntry +{ + public byte[] name; + public byte[] deps; + public string tname; + public string tdeps; + public uint group; + public uint priority; + public ulong size; + public ulong checksum; + public string hname; + public int kind; + + public ManifestEntry(byte[] name, byte[] deps, uint group, uint priority, ulong size, ulong checksum, string hname, int kind) + { + this.name = name; + this.deps = deps; + this.group = group; + this.priority = priority; + this.size = size; + this.checksum = checksum; + this.hname = hname; + this.kind = kind; + this.tname = Encoding.UTF8.GetString(this.name); + this.tdeps = (this.deps == null ? null : Encoding.UTF8.GetString(this.deps)); + } + + public enum Format + { + Simplified = 0, + Full = 1 + } + + + public static byte[] CalHName(ulong checksum, ulong size, byte[] name) + { + if (name == null) return null; + SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); + byte[] allbyte = new byte[name.Length + 16]; + byte[] checksumByte = BitConverter.GetBytes(checksum); + byte[] sizeByte = BitConverter.GetBytes(size); + for (int i = 0; i < 16; i++) + { + allbyte[i] = (i < 8 ? checksumByte[7 - i] : sizeByte[15 - i]); + } + Buffer.BlockCopy(name, 0, allbyte, 16, name.Length); + return sha1.ComputeHash(allbyte); + } + + + public static string CalHNameString(ulong checksum, ulong size, byte[] name) + { + var bytes = CalHName(checksum, size, name); + return new Base32().Encode(bytes); + } + + public static string CalHameString(ulong checksum, ulong size, string name) + { + return CalHNameString(checksum, size, Encoding.UTF8.GetBytes(name)); + } + + public string CalHameString() + { + return CalHNameString(checksum, size, name); + } + + public override string ToString() + { + StringBuilder s = new StringBuilder(); + s.AppendFormat("Name : {0} ", Encoding.UTF8.GetString(name)); + s.AppendFormat("Deps : {0} \n", deps == null ? "NULL" : Encoding.UTF8.GetString(deps)); + s.AppendFormat("Size : {0} ", size); + s.AppendFormat("Checksum : {0} ", (long)checksum); + s.AppendFormat("Group : {0} ", group); + s.AppendFormat("Priority : {0} ", priority); + s.AppendFormat("Kind : {0} ", kind); + s.AppendFormat("Hash : {0} ", CalHNameString(checksum, size, name)); + return s.ToString(); + } + + public static ILineParser GetBsvParser(Format format, IBSVReader bsvReader) + { + if (format == Format.Simplified) + { + return new SimplifiedEntryLineReader(); + } + else + { + if (bsvReader is AnonymousSchemaBSVReader) + { + return new FullEntryLineReader(); + } + else if (bsvReader is AprioriBSVReader) + { + return new LegacyFullEntryLineReader(); + } + return null; + } + } + + public class SimplifiedEntryLineReader : ILineParser + { + public void Parse(byte[] buf, ref int offset, ref ManifestEntry dat) + { + dat.name = GetArray(ReadText(buf, ref offset)); + dat.size = ReadVLQ(buf, ref offset); + dat.checksum = ReadUNum(buf, ref offset, 8); + dat.hname = CalHNameString(dat.checksum, dat.size, dat.name); + dat.tname = Encoding.UTF8.GetString(dat.name); + } + + } + + public class FullEntryLineReader : ILineParser + { + + public void Parse(byte[] buf, ref int offset, ref ManifestEntry dat) + { + dat.name = GetArray(ReadText(buf, ref offset)); + dat.deps = GetArray(ReadText(buf, ref offset)); + dat.group = (uint)ReadVLQ(buf, ref offset, 4); + dat.priority = (uint)ReadVLQ(buf, ref offset, 4); + dat.size = (uint)ReadVLQ(buf, ref offset, 8); + dat.checksum = ReadUNum(buf, ref offset, 8); + dat.hname = CalHNameString(dat.checksum, dat.size, dat.name); + dat.tname = Encoding.UTF8.GetString(dat.name); + dat.tdeps = (dat.deps == null ? null : Encoding.UTF8.GetString(dat.deps)); + } + + } + + public class LegacyFullEntryLineReader : ILineParser + { + public void Parse(byte[] buf, ref int offset, ref ManifestEntry dat) + { + dat.name = GetArray(ReadText(buf, ref offset)); + dat.deps = GetArray(ReadText(buf, ref offset)); + dat.group = (uint)ReadVLQ(buf, ref offset, 4); + dat.size = (uint)ReadVLQ(buf, ref offset, 8); + dat.checksum = ReadUNum(buf, ref offset, 8); + dat.hname = CalHNameString(dat.checksum, dat.size, dat.name); + dat.tname = Encoding.UTF8.GetString(dat.name); + dat.tdeps = (dat.deps == null ? null : Encoding.UTF8.GetString(dat.deps)); + dat.priority = 0; + } + } +} diff --git a/Assets/Scripts/Manifest/ManifestEntry.cs.meta b/Assets/Scripts/Manifest/ManifestEntry.cs.meta new file mode 100644 index 0000000..bc8a297 --- /dev/null +++ b/Assets/Scripts/Manifest/ManifestEntry.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f446452d64e1ef54399daba449bbb5dc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UmaDatabase/UmaDataTypes.cs b/Assets/Scripts/UmaDatabase/UmaDataTypes.cs index 80afe32..3ba085f 100644 --- a/Assets/Scripts/UmaDatabase/UmaDataTypes.cs +++ b/Assets/Scripts/UmaDatabase/UmaDataTypes.cs @@ -128,5 +128,6 @@ public enum UmaFileType uianimation, transferevent, teambuilding, - challengematch + challengematch, + collectevent } diff --git a/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs b/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs index 3c07c85..e508507 100644 --- a/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs +++ b/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs @@ -53,16 +53,9 @@ public class UmaDatabaseController { try { - if (Application.platform == RuntimePlatform.Android) - { - metaDb = new SqliteConnection($@"Data Source={GetGameRootPath()}/meta;"); - masterDb = new SqliteConnection($@"Data Source={GetGameRootPath()}/master/master.mdb;"); - } - else - { - metaDb = new SqliteConnection($@"Data Source={Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low"}\Cygames\umamusume\meta;"); - masterDb = new SqliteConnection($@"Data Source={Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low"}\Cygames\umamusume\master\master.mdb;"); - } + metaDb = new SqliteConnection($@"Data Source={Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low"}\Cygames\umamusume\meta;"); + masterDb = new SqliteConnection($@"Data Source={Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low"}\Cygames\umamusume\master\master.mdb;"); + metaDb.Open(); MetaEntries = ReadMeta(metaDb); diff --git a/Assets/Scripts/UmaViewerDownload.cs b/Assets/Scripts/UmaViewerDownload.cs index 3d7ee1e..ad2be80 100644 --- a/Assets/Scripts/UmaViewerDownload.cs +++ b/Assets/Scripts/UmaViewerDownload.cs @@ -5,6 +5,10 @@ using UnityEngine.Networking; public class UmaViewerDownload : MonoBehaviour { + public static string MANIFEST_ROOT_URL = "https://prd-storage-app-umamusume.akamaized.net/dl/resources/Manifest"; + public static string GENERIC_BASE_URL = "https://prd-storage-umamusume.akamaized.net/dl/resources/Generic"; + public static string ASSET_BASE_URL = "https://prd-storage-umamusume.akamaized.net/dl/resources/Windows/assetbundles/"; + public static IEnumerator DownloadText(string url, System.Action callback) { UnityWebRequest www = UnityWebRequest.Get(url); @@ -21,4 +25,19 @@ public class UmaViewerDownload : MonoBehaviour callback(www.downloadHandler.text); } } + + public static string GetManifestRequestUrl(string hash) + { + return $"{MANIFEST_ROOT_URL}/{hash.Substring(0, 2)}/{hash}"; + } + + public static string GetGenericRequestUrl(string hash) + { + return $"{GENERIC_BASE_URL}/{hash.Substring(0, 2)}/{hash}"; + } + + public static string GetAssetRequestUrl(string hash) + { + return $"{ASSET_BASE_URL}/{hash.Substring(0, 2)}/{hash}"; + } } diff --git a/Assets/Scripts/UmaViewerMain.cs b/Assets/Scripts/UmaViewerMain.cs index cde8f8f..918ec53 100644 --- a/Assets/Scripts/UmaViewerMain.cs +++ b/Assets/Scripts/UmaViewerMain.cs @@ -5,6 +5,9 @@ using UnityEngine; using System; using Newtonsoft.Json.Linq; using System.Collections; +using static ManifestCategory; +using BaseNcoding; +using System.Text; public class UmaViewerMain : MonoBehaviour { diff --git a/Assets/Scripts/UmaViewerUI.cs b/Assets/Scripts/UmaViewerUI.cs index 41a195d..fe89c0d 100644 --- a/Assets/Scripts/UmaViewerUI.cs +++ b/Assets/Scripts/UmaViewerUI.cs @@ -88,6 +88,8 @@ public class UmaViewerUI : MonoBehaviour public FaceDrivenKeyTarget currentFaceDrivenKeyTarget; + private IEnumerator UpdateResVerCoroutine; + private void Awake() { Instance = this; @@ -713,8 +715,6 @@ public class UmaViewerUI : MonoBehaviour } } - - public void AudioPause() { if (Builder.CurrentAudioSources.Count>0) @@ -919,4 +919,12 @@ public class UmaViewerUI : MonoBehaviour } } } + + public void UpdateGameDB() { + if (UpdateResVerCoroutine != null) return; + ManifestDB dB = new ManifestDB(); + UpdateResVerCoroutine = dB.UpdateResourceVersion(delegate (string msg) { LyricsText.text = msg; }); + StartCoroutine(UpdateResVerCoroutine); + } + } diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 99b6b22..1ebb68e 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -5,7 +5,7 @@ "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.assetbundlebrowser": { "version": "https://github.com/Unity-Technologies/AssetBundles-Browser.git", @@ -21,14 +21,14 @@ "dependencies": { "com.unity.services.core": "1.0.1" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ext.nunit": { "version": "1.0.6", "depth": 1, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.formats.fbx": { "version": "4.1.2", @@ -38,7 +38,7 @@ "com.unity.timeline": "1.5.2", "com.autodesk.fbx": "4.1.1" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ide.rider": { "version": "2.0.7", @@ -47,7 +47,7 @@ "dependencies": { "com.unity.test-framework": "1.1.1" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ide.visualstudio": { "version": "2.0.16", @@ -56,14 +56,14 @@ "dependencies": { "com.unity.test-framework": "1.1.9" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ide.vscode": { "version": "1.2.5", "depth": 0, "source": "registry", "dependencies": {}, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.postprocessing": { "version": "3.2.2", @@ -72,7 +72,7 @@ "dependencies": { "com.unity.modules.physics": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.services.core": { "version": "1.0.1", @@ -81,7 +81,7 @@ "dependencies": { "com.unity.modules.unitywebrequest": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.test-framework": { "version": "1.1.31", @@ -92,7 +92,7 @@ "com.unity.modules.imgui": "1.0.0", "com.unity.modules.jsonserialize": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.textmeshpro": { "version": "3.0.6", @@ -101,7 +101,7 @@ "dependencies": { "com.unity.ugui": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.timeline": { "version": "1.5.2", @@ -113,7 +113,7 @@ "com.unity.modules.audio": "1.0.0", "com.unity.modules.particlesystem": "1.0.0" }, - "url": "https://packages.unity.com" + "url": "https://packages.unity.cn" }, "com.unity.ugui": { "version": "1.0.0", diff --git a/ProjectSettings/PackageManagerSettings.asset b/ProjectSettings/PackageManagerSettings.asset index be4a797..bc93024 100644 --- a/ProjectSettings/PackageManagerSettings.asset +++ b/ProjectSettings/PackageManagerSettings.asset @@ -20,7 +20,7 @@ MonoBehaviour: m_Registries: - m_Id: main m_Name: - m_Url: https://packages.unity.com + m_Url: https://packages.unity.cn m_Scopes: [] m_IsDefault: 1 m_Capabilities: 7 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 3598ae5..9b8fc7a 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2020.3.28f1 -m_EditorVersionWithRevision: 2020.3.28f1 (f5400f52e03f) +m_EditorVersion: 2020.3.24f1c2 +m_EditorVersionWithRevision: 2020.3.24f1c2 (f3616f7ef48a)