using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace MU3.Data; public class DataStudioManager { public static bool needSave; private static Dictionary _dataCache; private static extern bool orig_Deserialize(string filePath, out T dsr) where T : new(); private static void InitCache() { if(File.Exists("data_cache.bin")) { System.Console.WriteLine("Loading cache..."); using FileStream serializationStream = File.OpenRead("data_cache.bin"); _dataCache = (Dictionary)new BinaryFormatter().Deserialize(serializationStream); return; } _dataCache = new Dictionary(); } private static void SaveCache() { if(File.Exists("data_cache.bin")) { File.Delete("data_cache.bin"); } using FileStream serializationStream = File.OpenWrite("data_cache.bin"); new BinaryFormatter().Serialize(serializationStream, _dataCache); } private static bool Deserialize(string filePath, out T dsr) where T : new() { if(_dataCache == null) { InitCache(); } if(_dataCache.ContainsKey(filePath)) { dsr = (T)_dataCache[filePath]; return true; } if(orig_Deserialize(filePath, out dsr)) { needSave = true; _dataCache.Add(filePath, dsr); return true; } return false; } public extern bool orig_IsLoaded(); public bool IsLoaded() { if(orig_IsLoaded()) { if(needSave) { System.Console.WriteLine("Saving cache..."); SaveCache(); } return true; } return false; } }