-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.cs
More file actions
129 lines (115 loc) · 4.19 KB
/
JSON.cs
File metadata and controls
129 lines (115 loc) · 4.19 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
121
122
123
124
125
126
127
128
129
using System;
using System.Text;
using JsonFx.Json;
using System.Collections.Generic;
using System.IO;
namespace SDLS;
internal static class JSON
{
private static readonly JsonReader JSONReader = new();
private static readonly JsonWriter JSONWriter = new();
public static string ReadFileSystemJson(string fullFilePath)
{
if (!File.Exists(fullFilePath)) return null; // Return null if the file is not found
string str = File.ReadAllText(fullFilePath);
return (str != null) ?
(str.StartsWith("[") && str.EndsWith("]") ?
str.Substring(1, str.Length - 2) :
str) : // Return the string if the file isn't in an array
null; // Return null if the file is empty
}
public static string ReadInternalJson(string resourceName) // Method for loading embedded JSON resources
{
try
{
string name = Plugin.GetLastWord(resourceName);
string fullPath = Plugin.GetEmbeddedPath("default");
string fullResourceName = $"{fullPath}.{name}.json"; // Construct the full resource name
return Plugin.ReadTextResource(fullResourceName);
}
catch (Exception ex)
{
Plugin.Error("Something went seriously wrong and SDLS was not able to load it's own embedded resource.");
Plugin.Error(ex.Message);
return "";
}
}
public static string Serialize(object obj)
{
string serializedData = JSONWriter.Write(obj);
return serializedData;
}
public static Dictionary<string, object> Deserialize(string strObj) => Deserialize<Dictionary<string, object>>(strObj);
public static T Deserialize<T>(string strObj)
{
var deserializedData = JSONReader.Read<T>(strObj);
return deserializedData;
}
// This is extremely stupid but it works so who fucking cares
public static string[] SplitJSON(string strObjJoined)
{
var jsonObjects = new List<string>();
for (int i = 0, depth = 0, start = 0; i < strObjJoined.Length; i++)
{
if (strObjJoined[i] == '{' && depth++ == 0) start = i;
if (strObjJoined[i] == '}' && --depth == 0) jsonObjects.Add(strObjJoined.Substring(start, i - start + 1));
}
string[] jsonObjectsArray = [.. jsonObjects];
return jsonObjectsArray;
}
public static void CreateJSON(string strObjJoined, string relativeWritePath)
{
string writePath = Path.Combine(Plugin.PersistentDataPath, relativeWritePath) + ".json";
string path = Plugin.GetParentPath(writePath);
try
{
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
File.WriteAllText(writePath, relativeWritePath.ToLower().Contains("constants")
? strObjJoined// Output file as a single object
: $"[{strObjJoined}]"); // Put file in an array
}
catch (Exception ex)
{
Plugin.Error("Error writing file: " + ex.Message);
}
}
public static void RemoveJSON(string fullpath)
{
string filePath = fullpath + ".json";
if (File.Exists(filePath))
{
Plugin.Warn("Removing " + fullpath);
File.Delete(filePath);
}
}
public static string JoinJSON(List<string> strList, string sign = ",")
{
if (strList.Count == 0) return string.Empty;
var sb = new StringBuilder();
for (int i = 0; i < strList.Count - 1; i++)
{
sb.Append(strList[i]).Append(sign);
}
sb.Append(strList[strList.Count - 1]);
return sb.ToString();
}
public static string[] GetFilePaths() =>
[
"entities/qualities",
"entities/areas",
"entities/events",
"entities/exchanges",
"entities/personas",
"geography/TileRules",
"geography/Tiles",
"geography/TileSets",
"encyclopaedia/CombatAttacks",
"encyclopaedia/CombatItems",
"encyclopaedia/SpawnedEntities",
"encyclopaedia/Associations",
"encyclopaedia/Tutorials",
"encyclopaedia/Flavours",
"constants/combatconstants",
"constants/navigationconstants"
];
}