mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
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<string, string> data = new Dictionary<string, string>();
|
|
|
|
public Cache(IModBehaviour mod, string cacheFilePath)
|
|
{
|
|
this.mod = mod;
|
|
this.filepath = cacheFilePath;
|
|
var fullPath = mod.ModHelper.Manifest.ModFolderPath + cacheFilePath;
|
|
|
|
if (!File.Exists(fullPath))
|
|
{
|
|
Logger.LogWarning("Cache file not found! Cache path: " + cacheFilePath);
|
|
data = new Dictionary<string, string>();
|
|
return;
|
|
}
|
|
|
|
var json = File.ReadAllText(fullPath);
|
|
data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
|
// the code above does exactly the same thing that the code below does, but the below for some reason always returns null. no clue why
|
|
// data = mod.ModHelper.Storage.Load<Dictionary<string, string>>(filepath);
|
|
}
|
|
|
|
public void WriteToFile()
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|