diff options
Diffstat (limited to 'DSACore/DSA_Game/Save')
-rw-r--r-- | DSACore/DSA_Game/Save/Properties.cs | 88 | ||||
-rw-r--r-- | DSACore/DSA_Game/Save/SaveCommand.cs | 70 | ||||
-rw-r--r-- | DSACore/DSA_Game/Save/Session.cs | 47 |
3 files changed, 205 insertions, 0 deletions
diff --git a/DSACore/DSA_Game/Save/Properties.cs b/DSACore/DSA_Game/Save/Properties.cs new file mode 100644 index 0000000..f201431 --- /dev/null +++ b/DSACore/DSA_Game/Save/Properties.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using DSACore.Audio; +using DSACore.Auxiliary; +using Newtonsoft.Json; + +namespace DSACore.DSA_Game.Save +{ + using System.Collections; + using System.IO; + using Newtonsoft.Json; + + public static class Properties + { + private 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 List<Sound> Sounds + { + get => objects["Sound"] as List<Sound>; + 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); + } + } + } +}
\ No newline at end of file diff --git a/DSACore/DSA_Game/Save/SaveCommand.cs b/DSACore/DSA_Game/Save/SaveCommand.cs new file mode 100644 index 0000000..198d707 --- /dev/null +++ b/DSACore/DSA_Game/Save/SaveCommand.cs @@ -0,0 +1,70 @@ +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace DSACore.DSA_Game.Save +{ + using System.IO; + + public class SaveCommand + { + public void LoadSession(string name = "") + { + if (name.Equals("?") || name.Equals(string.Empty)) + { + Console.WriteLine($"Gespeicherte Sessions:"); + Console.WriteLine(this.ListSessions()); + return; + } + + var path = Save.Session.DirectoryPath + @"\" + name; + + var files = Directory.GetFiles(path); + var session = files.OrderByDescending(x => Convert.ToInt32(x.Split('-').Last().Split('.').First())).First(); + Dsa.Session = Session.Load(session); + + Console.WriteLine($"{name} wurde geladen"); + } + + public void SessionSave(string name = "") + { + //var sendFile = this.Context.Channel.SendWebFile("https://cdn.discordapp.com/attachments/377123019673567232/465615882048110603/giphy.gif"); + + if (name.Equals("?") || name.Equals(string.Empty)) + { + Console.WriteLine($"Gespeicherte Sessions:"); + Console.WriteLine(this.ListSessions()); + return; + } + + var path = DSA_Game.Save.Session.DirectoryPath + @"\" + name; + if (Directory.Exists(path)) + { + var files = Directory.GetFiles(path); + int current = files.Max(x => Convert.ToInt32(x.Split('-').Last().Split('.').First())); + Dsa.Session.SessionName = name; + Dsa.Session.Save(path + "\\" + name + $"-{++current}.json"); + } + else + { + Directory.CreateDirectory(path); + Dsa.Session.SessionName = name; + Dsa.Session.Save(path + "\\" + name + $"-0.json"); + } + + Console.WriteLine($"{name} wurde gespeichert"); + //await sendFile; + } + + private string[] ListSessions() + { + string[] dirs = Directory.GetDirectories(Session.DirectoryPath).OrderByDescending(x => new DirectoryInfo(x).LastAccessTime.Ticks).ToArray(); + for (int i = 0; i < dirs.Length; i++) + { + dirs[i] += "; " + new DirectoryInfo(dirs[i]).LastAccessTime; + } + + return dirs; + } + } +} diff --git a/DSACore/DSA_Game/Save/Session.cs b/DSACore/DSA_Game/Save/Session.cs new file mode 100644 index 0000000..d343920 --- /dev/null +++ b/DSACore/DSA_Game/Save/Session.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; + +namespace DSACore.DSA_Game.Save +{ + using System.IO; + using Characters; + using Newtonsoft.Json; + + public class Session + { + public static string DirectoryPath { get; set; } = @"sessions"; + + public Dictionary<string, string> Relation { get; set; } = new Dictionary<string, string>(); // dictionary to match the char + + public List<SaveChar> Chars { get; set; } = new List<SaveChar>(); // list of all characters + + public string SessionName { get; set; } + + public static Session Load(string path) + { + try + { + return JsonConvert.DeserializeObject<Session>(File.ReadAllText(path)); // Deserialize Data and create Session Object + } + catch (Exception e) + { + // ignored + Console.WriteLine($"Laden von Save-File {path} fehlgeschlagen."+ e); + return null; + } + } + + public void Save(string path) + { + try + { + File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); // Deserialize Data and create CommandInfo Struct + } + catch (Exception e) + { + Console.WriteLine($"Speichern von Save-File {path} fehlgeschlagen.\n"+ e); + // ignored + } + } + } +} |