diff --git a/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs b/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs index 42f15192..e14c261b 100644 --- a/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs +++ b/NewHorizons/Builder/Props/NomaiText/NomaiTextBuilder.cs @@ -643,15 +643,12 @@ namespace NewHorizons.Builder.Props ArcCacheData[] cachedData = null; if (nhBody.Cache?.ContainsKey(cacheKey) ?? false) - { - var json = JsonConvert.SerializeObject(nhBody.Cache[cacheKey]); - cachedData = JsonConvert.DeserializeObject(json); - } + cachedData = nhBody.Cache.Get(cacheKey); Logger.LogWarning("CACHE DEBUG: cache key *" + cacheKey+"*"); Logger.LogWarning("CACHE DEBUG: cache is null? " + (nhBody.Cache == null)); Logger.LogWarning("CACHE DEBUG: cache contains key? " + (nhBody.Cache?.ContainsKey(cacheKey) ?? false)); - Logger.LogWarning("CACHE DEBUG: cache keys: " + String.Join(",", nhBody.Cache?.Keys)); + //Logger.LogWarning("CACHE DEBUG: cache keys: " + String.Join(",", nhBody.Cache?.Keys)); var arranger = nomaiWallText.gameObject.AddComponent(); @@ -733,7 +730,7 @@ namespace NewHorizons.Builder.Props zRotation = spiralManipulator.transform.localEulerAngles.z }).ToArray(); - nhBody.Cache[cacheKey] = cacheData; + nhBody.Cache.Set(cacheKey, cacheData); } } } diff --git a/NewHorizons/Utility/Cache.cs b/NewHorizons/Utility/Cache.cs index f4767a50..5860cc9e 100644 --- a/NewHorizons/Utility/Cache.cs +++ b/NewHorizons/Utility/Cache.cs @@ -1,34 +1,60 @@ +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 : Dictionary + public class Cache { - [NonSerialized] string filepath; + string filepath; + IModBehaviour mod; + Dictionary data = new Dictionary(); - public Cache(string cacheFilePath) + public Cache(IModBehaviour mod, string cacheFilePath) { + this.mod = mod; + filepath = cacheFilePath; - var existingEntries = NewHorizons.Main.Instance.ModHelper.Storage.Load>(filepath); + + 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? " + (existingEntries == null)); - Logger.LogWarning("CACHE DEBUG: Loaded cache keys: " + String.Join(",", existingEntries?.Keys)); - - if (existingEntries == null) return; - - foreach(var entry in existingEntries) - { - this[entry.Key] = entry.Value; - } + 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() { - NewHorizons.Main.Instance.ModHelper.Storage.Save>(this, filepath); + 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); } } } diff --git a/NewHorizons/Utility/NewHorizonBody.cs b/NewHorizons/Utility/NewHorizonBody.cs index e75bb562..2bd4370d 100644 --- a/NewHorizons/Utility/NewHorizonBody.cs +++ b/NewHorizons/Utility/NewHorizonBody.cs @@ -34,7 +34,7 @@ namespace NewHorizons.Utility } var pathWithoutExtension = RelativePath.Substring(0, RelativePath.LastIndexOf('.')); - Cache = new Cache(pathWithoutExtension+".nhcache"); + Cache = new Cache(Mod, pathWithoutExtension+".nhcache"); } public void UnloadCache(bool writeBeforeUnload=false)