Skip to content

Commit

Permalink
Fixes connect_loc related hard dels (tgstation#58945)
Browse files Browse the repository at this point in the history
Co-authored-by: LemonInTheDark <[email protected]>
  • Loading branch information
ninjanomnom and LemonInTheDark authored May 13, 2021
1 parent fd85fb7 commit f434bf5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions code/datums/elements/_element.dm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

/// 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)
arguments[1] = src
if(ele.Attach(arglist(arguments)) == ELEMENT_INCOMPATIBLE)
Expand Down
2 changes: 1 addition & 1 deletion code/datums/elements/caltrop.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Used for broken glass, cactuses and four sided dice.
*/
/datum/element/caltrop
element_flags = ELEMENT_BESPOKE
element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH
id_arg_index = 2

///Minimum damage done when crossed
Expand Down
35 changes: 23 additions & 12 deletions code/datums/elements/connect_loc.dm
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,23 @@

src.connections = connections

RegisterSignal(tracked, COMSIG_MOVABLE_LOCATION_CHANGE, .proc/on_moved)
RegisterSignal(tracked, COMSIG_MOVABLE_LOCATION_CHANGE, .proc/on_moved, override=TRUE)
update_signals(listener, tracked)

/datum/element/connect_loc/Detach(datum/listener, atom/movable/tracked, list/connections)
. = ..()

if(!tracked)
tracked = listener

if(!istype(tracked))
return

unregister_signals(listener, tracked, tracked.loc)

UnregisterSignal(tracked, COMSIG_MOVABLE_LOCATION_CHANGE)
unregister_all(listener)
else if(targets[tracked.loc]) // Detach can happen multiple times due to qdel
unregister_signals(listener, tracked, tracked.loc)
UnregisterSignal(tracked, COMSIG_MOVABLE_LOCATION_CHANGE)

/datum/element/connect_loc/proc/update_signals(datum/listener, atom/movable/tracked)
var/existing = length(targets[tracked.loc])
LAZYSET(targets[tracked.loc], tracked, listener)
if(!existing)
targets[tracked.loc] = list()
targets[tracked.loc][tracked] = listener

if(isnull(tracked.loc))
return
Expand All @@ -54,10 +52,23 @@
if (!existing && isturf(tracked.loc))
RegisterSignal(tracked.loc, COMSIG_TURF_CHANGE, .proc/on_turf_change)

/datum/element/connect_loc/proc/unregister_all(datum/listener)
for(var/atom/location as anything in targets)
var/list/loc_targets = targets[location]
for(var/atom/movable/tracked as anything in loc_targets)
if(tracked == listener)
unregister_signals(loc_targets[tracked], tracked, location)
else if(loc_targets[tracked] == listener)
unregister_signals(listener, tracked, location)
else
continue
UnregisterSignal(tracked, COMSIG_MOVABLE_LOCATION_CHANGE)

/datum/element/connect_loc/proc/unregister_signals(datum/listener, atom/movable/tracked, atom/old_loc)
targets[old_loc] -= tracked
if (length(targets[old_loc]) == 0)
if (length(targets[old_loc]) <= 1)
targets -= old_loc
else
targets[old_loc] -= tracked

// Yes this is after the above because we use null as a key when objects are in nullspace
if(isnull(old_loc))
Expand Down
3 changes: 3 additions & 0 deletions code/game/objects/items/stacks/stack.dm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
for(var/obj/item/stack/S in loc)
if(can_merge(S))
INVOKE_ASYNC(src, .proc/merge, S)
//Merge can call qdel on us, so let's be safe yeah?
if(QDELETED(src))
return
var/list/temp_recipes = get_main_recipes()
recipes = temp_recipes.Copy()
if(material_type)
Expand Down

0 comments on commit f434bf5

Please sign in to comment.