From 37ac7ee2925e2fe8aa61e923324c2602e3c08bbb Mon Sep 17 00:00:00 2001 From: CitadelStationBot Date: Sun, 16 Jul 2017 19:10:44 -0500 Subject: [PATCH] Ports /vg/'s datum component system --- code/__DEFINES/components.dm | 16 +++++ code/controllers/subsystem/garbage.dm | 3 + code/datums/components.dm | 97 +++++++++++++++++++++++++++ tgstation.dme | 1 + tgstation.dme.rej | 9 +++ 5 files changed, 126 insertions(+) create mode 100644 code/__DEFINES/components.dm create mode 100644 code/datums/components.dm create mode 100644 tgstation.dme.rej diff --git a/code/__DEFINES/components.dm b/code/__DEFINES/components.dm new file mode 100644 index 0000000000..eb6826d67b --- /dev/null +++ b/code/__DEFINES/components.dm @@ -0,0 +1,16 @@ +//shorthand +#define GET_COMPONENT_FROM(varname, path, target) var##path/##varname = ##target.GetComponent(##path) +#define GET_COMPONENT(varname, path) GET_COMPONENT_FROM(varname, path, src) + +// How multiple components of the exact same type are handled in the same datum + +#define COMPONENT_DUPE_HIGHLANDER 0 //old component is deleted (default) +#define COMPONENT_DUPE_ALLOWED 1 //duplicates allowed +#define COMPONENT_DUPE_UNIQUE 2 //new component is deleted + +// All signals. Format: +// When the signal is called: (signal arguments) + +#define COMSIG_COMPONENT_ADDED "component_added" //when a component is added to a datum: (datum/component) +#define COMSIG_COMPONENT_REMOVING "component_removing" //before a component is removed from a datum because of RemoveComponent: (datum/component) +#define COMSIG_PARENT_QDELETED "parent_qdeleted" //before a datum's Destroy() is called: () diff --git a/code/controllers/subsystem/garbage.dm b/code/controllers/subsystem/garbage.dm index c9ef6b7093..2cee9a192a 100644 --- a/code/controllers/subsystem/garbage.dm +++ b/code/controllers/subsystem/garbage.dm @@ -189,6 +189,7 @@ SUBSYSTEM_DEF(garbage) SSgarbage.qdel_list += "[D.type]" #endif if(isnull(D.gc_destroyed)) + D.SendSignal(COMSIG_PARENT_QDELETED) D.gc_destroyed = GC_CURRENTLY_BEING_QDELETED var/start_time = world.time var/hint = D.Destroy(force) // Let our friend know they're about to get fucked up. @@ -236,6 +237,7 @@ SUBSYSTEM_DEF(garbage) // Default implementation of clean-up code. // This should be overridden to remove all references pointing to the object being destroyed. // Return the appropriate QDEL_HINT; in most cases this is QDEL_HINT_QUEUE. +// TODO: Move this and all datum var definitions into code/datums/datum.dm /datum/proc/Destroy(force=FALSE) tag = null var/list/timers = active_timers @@ -245,6 +247,7 @@ SUBSYSTEM_DEF(garbage) if (timer.spent) continue qdel(timer) + QDEL_LIST(datum_components) return QDEL_HINT_QUEUE /datum/var/gc_destroyed //Time when this object was destroyed. diff --git a/code/datums/components.dm b/code/datums/components.dm new file mode 100644 index 0000000000..b9212d2786 --- /dev/null +++ b/code/datums/components.dm @@ -0,0 +1,97 @@ +/datum/component + var/enabled = TRUE // Enables or disables the components + var/dupe_mode = COMPONENT_DUPE_HIGHLANDER // How components of the same type are handled in the same parent + var/list/signal_procs // list of signals -> callbacks + var/datum/parent // parent datum + +/datum/component/New(datum/P, ...) + var/dm = dupe_mode + if(dm != COMPONENT_DUPE_ALLOWED) + var/datum/component/old = P.GetExactComponent(type) + if(old) + switch(dm) + if(COMPONENT_DUPE_HIGHLANDER) + P.RemoveComponent(old) + old = null //in case SendSignal() blocks + if(COMPONENT_DUPE_UNIQUE) + qdel(src) + return + P.SendSignal(COMSIG_COMPONENT_ADDED, list(src), FALSE) + LAZYADD(P.datum_components, src) + parent = P + +/datum/component/Destroy() + RemoveNoSignal() + return ..() + +/datum/component/proc/RemoveNoSignal() + var/datum/P = parent + if(P) + LAZYREMOVE(P.datum_components, src) + parent = null + +/datum/component/proc/RegisterSignal(sig_type, proc_on_self, override = FALSE) + var/list/procs = signal_procs + if(!procs) + procs = list() + signal_procs = procs + + if(!override) + . = procs[sig_type] + if(.) + stack_trace("[sig_type] overridden. Use override = TRUE to suppress this warning") + + procs[sig_type] = CALLBACK(src, proc_on_self) + +/datum/var/list/datum_components //list of /datum/component + +// Send a signal to all other components in the container. +/datum/proc/SendSignal(sigtype, list/sig_args, async = FALSE) + var/list/comps = datum_components + . = FALSE + for(var/I in comps) + var/datum/component/C = I + if(!C.enabled) + continue + var/list/sps = C.signal_procs + var/datum/callback/CB = LAZYACCESS(sps, sigtype) + if(!CB) + continue + if(!async) + . |= CB.Invoke(sig_args) + else + . |= CB.InvokeAsync(sig_args) + +/datum/proc/GetComponent(c_type) + for(var/I in datum_components) + if(istype(I, c_type)) + return I + +/datum/proc/GetExactComponent(c_type) + for(var/I in datum_components) + var/datum/component/C = I + if(C.type == c_type) + return I + +/datum/proc/GetComponents(c_type) + . = list() + for(var/I in datum_components) + if(istype(I, c_type)) + . += I + +/datum/proc/AddComponents(list/new_types) + for(var/new_type in new_types) + AddComponent(new_type) + +/datum/proc/AddComponent(new_type, ...) + var/nt = new_type + args[1] = src + var/datum/component/C = new nt(arglist(args)) + return QDELING(C) ? GetComponent(new_type) : C + +/datum/proc/RemoveComponent(datum/component/C) + if(!C) + return + C.RemoveNoSignal() + SendSignal(COMSIG_COMPONENT_REMOVING, list(C), FALSE) + qdel(C) diff --git a/tgstation.dme b/tgstation.dme index c76d71c104..4c8ea4fcb5 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -273,6 +273,7 @@ #include "code\datums\antagonists\datum_traitor.dm" #include "code\datums\antagonists\devil.dm" #include "code\datums\antagonists\ninja.dm" +#include "code\datums\components\component.dm" #include "code\datums\diseases\_disease.dm" #include "code\datums\diseases\_MobProcs.dm" #include "code\datums\diseases\anxiety.dm" diff --git a/tgstation.dme.rej b/tgstation.dme.rej new file mode 100644 index 0000000000..42c2d6d62d --- /dev/null +++ b/tgstation.dme.rej @@ -0,0 +1,9 @@ +diff a/tgstation.dme b/tgstation.dme (rejected hunks) +@@ -28,6 +28,7 @@ + #include "code\__DEFINES\callbacks.dm" + #include "code\__DEFINES\clockcult.dm" + #include "code\__DEFINES\combat.dm" ++#include "code\__DEFINES\components.dm" + #include "code\__DEFINES\construction.dm" + #include "code\__DEFINES\contracts.dm" + #include "code\__DEFINES\cult.dm"