mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Use JSON for settings.
This commit is contained in:
@@ -34,6 +34,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Aaru.CommonTypes.Interop;
|
using Aaru.CommonTypes.Interop;
|
||||||
using Claunia.PropertyList;
|
using Claunia.PropertyList;
|
||||||
@@ -42,6 +44,11 @@ using PlatformID = Aaru.CommonTypes.Interop.PlatformID;
|
|||||||
|
|
||||||
namespace Aaru.Settings;
|
namespace Aaru.Settings;
|
||||||
|
|
||||||
|
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||||
|
IncludeFields = true)]
|
||||||
|
[JsonSerializable(typeof(DicSettings))]
|
||||||
|
public partial class SettingsContext : JsonSerializerContext {}
|
||||||
|
|
||||||
/// <summary>Settings</summary>
|
/// <summary>Settings</summary>
|
||||||
public class DicSettings
|
public class DicSettings
|
||||||
{
|
{
|
||||||
@@ -457,20 +464,39 @@ public static class Settings
|
|||||||
Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ?? XDG_CONFIG_HOME_RESOLVED);
|
Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ?? XDG_CONFIG_HOME_RESOLVED);
|
||||||
|
|
||||||
string dicSettingsPath = Path.Combine(xdgConfigPath, "DiscImageChef.xml");
|
string dicSettingsPath = Path.Combine(xdgConfigPath, "DiscImageChef.xml");
|
||||||
string settingsPath = Path.Combine(xdgConfigPath, "Aaru.xml");
|
string xmlSettingsPath = Path.Combine(xdgConfigPath, "Aaru.xml");
|
||||||
|
string settingsPath = Path.Combine(xdgConfigPath, "Aaru.json");
|
||||||
|
|
||||||
if(File.Exists(oldSettingsPath) &&
|
if(File.Exists(oldSettingsPath) &&
|
||||||
!File.Exists(settingsPath))
|
!File.Exists(xmlSettingsPath))
|
||||||
{
|
{
|
||||||
if(!Directory.Exists(xdgConfigPath))
|
if(!Directory.Exists(xdgConfigPath))
|
||||||
Directory.CreateDirectory(xdgConfigPath);
|
Directory.CreateDirectory(xdgConfigPath);
|
||||||
|
|
||||||
File.Move(oldSettingsPath, settingsPath);
|
File.Move(oldSettingsPath, xmlSettingsPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(File.Exists(dicSettingsPath) &&
|
if(File.Exists(dicSettingsPath) &&
|
||||||
!File.Exists(settingsPath))
|
!File.Exists(xmlSettingsPath))
|
||||||
File.Move(dicSettingsPath, settingsPath);
|
File.Move(dicSettingsPath, xmlSettingsPath);
|
||||||
|
|
||||||
|
if(File.Exists(xmlSettingsPath))
|
||||||
|
{
|
||||||
|
// Should be working due to source generator for json below
|
||||||
|
#pragma warning disable IL2026
|
||||||
|
var xs = new XmlSerializer(Current.GetType());
|
||||||
|
#pragma warning restore IL2026
|
||||||
|
|
||||||
|
prefsSr = new StreamReader(xmlSettingsPath);
|
||||||
|
|
||||||
|
// Should be working due to source generator for json below
|
||||||
|
#pragma warning disable IL2026
|
||||||
|
Current = (DicSettings)xs.Deserialize(prefsSr);
|
||||||
|
#pragma warning restore IL2026
|
||||||
|
|
||||||
|
prefsSr.Close();
|
||||||
|
File.Delete(xmlSettingsPath);
|
||||||
|
}
|
||||||
|
|
||||||
if(!File.Exists(settingsPath))
|
if(!File.Exists(settingsPath))
|
||||||
{
|
{
|
||||||
@@ -480,9 +506,12 @@ public static class Settings
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var xs = new XmlSerializer(Current.GetType());
|
var fs = new FileStream(settingsPath, FileMode.Open);
|
||||||
prefsSr = new StreamReader(settingsPath);
|
|
||||||
Current = (DicSettings)xs.Deserialize(prefsSr);
|
Current =
|
||||||
|
JsonSerializer.Deserialize(fs, typeof(DicSettings), SettingsContext.Default) as DicSettings;
|
||||||
|
|
||||||
|
fs.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -640,14 +669,13 @@ public static class Settings
|
|||||||
Path.Combine(homePath,
|
Path.Combine(homePath,
|
||||||
Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ?? XDG_CONFIG_HOME_RESOLVED);
|
Environment.GetEnvironmentVariable(XDG_CONFIG_HOME) ?? XDG_CONFIG_HOME_RESOLVED);
|
||||||
|
|
||||||
string settingsPath = Path.Combine(xdgConfigPath, "Aaru.xml");
|
string settingsPath = Path.Combine(xdgConfigPath, "Aaru.json");
|
||||||
|
|
||||||
if(!Directory.Exists(xdgConfigPath))
|
if(!Directory.Exists(xdgConfigPath))
|
||||||
Directory.CreateDirectory(xdgConfigPath);
|
Directory.CreateDirectory(xdgConfigPath);
|
||||||
|
|
||||||
var fs = new FileStream(settingsPath, FileMode.Create);
|
var fs = new FileStream(settingsPath, FileMode.Create, FileAccess.ReadWrite);
|
||||||
var xs = new XmlSerializer(Current.GetType());
|
JsonSerializer.Serialize(fs, Current, Current.GetType(), SettingsContext.Default);
|
||||||
xs.Serialize(fs, Current);
|
|
||||||
fs.Close();
|
fs.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user