fixed the Cache class not being able to handle loading arbitrary data

This commit is contained in:
FreezeDriedMangoes 2023-02-01 12:40:13 -05:00
parent 7061a23e1f
commit c03938a1ab
3 changed files with 44 additions and 21 deletions

View File

@ -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<ArcCacheData[]>(json);
}
cachedData = nhBody.Cache.Get<ArcCacheData[]>(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<NomaiTextArcArranger>();
@ -733,7 +730,7 @@ namespace NewHorizons.Builder.Props
zRotation = spiralManipulator.transform.localEulerAngles.z
}).ToArray();
nhBody.Cache[cacheKey] = cacheData;
nhBody.Cache.Set(cacheKey, cacheData);
}
}
}

View File

@ -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<string, Object>
public class Cache
{
[NonSerialized] string filepath;
string filepath;
IModBehaviour mod;
Dictionary<string, string> data = new Dictionary<string, string>();
public Cache(string cacheFilePath)
public Cache(IModBehaviour mod, string cacheFilePath)
{
this.mod = mod;
filepath = cacheFilePath;
var existingEntries = NewHorizons.Main.Instance.ModHelper.Storage.Load<Dictionary<string, Object>>(filepath);
var json = File.ReadAllText(mod.ModHelper.Manifest.ModFolderPath + cacheFilePath);
data = JsonConvert.DeserializeObject<Dictionary<string, string>>(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<Dictionary<string, string>>(filepath);
if (data == null)
{
Logger.LogWarning("Failed to load cache! Cache path: " + cacheFilePath);
data = new Dictionary<string, string>();
}
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<string, string>().Keys));
}
public void WriteToFile()
{
NewHorizons.Main.Instance.ModHelper.Storage.Save<Dictionary<string, Object>>(this, filepath);
mod.ModHelper.Storage.Save<Dictionary<string, string>>(data, filepath);
}
public bool ContainsKey(string key)
{
return data.ContainsKey(key);
}
public T Get<T>(string key)
{
var json = data[key];
return JsonConvert.DeserializeObject<T>(json);
}
public void Set<T>(string key, T value)
{
data[key] = JsonConvert.SerializeObject(value);
}
}
}

View File

@ -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)