Load assets via Stream

* Load assets via Stream, Compatible with new versions of unencrypted DB
This commit is contained in:
Ji O
2025-10-04 06:21:48 +09:00
committed by GitHub
parent eb35f2db0f
commit 5351f82f09
6 changed files with 116 additions and 41 deletions
+6 -5
View File
@@ -68,9 +68,9 @@ public class UISettingsAssets : MonoBehaviour
Directory.CreateDirectory(Path.GetDirectoryName(outName)); Directory.CreateDirectory(Path.GetDirectoryName(outName));
//修改(导出解密ab包) //修改(导出解密ab包)
var decryptedData = AssetBundleDecryptor.DecryptFileToBytes(asset.FilePath, asset.FKey); using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey);
File.WriteAllBytes(outName, decryptedData); using var outputStream = new FileStream(outName, FileMode.Create);
assetStream.CopyTo(outputStream);
} }
} }
UmaViewerUI.Instance.ShowMessage($"{LoadedAssetEntries.Count} files copied", UIMessageType.Success); UmaViewerUI.Instance.ShowMessage($"{LoadedAssetEntries.Count} files copied", UIMessageType.Success);
@@ -102,8 +102,9 @@ public class UISettingsAssets : MonoBehaviour
Directory.CreateDirectory(Path.GetDirectoryName(outName)); Directory.CreateDirectory(Path.GetDirectoryName(outName));
//修改(导出解密ab包) //修改(导出解密ab包)
var decryptedData = AssetBundleDecryptor.DecryptFileToBytes(asset.FilePath, asset.FKey); using var assetStream = new UmaAssetBundleStream(asset.FilePath, asset.FKey);
File.WriteAllBytes(outName, decryptedData); using var outputStream = new FileStream(outName, FileMode.Create);
assetStream.CopyTo(outputStream);
} }
} }
+26
View File
@@ -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;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb42c52cc9462b44680480ec4f16548d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+2 -3
View File
@@ -133,14 +133,13 @@ public class UmaAssetManager : MonoBehaviour
else else
{ {
AssetBundle bundle; AssetBundle bundle;
if (string.IsNullOrEmpty(entry.Key) || long.Parse(entry.Key) == 0) if (entry.Key == 0)
{ {
bundle = AssetBundle.LoadFromFile(filePath); bundle = AssetBundle.LoadFromFile(filePath);
} }
else else
{ {
var data = AssetBundleDecryptor.DecryptFileToBytes(filePath, entry.FKey); bundle = AssetBundle.LoadFromStream(new UmaAssetBundleStream(filePath, entry.FKey));
bundle = AssetBundle.LoadFromMemory(data);
} }
if (!bundle) if (!bundle)
{ {
@@ -59,9 +59,9 @@ public class UmaDatabaseController
{ {
try 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;"); metaDb = new SqliteConnection($@"Data Source={Config.Instance.MainPath}/meta_umaviewer;");
masterDb = new SqliteConnection($@"Data Source={Config.Instance.MainPath}/master/master_umaviewer.mdb;"); masterDb = new SqliteConnection($@"Data Source={Config.Instance.MainPath}/master/master_umaviewer.mdb;");
} }
@@ -115,9 +115,9 @@ public class UmaDatabaseController
{ {
CloseAllConnection(); CloseAllConnection();
var msg = $"Database not found at: {Config.Instance.MainPath}"; 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."; 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相关) //修改(载入通用服装ColorSet相关)
static List<DataRow> ReadAllCharaDressColor(SqliteConnection conn) static List<DataRow> 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<DataRow>();
}
return ReadMaster(conn, "SELECT * FROM chara_dress_color_set"); return ReadMaster(conn, "SELECT * FROM chara_dress_color_set");
} }
static Dictionary<string,UmaDatabaseEntry> ReadMeta(SqliteConnection conn) static Dictionary<string, UmaDatabaseEntry> ReadMeta(SqliteConnection conn)
{ {
bool HasEncryption = false;
SqliteCommand sqlite_cmd = conn.CreateCommand(); 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(); 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<string, UmaDatabaseEntry> meta = new Dictionary<string, UmaDatabaseEntry>(); Dictionary<string, UmaDatabaseEntry> meta = new Dictionary<string, UmaDatabaseEntry>();
while (sqlite_datareader.Read()) while (sqlite_datareader.Read())
{ {
@@ -161,7 +184,13 @@ public class UmaDatabaseController
var name = sqlite_datareader.GetString(1); var name = sqlite_datareader.GetString(1);
var url = sqlite_datareader.GetString(2); var url = sqlite_datareader.GetString(2);
var prerequisites = sqlite_datareader.GetString(3); var prerequisites = sqlite_datareader.GetString(3);
var key = 0L;
if (HasEncryption)
{
key = sqlite_datareader.GetInt64(4);
}
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
{ {
throw new Exception($"Invalid entry name {name} or URL {url}"); throw new Exception($"Invalid entry name {name} or URL {url}");
@@ -172,7 +201,8 @@ public class UmaDatabaseController
Type = type, Type = type,
Name = name, Name = name,
Url = url, Url = url,
Prerequisites = prerequisites Prerequisites = prerequisites,
Key = key
}; };
} }
else else
@@ -180,12 +210,13 @@ public class UmaDatabaseController
Debug.LogWarning($"Unrecognized EntryType Enum Value :{typestr}"); Debug.LogWarning($"Unrecognized EntryType Enum Value :{typestr}");
} }
} }
catch(Exception e) catch (Exception e)
{ {
Debug.LogError($"Error caught while reading Entry : \n {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); meta.Add(entry.Name, entry);
} }
@@ -237,7 +268,7 @@ public class UmaDatabaseController
string h = Sqlite3MC.ColumnText(stmt, 2); string h = Sqlite3MC.ColumnText(stmt, 2);
string c = Sqlite3MC.ColumnText(stmt, 3); string c = Sqlite3MC.ColumnText(stmt, 3);
string d = Sqlite3MC.ColumnText(stmt, 4); string d = Sqlite3MC.ColumnText(stmt, 4);
string e = Sqlite3MC.ColumnText(stmt, 5); long e = Sqlite3MC.ColumnInt64(stmt, 5);
if (string.IsNullOrEmpty(m)) if (string.IsNullOrEmpty(m))
{ {
@@ -300,7 +331,7 @@ public class UmaDatabaseController
var str = ReadMaster(conn, "SELECT text FROM text_data LIMIT 1"); var str = ReadMaster(conn, "SELECT text FROM text_data LIMIT 1");
//check if first character of the first text_data entry is in English //check if first character of the first text_data entry is in English
bool isAscii = str[0][0].ToString()[0] < 128; 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) if (Config.Instance.Region != detectedRegion)
{ {
@@ -421,15 +452,15 @@ public class UmaDatabaseController
public static DataRow ReadMobDressColor(string mobid) 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; return results.Count > 0 ? results[0] : null;
} }
public static DataRow ReadMobHairColor(string colorid) public static DataRow ReadMobHairColor(string colorid)
{ {
var results= ReadMaster(instance.masterDb, $"SELECT * FROM mob_hair_color_set WHERE id LIKE {colorid}"); var results = ReadMaster(instance.masterDb, $"SELECT * FROM mob_hair_color_set WHERE id LIKE {colorid}");
foreach(var data in results) foreach (var data in results)
{ {
return data; return data;
} }
+24 -17
View File
@@ -1,8 +1,9 @@
using System.IO; using System;
using UnityEngine;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System; using System.IO;
using System.Linq;
using System.Windows.Forms;
using UnityEngine;
using Object = UnityEngine.Object; using Object = UnityEngine.Object;
public class UmaDatabaseEntry public class UmaDatabaseEntry
@@ -12,7 +13,7 @@ public class UmaDatabaseEntry
public string Url; public string Url;
public string Checksum; public string Checksum;
public string Prerequisites; public string Prerequisites;
public string Key; public long Key;
private byte[] fKey; private byte[] fKey;
[NonSerialized] [NonSerialized]
@@ -22,25 +23,31 @@ public class UmaDatabaseEntry
{ {
get get
{ {
if (fKey == null && !string.IsNullOrEmpty(Key)) if (fKey == null && Key != 0)
{ {
var baseKeys = Config.Instance.ABKey; var baseKeys = Config.Instance.ABKey;
int baseLen = baseKeys.Length; 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]; var keys = new byte[baseLen * 8];
int baseOffset = i << 3; // i * 8
for (int j = 0; j < 8; ++j) 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; fKey = keys;
} }
return fKey; return fKey;