Skip to content

Commit

Permalink
Added Config System
Browse files Browse the repository at this point in the history
  • Loading branch information
BadRyuner committed Sep 3, 2022
1 parent 56d6a8b commit fe2a038
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 48 deletions.
2 changes: 1 addition & 1 deletion PulsarModLoader/CustomGUI/GUIMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void WindowFunction(int WindowID)
{
#region ModList and ModInfo
case 0:
GUI.skin.label.alignment = PMLConfig.instance.ModInfoTextAnchor;
GUI.skin.label.alignment = PMLConfig.ModInfoTextAnchor;
BeginArea(ModListArea);
{
ModListScroll = BeginScrollView(ModListScroll);
Expand Down
13 changes: 4 additions & 9 deletions PulsarModLoader/CustomGUI/PMLSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ public override void Draw()
GUI.skin.label.alignment = TextAnchor.UpperLeft;
BeginHorizontal();
{
Label($"ModInfoTextAnchor: {PMLConfig.instance.ModInfoTextAnchor.ToString()}");
Label($"ModInfoTextAnchor: {PMLConfig.ModInfoTextAnchor.ToString()}");

if (Button("<"))
PMLConfig.instance.ModInfoTextAnchor = Enum.GetValues(typeof(TextAnchor)).Cast<TextAnchor>().SkipWhile(e => (int)e != (int)PMLConfig.instance.ModInfoTextAnchor - 1).First();
PMLConfig.ModInfoTextAnchor.Value = Enum.GetValues(typeof(TextAnchor)).Cast<TextAnchor>().SkipWhile(e => (int)e != (int)PMLConfig.ModInfoTextAnchor.Value - 1).First();
if (Button(">"))
PMLConfig.instance.ModInfoTextAnchor = Enum.GetValues(typeof(TextAnchor)).Cast<TextAnchor>().SkipWhile(e => (int)e != (int)PMLConfig.instance.ModInfoTextAnchor).Skip(1).First();
}
EndHorizontal();
BeginHorizontal();
{
if (Button("Save")) PMLConfig.SaveConfig(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/PulsarModLoaderConfig.json");
if (Button("Reset to default")) PMLConfig.CreateDefaultConfig(string.Empty, false);
PMLConfig.ModInfoTextAnchor.Value = Enum.GetValues(typeof(TextAnchor)).Cast<TextAnchor>().SkipWhile(e => (int)e != (int)PMLConfig.ModInfoTextAnchor.Value).Skip(1).First();
}
EndHorizontal();
if (Button("Reset to default")) PMLConfig.SetDefault();
}
}
}
38 changes: 5 additions & 33 deletions PulsarModLoader/PMLConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,14 @@

namespace PulsarModLoader
{
public class PMLConfig
public static class PMLConfig
{
internal static PMLConfig instance { get; private set; }
internal PMLConfig(bool @default)
{
if (@default)
{
instance = this;
ModInfoTextAnchor = TextAnchor.UpperLeft;
}
}

internal PMLConfig() => instance = this;
public static SaveValue<UnityEngine.TextAnchor> ModInfoTextAnchor =
new SaveValue<TextAnchor>("ModInfoTextAnchor", TextAnchor.UpperLeft);

internal static void CreateDefaultConfig(string path, bool save)
public static void SetDefault()
{
new PMLConfig(true);
if (save)
SaveConfig(path);
ModInfoTextAnchor.Value = TextAnchor.UpperLeft;
}

internal static void CreateConfigFromFile(string path) =>
JsonConvert.DeserializeObject<PMLConfig>(File.ReadAllText(path), settings());

internal static void SaveConfig(string path) =>
File.WriteAllText(path, JsonConvert.SerializeObject(instance, typeof(PMLConfig), settings()));
//.Replace("{", "{\n\t").Replace("}", "\n}").Replace(",\"", ",\n\t\""));

private static JsonSerializerSettings settings()
{
var settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.Converters.Add(new StringEnumConverter() { });
return settings;
}

public TextAnchor ModInfoTextAnchor { get; set; }
}
}
8 changes: 3 additions & 5 deletions PulsarModLoader/Patches/PLGlobalStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ static void Prefix()
}

