mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-07-01 16:33:43 -04:00
WIP: observatory UI overhaul
This commit is contained in:
@ -284,7 +284,7 @@ namespace Observatory.Utils
|
||||
if (Properties.Core.Default.JournalFolder != path)
|
||||
{
|
||||
Properties.Core.Default.JournalFolder = path;
|
||||
Properties.Core.Default.Save();
|
||||
SettingsManager.Save();
|
||||
}
|
||||
|
||||
return logDirectory;
|
||||
|
67
ObservatoryCore/Utils/SettingsManager.cs
Normal file
67
ObservatoryCore/Utils/SettingsManager.cs
Normal file
@ -0,0 +1,67 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user