fix: migrate from v1 configs

This commit is contained in:
Anna 2021-02-05 15:54:19 -05:00
parent 09c3ac7994
commit 0f50c8b589
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0

View File

@ -10,6 +10,7 @@ using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using XIVChatCommon.Message;
using XIVChatCommon.Message.Server;
@ -18,6 +19,8 @@ namespace XIVChat_Desktop {
public class Configuration : INotifyPropertyChanged {
public event PropertyChangedEventHandler? PropertyChanged;
public uint ConfigVersion => 2;
public string? LicenceKey { get; set; }
public KeyPair KeyPair { get; set; } = PublicKeyBox.GenerateKeyPair();
@ -45,26 +48,56 @@ namespace XIVChat_Desktop {
#region io
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.Auto,
ObjectCreationHandling = ObjectCreationHandling.Replace,
};
private static string FilePath() => Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"XIVChat for Windows",
"config.json"
);
public static void Migrate(string path) {
// read the json into a generic object
var json = File.ReadAllText(path);
var obj = JsonConvert.DeserializeObject<JObject>(json);
// read the version
uint version = 1;
if (obj.TryGetValue(nameof(ConfigVersion), out var token)) {
version = token.Value<uint>();
}
// we only have migration logic for version 1, so quit if that's not the version
if (version != 1) {
return;
}
// migrate from v1
foreach (var server in obj["Servers"]!.Values<JObject>()) {
server.AddFirst(new JProperty("$type", "XIVChat_Desktop.DirectServer, XIVChat Desktop"));
}
obj.Add("ConfigVersion", 2);
// write migrated json back to the path
var migrated = JsonConvert.SerializeObject(obj);
File.WriteAllText(path, migrated);
}
public static Configuration? Load() {
var path = FilePath();
if (!File.Exists(path)) {
return null;
}
using var reader = File.OpenText(path);
using var json = new JsonTextReader(reader);
// migrate earlier config versions
Migrate(path);
var serializer = new JsonSerializer {
TypeNameHandling = TypeNameHandling.Auto,
ObjectCreationHandling = ObjectCreationHandling.Replace,
};
return serializer.Deserialize<Configuration>(json);
var json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<Configuration>(json, Settings);
}
public void Save() {
@ -74,13 +107,8 @@ namespace XIVChat_Desktop {
Directory.CreateDirectory(dir);
}
using var file = File.CreateText(path);
using var json = new JsonTextWriter(file);
var serialiser = new JsonSerializer {
TypeNameHandling = TypeNameHandling.Auto,
};
serialiser.Serialize(json, this);
var json = JsonConvert.SerializeObject(this, Settings);
File.WriteAllText(path, json);
}
#endregion