From 68e49d3b0936101deba1251e95b9788716c0722a Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Mon, 18 Jun 2018 11:39:41 +0200 Subject: reworked Properties, to split each fiel into a different File --- DiscoBot/DSA_Game/Save/Properties.cs | 70 +++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 16 deletions(-) (limited to 'DiscoBot/DSA_Game/Save/Properties.cs') diff --git a/DiscoBot/DSA_Game/Save/Properties.cs b/DiscoBot/DSA_Game/Save/Properties.cs index fe2f798..45bad4a 100644 --- a/DiscoBot/DSA_Game/Save/Properties.cs +++ b/DiscoBot/DSA_Game/Save/Properties.cs @@ -6,38 +6,76 @@ using System.Threading.Tasks; namespace DiscoBot.DSA_Game.Save { + using System.Collections; using System.IO; + using System.Reflection; + using System.Runtime.CompilerServices; using DiscoBot.Audio; using DiscoBot.Auxiliary; using DiscoBot.Commands; + using Discord; + using Newtonsoft.Json; - public class Properties + public static class Properties { - public List CommandInfos { get; set; } + private static Dictionary objects; - public List Sounds { get; set; } + static Properties() + { + objects = new Dictionary(); + /*this.objects.Add("Sounds", new List()); + this.objects.Add("CommandInfos", new List());*/ + } - public static Properties Deserialize(string path = @"..\..\Properties.json") - { - try - { - return JsonConvert.DeserializeObject(File.ReadAllText(path)); // Deserialize Data and create CommandInfo Struct - } - catch (Exception e) - { - // ignored - return null; - } + public static List CommandInfos { get => objects["CommandInfo"] as List; set => objects["CommandInfo"] = value; } // use Properties.Commandinfos to access the abstract Object array + + public static List Sounds { get => objects["Sound"] as List; set => objects["Sound"] = value; } + + public static void Deserialize(string path = @"..\..\sessions") + { + + var files = Directory.GetFiles(path, "*.json"); + + foreach (string file in files) + { + try + { + string name = file.Split('\\').Last().Split('.')[0].Replace('-', '.'); + string data = File.ReadAllText(file); + Type type = Type.GetType(name); + if (data.StartsWith("[")) + { + type = typeof(List<>).MakeGenericType(type); + } + + var o = JsonConvert.DeserializeObject(data, type); + objects.Add(name.Split('.').Last(), o); + } + catch (Exception e) + { + // ignored + var log = new LogMessage(LogSeverity.Warning, "Properties", $"Laden von Save-File {file} fehlgeschlagen.", e); + Console.WriteLine(log); + } + + } + } - public void Serialize(string path = @"..\..\Properties.json") + public static void Serialize(string path = @"..\..\sessions\") { try { - File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); // Deserialize Data and create CommandInfo Struct + foreach (var o in objects) + { + string assembly = o.Value is IList list ? ((IList)list)[0]?.GetType().FullName : o.Value.GetType().FullName; + + var name = path + assembly.Replace('.', '-') + ".json"; + File.WriteAllText(name, JsonConvert.SerializeObject(o.Value, Formatting.Indented)); // Deserialize Data and create CommandInfo Struct + } } catch (Exception e) { -- cgit v1.2.3-70-g09d2