Ports /vg/'s datum component system

This commit is contained in:
CitadelStationBot
2017-07-16 19:10:44 -05:00
parent 344dc1a061
commit 37ac7ee292
5 changed files with 126 additions and 0 deletions

View File

@@ -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: ()

View File

@@ -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.

97
code/datums/components.dm Normal file
View File

@@ -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)

View File

@@ -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"

9
tgstation.dme.rej Normal file
View File

@@ -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"