Skip to content

Commit

Permalink
Add GLOB.cassettes
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Dec 15, 2024
1 parent 9633658 commit b009615
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// An associative list of cassette IDs to cassette datums.
GLOBAL_LIST_EMPTY_TYPED(cassettes, /datum/cassette)

/datum/cassette
/// The name of the cassette.
var/name
Expand All @@ -21,6 +24,12 @@
front = new
back = new

/datum/cassette/Destroy(force)
QDEL_NULL(author)
QDEL_NULL(front)
QDEL_NULL(back)
return ..()

/// Imports cassette date from the old format.
/datum/cassette/proc/import_old_format(list/data)
name = data["name"]
Expand All @@ -43,7 +52,8 @@
side.songs += new /datum/cassette_song(song_names[idx], song_urls[idx])

/// Exports cassette date in the old format.
/datum/cassette/proc/export_old_format()
/datum/cassette/proc/export_old_format() as /list
RETURN_TYPE(/list)
. = list(
"name" = name,
"desc" = desc,
Expand Down Expand Up @@ -73,6 +83,12 @@
.["song_names"][side_name] = names
.["songs"][side_name] = urls

/// Saves the cassette to the data folder, in JSON format.
/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")

/// Simple helper to get a side of the cassette.
/// TRUE is front side, FALSE is back side.
/datum/cassette/proc/get_side(front_side = TRUE) as /datum/cassette_side
Expand All @@ -91,6 +107,10 @@
/// The songs on this side of the cassette.
var/list/datum/cassette_song/songs = list()

/datum/cassette_side/Destroy(force)
QDEL_LIST(songs)
return ..()

/datum/cassette_song
/// The name of the song.
var/name
Expand All @@ -101,3 +121,20 @@
. = ..()
src.name = name
src.url = url

/// Loads the cassette with the given ID from a JSON in the `data/cassette_storage` folder.
/proc/load_cassette(id) as /datum/cassette
RETURN_TYPE(/datum/cassette)
if(!id)
return null
if(GLOB.cassettes[id])
return GLOB.cassettes[id]
var/cassette_file = "data/cassette_storage/[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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ SUBSYSTEM_DEF(cassette_storage)
runlevels = RUNLEVEL_LOBBY | RUNLEVELS_DEFAULT
var/list/cassette_datums = list()


/datum/controller/subsystem/cassette_storage/Initialize()
if(!length(GLOB.approved_ids))
GLOB.approved_ids = initialize_approved_ids()
Expand Down

0 comments on commit b009615

Please sign in to comment.