Files
Bubberstation/code/datums/callback.dm
nevimer 424ed3d160 Revert "Revert "Merge upstream""
This reverts commit e6bb4098c4.

# Conflicts:
#	_maps/RandomRuins/LavaRuins/skyrat/lavaland_surface_syndicate_base1_skyrat.dmm
#	_maps/RandomRuins/SpaceRuins/bus.dmm
#	_maps/RandomZLevels/blackmesa.dmm
#	_maps/map_files/generic/CentCom_skyrat_z2.dmm
#	_maps/shuttles/skyrat/goldeneye_cruiser.dmm
#	_maps/templates/lazy_templates/wizard_den.dmm
#	code/__DEFINES/callbacks.dm
#	code/datums/components/transforming.dm
#	code/datums/elements/bane.dm
#	code/datums/votes/map_vote.dm
#	code/game/objects/items/AI_modules/hacked.dm
#	code/game/objects/items/food/egg.dm
#	code/game/objects/items/stacks/sheets/glass.dm
#	code/modules/antagonists/fugitive/hunters/hunter.dm
#	code/modules/antagonists/traitor/objectives/final_objective/final_objective.dm
#	code/modules/antagonists/traitor/objectives/kidnapping.dm
#	code/modules/art/statues.dm
#	code/modules/events/ghost_role/changeling_event.dm
#	code/modules/events/spacevine.dm
#	code/modules/mining/machine_redemption.dm
#	code/modules/mob/living/simple_animal/friendly/farm_animals.dm
#	code/modules/mob_spawn/mob_spawn.dm
#	code/modules/projectiles/guns/ballistic/pistol.dm
#	code/modules/projectiles/guns/ballistic/rifle.dm
#	code/modules/uplink/uplink_items.dm
#	code/modules/vending/autodrobe.dm
#	html/changelogs/archive/2023-03.yml
#	icons/mob/clothing/feet.dmi
#	icons/mob/clothing/under/costume.dmi
#	icons/mob/inhands/clothing/shoes_lefthand.dmi
#	icons/mob/inhands/clothing/shoes_righthand.dmi
#	icons/mob/inhands/clothing/suits_lefthand.dmi
#	icons/mob/inhands/clothing/suits_righthand.dmi
#	icons/mob/species/human/human_face.dmi
#	icons/obj/clothing/shoes.dmi
#	icons/obj/clothing/under/costume.dmi
#	modular_skyrat/master_files/code/datums/components/fullauto.dm
#	modular_skyrat/master_files/code/modules/clothing/under/jobs/security.dm
#	modular_skyrat/master_files/code/modules/projectiles/guns/gun.dm
#	modular_skyrat/master_files/icons/mob/clothing/under/civilian.dmi
#	modular_skyrat/master_files/icons/mob/clothing/under/civilian_digi.dmi
#	modular_skyrat/modules/aesthetics/guns/code/guns.dm
#	modular_skyrat/modules/aesthetics/guns/icons/guns.dmi
#	modular_skyrat/modules/blueshield/code/blueshield.dm
#	modular_skyrat/modules/customization/modules/clothing/under/misc.dm
#	modular_skyrat/modules/ghostcafe/code/ghost_role_spawners.dm
#	modular_skyrat/modules/gunsgalore/icons/guns/gunsgalore_guns40x32.dmi
#	modular_skyrat/modules/manufacturer_examine/code/gun_company_additions.dm
#	modular_skyrat/modules/manufacturer_examine/code/manufacturer_component.dm
#	modular_skyrat/modules/mapping/code/mob_spawns.dm
#	modular_skyrat/modules/modular_weapons/code/rifle.dm
#	modular_skyrat/modules/novaya_ert/code/automatic.dm
#	modular_skyrat/modules/sec_haul/code/guns/guns.dm
#	modular_skyrat/modules/tribal_extended/code/weapons/bow.dm
#	tgstation.dme
#	tgui/packages/tgui/interfaces/OreRedemptionMachine.js
#	tgui/packages/tgui/interfaces/VotePanel.tsx
2023-05-20 22:06:42 -04:00

211 lines
6.7 KiB
Plaintext

