From 0f50c8b589738e6d0a02179891a3a18912b8b6a9 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Fri, 5 Feb 2021 15:54:19 -0500 Subject: [PATCH] fix: migrate from v1 configs --- XIVChat Desktop/Configuration.cs | 56 ++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/XIVChat Desktop/Configuration.cs b/XIVChat Desktop/Configuration.cs index 9c9a53a..2e1a3d2 100644 --- a/XIVChat Desktop/Configuration.cs +++ b/XIVChat Desktop/Configuration.cs @@ -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(json); + + // read the version + uint version = 1; + if (obj.TryGetValue(nameof(ConfigVersion), out var token)) { + version = token.Value(); + } + + // 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()) { + 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(json); + var json = File.ReadAllText(path); + return JsonConvert.DeserializeObject(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