/
json_database.dm
128 lines (98 loc) · 3.82 KB
/
json_database.dm
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
/// Represents a json file being used as a database in the data/ folder.
/// Changes made here will save back to the associated file, with recovery.
/// Will defer writes until later if multiple happen in the same tick.
/// Do not add an extra cache on top of this. This IS your cache.
/datum/json_database
VAR_PRIVATE
filepath
backup_filepath
cached_data
save_queued = FALSE
static/existing_json_database = list()
/datum/json_database/New(filepath)
if (IsAdminAdvancedProcCall())
to_chat(usr, "<span class='admin prefix'>json_database creation, linking to [html_encode(filepath)], was blocked.</span>", confidential = TRUE)
return
ASSERT(isnull(existing_json_database[filepath]), "[filepath] already has an associated json_database. You must expose it somehow and use that instead of making a new one.")
existing_json_database[filepath] = TRUE
src.filepath = filepath
backup_filepath = "[filepath].savebac"
if (fexists(filepath))
cached_data = safe_json_decode(file2text(filepath))
if (isnull(cached_data))
var/scenario = "[filepath] existed, but did not have valid JSON"
if (fexists(backup_filepath))
load_backup(scenario)
else
stack_trace("[scenario]. No backup could be found.")
cached_data = list()
else
if (fexists(backup_filepath))
load_backup("[filepath] didn't exist")
else
cached_data = list()
/datum/json_database/Destroy()
if (save_queued)
save()
existing_json_database -= filepath
return ..()
/// Returns the cached data.
/// Be careful on holding onto this data for too long, as it can mutate when other stuff changes it.
/// Do not mutate it yourself.
/datum/json_database/proc/get()
return cached_data
/// Returns the data with the given key.
/// For arrays, this is a number.
/// Be careful on holding onto this data for too long, as it can mutate when other stuff changes it.
/// Do not mutate it yourself.
/datum/json_database/proc/get_key(key)
return cached_data[key]
/// Sets the data at the key to the value, and queues a save.
/datum/json_database/proc/set_key(key, value)
cached_data[key] = value
queue_save()
/// Removes the data at the given item, and queues a save.
/// For dictionaries, this can be the key.
/// For arrays, this can be the value.
/datum/json_database/proc/remove(item)
UNTYPED_LIST_REMOVE(cached_data, item)
queue_save()
/// Inserts the data at the end of what is assumed to be an array, and queues a save.
/datum/json_database/proc/insert(value)
UNTYPED_LIST_ADD(cached_data, value)
queue_save()
/// Replaces the cache with the new data completely, and queues a save.
/// Do not touch the new data after passing it in.
/datum/json_database/proc/replace(list/new_data)
cached_data = new_data
queue_save()
/datum/json_database/proc/queue_save()
PRIVATE_PROC(TRUE)
if (save_queued)
return
addtimer(CALLBACK(src, PROC_REF(save)), 0)
/datum/json_database/proc/save()
PRIVATE_PROC(TRUE)
save_queued = FALSE
if (fexists(filepath))
rustg_file_write(file2text(filepath), backup_filepath)
rustg_file_write(json_encode(cached_data, JSON_PRETTY_PRINT), filepath)
ASSERT(!isnull(safe_json_decode(file2text(filepath))), "JSON written to [filepath] was not valid. Backup will be preserved.")
fdel(backup_filepath)
/datum/json_database/proc/load_backup(scenario)
PRIVATE_PROC(TRUE)
var/cached_contents = file2text(backup_filepath)
var/list/backed_up_data = safe_json_decode(cached_contents)
if (isnull(backed_up_data))
stack_trace("[scenario]. Backup existed, but also did not have valid JSON.")
cached_data = list()
else
stack_trace("[scenario]. Backup existed and was used instead. The JSON file has been updated.")
cached_data = backed_up_data
rustg_file_write(cached_contents, filepath)
/datum/json_database/vv_edit_var(var_name, var_value)
switch (var_name)
if (nameof(filepath), nameof(backup_filepath))
return FALSE
else
return ..()