using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Newtonsoft.Json;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ULTRAKIT.UniversalSaveSystem")]
[assembly: AssemblyDescription("BepInEx plugin to store/retrieve persistent data via json in a text file.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ULTRAKIT")]
[assembly: AssemblyProduct("UniversalSaveSystem")]
[assembly: AssemblyCopyright("Copyright © HP 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e4bdd1b9-e97b-4a31-8f1f-b1f8e922b291")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace ULTRAKIT.UniversalSaveSystem;
[BepInPlugin("ULTRAKIT.UniversalSaveSystem", "Universal Save System", "1.0.0")]
public class Mod : BaseUnityPlugin
{
public static ManualLogSource Logger { get; private set; }
private void Awake()
{
Logger = ((BaseUnityPlugin)this).Logger;
Logger.LogInfo((object)"ULTRAKIT Module Loaded: Universal Save System");
}
private void Start()
{
SaveData.Load();
}
}
public static class SaveData
{
private const string folderName = "data";
private static string dataFilePath = GetDataPath("save.ultradata");
private static PersistentData _data;
internal static PersistentData data
{
get
{
if (_data == null)
{
Load();
}
return _data;
}
private set
{
data = value;
}
}
internal static string GetDataPath(params string[] subpath)
{
string location = Assembly.GetExecutingAssembly().Location;
location = Path.GetDirectoryName(location);
string text = Path.Combine(location, "data");
if (subpath.Length != 0)
{
string path = Path.Combine(subpath);
text = Path.Combine(text, path);
}
return text;
}
private static bool Internal_SetValue<TKey, TValue>(Dictionary<TKey, TValue> dict, TKey key, TValue value)
{
if (dict.ContainsKey(key))
{
dict[key] = value;
Save();
return true;
}
dict.Add(key, value);
Save();
return false;
}
private static bool Internal_SetPrivateValue<TKey, TValue>(Dictionary<string, Dictionary<TKey, TValue>> dict, TKey key, TValue value, string assemblyName)
{
if (dict.ContainsKey(assemblyName))
{
return Internal_SetValue(dict[assemblyName], key, value);
}
dict.Add(assemblyName, new Dictionary<TKey, TValue>());
Internal_SetValue(dict[assemblyName], key, value);
return false;
}
internal static void Save()
{
if (!Directory.Exists(GetDataPath()))
{
Directory.CreateDirectory(GetDataPath());
}
string contents = JsonConvert.SerializeObject((object)_data);
File.WriteAllText(dataFilePath, contents);
Mod.Logger.LogInfo((object)"Saved persistent data");
}
internal static void Load()
{
Mod.Logger.LogInfo((object)"Loading persistent data...");
if (!File.Exists(dataFilePath))
{
_data = PersistentData.Default;
Save();
return;
}
string text;
using (StreamReader streamReader = new StreamReader(dataFilePath))
{
text = streamReader.ReadToEnd();
}
_data = JsonConvert.DeserializeObject<PersistentData>(text);
}
public static bool SetPersistent(string key, object value, bool global)
{
if (global)
{
if (value is string)
{
return Internal_SetValue(data.g_string_data, key, (string)value);
}
if (value is int)
{
return Internal_SetValue(data.g_int_data, key, (int)value);
}
if (value is float)
{
return Internal_SetValue(data.g_float_data, key, (float)value);
}
if (value is bool)
{
return Internal_SetValue(data.g_bool_data, key, (bool)value);
}
if (value is string[])
{
return Internal_SetValue(data.g_string_data_array, key, (string[])value);
}
if (value is int[])
{
return Internal_SetValue(data.g_int_data_array, key, (int[])value);
}
if (value is float[])
{
return Internal_SetValue(data.g_float_data_array, key, (float[])value);
}
if (value is bool[])
{
return Internal_SetValue(data.g_bool_data_array, key, (bool[])value);
}
return false;
}
string name = Assembly.GetCallingAssembly().GetName().Name;
if (value is string)
{
return Internal_SetPrivateValue(data.p_string_data, key, (string)value, name);
}
if (value is int)
{
return Internal_SetPrivateValue(data.p_int_data, key, (int)value, name);
}
if (value is float)
{
return Internal_SetPrivateValue(data.p_float_data, key, (float)value, name);
}
if (value is bool)
{
return Internal_SetPrivateValue(data.p_bool_data, key, (bool)value, name);
}
if (value is string[])
{
return Internal_SetPrivateValue(data.p_string_data_array, key, (string[])value, name);
}
if (value is int[])
{
return Internal_SetPrivateValue(data.p_int_data_array, key, (int[])value, name);
}
if (value is float[])
{
return Internal_SetPrivateValue(data.p_float_data_array, key, (float[])value, name);
}
if (value is bool[])
{
return Internal_SetPrivateValue(data.p_bool_data_array, key, (bool[])value, name);
}
return false;
}
private static bool Internal_TryGetPersistent<T>(string key, bool global, string assembly, out object value)
{
Type typeFromHandle = typeof(T);
if (global)
{
if (typeFromHandle.IsEquivalentTo(typeof(string)))
{
string value2;
bool result = data.g_string_data.TryGetValue(key, out value2);
value = value2;
return result;
}
if (typeFromHandle.IsEquivalentTo(typeof(int)))
{
int value3;
bool result2 = data.g_int_data.TryGetValue(key, out value3);
value = value3;
return result2;
}
if (typeFromHandle.IsEquivalentTo(typeof(float)))
{
float value4;
bool result3 = data.g_float_data.TryGetValue(key, out value4);
value = value4;
return result3;
}
if (typeFromHandle.IsEquivalentTo(typeof(bool)))
{
bool value5;
bool result4 = data.g_bool_data.TryGetValue(key, out value5);
value = value5;
return result4;
}
if (typeFromHandle.IsEquivalentTo(typeof(string[])))
{
string[] value6;
bool result5 = data.g_string_data_array.TryGetValue(key, out value6);
value = value6;
return result5;
}
if (typeFromHandle.IsEquivalentTo(typeof(int[])))
{
int[] value7;
bool result6 = data.g_int_data_array.TryGetValue(key, out value7);
value = value7;
return result6;
}
if (typeFromHandle.IsEquivalentTo(typeof(float[])))
{
float[] value8;
bool result7 = data.g_float_data_array.TryGetValue(key, out value8);
value = value8;
return result7;
}
if (typeFromHandle.IsEquivalentTo(typeof(bool[])))
{
bool[] value9;
bool result8 = data.g_bool_data_array.TryGetValue(key, out value9);
value = value9;
return result8;
}
value = default(T);
return false;
}
if (typeFromHandle.IsEquivalentTo(typeof(string)))
{
bool result9 = false;
string value10 = null;
if (data.p_string_data.TryGetValue(assembly, out var value11))
{
result9 = value11.TryGetValue(key, out value10);
}
value = value10;
return result9;
}
if (typeFromHandle.IsEquivalentTo(typeof(int)))
{
bool result10 = false;
int value12 = 0;
if (data.p_int_data.TryGetValue(assembly, out var value13))
{
result10 = value13.TryGetValue(key, out value12);
}
value = value12;
return result10;
}
if (typeFromHandle.IsEquivalentTo(typeof(float)))
{
bool result11 = false;
float value14 = 0f;
if (data.p_float_data.TryGetValue(assembly, out var value15))
{
result11 = value15.TryGetValue(key, out value14);
}
value = value14;
return result11;
}
if (typeFromHandle.IsEquivalentTo(typeof(bool)))
{
bool result12 = false;
bool value16 = false;
if (data.p_bool_data.TryGetValue(assembly, out var value17))
{
result12 = value17.TryGetValue(key, out value16);
}
value = value16;
return result12;
}
if (typeFromHandle.IsEquivalentTo(typeof(string[])))
{
bool result13 = false;
string[] value18 = null;
if (data.p_string_data_array.TryGetValue(assembly, out var value19))
{
result13 = value19.TryGetValue(key, out value18);
}
value = value18;
return result13;
}
if (typeFromHandle.IsEquivalentTo(typeof(int[])))
{
bool result14 = false;
int[] value20 = null;
if (data.p_int_data_array.TryGetValue(assembly, out var value21))
{
result14 = value21.TryGetValue(key, out value20);
}
value = value20;
return result14;
}
if (typeFromHandle.IsEquivalentTo(typeof(float[])))
{
bool result15 = false;
float[] value22 = null;
if (data.p_float_data_array.TryGetValue(assembly, out var value23))
{
result15 = value23.TryGetValue(key, out value22);
}
value = value22;
return result15;
}
if (typeFromHandle.IsEquivalentTo(typeof(bool[])))
{
bool result16 = false;
bool[] value24 = null;
if (data.p_bool_data_array.TryGetValue(assembly, out var value25))
{
result16 = value25.TryGetValue(key, out value24);
}
value = value24;
return result16;
}
value = default(T);
return false;
}
public static bool TryGetPersistent<T>(string key, bool global, out object value)
{
return Internal_TryGetPersistent<T>(key, global, Assembly.GetCallingAssembly().GetName().Name, out value);
}
public static T GetPersistent<T>(string key, bool global)
{
string name = Assembly.GetCallingAssembly().GetName().Name;
if (Internal_TryGetPersistent<T>(key, global, name, out var value))
{
return (T)value;
}
throw new KeyNotFoundException("Key '" + key + "' not found in global data.");
}
}
[Serializable]
public class PersistentData
{
public Dictionary<string, string> g_string_data;
public Dictionary<string, int> g_int_data;
public Dictionary<string, float> g_float_data;
public Dictionary<string, bool> g_bool_data;
public Dictionary<string, string[]> g_string_data_array;
public Dictionary<string, int[]> g_int_data_array;
public Dictionary<string, float[]> g_float_data_array;
public Dictionary<string, bool[]> g_bool_data_array;
public Dictionary<string, Dictionary<string, string>> p_string_data;
public Dictionary<string, Dictionary<string, int>> p_int_data;
public Dictionary<string, Dictionary<string, float>> p_float_data;
public Dictionary<string, Dictionary<string, bool>> p_bool_data;
public Dictionary<string, Dictionary<string, string[]>> p_string_data_array;
public Dictionary<string, Dictionary<string, int[]>> p_int_data_array;
public Dictionary<string, Dictionary<string, float[]>> p_float_data_array;
public Dictionary<string, Dictionary<string, bool[]>> p_bool_data_array;
internal static readonly PersistentData Default = new PersistentData
{
g_string_data = new Dictionary<string, string>(),
g_int_data = new Dictionary<string, int>(),
g_float_data = new Dictionary<string, float>(),
g_bool_data = new Dictionary<string, bool>(),
g_string_data_array = new Dictionary<string, string[]>(),
g_int_data_array = new Dictionary<string, int[]>(),
g_float_data_array = new Dictionary<string, float[]>(),
g_bool_data_array = new Dictionary<string, bool[]>(),
p_string_data = new Dictionary<string, Dictionary<string, string>>(),
p_int_data = new Dictionary<string, Dictionary<string, int>>(),
p_float_data = new Dictionary<string, Dictionary<string, float>>(),
p_bool_data = new Dictionary<string, Dictionary<string, bool>>(),
p_string_data_array = new Dictionary<string, Dictionary<string, string[]>>(),
p_int_data_array = new Dictionary<string, Dictionary<string, int[]>>(),
p_float_data_array = new Dictionary<string, Dictionary<string, float[]>>(),
p_bool_data_array = new Dictionary<string, Dictionary<string, bool[]>>()
};
}