mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2025-12-18 05:32:58 +00:00
* Adds a unit test to stop elements from using identical lists for their arguments. (#76322) ## About The Pull Request Ok, so a few days ago I made an issue report about multiple instances of identical elements being generated because of uncached lists. ninjanomnom (the mind being the element datums) cleared it up and said an implementation of GetIdFromArguments() that also checks the list contents wouldn't be worth the performance cost, while adding that a unit test should be written to check that it doesn't happen at least during init, which should catch a good chunk of cases. Also, i'm stopping RemoveElement() from initializing new elements whenever a cached element is not found. Ideally, there should be a focus only unit test for that too, but that's something we should tackle on a different PR. Some of the code comments may be a tad inaccurate, as much as I'd like to blame drowsiness for it. Regardless, the unit test takes less than 0.2 seconds to complete on my potato so it's fairly lite. ## Why It's Good For The Game This will close #76279. ## Changelog No player-facing change to be logged. * Adds a unit test to stop elements from using identical lists for their arguments. * Fixes unit test * seeing double --------- Co-authored-by: Ghom <42542238+Ghommie@users.noreply.github.com> Co-authored-by: Giz <13398309+vinylspiders@users.noreply.github.com>
75 lines
2.6 KiB
Plaintext
75 lines
2.6 KiB
Plaintext
/**
|
|
* A holder for simple behaviour that can be attached to many different types
|
|
*
|
|
* Only one element of each type is instanced during game init.
|
|
* Otherwise acts basically like a lightweight component.
|
|
*/
|
|
/datum/element
|
|
/// Option flags for element behaviour
|
|
var/element_flags = NONE
|
|
/**
|
|
* The index of the first attach argument to consider for duplicate elements
|
|
*
|
|
* All arguments from this index onwards (1 based) are hashed into the key to determine
|
|
* if this is a new unique element or one already exists
|
|
*
|
|
* Is only used when flags contains [ELEMENT_BESPOKE]
|
|
*
|
|
* This is infinity so you must explicitly set this
|
|
*/
|
|
var/argument_hash_start_idx = INFINITY
|
|
|
|
/// Activates the functionality defined by the element on the given target datum
|
|
/datum/element/proc/Attach(datum/target)
|
|
SHOULD_CALL_PARENT(TRUE)
|
|
if(type == /datum/element)
|
|
return ELEMENT_INCOMPATIBLE
|
|
SEND_SIGNAL(target, COMSIG_ELEMENT_ATTACH, src)
|
|
if(element_flags & ELEMENT_DETACH_ON_HOST_DESTROY)
|
|
RegisterSignal(target, COMSIG_QDELETING, PROC_REF(OnTargetDelete), override = TRUE)
|
|
|
|
/datum/element/proc/OnTargetDelete(datum/source, force)
|
|
SIGNAL_HANDLER
|
|
Detach(source)
|
|
|
|
/// Deactivates the functionality defines by the element on the given datum
|
|
/datum/element/proc/Detach(datum/source, ...)
|
|
SIGNAL_HANDLER
|
|
SHOULD_CALL_PARENT(TRUE)
|
|
|
|
SEND_SIGNAL(source, COMSIG_ELEMENT_DETACH, src)
|
|
UnregisterSignal(source, COMSIG_QDELETING)
|
|
|
|
/datum/element/Destroy(force)
|
|
if(!force)
|
|
return QDEL_HINT_LETMELIVE
|
|
SSdcs.elements_by_type -= type
|
|
return ..()
|
|
|
|
//DATUM PROCS
|
|
|
|
/// Finds the singleton for the element type given and attaches it to src
|
|
/datum/proc/_AddElement(list/arguments)
|
|
if(QDELING(src))
|
|
CRASH("We just tried to add an element to a qdeleted datum, something is fucked")
|
|
var/datum/element/ele = SSdcs.GetElement(arguments)
|
|
if(!ele) // We couldn't fetch the element, likely because it was not an element.
|
|
return // the crash message has already been sent
|
|
arguments[1] = src
|
|
if(ele.Attach(arglist(arguments)) == ELEMENT_INCOMPATIBLE)
|
|
CRASH("Incompatible element [ele.type] was assigned to a [type]! args: [json_encode(args)]")
|
|
|
|
/**
|
|
* Finds the singleton for the element type given and detaches it from src
|
|
* You only need additional arguments beyond the type if you're using [ELEMENT_BESPOKE]
|
|
*/
|
|
/datum/proc/_RemoveElement(list/arguments)
|
|
var/datum/element/ele = SSdcs.GetElement(arguments, FALSE)
|
|
if(!ele) // We couldn't fetch the element, likely because it didn't exist.
|
|
return
|
|
if(ele.element_flags & ELEMENT_COMPLEX_DETACH)
|
|
arguments[1] = src
|
|
ele.Detach(arglist(arguments))
|
|
else
|
|
ele.Detach(src)
|