2024-07-04 12:43:10 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
|
|
|
|
|
|
namespace MU3.Data;
|
|
|
|
|
2024-07-22 16:45:36 +00:00
|
|
|
public class patch_DataStudioManager: DataStudioManager {
|
2024-07-04 12:43:10 +00:00
|
|
|
public static bool needSave;
|
2024-07-22 16:45:36 +00:00
|
|
|
private static string _fileName;
|
2024-07-04 12:43:10 +00:00
|
|
|
private static Dictionary<string, object> _dataCache;
|
|
|
|
|
|
|
|
private static extern bool orig_Deserialize<T>(string filePath, out T dsr) where T : new();
|
|
|
|
|
2024-07-22 16:45:36 +00:00
|
|
|
private static void initCache() {
|
|
|
|
using IniFile iniFile = new("mu3.ini");
|
|
|
|
_fileName = Path.Combine(iniFile.getValue("Extra", "CacheDir", "."), "data_cache.bin");
|
|
|
|
|
|
|
|
if(File.Exists(_fileName)) {
|
|
|
|
System.Console.WriteLine("Loading data cache...");
|
|
|
|
using FileStream serializationStream = File.OpenRead(_fileName);
|
2024-07-04 12:43:10 +00:00
|
|
|
_dataCache = (Dictionary<string, object>)new BinaryFormatter().Deserialize(serializationStream);
|
|
|
|
return;
|
|
|
|
}
|
2024-07-22 16:45:36 +00:00
|
|
|
_dataCache = new();
|
2024-07-04 12:43:10 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 16:45:36 +00:00
|
|
|
private static void saveCache() {
|
|
|
|
if(_fileName == "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(File.Exists(_fileName)) {
|
|
|
|
File.Delete(_fileName);
|
2024-07-04 12:43:10 +00:00
|
|
|
}
|
2024-07-22 16:45:36 +00:00
|
|
|
using FileStream serializationStream = File.OpenWrite(_fileName);
|
2024-07-04 12:43:10 +00:00
|
|
|
new BinaryFormatter().Serialize(serializationStream, _dataCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool Deserialize<T>(string filePath, out T dsr) where T : new() {
|
|
|
|
if(_dataCache == null) {
|
2024-07-22 16:45:36 +00:00
|
|
|
initCache();
|
2024-07-04 12:43:10 +00:00
|
|
|
}
|
|
|
|
if(_dataCache.ContainsKey(filePath)) {
|
|
|
|
dsr = (T)_dataCache[filePath];
|
|
|
|
return true;
|
|
|
|
}
|
2024-07-22 16:45:36 +00:00
|
|
|
if(orig_Deserialize(filePath, out dsr)) {
|
2024-07-04 12:43:10 +00:00
|
|
|
needSave = true;
|
|
|
|
_dataCache.Add(filePath, dsr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public extern bool orig_IsLoaded();
|
|
|
|
|
2024-07-22 16:45:36 +00:00
|
|
|
public new bool IsLoaded() {
|
2024-07-04 12:43:10 +00:00
|
|
|
if(orig_IsLoaded()) {
|
|
|
|
if(needSave) {
|
2024-07-22 16:45:36 +00:00
|
|
|
System.Console.WriteLine("Saving data cache...");
|
|
|
|
saveCache();
|
2024-07-04 12:43:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|