-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataStore.cs
More file actions
284 lines (249 loc) · 6.56 KB
/
DataStore.cs
File metadata and controls
284 lines (249 loc) · 6.56 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
namespace Pluton.Core
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using Serialize;
public class DataStore : CountedInstance
{
public readonly Hashtable datastore;
static DataStore instance;
public string PATH;
public object this[string tablename, object key] {
get {
return Get(tablename, key);
}
set {
Add(tablename, key, value);
}
}
static object SerializeIfPossible(object keyorval)
{
if (keyorval == null)
return keyorval;
if (keyorval is Vector3) {
return ((Vector3)keyorval).Serialize();
}
if (keyorval is Quaternion) {
return ((Quaternion)keyorval).Serialize();
}
return keyorval;
}
static object DeserializeIfPossible(object keyorval)
{
if (keyorval is ISerializable) {
return ((ISerializable)keyorval).Deserialize();
}
return keyorval;
}
public string RemoveChars(string str)
{
foreach (string c in new string[] { "/", "\\", "%", "$" }) {
str = str.Replace(c, "");
}
return str;
}
public bool ToIni(string inifilename = "DataStore")
{
string inipath = Path.Combine(Util.GetInstance().GetPublicFolder(), RemoveChars(inifilename).Trim() + ".ini");
File.WriteAllText(inipath, "");
var ini = new IniParser(inipath);
ini.Save();
foreach (string section in datastore.Keys) {
var ht = (Hashtable)datastore[section];
foreach (object setting in ht.Keys) {
try {
string key = "NullReference";
string val = "NullReference";
if (setting != null) {
if (setting.GetType().GetMethod("ToString", Type.EmptyTypes) == null) {
key = "type:" + setting.GetType().ToString();
} else {
key = setting.ToString();
}
}
if (ht[setting] != null) {
if (ht[setting].GetType().GetMethod("ToString", Type.EmptyTypes) == null) {
val = "type:" + ht[setting].GetType().ToString();
} else {
val = ht[setting].ToString();
}
}
ini.AddSetting(section, key, val);
} catch (Exception ex) {
Logger.LogException(ex);
}
}
}
ini.Save();
return true;
}
public bool TableToIni(string tablename, string inipath)
{
var hashtable = (Hashtable)datastore[tablename];
if (hashtable != null) {
if (!Path.HasExtension(inipath))
inipath += ".ini";
File.WriteAllText(inipath, "");
var ini = new IniParser(inipath);
ini.Save();
foreach (string setting in hashtable.Keys) {
ini.AddSetting(tablename, setting, hashtable[setting].ToString());
}
ini.Save();
return true;
}
return false;
}
public bool AddFromIni(string inipath)
{
if (File.Exists(inipath)) {
var ini = new IniParser(inipath);
foreach (string section in ini.Sections.Keys) {
foreach (string setting in ini.EnumSection(section)) {
Add(section, setting, ini.GetSetting(section, setting));
}
}
return true;
}
return false;
}
public void Add(string tablename, object key, object val)
{
if (key == null)
key = "NullReference";
var hashtable = (Hashtable)datastore[tablename];
if (hashtable == null) {
hashtable = new Hashtable();
datastore.Add(tablename, hashtable);
}
hashtable[SerializeIfPossible(key)] = SerializeIfPossible(val);
}
public bool ContainsKey(string tablename, object key)
{
if (key == null)
return false;
var hashtable = (Hashtable)datastore[tablename];
if (hashtable != null) {
return hashtable.ContainsKey(SerializeIfPossible(key));
}
return false;
}
public bool ContainsValue(string tablename, object val)
{
var hashtable = (Hashtable)datastore[tablename];
if (hashtable != null) {
return hashtable.ContainsValue(SerializeIfPossible(val));
}
return false;
}
public int Count(string tablename)
{
var hashtable = (Hashtable)datastore[tablename];
if (hashtable == null) {
return 0;
}
return hashtable.Count;
}
public void Flush(string tablename)
{
if (((Hashtable)datastore[tablename]) != null) {
datastore.Remove(tablename);
}
}
public object Get(string tablename, object key)
{
if (key == null)
return null;
var hashtable = (Hashtable)datastore[tablename];
return hashtable == null ? null : DeserializeIfPossible(hashtable[SerializeIfPossible(key)]);
}
public static DataStore GetInstance()
{
if (instance == null) {
instance = new DataStore("PlutonDatastore.ds");
}
return instance;
}
public Hashtable GetTable(string tablename)
{
var hashtable = (Hashtable)datastore[tablename];
if (hashtable == null) {
return null;
}
var parse = new Hashtable(hashtable.Count);
foreach (DictionaryEntry entry in hashtable) {
parse.Add(DeserializeIfPossible(entry.Key), DeserializeIfPossible(entry.Value));
}
return parse;
}
public object[] Keys(string tablename)
{
var hashtable = (Hashtable)datastore[tablename];
if (hashtable == null) {
return null;
}
var parse = new List<object>(hashtable.Keys.Count);
foreach (object key in hashtable.Keys) {
parse.Add(DeserializeIfPossible(key));
}
return parse.ToArray<object>();
}
public void Load()
{
if (File.Exists(PATH)) {
try {
Hashtable hashtable = Util.HashtableFromFile(PATH);
datastore.Clear();
int count = 0;
foreach (DictionaryEntry entry in hashtable) {
datastore[entry.Key] = entry.Value;
count += (entry.Value as Hashtable).Count;
}
Debug.Log("DataStore Loaded from " + PATH);
Debug.Log($"Tables: {datastore.Count}! Keys: {count}");
} catch (Exception ex) {
Logger.LogException(ex);
}
}
}
public void Remove(string tablename, object key)
{
if (key == null)
return;
var hashtable = (Hashtable)datastore[tablename];
if (hashtable != null) {
hashtable.Remove(SerializeIfPossible(key));
}
}
public void Save()
{
if (datastore.Count != 0) {
Util.HashtableToFile(datastore, PATH);
Debug.Log("DataStore saved to " + PATH);
}
}
public object[] Values(string tablename)
{
var hashtable = (Hashtable)datastore[tablename];
if (hashtable == null) {
return null;
}
var parse = new List<object>(hashtable.Values.Count);
foreach (object val in hashtable.Values) {
parse.Add(DeserializeIfPossible(val));
}
return parse.ToArray();
}
public DataStore(string path)
{
path = RemoveChars(path);
datastore = new Hashtable();
PATH = Path.Combine(Util.GetInstance().GetPublicFolder(), path);
Debug.Log("New DataStore instance: " + PATH);
}
}
}