-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
_string_lists.dm
42 lines (33 loc) · 1.52 KB
/
_string_lists.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
#define pick_list(FILE, KEY) (pick(strings(FILE, KEY)))
#define pick_list_weighted(FILE, KEY) (pickweight(strings(FILE, KEY)))
#define pick_list_replacements(FILE, KEY) (strings_replacement(FILE, KEY))
#define json_load(FILE) (json_decode(file2text(FILE)))
GLOBAL_LIST(string_cache)
GLOBAL_VAR(string_filename_current_key)
/proc/strings_replacement(filename, key, directory = "strings")
load_strings_file(filename, directory)
if((filename in GLOB.string_cache) && (key in GLOB.string_cache[filename]))
var/response = pick(GLOB.string_cache[filename][key])
var/regex/r = regex("@pick\\((\\D+?)\\)", "g")
response = r.Replace(response, /proc/strings_subkey_lookup)
return response
else
CRASH("strings list not found: [directory]/[filename], index=[key]")
/proc/strings(filename as text, key as text, directory = "strings")
load_strings_file(filename, directory)
if((filename in GLOB.string_cache) && (key in GLOB.string_cache[filename]))
return GLOB.string_cache[filename][key]
else
CRASH("strings list not found: [directory]/[filename], index=[key]")
/proc/strings_subkey_lookup(match, group1)
return pick_list(GLOB.string_filename_current_key, group1)
/proc/load_strings_file(filename, directory = "strings")
GLOB.string_filename_current_key = filename
if(filename in GLOB.string_cache)
return //no work to do
if(!GLOB.string_cache)
GLOB.string_cache = new
if(fexists("[directory]/[filename]"))
GLOB.string_cache[filename] = json_load("[directory]/[filename]")
else
CRASH("file not found: [directory]/[filename]")