SecondBot/SecondBotEvents/Config/config.cs
Madpeter 008cb44e24 Use UnsafeRelaxed JSON serialization globally
Replaces all usages of JsonSerializer.Serialize with the UnsafeRelaxed option via a new JsonOptions.UnsafeRelaxed static property. This ensures consistent relaxed escaping for all JSON output, improving compatibility with consumers expecting unescaped JSON. Refactors affected command, service, and static classes to use the new option.
2025-10-05 20:07:42 +01:00

199 lines
5.8 KiB
C#

using Newtonsoft.Json.Linq;
using SecondBotEvents.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Text;
using System.Text.Json;
namespace SecondBotEvents.Config
{
public abstract class Config
{
protected Dictionary<string, string> mysettings = [];
protected List<string> settings = [];
protected string filename = "config";
protected bool loaded = false;
protected bool tryLoadfromEnv = false;
public Config(bool fromENV, string fromFolder="")
{
MakeSettings();
LoadSettings(fromENV, fromFolder);
}
public void updateKey(string key, string value)
{
if(mysettings.ContainsKey(key))
{
mysettings[key] = value;
return;
}
mysettings.Add(key, value);
}
public void setEnabled(bool enabled)
{
mysettings["Enabled"] = enabled.ToString();
}
public bool GetHideStatusOutput()
{
return ReadSettingAsBool("HideStatusOutput", false);
}
public bool IsLoaded()
{
return loaded;
}
protected void LoadSettings(bool fromENV, string fromFolder = "")
{
if(fromENV == true)
{
tryLoadfromEnv = true;
LoadSettingsFromEnv();
return;
}
LoadSettingsFromFile(fromFolder);
}
protected void MakeSettingsFile(string fromFolder)
{
Dictionary<string, string> saveMe = [];
Type thisType = this.GetType();
foreach (string setting in settings)
{
string reader = "Get"+ StringExtensions.FirstCharToUpper(setting);
MethodInfo theMethod = thisType.GetMethod(reader);
if (theMethod == null)
{
continue;
}
string value = theMethod.Invoke(this, null).ToString();
saveMe.Add(setting, value);
}
SimpleIO IO = new();
IO.ChangeRoot(fromFolder);
string writeFile = filename + ".json";
IO.WriteFile(writeFile, JsonSerializer.Serialize(saveMe, JsonOptions.UnsafeRelaxed));
}
protected void LoadSettingsFromFile(string fromFolder)
{
SimpleIO IO = new();
IO.ChangeRoot(fromFolder);
string readfile = filename + ".json";
if (IO.Exists(readfile) == false)
{
MakeSettingsFile(fromFolder);
return;
}
try
{
JObject result = JObject.Parse(IO.ReadFile(readfile));
foreach (var x in result)
{
settings.Add(x.Key);
string value = x.Value.ToString();
if (SecondbotHelpers.NotEmpty(value) == false)
{
mysettings.Add(x.Key, "");
continue;
}
mysettings.Add(x.Key, value);
}
loaded = true;
}
catch (Exception ex)
{
LogFormater.Warn("Json format for " + readfile + " is broken: " + ex.Message);
}
}
protected void LoadSettingsFromEnv()
{
foreach (string setting in settings)
{
TryLoadSetting(setting);
}
loaded = true;
}
protected virtual void MakeSettings()
{
}
protected bool TryLoadSetting(string setting)
{
if(tryLoadfromEnv == false)
{
return false;
}
string key = filename + "_" + setting;
string value = Environment.GetEnvironmentVariable(key);
if(value == null)
{
return false;
}
mysettings.Add(setting, value);
return true;
}
protected string ReadSettingAsString(string key, string defaultValue)
{
if (mysettings.ContainsKey(key) == false)
{
if(TryLoadSetting(key) == false)
{
return defaultValue;
}
}
return mysettings[key];
}
protected bool ReadSettingAsBool(string key,bool defaultValue=false)
{
bool result = bool.TryParse(ReadSettingAsString(key, defaultValue.ToString()), out bool enabled);
if (result == false)
{
return defaultValue;
}
return enabled;
}
protected int ReadSettingAsInt(string key, int defaultValue = 30)
{
string read = ReadSettingAsString(key, defaultValue.ToString());
bool result = int.TryParse(read, out int value);
if (result == false)
{
return defaultValue;
}
return value;
}
protected ulong ReadSettingAsUlong(string key, ulong defaultValue = 0)
{
bool result = ulong.TryParse(ReadSettingAsString(key, defaultValue.ToString()), out ulong value);
if (result == false)
{
return defaultValue;
}
return value;
}
protected double ReadSettingAsDouble(string key, double defaultValue = 0)
{
bool result = double.TryParse(ReadSettingAsString(key, defaultValue.ToString()), out double value);
if (result == false)
{
return defaultValue;
}
return value;
}
}
}