-
Notifications
You must be signed in to change notification settings - Fork 10
/
SQLVariableStorage.cs
257 lines (224 loc) · 7.51 KB
/
SQLVariableStorage.cs
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
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
using SQLite;
using YarnSpinnerGodot;
/// <summary>
/// SQLite based custom variable storage example.
/// </summary>
public partial class SQLVariableStorage : VariableStorageBehaviour
{
private SQLiteConnection db; // the database connector
public override void _EnterTree()
{
// pick a place on disk for the database to save to
string path = ProjectSettings.GlobalizePath("user://db.sqlite");
// create a new database connection to speak to it
db = new SQLiteConnection(path);
// create the tables we need
db.CreateTable<YarnString>();
db.CreateTable<YarnFloat>();
db.CreateTable<YarnBool>();
GD.Print($"Initialized database at {path}");
}
public override bool TryGetValue<T>(string variableName, out T result)
{
string query = "";
List<object> results = new();
if (typeof(T) == typeof(IConvertible))
{
// we don't know the expected type
if (TryGetValue<string>(variableName, out string stringResult))
{
result = (T) (object) stringResult;
return true;
}
if (TryGetValue<float>(variableName, out float floatResult))
{
result = (T) (object) floatResult;
return true;
}
if (TryGetValue<bool>(variableName, out bool boolResult))
{
result = (T) (object) boolResult;
return true;
}
result = default(T);
return false;
}
if (typeof(T) == typeof(string))
{
query = "SELECT * FROM YarnString WHERE key = ?";
results.AddRange(db.Query<YarnString>(query, variableName));
}
else if (typeof(T) == typeof(bool))
{
query = "SELECT * FROM YarnBool WHERE key = ?";
results.AddRange(db.Query<YarnBool>(query, variableName));
}
else if (typeof(T) == typeof(float))
{
query = "SELECT * FROM YarnFloat WHERE key = ?";
results.AddRange(db.Query<YarnFloat>(query, variableName));
}
// if a result was found, convert it to type T and assign it
if (results?.Count > 0)
{
if (results[0] is YarnFloat f)
{
result = (T) (object) f.value;
}
else if (results[0] is YarnBool b)
{
result = (T) (object) b.value;
}
else if (results[0] is YarnString s)
{
result = (T) (object) s.value;
}
else
{
throw new ArgumentException($"Unknown variable type {typeof(T)}");
}
return true;
}
// otherwise TryGetValue has failed
result = default(T);
return false;
}
public override void SetValue(string variableName, string stringValue)
{
// check it doesn't exist already in other table
if (Exists(variableName, typeof(bool)))
{
throw new ArgumentException($"{variableName} is a bool.");
}
if (Exists(variableName, typeof(float)))
{
throw new ArgumentException($"{variableName} is a float.");
}
// if not, insert or update row in this table to the given value
string query = "INSERT OR REPLACE INTO YarnString (key, value)";
query += "VALUES (?, ?)";
db.Execute(query, variableName, stringValue);
}
public override void SetValue(string variableName, float floatValue)
{
// check it doesn't exist already in other table
if (Exists(variableName, typeof(string)))
{
throw new ArgumentException($"{variableName} is a string.");
}
if (Exists(variableName, typeof(bool)))
{
throw new ArgumentException($"{variableName} is a bool.");
}
// if not, insert or update row in this table to the given value
string query = "INSERT OR REPLACE INTO YarnFloat (key, value)";
query += "VALUES (?, ?)";
db.Execute(query, variableName, floatValue);
}
public override void SetValue(string variableName, bool boolValue)
{
// check it doesn't exist already in other table
if (Exists(variableName, typeof(string)))
{
throw new ArgumentException($"{variableName} is a string.");
}
if (Exists(variableName, typeof(float)))
{
throw new ArgumentException($"{variableName} is a float.");
}
// if not, insert or update row in this table to the given value
string query = "INSERT OR REPLACE INTO YarnBool (key, value)";
query += "VALUES (?, ?)";
db.Execute(query, variableName, boolValue);
}
public override void Clear()
{
db.Execute("DELETE * FROM YarnString;");
db.Execute("DELETE * FROM YarnBool;");
db.Execute("DELETE * FROM YarnFloat;");
}
public override bool Contains(string variableName)
{
return Exists(variableName, typeof(string)) ||
Exists(variableName, typeof(bool)) ||
Exists(variableName, typeof(float));
}
public override void SetAllVariables(Dictionary<string, float> floats, Dictionary<string, string> strings,
Dictionary<string, bool> bools, bool clear = true)
{
foreach (var entry in floats)
{
SetValue(entry.Key, entry.Value);
}
foreach (var entry in bools)
{
SetValue(entry.Key, entry.Value);
}
foreach (var entry in strings)
{
SetValue(entry.Key, entry.Value);
}
}
public override (Dictionary<string, float>, Dictionary<string, string>, Dictionary<string, bool>) GetAllVariables()
{
var floatQuery = "SELECT value FROM YarnFloat";
var floats = db.Query<YarnFloat>(floatQuery)
.AsEnumerable()
.ToDictionary(f => f.key, f => f.value);
var stringQuery = "SELECT value FROM YarnString";
var strings = db.Query<YarnString>(stringQuery)
.AsEnumerable()
.ToDictionary(f => f.key, f => f.value);
var boolQuery = "SELECT value FROM YarnBool";
var booleans = db.Query<YarnBool>(boolQuery)
.AsEnumerable()
.ToDictionary(f => f.key, f => f.value);
return (floats, strings, booleans);
}
private bool Exists(string variableName, Type type)
{
if (type == typeof(string))
{
string stringResult;
if (TryGetValue(variableName, out stringResult))
{
return (stringResult != null);
}
}
else if (type == typeof(bool))
{
if (TryGetValue(variableName, out bool _))
{
return true;
}
}
else if (type == typeof(float))
{
if (TryGetValue(variableName, out float _))
{
return true;
}
}
return false;
}
}
public class YarnString
{
[PrimaryKey] public string key { get; set; }
public string value { get; set; }
}
public class YarnFloat
{
[PrimaryKey] public string key { get; set; }
public float value { get; set; }
}
public class YarnBool
{
[PrimaryKey] public string key { get; set; }
public bool value { get; set; }
}