summaryrefslogtreecommitdiff
path: root/DiscoBot/DSA_Game/Save/Properties.cs
blob: 67d30b0cc9560fb7e50d561ffd856f8b1ffe5945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 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
                        var log = new LogMessage(LogSeverity.Warning, "Properties", $"Laden von Save-File {file} fehlgeschlagen.", e);
                        Console.WriteLine(log);
                    }

                }
            
        }

        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
                var log = new LogMessage(LogSeverity.Warning, "Properties", $"Speichern von Save-File fehlgeschlagen.", e);
                Console.WriteLine(log);
            }
        }
    }
}