//PML Config
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/PulsarModLoaderConfig.json";
if (!File.Exists(path))
PMLConfig.CreateDefaultConfig(path, true);
else
PMLConfig.CreateConfigFromFile(path);
//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/PulsarModLoaderConfig.json";
//if (!File.Exists(path)) PMLConfig.CreateDefaultConfig(path, true);
//else PMLConfig.CreateConfigFromFile(path);

//Modmanager GUI Init.
new GameObject("ModManager", typeof(CustomGUI.GUIMain)) { hideFlags = HideFlags.HideAndDontSave };
Expand Down
1 change: 1 addition & 0 deletions PulsarModLoader/PulsarModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
<Compile Include="SaveData\DisplayModdedSavePatch.cs" />
<Compile Include="SaveData\PMLSaveData.cs" />
<Compile Include="SaveData\SaveDataManager.cs" />
<Compile Include="SaveValue.cs" />
<Compile Include="Utilities\Clipboard.cs" />
<Compile Include="Utilities\HelperMethods.cs" />
<Compile Include="Utilities\ExceptionWarningPatch.cs" />
Expand Down
114 changes: 114 additions & 0 deletions PulsarModLoader/SaveValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using HarmonyLib;
using PulsarModLoader.Utilities;
using Valve.Newtonsoft.Json;
using Valve.Newtonsoft.Json.Converters;

namespace PulsarModLoader
{
internal class SaveValueManager
{
private static JsonSerializerSettings serializerSettings;
static SaveValueManager()
{
serializerSettings = new JsonSerializerSettings();
serializerSettings.Formatting = Formatting.Indented;
}

internal static void SaveValueFor(string id, Assembly mod, string value)
{
ModToCacheValues[mod][id] = value;
var cfg = GetConfigFile(mod);
File.WriteAllText(cfg, JsonConvert.SerializeObject(ModToCacheValues[mod], serializerSettings));
}

internal static T GetValueFor<T>(string id, Assembly mod, T @default)
{
var cfg = GetConfigFile(mod);
if (ModToCacheValues.TryGetValue(mod, out var values))
{
if (values.TryGetValue(id, out var ret))
{
if (@default is Enum)
return (T)Enum.Parse(typeof(T), ret);

return (T)AccessTools.Method(@default.GetType(), "Parse", new Type[] { typeof(string) })
.Invoke(null, new[] { ret });
}
}
else
{
ModToCacheValues.Add(mod, new Dictionary<string, string>());
ModToCacheValues[mod].Add(id, @default.ToString());
File.WriteAllText(cfg, JsonConvert.SerializeObject(ModToCacheValues[mod], serializerSettings));
return @default;
}

ModToCacheValues[mod].Add(id, @default.ToString());
return @default;
}

private static string GetConfigFile(Assembly mod)
{
if (ModToConfigFile.ContainsKey(mod)) return ModToConfigFile[mod];
string newFile = mod.Location.Replace(".dll", ".json");
ModToConfigFile.Add(mod, newFile);
if (!ModToCacheValues.ContainsKey(mod) && File.Exists(newFile))
ModToCacheValues.Add(mod, JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(newFile)));
return newFile;
}

private static Dictionary<Assembly, string> ModToConfigFile = new Dictionary<Assembly, string>();

private static Dictionary<Assembly, Dictionary<string, string>> ModToCacheValues =
new Dictionary<Assembly, Dictionary<string, string>>();
}

public class SaveValue<T> : IEquatable<T>
{
public SaveValue(string id, T @defualt)
{
this.id = id;
this.mod = Assembly.GetCallingAssembly();
_value = SaveValueManager.GetValueFor(id, mod, @defualt);
}

private string id;
private Assembly mod;

private T _value;

public T Value
{
get => _value;
set
{
if (!_value.Equals(value)) SetValue(value);
}
}

private void SetValue(T newValue)
{
_value = newValue;
SaveValueManager.SaveValueFor(id, mod, newValue.ToString());
}

public static implicit operator T(SaveValue<T> v) => v._value;

public override bool Equals(object obj) => _value.Equals(obj);

public bool Equals(T other) => _value.Equals(other);

public override string ToString() => _value.ToString();
}

// Example Usage
//public static class MyCfg {
// public static SaveValue<string> CoolName = new SaveValue<string>("CoolName", "Default Nickname");
// public static SaveValue<int> Offset = new SaveValue<int>("Offset", 10);
//}
}

0 comments on commit fe2a038

Please sign in to comment.