Add UpdateDatabase Button

This commit is contained in:
CroakFang
2022-12-21 17:54:36 +08:00
parent c6434cadbc
commit 49305e7622
10 changed files with 497 additions and 62 deletions
+2 -15
View File
@@ -1,5 +1,4 @@
using K4os.Compression.LZ4.Streams;
using System;
using System;
using System.IO;
using UnityEngine;
@@ -107,19 +106,7 @@ public class BSVReader
{
try
{
byte[] input;
if (isCompressed)
{
var source = new MemoryStream(File.ReadAllBytes(path));
var decoder = LZ4Stream.Decode(source);
var target = new MemoryStream();
decoder.CopyTo(target);
input = target.ToArray();
}
else
{
input = File.ReadAllBytes(path);
}
byte[] input = isCompressed? LZ4Util.DecompressFromFile(path): File.ReadAllBytes(path);
if (mode == ReadMode.Stream)
{
+54
View File
@@ -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();
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ad28139c90f43ce4eba7c018a861ca37
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+103 -30
View File
@@ -1,4 +1,5 @@
using Mono.Data.Sqlite;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.IO;
@@ -11,11 +12,12 @@ using static ManifestCategory;
public class ManifestDB
{
public SqliteConnection MetaDB;
public string DBPath;
public static string MANIFEST_ROOT_URL = "https://prd-storage-app-umamusume.akamaized.net/dl/resources/Manifest";
public ManifestDB(string DBPath)
public static string DBPath;
public const string DefaultDBPath = "umamusume/meta";
public Action<string> callback;
public ManifestDB(string DBPath = DefaultDBPath)
{
this.DBPath = DBPath;
ManifestDB.DBPath = DBPath;
if (File.Exists(DBPath))
{
MetaDB = new SqliteConnection($@"Data Source={DBPath}");
@@ -43,31 +45,68 @@ public class ManifestDB
}
}
public IEnumerator UpdateResourceVersion(Action<string> 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 GenerateMetaDB(ulong rootSize, ulong rootCheckSum)
public IEnumerator UpdateMetaDB()
{
if (MetaDB == null || MetaDB.State == System.Data.ConnectionState.Closed) {
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;
}
//Insert root
ManifestEntry rootEntry = new ManifestEntry(Encoding.UTF8.GetBytes("root"), null, 0, 0, rootSize, rootCheckSum, "", 3);
rootEntry.hname = rootEntry.CalHameString();
SqliteCommand command = new SqliteCommand(MetaDB);
command.CommandText = "DELETE FROM a";
command.ExecuteNonQuery();
//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)
@@ -91,14 +130,15 @@ public class ManifestDB
}
using (SqliteTransaction tran = MetaDB.BeginTransaction())
{
foreach (var entry in assetEntrys)
for (int i = 0; i < assetEntrys.Length; i++)
{
index++;
UpdateManifestEntry(index, "manifest", entry, ref command);
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)
{
@@ -109,17 +149,54 @@ public class ManifestDB
Debug.LogError("Read AssetManifest Error");
continue;
}
using (SqliteTransaction tran = MetaDB.BeginTransaction())
using SqliteTransaction tran = MetaDB.BeginTransaction();
for (int i = 0; i < assets.Length; i++)
{
foreach (var entry in assets)
{
index++;
UpdateManifestEntry(index, assetEntry.tname, entry, ref command);
}
tran.Commit();
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));
}
MetaDB.Close();
}
private void UpdateManifestEntry(int index, string type, ManifestEntry entry, ref SqliteCommand command)
@@ -180,6 +257,7 @@ public class ManifestDB
var path = GetManifestPath(hash);
if (!File.Exists(path))
{
this.callback?.Invoke($"Downloading Manifest :{hash}");
yield return DownloadManifest(hash);
}
if (File.Exists(path))
@@ -190,7 +268,7 @@ public class ManifestDB
public IEnumerator DownloadManifest(string hash)
{
UnityWebRequest www = UnityWebRequest.Get(GetManifestRequestUrl(hash));
UnityWebRequest www = UnityWebRequest.Get(UmaViewerDownload.GetManifestRequestUrl(hash));
www.timeout = 15;
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
@@ -206,18 +284,13 @@ public class ManifestDB
public string GetManifestPath(string hash, bool createPath = true)
{
string path = $"{Path.GetDirectoryName(DBPath)}/dat/{hash.Substring(0, 2)}";
if (createPath && !Directory.Exists(path))
if (createPath)
{
Directory.CreateDirectory(path);
}
return path + $"/{hash}";
}
public static string GetManifestRequestUrl(string hash)
{
return $"{MANIFEST_ROOT_URL}/{hash.Substring(0, 2)}/{hash}";
}
public static ManifestEntry GetLocalPlatformEntry(ManifestEntry[] platformEntrys)
{
foreach (var entry in platformEntrys)
+1 -2
View File
@@ -58,8 +58,7 @@ public struct ManifestEntry
public static string CalHNameString(ulong checksum, ulong size, byte[] name)
{
var bytes = CalHName(checksum, size, name);
Base32 base32 = new Base32();
return base32.Encode(bytes);
return new Base32().Encode(bytes);
}
public static string CalHameString(ulong checksum, ulong size, string name)
@@ -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);
+19
View File
@@ -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<string> 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}";
}
}
+2
View File
@@ -6,6 +6,8 @@ using System;
using Newtonsoft.Json.Linq;
using System.Collections;
using static ManifestCategory;
using BaseNcoding;
using System.Text;
public class UmaViewerMain : MonoBehaviour
{
+10 -2
View File
@@ -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);
}
}