Preference support for Guests (#72332)

## About The Pull Request

Makes it to where if you connect as a guest preferences don't error out
the ass
Resolves https://github.com/tgstation/tgstation/issues/72268
## Why It's Good For The Game

Easier local debugging, and for servers that let guests join
## Changelog
🆑
server: Preferences now support guests
/🆑
This commit is contained in:
Zephyr
2023-01-04 00:15:04 -05:00
committed by GitHub
parent 282961aee6
commit a95e2f0b3f
3 changed files with 29 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
/**
* A savefile implementation that handles all data using json.
* Also saves it using JSON too, fancy.
* If you pass in a null path, it simply acts as a memory tree instead, and cannot be saved.
*/
/datum/json_savefile
var/path = ""
@@ -13,7 +14,7 @@ GENERAL_PROTECT_DATUM(/datum/json_savefile)
/datum/json_savefile/New(path)
src.path = path
tree = list()
if(fexists(path))
if(path && fexists(path))
load()
/**
@@ -43,7 +44,7 @@ GENERAL_PROTECT_DATUM(/datum/json_savefile)
tree?.Cut()
/datum/json_savefile/proc/load()
if(!fexists(path))
if(!path || !fexists(path))
return FALSE
try
tree = json_decode(rustg_file_read(path))
@@ -53,6 +54,7 @@ GENERAL_PROTECT_DATUM(/datum/json_savefile)
return FALSE
/datum/json_savefile/proc/save()
if(path)
rustg_file_write(json_encode(tree), path)
/// Traverses the entire dir tree of the given savefile and dynamically assembles the tree from it

View File

@@ -2,20 +2,26 @@ GLOBAL_LIST_EMPTY(preferences_datums)
/datum/preferences
var/client/parent
//doohickeys for savefiles
/// The path to the general savefile for this datum
var/path
var/default_slot = 1 //Holder so it doesn't default to slot 1, rather the last one used
/// Whether or not we allow saving/loading. Used for guests, if they're enabled
var/load_and_save = TRUE
/// Ensures that we always load the last used save, QOL
var/default_slot = 1
/// The maximum number of slots we're allowed to contain
var/max_save_slots = 3
//non-preference stuff
var/muted = 0
/// Bitflags for communications that are muted
var/muted = NONE
/// Last IP that this client has connected from
var/last_ip
/// Last CID that this client has connected from
var/last_id
//game-preferences
var/lastchangelog = "" //Saved changlog filesize to detect if there was a change
/// Cached changelog size, to detect new changelogs since last join
var/lastchangelog = ""
//Antag preferences
/// List of ROLE_X that the client wants to be eligible for
var/list/be_special = list() //Special role selection
/// Custom keybindings. Map of keybind names to keyboard inputs.
@@ -95,9 +101,9 @@ GLOBAL_LIST_EMPTY(preferences_datums)
middleware += new middleware_type(src)
if(IS_CLIENT_OR_MOCK(parent))
if(!is_guest_key(parent.key))
load_and_save = !is_guest_key(parent.key)
load_path(parent.ckey)
if(!fexists(path))
if(load_and_save && !fexists(path))
try_savefile_type_migration()
unlock_content = !!parent.IsByondMember()
if(unlock_content)

View File

@@ -140,14 +140,14 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
to_chat(parent, span_danger("[conflicted.category]: [conflicted.full_name] needs updating"))
/datum/preferences/proc/load_path(ckey, filename="preferences.json")
if(!ckey)
if(!ckey || !load_and_save)
return
path = "data/player_saves/[ckey[1]]/[ckey]/[filename]"
/datum/preferences/proc/load_savefile()
if(!path)
if(load_and_save && !path)
CRASH("Attempted to load savefile without first loading a path!")
savefile = new /datum/json_savefile(path)
savefile = new /datum/json_savefile(load_and_save ? path : null)
/datum/preferences/proc/load_preferences()
if(!savefile)
@@ -158,7 +158,7 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
return FALSE
var/needs_update = save_data_needs_update(savefile.get_entry())
if(needs_update == -2) //fatal, can't load any data
if(load_and_save && (needs_update == -2)) //fatal, can't load any data
var/bacpath = "[path].updatebac" //todo: if the savefile version is higher then the server, check the backup, and give the player a prompt to load the backup
if (fexists(bacpath))
fdel(bacpath) //only keep 1 version of backup
@@ -263,7 +263,6 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
/datum/preferences/proc/load_character(slot)
SHOULD_NOT_SLEEP(TRUE)
if(!slot)
slot = default_slot
slot = sanitize_integer(slot, 1, max_save_slots, initial(default_slot))
@@ -317,7 +316,6 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
/datum/preferences/proc/save_character()
SHOULD_NOT_SLEEP(TRUE)
if(!path)
return FALSE
var/tree_key = "character[default_slot]"