diff --git a/code/datums/datum.dm b/code/datums/datum.dm index 12b3df07e48..471d7d733ac 100644 --- a/code/datums/datum.dm +++ b/code/datums/datum.dm @@ -148,13 +148,24 @@ to_chat(target, txt_changed_vars()) #endif -///Return a LIST for serialize_datum to encode! Not the actual json! +/** + * Saves our data into a data list. + * + * @params + * * options - associative list of options to use. Not yet particularly used/standardized, but can be easily implemented. + */ /datum/proc/serialize_list(list/options) - CRASH("Attempted to serialize datum [src] of type [type] without serialize_list being implemented!") + return -///Accepts a LIST from deserialize_datum. Should return src or another datum. -/datum/proc/deserialize_list(json, list/options) - CRASH("Attempted to deserialize datum [src] of type [type] without deserialize_list being implemented!") +/** + * Takes a data list in and loads data into ourselves. Persistence elements currently use this. + * + * @params + * * data - list of data for us to use + * * options - associatve list of options to use. Not yet particularly used/standardized, but can be easily implemented. + */ +/datum/proc/deserialize_list(list/data, list/options) + return ///Serializes into JSON. Does not encode type. /datum/proc/serialize_json(list/options) diff --git a/code/datums/elements/persistence.dm b/code/datums/elements/persistence.dm new file mode 100644 index 00000000000..3b590baf775 --- /dev/null +++ b/code/datums/elements/persistence.dm @@ -0,0 +1,60 @@ +/** + * Simple object persistence elements + * For now, it just attachs and detaches and all objects with it are recorded to disk based on map and stuff at round end + * Can expand later for more functionality + * + * WARNING: All atoms recorded are stored to turf! This does not yet support things like loading into inventories. This is mostly for on-floor objects. + */ +/datum/element/persistence + flags = ELEMENT_DETACH + + /// List of persistent atoms. Associated list to their GUID + var/list/atom/objects = list() + +/** + * Serializes all objects into a list for json encode or anything else needed. + * Includes data the datum includes in serialize_list(), the GUID of the datum's entry, and its turf location. + */ +/datum/element/persistence/proc/SerializeAll() + . = list() + var/atom/A + var/guid + for(var/i in objects) + A = i + guid = objects[A] + var/turf/location = get_turf(A) + if(!A || !guid || !location) + continue + . += list(A.serialize_list(), guid, list(location.x, location.y, location.z), "[A.type]") + +/** + * Instantiates all objects from a data list and attaches to them. + */ +/datum/element/persistence/proc/DeserializeAndInstantiateAll(list/data) + for(var/i in data) + var/list/L = i + if(!istype(L) || (length(L) < 3)) + continue + var/list/datumdata = L[1] + var/guid = L[2] + var/coords = L[3] + var/datumtype = text2path(L[4]) + if(!ispath(datumtype)) + continue + var/turf/T = locate(coords[1], coords[2], coords[3]) + if(!T) + continue + var/atom/A = new datumtype(T) + A.deserialize_list(datumdata) + Attach(A, guid) + +/datum/element/persistence/Attach(datum/D, guid = GUID()) + if(!isatom(D)) + return ELEMENT_INCOMPATIBLE + . = ..() + if(. == ELEMENT_INCOMPATIBLE) + return + objects[D] = guid + +/datum/element/persistence/Detach(datum/D) + objects -= D diff --git a/vorestation.dme b/vorestation.dme index c5597a47155..5103330f142 100644 --- a/vorestation.dme +++ b/vorestation.dme @@ -322,6 +322,7 @@ #include "code\datums\components\_component.dm" #include "code\datums\components\orbiter.dm" #include "code\datums\elements\_element.dm" +#include "code\datums\elements\persistence.dm" #include "code\datums\helper_datums\construction_datum.dm" #include "code\datums\helper_datums\events.dm" #include "code\datums\helper_datums\getrev.dm" @@ -2205,6 +2206,7 @@ #include "code\modules\lore_codex\robutt_data\bybrand.dm" #include "code\modules\lore_codex\robutt_data\main_robutts.dm" #include "code\modules\lore_codex\robutt_data\more.dm" +#include "code\modules\mapping\map_config.dm" #include "code\modules\mapping\minimaps.dm" #include "code\modules\mapping\preloader.dm" #include "code\modules\maps\tg\dmm_suite.dm"