2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/ObservatoryCore/Utils/SettingsManager.cs
2023-07-07 08:36:27 -02:30

68 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Observatory.Utils
{
internal static class SettingsManager
{
internal static void Save()
{
#if DEBUG || RELEASE
Properties.Core.Default.Save();
#elif PORTABLE
Dictionary<string, object?> settings = new();
foreach (PropertyInfo property in Properties.Core.Default.GetType().GetProperties())
{
if (property.CanRead && property.CanWrite && !property.GetIndexParameters().Any())
settings.Add(
property.Name,
property.GetValue(Properties.Core.Default)
);
}
string serializedSettings = JsonSerializer.Serialize(settings, new JsonSerializerOptions()
{
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve,
});
File.WriteAllText("Observatory.config", serializedSettings);
#endif
}
internal static void Load()
{
#if PORTABLE
if (File.Exists("Observatory.config"))
{
string savedSettings = File.ReadAllText("Observatory.config");
Dictionary<string, object?>? settings = JsonSerializer.Deserialize<Dictionary<string, object?>>(savedSettings);
if (settings != null)
{
var properties = Properties.Core.Default.GetType().GetProperties();
foreach (var savedProperty in settings)
{
var currentProperty = properties.Where(p => p.Name == savedProperty.Key);
if (currentProperty.Any())
{
JsonElement? value = (JsonElement?)savedProperty.Value;
var deserializedValue = value?.Deserialize(currentProperty.First().PropertyType);
currentProperty.First().SetValue(Properties.Core.Default, deserializedValue);
}
}
}
}
#endif
}
}
}