forked from prestonelam2003/CobaltEssentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCobaltDB.lua
284 lines (205 loc) · 7.37 KB
/
CobaltDB.lua
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
284
--Copyright (C) 2023, Preston Elam (CobaltTetra) ALL RIGHTS RESERVED
--COBALTESSENTIALS IS PROTECTED UNDER AN GPLv3 LICENSE
-- PRE: Precondition
-- POST: Postcondition
--RETURNS: What the method returns
--TODO: CHANGE THE FORMAT TO DATABASES > TABLES > KEYS > VALUES
local M = {}
local loadedDatabases = {}
--local loadedJson = {}
local dbpath
local cobaltSysChar = string.char(0x99, 0x99, 0x99, 0x99)
local CobaltDBport = 58933
MP.RegisterEvent("initDB","initDB")
MP.RegisterEvent("openDatabase","openDatabase")
MP.RegisterEvent("closeDatabase","closeDatabase")
MP.RegisterEvent("setCobaltDBport","setCobaltDBport")
MP.RegisterEvent("DBresend","resend")
MP.RegisterEvent("repairDBconnection","repairDBconnection")
MP.RegisterEvent("testCobaltDBconnection","testConnection")
MP.RegisterEvent("query","query")
MP.RegisterEvent("getTable","getTable")
MP.RegisterEvent("getTables","getTables")
MP.RegisterEvent("getKeys","getKeys")
MP.RegisterEvent("tableExists","tableExists")
MP.RegisterEvent("HandleSyncEvent","HandleSyncEvent")
MP.RegisterEvent("set","set")
----------------------------------------------------------EVENTS-----------------------------------------------------------
--give the CobaltDBconnector all the information it needs without having to re-calculate it all
function initDB()
json = require("json")
utils = require("CobaltUtils")
dbpath = pluginPath .. "/CobaltDB/"
if not dontusesocket then
socket = require("socket")
local jsonFile, error = io.open(dbpath .."config.json")
if error == nil then
CobaltDBport = tonumber(json.parse(jsonFile:read("*a")).CobaltDBport.value)
jsonFile:close()
end
end
CElog("CobaltDB Initiated","CobaltDB")
MP.TriggerLocalEvent("onCobaltDBhandshake",CobaltDBport)
if not dontusesocket then
connector = socket.udp()
end
end
----------------------------------------------------------MUTATORS---------------------------------------------------------
function openDatabase(DBname, requestID)
local jsonPath = dbpath .. DBname .. ".json"
local databaseLoaderInfo = "error" -- defines if the DB was created just now or if it was pre-existing.
local contents, error = utils.readJson(jsonPath)
if error then
if error == "File does not exist" then
CElog("JSON file does not exist, creating a new one.","CobaltDB")
databaseLoaderInfo = "new"
local success, error = utils.writeJson(jsonPath, nil)
if not success then
CElog('failed to write file "' .. tostring(jsonPath) .. '", error: ' .. tostring(error),"WARN")
end
loadedDatabases[DBname] = {}
end
else
databaseLoaderInfo = "loaded"
loadedDatabases[DBname] = contents
end
if dontusesocket then return databaseLoaderInfo end
databaseLoaderInfo = requestID .. "[requestIDsplitter]" .. databaseLoaderInfo
connector:sendto(databaseLoaderInfo ,"127.0.0.1", CobaltDBport)
lastSent = databaseLoaderInfo
end
function closeDatabase(DBname)
updateDatabase(DBname)
--loadedJson[DBname] = nil
loadedDatabases[DBname] = nil
end
function setCobaltDBport(port)
CobaltDBport = tonumber(port)
connector:sendto(CobaltDBport ,"127.0.0.1", CobaltDBport)
lastSent = CobaltDBport
end
--tells CobaltDB that the data was not received.
function resend()
connector:sendto(lastSent ,"127.0.0.1", CobaltDBport)
end
function testConnection()
connector:sendto(CobaltDBport ,"127.0.0.1", CobaltDBport)
end
--saves the table's changes to a file
function updateDatabase(DBname)
local filePath = dbpath .. DBname
local success, error = utils.writeJson(filePath..".temp", loadedDatabases[DBname])
if success then
success, error = FS.Remove(filePath .. ".json")
if success then
success, error = FS.Rename(filePath .. ".temp", filePath .. ".json")
end
end
if not success then
CElog('Failed to update database "'..DBname..'"on disk: '..tostring(error), "WARN")
end
----CElog("Updated: '" .. dbpath .. DBname .. ".json'","DEBUG")
end
--changes the table
function set(DBname, tableName, key, value)
if loadedDatabases[DBname] ~= nil then
if loadedDatabases[DBname][tableName] == nil then
loadedDatabases[DBname][tableName] = {}
end
if key ~= nil then
if value == "null" then
loadedDatabases[DBname][tableName][key] = nil
else
loadedDatabases[DBname][tableName][key] = json.parse(value)
end
updateDatabase(DBname)
end
else --TABLE DOESN'T EXIST
error("CobaltDB Table " .. DBname .. " not loaded!")
end
end
---------------------------------------------------------ACCESSORS---------------------------------------------------------
--returns a specific value from the table
function query(DBname, tableName, key, requestID)
local data
if loadedDatabases[DBname] == nil then
--error here, database isn't open
data = cobaltSysChar .. "E:" .. DBname .. "not found."
else
if loadedDatabases[DBname][tableName] == nil then
--error here, table doesn't exist
data = cobaltSysChar .. "E:" .. DBname .. " > " .. tableName .. " not found."
else
if loadedDatabases[DBname][tableName][key] == nil then
data = cobaltSysChar .. "E:" .. DBname .. " > " .. tableName .. " > " .. key .. " not found."
else
--send the value as json
data = json.stringify(loadedDatabases[DBname][tableName][key])
end
end
end
if dontusesocket then return data end
data = requestID .. "[requestIDsplitter]" .. data
connector:sendto(data ,"127.0.0.1", CobaltDBport)
lastSent = data
end
--returns a read-only version of the table as json.
function getTable(DBname, tableName, requestID)
local data
if loadedDatabases[DBname] == nil then
--error here, database isn't open
data = cobaltSysChar .. "E:" .. DBname .. "not found."
else
if loadedDatabases[DBname][tableName] == nil then
--error here, tableName doesn't exist
data = cobaltSysChar .. "E:" .. DBname .. " > " .. tableName .. " not found."
else
--send the table as json
data = json.stringify(loadedDatabases[DBname][tableName])
end
end
if dontusesocket then return data end
data = requestID .. "[requestIDsplitter]" .. data
connector:sendto(data ,"127.0.0.1", CobaltDBport)
lastSent = data
end
--returns a read-only list of all table names within the database
function getTables(DBname, requestID)
local data = {}
for id, _ in pairs(loadedDatabases[DBname]) do
data[id] = id
end
data = json.stringify(data)
if dontusesocket then return data end
data = requestID .. "[requestIDsplitter]" .. data
connector:sendto(data ,"127.0.0.1", CobaltDBport)
lastSent = data
end
function getKeys(DBname, tableName, requestID)
local data = {}
for id, _ in pairs(loadedDatabases[DBname][tableName]) do
data[id] = id
end
data = json.stringify(data)
if dontusesocket then return data end
data = requestID .. "[requestIDsplitter]" .. data
connector:sendto(data ,"127.0.0.1", CobaltDBport)
lastSent = data
end
function tableExists(DBname, tableName, requestID)
local data = "E: database not open"
if loadedDatabases[DBname] ~= nil and loadedDatabases[DBname][tableName] ~= nil then
data = tableName
end
if dontusesocket then return data end
data = requestID .. "[requestIDsplitter]" .. data
connector:sendto(data ,"127.0.0.1", CobaltDBport)
lastSent = data
end
---------------------------------------------------------FUNCTIONS---------------------------------------------------------
------------------------------------------------------PUBLICINTERFACE------------------------------------------------------
----EVENTS-----
----MUTATORS-----
----ACCESSORS----
----FUNCTIONS----
return M