-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationHandler.cs
More file actions
120 lines (109 loc) · 4.04 KB
/
SerializationHandler.cs
File metadata and controls
120 lines (109 loc) · 4.04 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using BloonFactory.Modules.Core;
using BTD_Mod_Helper;
using BTD_Mod_Helper.Api;
using BTD_Mod_Helper.Extensions;
using FactoryCore.API;
using Il2CppSystem.Security.Cryptography;
using MelonLoader;
using MelonLoader.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace BloonFactory
{
internal static class SerializationHandler
{
internal static List<BloonTemplate> Templates = new List<BloonTemplate>();
internal static JsonSerializerSettings Settings => new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, Formatting = Formatting.Indented };
internal static string FolderDirectory => Path.Combine(MelonEnvironment.ModsDirectory, "Factory");
internal const string FileExtention = ".cstmbln";
public static bool HasLoaded = false;
internal static void EnsureFolderExists()
{
if (!Directory.Exists(FolderDirectory))
Directory.CreateDirectory(FolderDirectory);
}
internal static void SaveTemplate(BloonTemplate template, string path)
{
EnsureFolderExists();
var content = JsonConvert.SerializeObject(template, Settings);
File.WriteAllText(path + FileExtention, content);
}
internal static void SaveTemplate(BloonTemplate template)
{
EnsureFolderExists();
var content = JsonConvert.SerializeObject(template, Settings);
var path = Path.Combine(FolderDirectory, template.Guid.ToString() + FileExtention);
File.WriteAllText(path, content);
}
internal static bool ContainGuid(Guid guid)
{
return Templates.Any(a => a.Guid == guid);
}
internal static bool TryLoadTemplate(BloonTemplate template)
{
EnsureFolderExists();
if (ContainGuid(template.Guid))
{
MelonLogger.Msg("File already exists");
return false;
}
template.IsLoaded = false;
SaveTemplate(template);
Templates.Add(template);
return true;
}
internal static BloonTemplate GetTemplateFromPath(string path)
{
EnsureFolderExists();
if (!File.Exists(path))
return null;
File.ReadAllText(path);
var content = JsonConvert.DeserializeObject<BloonTemplate>(File.ReadAllText(path), Settings);
return content;
}
internal static void LoadTemplate(BloonTemplate template)
{
if (!ContainGuid(template.Guid))
{
template.SetReferences();
Templates.Add(template);
}
}
internal static void LoadAllTemplates()
{
EnsureFolderExists();
foreach (var path in Directory.GetFiles(FolderDirectory).Where(f => f.EndsWith(".cstmbln")))
{
var template = GetTemplateFromPath(path);
LoadTemplate(template);
}
HasLoaded = true;
}
internal static BloonTemplate CreateTemplate(string name)
{
EnsureFolderExists();
var template = JsonConvert.DeserializeObject<BloonTemplate>(Assembly.GetCallingAssembly().GetEmbeddedText("DefaultTemplate" + FileExtention), Settings);
template.IsLoaded = false;
template.Name = name;
template.Guid = Guid.NewGuid();
template.SetReferences();
SaveTemplate(template);
Templates.Add(template);
return template;
}
internal static void DeleteTemplate(BloonTemplate template)
{
EnsureFolderExists();
var path = Path.Combine(FolderDirectory, template.Guid.ToString() + FileExtention);
template.IsQueueForDeletion = true;
if (File.Exists(path))
{
File.Delete(path);
}
}
}
}