From b411aa2128c2724bec0ecedb8cb4e1ffa59f3b53 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Wed, 26 Sep 2018 20:51:37 +0200 Subject: disconnected most vital calles from the Discord Bot --- DSACore/DSA_Game/Save/Properties.cs | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 DSACore/DSA_Game/Save/Properties.cs (limited to 'DSACore/DSA_Game/Save/Properties.cs') diff --git a/DSACore/DSA_Game/Save/Properties.cs b/DSACore/DSA_Game/Save/Properties.cs new file mode 100644 index 0000000..a27c9d7 --- /dev/null +++ b/DSACore/DSA_Game/Save/Properties.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using DSACore.Audio; +using DSACore.Auxiliary; + +namespace DiscoBot.DSA_Game.Save +{ + using System.Collections; + using System.IO; + using Newtonsoft.Json; + + public static class Properties + { + private static Dictionary objects; + + static Properties() + { + objects = new Dictionary(); + /*this.objects.Add("Sounds", new List()); + this.objects.Add("CommandInfos", new List());*/ + } + + 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 = @"..\..\Properties") + { + + 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 + Console.WriteLine($"Laden von Save-File {file} fehlgeschlagen."+ e); + } + + } + + } + + public static void Serialize(string path = @"..\..\Properties\") + { + try + { + 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) + { + // ignored + Console.WriteLine($"Speichern von Save-File fehlgeschlagen."+ e); + } + } + } +} -- cgit v1.2.3-70-g09d2