Skip to content

Commit

Permalink
Cassette loading stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Dec 15, 2024
1 parent b009615 commit 5f10ddc
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions monkestation/code/modules/cassettes/cassette_db/cassette_datum.dm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define BASE_CASSETTE_DIR "data/cassette_storage/"
/// An associative list of cassette IDs to cassette datums.
GLOBAL_LIST_EMPTY_TYPED(cassettes, /datum/cassette)
GLOBAL_LIST_INIT_TYPED(cassettes, /datum/cassette, load_all_cassettes())

/datum/cassette
/// The name of the cassette.
Expand Down Expand Up @@ -87,7 +88,7 @@ GLOBAL_LIST_EMPTY_TYPED(cassettes, /datum/cassette)
/datum/cassette/proc/save()
if(!id)
CRASH("Attempted to save cassette without an ID to disk")
rustg_file_write(json_encode(export_old_format(), JSON_PRETTY_PRINT), "data/cassette_storage/[id].json")
rustg_file_write(json_encode(export_old_format(), JSON_PRETTY_PRINT), BASE_CASSETTE_DIR + "[id].json")

/// Simple helper to get a side of the cassette.
/// TRUE is front side, FALSE is back side.
Expand Down Expand Up @@ -129,12 +130,39 @@ GLOBAL_LIST_EMPTY_TYPED(cassettes, /datum/cassette)
return null
if(GLOB.cassettes[id])
return GLOB.cassettes[id]
var/cassette_file = "data/cassette_storage/[id].json"
var/datum/cassette/cassette_data = load_cassette_raw(id)
if(cassette_data)
GLOB.cassettes[id] = cassette_data
return cassette_data

/// Loads the cassette with the given ID from a JSON in the `data/cassette_storage` folder.
/// This does not check the GLOB.cassettes cache, and you should not use this - this is only used to initialize GLOB.cassettes
/proc/load_cassette_raw(id) as /datum/cassette
RETURN_TYPE(/datum/cassette)
var/cassette_file = BASE_CASSETTE_DIR + "[id].json"
if(!rustg_file_exists(cassette_file))
return null
var/list/cassette_json = json_decode(rustg_file_read(cassette_file))
var/datum/cassette/cassette_data = new
cassette_data.import_old_format(cassette_json)
cassette_data.id = id
GLOB.cassettes[id] = cassette_data
return cassette_data

/// Returns an associative list of id to cassette datums, of all existing saved cassettes.
/proc/load_all_cassettes() as /list
RETURN_TYPE(/list)
. = list()
var/ids_file = BASE_CASSETTE_DIR + "ids.json"
if(!rustg_file_exists(ids_file))
return
var/list/ids = json_decode(rustg_file_read(ids_file))
for(var/id in ids)
if(!ids)
continue
var/datum/cassette/cassette_data = load_cassette_raw(id)
if(isnull(cassette_data))
stack_trace("Failed to load cassette [id]")
continue
.[id] = cassette_data

#undef BASE_CASSETTE_DIR

0 comments on commit 5f10ddc

Please sign in to comment.