diff options
author | uzvkl <dennis.kobert@student.kit.edu> | 2019-06-11 23:05:52 +0200 |
---|---|---|
committer | uzvkl <dennis.kobert@student.kit.edu> | 2019-06-11 23:05:52 +0200 |
commit | e6181c24124d97f2fbc932b8a68311e625463156 (patch) | |
tree | c1f097c344ca266b7941c9668590b0fd35c7870a /dsa/DSALib/DSA_Game/Save/Properties.cs | |
parent | 2490ad5d31fe2ac778ff9303776f0e91f47a2862 (diff) |
Move dsa related stuff to subfolder
Diffstat (limited to 'dsa/DSALib/DSA_Game/Save/Properties.cs')
-rw-r--r-- | dsa/DSALib/DSA_Game/Save/Properties.cs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/dsa/DSALib/DSA_Game/Save/Properties.cs b/dsa/DSALib/DSA_Game/Save/Properties.cs new file mode 100644 index 0000000..2312af0 --- /dev/null +++ b/dsa/DSALib/DSA_Game/Save/Properties.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using DSALib.Auxiliary; +using Newtonsoft.Json; + +namespace DSALib.DSA_Game.Save +{ + public static class Properties + { + public static Dictionary<string, object> objects; + + static Properties() + { + objects = new Dictionary<string, object>(); + /*this.objects.Add("Sounds", new List<Sound>()); + this.objects.Add("CommandInfos", new List<CommandInfo>());*/ + } + + public static List<CommandInfo> CommandInfos + { + get => objects["CommandInfo"] as List<CommandInfo>; + set => objects["CommandInfo"] = value; + } // use Properties.Commandinfos to access the abstract Object array + + + public static void Deserialize(string path = @"Properties") + { + var files = Directory.GetFiles(path, "*.json"); + + foreach (var file in files) + try + { + var name = file.Split('\\').Last().Split('.')[0].Replace('-', '.'); + var data = File.ReadAllText(file); + var 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) + { + var assembly = o.Value is IList list + ? 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); + } + } + } +}
\ No newline at end of file |