/**
*# Callback Datums
*A datum that holds a proc to be called on another object, used to track proccalls to other objects
*
* ## USAGE
*
* ```
* var/datum/callback/C = new(object|null, PROC_REF(procname), arg1, arg2, ... argn)
* var/timerid = addtimer(C, time, timertype)
* you can also use the compiler define shorthand
* var/timerid = addtimer(CALLBACK(object|null, PROC_REF(procname), arg1, arg2, ... argn), time, timertype)
* ```
*
* Note: proc strings can only be given for datum proc calls, global procs must be proc paths
*
* Also proc strings are strongly advised against because they don't compile error if the proc stops existing
*
* In some cases you can provide a shortform of the procname, see the proc typepath shortcuts documentation below
*
* ## INVOKING THE CALLBACK
*`var/result = C.Invoke(args, to, add)` additional args are added after the ones given when the callback was created
*
* `var/result = C.InvokeAsync(args, to, add)` Asyncronous - returns . on the first sleep then continues on in the background
* after the sleep/block ends, otherwise operates normally.
*
* ## PROC TYPEPATH SHORTCUTS
* (these operate on paths, not types, so to these shortcuts, datum is NOT a parent of atom, etc...)
*
* ### proc defined on current(src) object OR overridden at src or any of it's parents:
* PROC_REF(procname)
*
* `CALLBACK(src, PROC_REF(some_proc_here))`
*
* ### global proc
* GLOBAL_PROC_REF(procname)
*
* `CALLBACK(src, GLOBAL_PROC_REF(some_proc_here))`
*
*
* ### proc defined on some type
* TYPE_PROC_REF(/some/type/, some_proc_here)
*/
/datum/callback
///The object we will be calling the proc on
var/datum/object = GLOBAL_PROC
///The proc we will be calling on the object
var/delegate
///A list of arguments to pass into the proc
var/list/arguments
///A weak reference to the user who triggered this callback
var/datum/weakref/user
/**
* Create a new callback datum
*
* Arguments
* * thingtocall the object to call the proc on
* * proctocall the proc to call on the target object
* * ... an optional list of extra arguments to pass to the proc
*/
/datum/callback/New(thingtocall, proctocall, ...)
if (thingtocall)
object = thingtocall
delegate = proctocall
if (length(args) > 2)
arguments = args.Copy(3)
if(usr)
user = WEAKREF(usr)
/**
* Invoke this callback
*
* Calls the registered proc on the registered object, if the user ref
* can be resolved it also inclues that as an arg
*
* If the datum being called on is varedited, the call is wrapped via [WrapAdminProcCall][/proc/WrapAdminProcCall]
*/
/datum/callback/proc/Invoke(...)
if(!usr)
var/datum/weakref/W = user
if(W)
var/mob/M = W.resolve()
if(M)
if (length(args))
return world.push_usr(arglist(list(M, src) + args))
return world.push_usr(M, src)
if (!object)
return
#if DM_VERSION <= 514
if(istext(object) && object != GLOBAL_PROC)
to_chat(usr, "[object] may be an external library. Calling external libraries is disallowed.", confidential = TRUE)
return
#endif
var/list/calling_arguments = arguments
if (length(args))
if (length(arguments))
calling_arguments = calling_arguments + args //not += so that it creates a new list so the arguments list stays clean
else
calling_arguments = args
if(datum_flags & DF_VAR_EDITED)
if(usr != GLOB.AdminProcCallHandler && !usr?.client?.ckey) //This happens when a timer or the MC invokes a callback
return HandleUserlessProcCall(usr, object, delegate, calling_arguments)
return WrapAdminProcCall(object, delegate, calling_arguments)
if (object == GLOBAL_PROC)
return call(delegate)(arglist(calling_arguments))
return call(object, delegate)(arglist(calling_arguments))
/**
* Invoke this callback async (waitfor=false)
*
* Calls the registered proc on the registered object, if the user ref
* can be resolved it also inclues that as an arg
*
* If the datum being called on is varedited, the call is wrapped via WrapAdminProcCall
*/
/datum/callback/proc/InvokeAsync(...)
set waitfor = FALSE
if(!usr)
var/datum/weakref/W = user
if(W)
var/mob/M = W.resolve()
if(M)
if (length(args))
return world.push_usr(arglist(list(M, src) + args))
return world.push_usr(M, src)
if (!object)
return
#if DM_VERSION <= 514
if(istext(object) && object != GLOBAL_PROC)
to_chat(usr, "[object] may be an external library. Calling external libraries is disallowed.", confidential = TRUE)
return
#endif
var/list/calling_arguments = arguments
if (length(args))
if (length(arguments))
calling_arguments = calling_arguments + args //not += so that it creates a new list so the arguments list stays clean
else
calling_arguments = args
if(datum_flags & DF_VAR_EDITED)
if(usr != GLOB.AdminProcCallHandler && !usr?.client?.ckey) //This happens when a timer or the MC invokes a callback
return HandleUserlessProcCall(usr, object, delegate, calling_arguments)
return WrapAdminProcCall(object, delegate, calling_arguments)
if (object == GLOBAL_PROC)
return call(delegate)(arglist(calling_arguments))
return call(object, delegate)(arglist(calling_arguments))
/**
Helper datum for the select callbacks proc
*/
/datum/callback_select
var/list/finished
var/pendingcount
var/total
/datum/callback_select/New(count, savereturns)
total = count
if (savereturns)
finished = new(count)
/datum/callback_select/proc/invoke_callback(index, datum/callback/callback, list/callback_args, savereturn = TRUE)
set waitfor = FALSE
if (!callback || !istype(callback))
//This check only exists because the alternative is callback_select would block forever if given invalid data
CRASH("invalid callback passed to invoke_callback")
if (!length(callback_args))
callback_args = list()
pendingcount++
var/rtn = callback.Invoke(arglist(callback_args))
pendingcount--
if (savereturn)
finished[index] = rtn
/**
* Runs a list of callbacks asyncronously, returning only when all have finished
*
* Callbacks can be repeated, to call it multiple times
*
* Arguments:
* * list/callbacks the list of callbacks to be called
* * list/callback_args the list of lists of arguments to pass into each callback
* * savereturns Optionally save and return the list of returned values from each of the callbacks
* * resolution The number of byond ticks between each time you check if all callbacks are complete
*/
/proc/callback_select(list/callbacks, list/callback_args, savereturns = TRUE, resolution = 1)
if (!callbacks)
return
var/count = length(callbacks)
if (!count)
return
if (!callback_args)
callback_args = list()
callback_args.len = count
var/datum/callback_select/CS = new(count, savereturns)
for (var/i in 1 to count)
CS.invoke_callback(i, callbacks[i], callback_args[i], savereturns)
while(CS.pendingcount)
sleep(resolution*world.tick_lag)
return CS.finished