From ded80504260f042c46a388ec17e5cd92c6cc45ba Mon Sep 17 00:00:00 2001 From: Razmoth <32140579+Razmoth@users.noreply.github.com> Date: Sat, 12 Aug 2023 14:06:45 +0400 Subject: [PATCH] - [Core] renamed `AlchemyStars` to `FakeHeader` - [Core] added new entry. --- AssetStudio/FileReader.cs | 7 +- AssetStudio/GameManager.cs | 8 ++- AssetStudio/ImportHelper.cs | 124 +++++++++++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 7 deletions(-) diff --git a/AssetStudio/FileReader.cs b/AssetStudio/FileReader.cs index 48b0030..d79944f 100644 --- a/AssetStudio/FileReader.cs +++ b/AssetStudio/FileReader.cs @@ -149,8 +149,8 @@ namespace AssetStudio case GameType.OPFP: reader = ParseOPFP(reader); break; - case GameType.AlchemyStars: - reader = ParseAlchemyStars(reader); + case GameType.FakeHeader: + reader = ParseFakeHeader(reader); break; case GameType.FantasyOfWind: reader = DecryptFantasyOfWind(reader); @@ -167,6 +167,9 @@ namespace AssetStudio case GameType.DreamscapeAlbireo: reader = DecryptDreamscapeAlbireo(reader); break; + case GameType.ImaginaryFest: + reader = DecryptImaginaryFest(reader); + break; } } if (reader.FileType == FileType.BundleFile && game.Type.IsBlockFile()) diff --git a/AssetStudio/GameManager.cs b/AssetStudio/GameManager.cs index e19087d..b667b09 100644 --- a/AssetStudio/GameManager.cs +++ b/AssetStudio/GameManager.cs @@ -28,13 +28,14 @@ namespace AssetStudio Games.Add(index++, new Game(GameType.Naraka)); Games.Add(index++, new Game(GameType.EnsembleStars)); Games.Add(index++, new Game(GameType.OPFP)); - Games.Add(index++, new Game(GameType.AlchemyStars)); + Games.Add(index++, new Game(GameType.FakeHeader)); Games.Add(index++, new Game(GameType.FantasyOfWind)); Games.Add(index++, new Game(GameType.ShiningNikki)); Games.Add(index++, new Game(GameType.HelixWaltz2)); Games.Add(index++, new Game(GameType.NetEase)); Games.Add(index++, new Game(GameType.AnchorPanic)); Games.Add(index++, new Game(GameType.DreamscapeAlbireo)); + Games.Add(index++, new Game(GameType.ImaginaryFest)); } public static Game GetGame(GameType gameType) => GetGame((int)gameType); public static Game GetGame(int index) @@ -134,13 +135,14 @@ namespace AssetStudio Naraka, EnsembleStars, OPFP, - AlchemyStars, + FakeHeader, FantasyOfWind, ShiningNikki, HelixWaltz2, NetEase, AnchorPanic, - DreamscapeAlbireo + DreamscapeAlbireo, + ImaginaryFest, } public static class GameTypes diff --git a/AssetStudio/ImportHelper.cs b/AssetStudio/ImportHelper.cs index 2915d34..ebd10df 100644 --- a/AssetStudio/ImportHelper.cs +++ b/AssetStudio/ImportHelper.cs @@ -286,7 +286,7 @@ namespace AssetStudio return new FileReader(reader.FullPath, stream); } - public static FileReader ParseAlchemyStars(FileReader reader) + public static FileReader ParseFakeHeader(FileReader reader) { var stream = reader.BaseStream; var data = reader.ReadBytes(0x1000); @@ -432,7 +432,7 @@ namespace AssetStudio var idx = data.Search("UnityFS"); if (idx != -1) { - return ParseAlchemyStars(reader); + return ParseFakeHeader(reader); } var key = GetKey(Path.GetFileNameWithoutExtension(reader.FileName)); @@ -591,5 +591,125 @@ namespace AssetStudio static uint Scrample(uint value) => (value >> 5) & 0xFFE000 | (value >> 29) | (value << 14) & 0xFF000000 | (8 * value) & 0x1FF8; } + + public static FileReader DecryptImaginaryFest(FileReader reader) + { + const string dataRoot = "data"; + var key = new byte[] { 0xBD, 0x65, 0xF2, 0x4F, 0xBE, 0xD1, 0x36, 0xD4, 0x95, 0xFE, 0x64, 0x94, 0xCB, 0xD3, 0x7E, 0x91, 0x57, 0xB7, 0x94, 0xB7, 0x9F, 0x70, 0xB2, 0xA9, 0xA0, 0xD5, 0x4E, 0x36, 0xC6, 0x40, 0xE0, 0x2F, 0x4E, 0x6E, 0x76, 0x6D, 0xCD, 0xAE, 0xEA, 0x05, 0x13, 0x6B, 0xA7, 0x84, 0xFF, 0xED, 0x90, 0x91, 0x15, 0x7E, 0xF1, 0xF8, 0xA5, 0x9C, 0xB6, 0xDE, 0xF9, 0x56, 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0x98, 0xB2, 0xFC, 0xFB, 0xF5, 0x9A, 0x19, 0x03, 0xB0, 0xB7, 0xAC, 0xAE, 0x8B }; + + var signatureBytes = reader.ReadBytes(8); + var signature = Encoding.UTF8.GetString(signatureBytes[..7]); + if (signature == "UnityFS") + { + reader.Position = 0; + return reader; + } + + if (signatureBytes[7] != 0) + { + var xorKey = signatureBytes[7]; + for (int i = 0; i < signatureBytes.Length; i++) + { + signatureBytes[i] ^= xorKey; + } + signature = Encoding.UTF8.GetString(signatureBytes[..7]); + if (signature == "UnityFS") + { + var remaining = reader.ReadBytes((int)reader.Remaining); + for (int i = 0; i < remaining.Length; i++) + { + remaining[i] ^= xorKey; + } + + var stream = new MemoryStream(); + stream.Write(signatureBytes); + stream.Write(remaining); + stream.Position = 0; + return new FileReader(reader.FullPath, stream); + } + } + + reader.Position = 0; + + var paths = reader.FullPath.Split(Path.DirectorySeparatorChar); + var startIdx = Array.FindIndex(paths, x => x == dataRoot); + if (startIdx != -1 && startIdx != paths.Length - 1) + { + var path = string.Join(Path.AltDirectorySeparatorChar, paths[(startIdx+1)..]); + var offset = GetLoadAssetBundleOffset(path); + if (offset > 0 && offset < reader.Length) + { + reader.Position = offset; + signature = reader.ReadStringToNull(7); + if (signature == "UnityFS") + { + reader.Position = offset; + return new FileReader(reader.FullPath, new MemoryStream(reader.ReadBytes((int)reader.Remaining))); + } + } + reader.Position = 0; + var data = reader.ReadBytes((int)reader.Remaining); + var key_value = GetHashCode(path); + Decrypt(data, key_value); + return new FileReader(reader.FullPath, new MemoryStream(data)); + } + + reader.Position = 0; + return reader; + + int GetLoadAssetBundleOffset(string str) + { + var hashCode = GetHashCode(str); + var offset = 1; + var index = -4; + do + { + var s = hashCode >> (index + 8); + index += 4; + offset += s % 0x80 | 0x80; + } + while (4 * (hashCode & 3) != index); + return offset; + } + + int GetHashCode(string str, int pattern = 0) + { + var table = new int[4]; + + var len = str.Length - 1; + for (int i = 0; i < table.Length; i++) + { + var c = str[len & ~(len >> 0x1F)]; + table[i] = GetJammingInt(pattern + c); + pattern += table.Length; + len--; + } + + var shift = 0; + for (int i = str.Length - 1; i >= 0; i--) + { + var c = str[i]; + shift = (shift + i) ^ c; + table[i % table.Length] += c << shift; + } + return table[0] ^ table[1] ^ table[2] ^ table[3]; + } + + int GetJammingInt(int top_index) + { + return BinaryPrimitives.TryReadInt32LittleEndian(key.AsSpan(top_index), out var value) ? value : -1; + } + + void Decrypt(byte[] bytes, int key_value) + { + var step = (key_value >> 8) % 3 + 1; + for (int i = 0; i < bytes.Length; i++) + { + var index = (byte)key_value; + bytes[i] ^= key[index]; + key_value += step; + } + } + } } }