using Newtonsoft.Json; using OWML.Common; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; namespace NewHorizons.Utility { public class Cache { string filepath; IModBehaviour mod; Dictionary data = new Dictionary(); public Cache(IModBehaviour mod, string cacheFilePath) { this.mod = mod; filepath = cacheFilePath; var json = File.ReadAllText(mod.ModHelper.Manifest.ModFolderPath + cacheFilePath); data = JsonConvert.DeserializeObject>(json); // the above is exactly the same thing that the below does, but the below for some reason always returns null. no clue why // data = mod.ModHelper.Storage.Load>(filepath); if (data == null) { Logger.LogWarning("Failed to load cache! Cache path: " + cacheFilePath); data = new Dictionary(); } Logger.LogWarning("CACHE DEBUG: Cache path: " + cacheFilePath); Logger.LogWarning("CACHE DEBUG: Loaded cache == null? " + (data == null)); Logger.LogWarning("CACHE DEBUG: Loaded cache keys: " + String.Join(",", data?.Keys ?? new Dictionary().Keys)); } public void WriteToFile() { mod.ModHelper.Storage.Save>(data, filepath); } public bool ContainsKey(string key) { return data.ContainsKey(key); } public T Get(string key) { var json = data[key]; return JsonConvert.DeserializeObject(json); } public void Set(string key, T value) { data[key] = JsonConvert.SerializeObject(value); } } }