From 5351f82f09f5665b15942f8f8336d19dae9e1832 Mon Sep 17 00:00:00 2001 From: Ji O Date: Sat, 4 Oct 2025 06:21:48 +0900 Subject: [PATCH] Load assets via Stream * Load assets via Stream, Compatible with new versions of unencrypted DB --- Assets/Scripts/Settings/UISettingsAssets.cs | 11 ++-- Assets/Scripts/UmaAssetBundleStream.cs | 26 ++++++++ Assets/Scripts/UmaAssetBundleStream.cs.meta | 11 ++++ Assets/Scripts/UmaAssetManager.cs | 5 +- .../UmaDatabase/UmaDatabaseController.cs | 63 ++++++++++++++----- .../Scripts/UmaDatabase/UmaDatabaseEntry.cs | 41 +++++++----- 6 files changed, 116 insertions(+), 41 deletions(-) create mode 100644 Assets/Scripts/UmaAssetBundleStream.cs create mode 100644 Assets/Scripts/UmaAssetBundleStream.cs.meta diff --git a/Assets/Scripts/Settings/UISettingsAssets.cs b/Assets/Scripts/Settings/UISettingsAssets.cs index 5bb0fbf..eeda84a 100644 --- a/Assets/Scripts/Settings/UISettingsAssets.cs +++ b/Assets/Scripts/Settings/UISettingsAssets.cs @@ -68,9 +68,9 @@ public class UISettingsAssets : MonoBehaviour Directory.CreateDirectory(Path.GetDirectoryName(outName)); //修改(导出解密ab包) - var decryptedData = AssetBundleDecryptor.DecryptFileToBytes(asset.FilePath, asset.FKey); - File.WriteAllBytes(outName, decryptedData); - + using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey); + using var outputStream = new FileStream(outName, FileMode.Create); + assetStream.CopyTo(outputStream); } } UmaViewerUI.Instance.ShowMessage($"{LoadedAssetEntries.Count} files copied", UIMessageType.Success); @@ -102,8 +102,9 @@ public class UISettingsAssets : MonoBehaviour Directory.CreateDirectory(Path.GetDirectoryName(outName)); //修改(导出解密ab包) - var decryptedData = AssetBundleDecryptor.DecryptFileToBytes(asset.FilePath, asset.FKey); - File.WriteAllBytes(outName, decryptedData); + using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey); + using var outputStream = new FileStream(outName, FileMode.Create); + assetStream.CopyTo(outputStream); } } diff --git a/Assets/Scripts/UmaAssetBundleStream.cs b/Assets/Scripts/UmaAssetBundleStream.cs new file mode 100644 index 0000000..9d9b63b --- /dev/null +++ b/Assets/Scripts/UmaAssetBundleStream.cs @@ -0,0 +1,26 @@ +using System.IO; + +public class UmaAssetBundleStream : FileStream +{ + private const int headerSize = 256; + private readonly byte[] baseKeys = Config.Instance.ABKey; + private readonly byte[] keys; + + public UmaAssetBundleStream(string filename, byte[] keys) : base(filename, FileMode.Open, FileAccess.Read) + { + this.keys = keys; + } + + public override int Read(byte[] buffer, int offset, int count) + { + long StartPosition = Position; + int res = base.Read(buffer, offset, count); + + for (long i = (StartPosition < headerSize ? headerSize - StartPosition : 0); i < count; i++) + { + buffer[i] ^= keys[(StartPosition + i) % (baseKeys.Length * 8)]; + } + + return res; + } +} diff --git a/Assets/Scripts/UmaAssetBundleStream.cs.meta b/Assets/Scripts/UmaAssetBundleStream.cs.meta new file mode 100644 index 0000000..7b4a0db --- /dev/null +++ b/Assets/Scripts/UmaAssetBundleStream.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bb42c52cc9462b44680480ec4f16548d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UmaAssetManager.cs b/Assets/Scripts/UmaAssetManager.cs index b5f3234..34ede96 100644 --- a/Assets/Scripts/UmaAssetManager.cs +++ b/Assets/Scripts/UmaAssetManager.cs @@ -133,14 +133,13 @@ public class UmaAssetManager : MonoBehaviour else { AssetBundle bundle; - if (string.IsNullOrEmpty(entry.Key) || long.Parse(entry.Key) == 0) + if (entry.Key == 0) { bundle = AssetBundle.LoadFromFile(filePath); } else { - var data = AssetBundleDecryptor.DecryptFileToBytes(filePath, entry.FKey); - bundle = AssetBundle.LoadFromMemory(data); + bundle = AssetBundle.LoadFromStream(new UmaAssetBundleStream(filePath, entry.FKey)); } if (!bundle) { diff --git a/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs b/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs index e27302f..0c071f3 100644 --- a/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs +++ b/Assets/Scripts/UmaDatabase/UmaDatabaseController.cs @@ -59,9 +59,9 @@ public class UmaDatabaseController { try { - if(Config.Instance.WorkMode == WorkMode.Standalone) + if (Config.Instance.WorkMode == WorkMode.Standalone) { - if(!File.Exists($@"{Config.Instance.MainPath}/meta_umaviewer")) throw new Exception(); + if (!File.Exists($@"{Config.Instance.MainPath}/meta_umaviewer")) throw new Exception(); metaDb = new SqliteConnection($@"Data Source={Config.Instance.MainPath}/meta_umaviewer;"); masterDb = new SqliteConnection($@"Data Source={Config.Instance.MainPath}/master/master_umaviewer.mdb;"); } @@ -115,9 +115,9 @@ public class UmaDatabaseController { CloseAllConnection(); var msg = $"Database not found at: {Config.Instance.MainPath}"; - if(Config.Instance.WorkMode == WorkMode.Standalone) + if (Config.Instance.WorkMode == WorkMode.Standalone) { - if(Config.Instance.Region == Region.Global) + if (Config.Instance.Region == Region.Global) { msg = "Only `Default` mode is supported for now. Please change Work Mode to Default in `Other` settings."; } @@ -139,16 +139,39 @@ public class UmaDatabaseController //修改(载入通用服装ColorSet相关) static List ReadAllCharaDressColor(SqliteConnection conn) { + SqliteCommand sqlite_cmd = conn.CreateCommand(); + sqlite_cmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name='chara_dress_color_set'"; + SqliteDataReader sqlite_datareader = sqlite_cmd.ExecuteReader(); + bool HasTable = sqlite_datareader.HasRows; + + if (!HasTable) + { + return new List(); + } + return ReadMaster(conn, "SELECT * FROM chara_dress_color_set"); } - static Dictionary ReadMeta(SqliteConnection conn) + static Dictionary ReadMeta(SqliteConnection conn) { + bool HasEncryption = false; SqliteCommand sqlite_cmd = conn.CreateCommand(); - sqlite_cmd.CommandText = "SELECT m,n,h,d FROM a WHERE d IS NOT NULL"; //filter out manifest entries, whitch have null prerequisites + sqlite_cmd.CommandText = "SELECT name FROM pragma_table_info('a') WHERE name = 'e'"; SqliteDataReader sqlite_datareader = sqlite_cmd.ExecuteReader(); + HasEncryption = sqlite_datareader.HasRows; + + sqlite_cmd = conn.CreateCommand(); + if (HasEncryption) + { + sqlite_cmd.CommandText = "SELECT m,n,h,d,e FROM a WHERE d IS NOT NULL"; + } + else + { + sqlite_cmd.CommandText = "SELECT m,n,h,d FROM a WHERE d IS NOT NULL"; //filter out manifest entries, whitch have null prerequisites + } + sqlite_datareader = sqlite_cmd.ExecuteReader(); Dictionary meta = new Dictionary(); while (sqlite_datareader.Read()) { @@ -161,7 +184,13 @@ public class UmaDatabaseController var name = sqlite_datareader.GetString(1); var url = sqlite_datareader.GetString(2); var prerequisites = sqlite_datareader.GetString(3); - + var key = 0L; + + if (HasEncryption) + { + key = sqlite_datareader.GetInt64(4); + } + if (string.IsNullOrEmpty(name)) { throw new Exception($"Invalid entry name {name} or URL {url}"); @@ -172,7 +201,8 @@ public class UmaDatabaseController Type = type, Name = name, Url = url, - Prerequisites = prerequisites + Prerequisites = prerequisites, + Key = key }; } else @@ -180,12 +210,13 @@ public class UmaDatabaseController Debug.LogWarning($"Unrecognized EntryType Enum Value :{typestr}"); } } - catch(Exception e) + catch (Exception e) { Debug.LogError($"Error caught while reading Entry : \n {e}"); } - - if (entry != null && !meta.ContainsKey(entry.Name)) { + + if (entry != null && !meta.ContainsKey(entry.Name)) + { meta.Add(entry.Name, entry); } @@ -237,7 +268,7 @@ public class UmaDatabaseController string h = Sqlite3MC.ColumnText(stmt, 2); string c = Sqlite3MC.ColumnText(stmt, 3); string d = Sqlite3MC.ColumnText(stmt, 4); - string e = Sqlite3MC.ColumnText(stmt, 5); + long e = Sqlite3MC.ColumnInt64(stmt, 5); if (string.IsNullOrEmpty(m)) { @@ -300,7 +331,7 @@ public class UmaDatabaseController var str = ReadMaster(conn, "SELECT text FROM text_data LIMIT 1"); //check if first character of the first text_data entry is in English bool isAscii = str[0][0].ToString()[0] < 128; - Region detectedRegion = isAscii? Region.Global : Region.Jp; + Region detectedRegion = isAscii ? Region.Global : Region.Jp; if (Config.Instance.Region != detectedRegion) { @@ -421,15 +452,15 @@ public class UmaDatabaseController public static DataRow ReadMobDressColor(string mobid) { - var results= ReadMaster(instance.masterDb, $"SELECT * FROM mob_dress_color_set WHERE id LIKE {mobid}"); + var results = ReadMaster(instance.masterDb, $"SELECT * FROM mob_dress_color_set WHERE id LIKE {mobid}"); return results.Count > 0 ? results[0] : null; } public static DataRow ReadMobHairColor(string colorid) { - var results= ReadMaster(instance.masterDb, $"SELECT * FROM mob_hair_color_set WHERE id LIKE {colorid}"); - foreach(var data in results) + var results = ReadMaster(instance.masterDb, $"SELECT * FROM mob_hair_color_set WHERE id LIKE {colorid}"); + foreach (var data in results) { return data; } diff --git a/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs b/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs index 4777599..baf3dcf 100644 --- a/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs +++ b/Assets/Scripts/UmaDatabase/UmaDatabaseEntry.cs @@ -1,8 +1,9 @@ -using System.IO; -using UnityEngine; -using System.Linq; +using System; using System.Collections.Generic; -using System; +using System.IO; +using System.Linq; +using System.Windows.Forms; +using UnityEngine; using Object = UnityEngine.Object; public class UmaDatabaseEntry @@ -12,7 +13,7 @@ public class UmaDatabaseEntry public string Url; public string Checksum; public string Prerequisites; - public string Key; + public long Key; private byte[] fKey; [NonSerialized] @@ -22,25 +23,31 @@ public class UmaDatabaseEntry { get { - if (fKey == null && !string.IsNullOrEmpty(Key)) + if (fKey == null && Key != 0) { var baseKeys = Config.Instance.ABKey; int baseLen = baseKeys.Length; - int keysLen = baseLen * 8; - byte[] keys = new byte[keysLen]; - for (int i = 0; i < baseLen; ++i) - { - byte[] keyBytes = BitConverter.GetBytes(long.Parse(Key)); - if (!BitConverter.IsLittleEndian) - Array.Reverse(keyBytes); - byte b = baseKeys[i]; - int baseOffset = i << 3; // i * 8 - for (int j = 0; j < 8; ++j) + var keys = new byte[baseLen * 8]; + + if (Key != 0) + { + byte[] keyBytes = BitConverter.GetBytes(Key); + if (!BitConverter.IsLittleEndian) { - keys[baseOffset + j] = (byte)(b ^ keyBytes[j]); + Array.Reverse(keyBytes); + } + for (int i = 0; i < baseLen; ++i) + { + byte b = baseKeys[i]; + int baseOffset = i << 3; // i * 8 + for (int j = 0; j < 8; ++j) + { + keys[baseOffset + j] = (byte)(b ^ keyBytes[j]); + } } } + fKey = keys; } return fKey;