-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathpreferences.dm
573 lines (418 loc) · 16.6 KB
/
preferences.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
GLOBAL_LIST_EMPTY(preferences_datums)
/datum/preferences
var/client/parent
//doohickeys for savefiles
var/path
var/default_slot = 1 //Holder so it doesn't default to slot 1, rather the last one used
var/max_save_slots = 3
//non-preference stuff
var/muted = 0
var/last_ip
var/last_id
//game-preferences
var/lastchangelog = "" //Saved changlog filesize to detect if there was a change
//Antag preferences
var/list/be_special = list() //Special role selection
/// Custom keybindings. Map of keybind names to keyboard inputs.
/// For example, by default would have "swap_hands" -> list("X")
var/list/key_bindings = list()
/// Cached list of keybindings, mapping keys to actions.
/// For example, by default would have "X" -> list("swap_hands")
var/list/key_bindings_by_key = list()
var/toggles = TOGGLES_DEFAULT
var/db_flags
var/chat_toggles = TOGGLES_DEFAULT_CHAT
var/extra_toggles = TOGGLES_DEFAULT_EXTRA
var/ghost_form = "ghost"
var/list/player_alt_titles = list()
var/list/randomise = list()
//Quirk list
var/list/all_quirks = list()
//Job preferences 2.0 - indexed by job title , no key or value implies never
var/list/job_preferences = list()
/// The current window, PREFERENCE_TAB_* in [`code/__DEFINES/preferences.dm`]
var/current_window = PREFERENCE_TAB_CHARACTER_PREFERENCES
var/unlock_content = 0
var/list/ignoring = list()
var/list/exp = list()
var/action_buttons_screen_locs = list()
/// A preview of the current character
var/atom/movable/screen/character_preview_view/character_preview_view
/// Icon for the preview background
var/icon/background = "floor"
/// A list of instantiated middleware
var/list/datum/preference_middleware/middleware = list()
/// The savefile relating to core preferences, PREFERENCE_PLAYER
var/savefile/game_savefile
/// The savefile relating to character preferences, PREFERENCE_CHARACTER
var/savefile/character_savefile
/// A list of keys that have been updated since the last save.
var/list/recently_updated_keys = list()
/// A cache of preference entries to values.
/// Used to avoid expensive READ_FILE every time a preference is retrieved.
var/value_cache = list()
/// If set to TRUE, will update character_profiles on the next ui_data tick.
var/tainted_character_profiles = FALSE
///removed, kept here for migration in 'legacy_mood_migration.dm'
///DO NOT USE THIS!
var/yogtoggles
/datum/preferences/Destroy(force, ...)
QDEL_NULL(character_preview_view)
QDEL_LIST(middleware)
value_cache = null
return ..()
/datum/preferences/New(client/C)
parent = C
for (var/middleware_type in subtypesof(/datum/preference_middleware))
middleware += new middleware_type(src)
if(istype(C))
if(!IsGuestKey(C.key))
load_path(C.ckey)
if (C.IsByondMember())
unlock_content |= DONOR_BYOND
// the latter handles race cases where the prefs are not fully loaded in, or GLOB.donators hasn't loaded in yet
if(is_donator(C) || (C.ckey in get_donators()))
unlock_content |= DONOR_YOGS
// give save slots to donors
if (unlock_content & DONOR_YOGS)
max_save_slots += DONOR_YOGS_SLOTS + DONOR_BYOND_SLOTS
else if (unlock_content & DONOR_BYOND)
max_save_slots += DONOR_BYOND_SLOTS
// give them default keybinds and update their movement keys
key_bindings = deepCopyList(GLOB.default_hotkeys)
key_bindings_by_key = get_key_bindings_by_key(key_bindings)
randomise = get_default_randomization()
var/loaded_preferences_successfully = load_preferences()
if(loaded_preferences_successfully)
if(load_character())
return
//we couldn't load character data so just randomize the character appearance + name
randomise_appearance_prefs() //let's create a random character then - rather than a fat, bald and naked man.
if(C)
apply_all_client_preferences()
C.set_macros()
if(!loaded_preferences_successfully)
save_preferences()
save_character() //let's save this new random character so it doesn't keep generating new ones.
/datum/preferences/ui_interact(mob/user, datum/tgui/ui)
if(!SSjob.initialized)
tgui_alert(user, "You cannot open the preferences menu before the job subsystem is initialized!")
return
// If you leave and come back, re-register the character preview
if (!isnull(character_preview_view) && !(character_preview_view in user.client?.screen))
user.client?.register_map_obj(character_preview_view)
ui = SStgui.try_update_ui(user, src, ui)
if(!ui)
ui = new(user, src, "PreferencesMenu")
ui.set_autoupdate(FALSE)
ui.open()
// HACK: Without this the character starts out really tiny because of some BYOND bug.
// You can fix it by changing a preference, so let's just forcably update the body to emulate this.
addtimer(CALLBACK(character_preview_view, /atom/movable/screen/character_preview_view/proc/update_body), 1 SECONDS)
/datum/preferences/ui_state(mob/user)
return GLOB.always_state
// Without this, a hacker would be able to edit other people's preferences if
// they had the ref to Topic to.
/datum/preferences/ui_status(mob/user, datum/ui_state/state)
return user.client == parent ? UI_INTERACTIVE : UI_CLOSE
/datum/preferences/ui_data(mob/user)
var/list/data = list()
if (isnull(character_preview_view))
character_preview_view = create_character_preview_view(user)
else if (character_preview_view.client != parent)
// The client re-logged, and doing this when they log back in doesn't seem to properly
// carry emissives.
character_preview_view.register_to_client(parent)
if (tainted_character_profiles)
data["character_profiles"] = create_character_profiles()
tainted_character_profiles = FALSE
data["character_preferences"] = compile_character_preferences(user)
data["active_slot"] = default_slot
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
data += preference_middleware.get_ui_data(user)
return data
/datum/preferences/ui_static_data(mob/user)
var/list/data = list()
data["character_profiles"] = create_character_profiles()
data["character_preview_view"] = character_preview_view.assigned_map
data["overflow_role"] = SSjob.GetJob(SSjob.overflow_role).title
data["window"] = current_window
data["content_unlocked"] = unlock_content
data["ckey"] = lowertext(user.client.ckey)
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
data += preference_middleware.get_ui_static_data(user)
return data
/datum/preferences/ui_assets(mob/user)
var/list/assets = list(
get_asset_datum(/datum/asset/spritesheet/preferences),
get_asset_datum(/datum/asset/json/preferences),
)
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
assets += preference_middleware.get_ui_assets()
return assets
/datum/preferences/ui_act(action, list/params, datum/tgui/ui, datum/ui_state/state)
. = ..()
if (.)
return
switch (action)
if ("change_slot")
// Save existing character
save_character()
// SAFETY: `load_character` performs sanitization the slot number
if (!load_character(params["slot"]))
tainted_character_profiles = TRUE
randomise_appearance_prefs()
save_character()
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
preference_middleware.on_new_character(usr)
character_preview_view.update_body()
return TRUE
if ("rotate")
character_preview_view.dir = turn(character_preview_view.dir, -90)
return TRUE
if ("cycle")
background = next_list_item(background, GLOB.preview_backgrounds)
character_preview_view.update_body()
return TRUE
if ("set_preference")
var/requested_preference_key = params["preference"]
var/value = params["value"]
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
if (preference_middleware.pre_set_preference(usr, requested_preference_key, value))
return TRUE
var/datum/preference/requested_preference = GLOB.preference_entries_by_key[requested_preference_key]
if (isnull(requested_preference))
return FALSE
// SAFETY: `update_preference` performs validation checks
if (!update_preference(requested_preference, value))
return FALSE
if (istype(requested_preference, /datum/preference/name))
tainted_character_profiles = TRUE
return TRUE
if ("set_color_preference")
var/requested_preference_key = params["preference"]
var/datum/preference/requested_preference = GLOB.preference_entries_by_key[requested_preference_key]
if (isnull(requested_preference))
return FALSE
if (!istype(requested_preference, /datum/preference/color))
return FALSE
var/default_value = read_preference(requested_preference.type)
// Yielding
var/new_color = input(
usr,
"Select new color",
null,
default_value || COLOR_WHITE,
) as color | null
if (!new_color)
return FALSE
if (!update_preference(requested_preference, new_color))
return FALSE
return TRUE
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
var/delegation = preference_middleware.action_delegations[action]
if (!isnull(delegation))
return call(preference_middleware, delegation)(params, usr)
return FALSE
/datum/preferences/ui_close(mob/user)
save_character()
save_preferences()
QDEL_NULL(character_preview_view)
/datum/preferences/Topic(href, list/href_list)
. = ..()
if (.)
return
if (href_list["open_keybindings"])
current_window = PREFERENCE_TAB_KEYBINDINGS
update_static_data(usr)
ui_interact(usr)
return TRUE
/datum/preferences/Topic(href, list/href_list)
. = ..()
if (.)
return
if (href_list["open_keybindings"])
current_window = PREFERENCE_TAB_KEYBINDINGS
update_static_data(usr)
ui_interact(usr)
return TRUE
/datum/preferences/proc/create_character_preview_view(mob/user)
character_preview_view = new(null, src, user.client)
character_preview_view.update_body()
character_preview_view.register_to_client(user.client)
return character_preview_view
/datum/preferences/proc/compile_character_preferences(mob/user)
var/list/preferences = list()
for (var/datum/preference/preference as anything in get_preferences_in_priority_order())
if (!preference.is_accessible(src))
continue
LAZYINITLIST(preferences[preference.category])
var/value = read_preference(preference.type)
var/data = preference.compile_ui_data(user, value)
preferences[preference.category][preference.savefile_key] = data
for (var/datum/preference_middleware/preference_middleware as anything in middleware)
var/list/append_character_preferences = preference_middleware.get_character_preferences(user)
if (isnull(append_character_preferences))
continue
for (var/category in append_character_preferences)
if (category in preferences)
preferences[category] += append_character_preferences[category]
else
preferences[category] = append_character_preferences[category]
return preferences
/// Applies all PREFERENCE_PLAYER preferences
/datum/preferences/proc/apply_all_client_preferences()
for (var/datum/preference/preference as anything in get_preferences_in_priority_order())
if (preference.savefile_identifier != PREFERENCE_PLAYER)
continue
value_cache -= preference.type
preference.apply_to_client(parent, read_preference(preference.type))
// This is necessary because you can open the set preferences menu before
// the atoms SS is done loading.
INITIALIZE_IMMEDIATE(/atom/movable/screen/character_preview_view)
/// A preview of a character for use in the preferences menu
/atom/movable/screen/character_preview_view
name = "character_preview"
del_on_map_removal = FALSE
layer = GAME_PLANE
plane = GAME_PLANE
/// The body that is displayed
var/mob/living/carbon/human/dummy/body
/// The preferences this refers to
var/datum/preferences/preferences
var/list/plane_masters = list()
/// The client that is watching this view
var/client/client
/atom/movable/screen/character_preview_view/Initialize(mapload, datum/preferences/preferences, client/client)
. = ..()
assigned_map = "character_preview_[REF(src)]"
set_position(1, 1)
src.preferences = preferences
/atom/movable/screen/character_preview_view/Destroy()
QDEL_NULL(body)
for (var/plane_master in plane_masters)
client?.screen -= plane_master
qdel(plane_master)
client?.clear_map(assigned_map)
client?.screen -= src
preferences?.character_preview_view = null
client = null
plane_masters = null
preferences = null
return ..()
/// Updates the currently displayed body
/atom/movable/screen/character_preview_view/proc/update_body()
if (isnull(body))
create_body()
else
body.wipe_state()
appearance = preferences.render_new_preview_appearance(body)
/atom/movable/screen/character_preview_view/proc/create_body()
QDEL_NULL(body)
body = new
// Without this, it doesn't show up in the menu
body.appearance_flags &= ~KEEP_TOGETHER
/// Registers the relevant map objects to a client
/atom/movable/screen/character_preview_view/proc/register_to_client(client/client)
QDEL_LIST(plane_masters)
src.client = client
if (!client)
return
for (var/plane_master_type in subtypesof(/atom/movable/screen/plane_master))
var/atom/movable/screen/plane_master/plane_master = new plane_master_type
plane_master.screen_loc = "[assigned_map]:CENTER"
client?.screen |= plane_master
plane_masters += plane_master
client?.register_map_obj(src)
/datum/preferences/proc/create_character_profiles()
var/list/profiles = list()
var/savefile/savefile = new(path)
for (var/index in 1 to max_save_slots)
// It won't be updated in the savefile yet, so just read the name directly
if (index == default_slot)
profiles += read_preference(/datum/preference/name/real_name)
continue
savefile.cd = "/character[index]"
var/name
READ_FILE(savefile["real_name"], name)
if (isnull(name))
profiles += null
continue
profiles += name
return profiles
/datum/preferences/proc/set_job_preference_level(datum/job/job, level)
if (!job)
return FALSE
if (level == JP_HIGH)
var/datum/job/overflow_role = SSjob.overflow_role
var/overflow_role_title = initial(overflow_role.title)
for(var/other_job in job_preferences)
if(job_preferences[other_job] == JP_HIGH)
// Overflow role needs to go to NEVER, not medium!
if(other_job == overflow_role_title)
job_preferences[other_job] = null
else
job_preferences[other_job] = JP_MEDIUM
if(level == null)
job_preferences -= job.title
else
job_preferences[job.title] = level
return TRUE
/datum/preferences/proc/GetQuirkBalance()
var/bal = 0
for(var/V in all_quirks)
var/datum/quirk/T = SSquirks.quirks[V]
bal -= initial(T.value)
return bal
/datum/preferences/proc/GetPositiveQuirkCount()
var/sum = 0
for(var/q in all_quirks)
if(SSquirks.quirk_points[q] > 0)
sum++
return sum
/datum/preferences/proc/validate_quirks()
if(GetQuirkBalance() < 0)
all_quirks = list()
/datum/preferences/proc/apply_prefs_to(mob/living/carbon/human/character, icon_updates = TRUE)
character.dna.features = list()
for (var/datum/preference/preference as anything in get_preferences_in_priority_order())
if (preference.savefile_identifier != PREFERENCE_CHARACTER)
continue
// Dont apply if it's unique and we can't customize it
// This fixes pod hair color overwriting human hair color
if (preference.unique && !preference.can_apply(src))
continue
preference.apply_to_human(character, read_preference(preference.type))
character.dna.real_name = character.real_name
if(icon_updates)
character.icon_render_key = null //turns out if you don't set this to null update_body_parts does nothing, since it assumes the operation was cached
character.update_body()
character.update_hair()
character.update_body_parts()
/// Inverts the key_bindings list such that it can be used for key_bindings_by_key
/datum/preferences/proc/get_key_bindings_by_key(list/key_bindings)
var/list/output = list()
for (var/action in key_bindings)
for (var/key in key_bindings[action])
LAZYADD(output[key], action)
return output
/// Returns the default `randomise` variable ouptut
/datum/preferences/proc/get_default_randomization()
var/list/default_randomization = list()
for (var/preference_key in GLOB.preference_entries_by_key)
var/datum/preference/preference = GLOB.preference_entries_by_key[preference_key]
if (preference.is_randomizable() && preference.randomize_by_default)
default_randomization[preference_key] = RANDOM_ENABLED
return default_randomization
// yogs procs
/datum/preferences/proc/GetPlayerAltTitle(datum/job/job)
return player_alt_titles.Find(job.title) > 0 \
? player_alt_titles[job.title] \
: job.title
/datum/preferences/proc/SetPlayerAltTitle(datum/job/job, new_title)
// remove existing entry
if(player_alt_titles.Find(job.title))
player_alt_titles -= job.title
// add one if it's not default
if(job.title != new_title)
player_alt_titles[job.title] = new_title