jellyfin-rich-presence/WindowsFormsApplication2/ConfigManager.cs
2025-05-14 14:46:17 -06:00

81 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JellyfinRPC
{
internal class ConfigManager
{
private static string ConfigFile = Environment.GetEnvironmentVariable("appdata") + "/JellyfinRPC/Config.txt";
private const string SEPERATOR = "=";
private static Dictionary<string, string> cfgEntries = new Dictionary<string, string>();
static ConfigManager()
{
loadCfg();
}
private static void loadCfg()
{
cfgEntries.Clear();
if (!File.Exists(ConfigFile))
saveCfg();
using StreamReader cfgReader = File.OpenText(ConfigFile);
for (string line = cfgReader.ReadLine(); line != null; line = cfgReader.ReadLine())
{
if (line.StartsWith("*")) continue;
string[] values = line.Split(SEPERATOR.First());
cfgEntries.Add(values.First().Trim(), String.Join(SEPERATOR, values.Skip(1)).Trim());
}
}
private static void saveCfg()
{
using StreamWriter cfgWriter = File.CreateText(ConfigFile);
cfgWriter.WriteLine("**JellyfinDiscordRPC Config**");
foreach (KeyValuePair<String, String> values in cfgEntries)
{
cfgWriter.WriteLine(String.Join(SEPERATOR, new String[] { values.Key.Trim(), values.Value.Trim() }));
}
}
public static void SetEntry(string key, string value)
{
key = key.Trim();
value = value.Trim().Replace("\n", String.Empty).Replace("\r", String.Empty);
if (EntryExists(key))
cfgEntries[key] = value;
else
cfgEntries.Add(key, value);
saveCfg();
}
public static bool EntryExists(string key)
{
return cfgEntries.ContainsKey(key);
}
public static string GetEntry(string key)
{
return EntryExists(key) ? cfgEntries[key] : String.Empty;
}
public static void RemoveEntry(string key)
{
cfgEntries.Remove(key.Trim());
saveCfg();
}
public static string GetEntry(string key, string defaultValue)
{
if (!EntryExists(key))
{
SetEntry(key, defaultValue);
return defaultValue;
}
return GetEntry(key);
}
}
}