mirror of
https://github.com/VOREStation/VOREStation.git
synced 2026-08-26 21:49:11 +01:00
ss atoms update (#17529)
* draft ss atoms update * . * . * . * low wall * do that when not asleep * late init handling * . * smes * try this
This commit is contained in:
@@ -1,112 +1,174 @@
|
||||
#define BAD_INIT_QDEL_BEFORE 1
|
||||
#define BAD_INIT_DIDNT_INIT 2
|
||||
#define BAD_INIT_SLEPT 4
|
||||
#define BAD_INIT_NO_HINT 8
|
||||
|
||||
SUBSYSTEM_DEF(atoms)
|
||||
name = "Atoms"
|
||||
init_order = INIT_ORDER_ATOMS
|
||||
flags = SS_NO_FIRE
|
||||
|
||||
var/static/initialized = INITIALIZATION_INSSATOMS
|
||||
var/static/old_initialized
|
||||
/// A stack of list(source, desired initialized state)
|
||||
/// We read the source of init changes from the last entry, and assert that all changes will come with a reset
|
||||
var/list/initialized_state = list()
|
||||
var/base_initialized
|
||||
|
||||
var/list/late_loaders
|
||||
var/list/created_atoms
|
||||
var/initialized = INITIALIZATION_INSSATOMS
|
||||
var/list/late_loaders = list()
|
||||
|
||||
var/list/BadInitializeCalls = list()
|
||||
|
||||
///initAtom() adds the atom its creating to this list iff InitializeAtoms() has been given a list to populate as an argument
|
||||
var/list/created_atoms
|
||||
|
||||
/// Atoms that will be deleted once the subsystem is initialized
|
||||
var/list/queued_deletions = list()
|
||||
|
||||
var/init_start_time
|
||||
|
||||
#ifdef PROFILE_MAPLOAD_INIT_ATOM
|
||||
var/list/mapload_init_times = list()
|
||||
#endif
|
||||
|
||||
initialized = INITIALIZATION_INSSATOMS
|
||||
|
||||
/datum/controller/subsystem/atoms/Initialize()
|
||||
init_start_time = world.time
|
||||
|
||||
initialized = INITIALIZATION_INNEW_MAPLOAD
|
||||
to_world_log("Initializing objects")
|
||||
admin_notice(span_danger("Initializing objects"), R_DEBUG)
|
||||
InitializeAtoms()
|
||||
initialized = INITIALIZATION_INNEW_REGULAR
|
||||
|
||||
return SS_INIT_SUCCESS
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/InitializeAtoms(list/atoms)
|
||||
/datum/controller/subsystem/atoms/proc/InitializeAtoms(list/atoms, list/atoms_to_return)
|
||||
if(initialized == INITIALIZATION_INSSATOMS)
|
||||
return
|
||||
|
||||
initialized = INITIALIZATION_INNEW_MAPLOAD
|
||||
// Generate a unique mapload source for this run of InitializeAtoms
|
||||
var/static/uid = 0
|
||||
uid = (uid + 1) % (SHORT_REAL_LIMIT - 1)
|
||||
var/source = "subsystem init [uid]"
|
||||
set_tracked_initalized(INITIALIZATION_INNEW_MAPLOAD, source)
|
||||
|
||||
LAZYINITLIST(late_loaders)
|
||||
|
||||
var/count
|
||||
var/list/mapload_arg = list(TRUE)
|
||||
if(atoms)
|
||||
created_atoms = list()
|
||||
count = atoms.len
|
||||
for(var/atom/A as anything in atoms)
|
||||
if(!(A.flags & ATOM_INITIALIZED))
|
||||
if(InitAtom(A, mapload_arg))
|
||||
atoms -= A
|
||||
CHECK_TICK
|
||||
else
|
||||
count = 0
|
||||
for(var/atom/A in world) // This must be world, since this operation adds all the atoms to their specific lists.
|
||||
if(!(A.flags & ATOM_INITIALIZED))
|
||||
InitAtom(A, mapload_arg)
|
||||
++count
|
||||
CHECK_TICK
|
||||
|
||||
log_world("Initialized [count] atoms")
|
||||
|
||||
initialized = INITIALIZATION_INNEW_REGULAR
|
||||
// This may look a bit odd, but if the actual atom creation runtimes for some reason, we absolutely need to set initialized BACK
|
||||
CreateAtoms(atoms, atoms_to_return, source)
|
||||
clear_tracked_initalize(source)
|
||||
|
||||
if(late_loaders.len)
|
||||
for(var/atom/A as anything in late_loaders)
|
||||
for(var/I in 1 to late_loaders.len)
|
||||
var/atom/A = late_loaders[I]
|
||||
//I hate that we need this
|
||||
if(QDELETED(A))
|
||||
continue
|
||||
A.LateInitialize()
|
||||
CHECK_TICK
|
||||
testing("Late initialized [late_loaders.len] atoms")
|
||||
late_loaders.Cut()
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/InitAtom(atom/A, list/arguments)
|
||||
var/the_type = A.type
|
||||
if(QDELING(A))
|
||||
BadInitializeCalls[the_type] |= BAD_INIT_QDEL_BEFORE
|
||||
return TRUE
|
||||
if (created_atoms)
|
||||
atoms_to_return += created_atoms
|
||||
created_atoms = null
|
||||
|
||||
var/start_tick = world.time
|
||||
for (var/queued_deletion in queued_deletions)
|
||||
qdel(queued_deletion)
|
||||
|
||||
var/result = A.Initialize(arglist(arguments))
|
||||
testing("[queued_deletions.len] atoms were queued for deletion.")
|
||||
queued_deletions.Cut()
|
||||
|
||||
if(start_tick != world.time)
|
||||
BadInitializeCalls[the_type] |= BAD_INIT_SLEPT
|
||||
#ifdef PROFILE_MAPLOAD_INIT_ATOM
|
||||
rustg_file_write(json_encode(mapload_init_times), "[GLOB.log_directory]/init_times.json")
|
||||
#endif
|
||||
|
||||
var/qdeleted = FALSE
|
||||
/// Actually creates the list of atoms. Exists solely so a runtime in the creation logic doesn't cause initialized to totally break
|
||||
/datum/controller/subsystem/atoms/proc/CreateAtoms(list/atoms, list/atoms_to_return = null, mapload_source = null)
|
||||
if (atoms_to_return)
|
||||
LAZYINITLIST(created_atoms)
|
||||
|
||||
if(result != INITIALIZE_HINT_NORMAL)
|
||||
switch(result)
|
||||
if(INITIALIZE_HINT_LATELOAD)
|
||||
if(arguments[1]) //mapload
|
||||
late_loaders += A
|
||||
else
|
||||
A.LateInitialize()
|
||||
if(INITIALIZE_HINT_QDEL)
|
||||
qdel(A)
|
||||
qdeleted = TRUE
|
||||
else
|
||||
BadInitializeCalls[the_type] |= BAD_INIT_NO_HINT
|
||||
#ifdef TESTING
|
||||
var/count
|
||||
#endif
|
||||
|
||||
if(!A) //possible harddel
|
||||
qdeleted = TRUE
|
||||
else if(!(A.flags & ATOM_INITIALIZED))
|
||||
BadInitializeCalls[the_type] |= BAD_INIT_DIDNT_INIT
|
||||
var/list/mapload_arg = list(TRUE)
|
||||
|
||||
return qdeleted || QDELING(A)
|
||||
if(atoms)
|
||||
#ifdef TESTING
|
||||
count = atoms.len
|
||||
#endif
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/map_loader_begin()
|
||||
old_initialized = initialized
|
||||
initialized = INITIALIZATION_INSSATOMS
|
||||
for(var/I in 1 to atoms.len)
|
||||
var/atom/A = atoms[I]
|
||||
if(!(A.flags & ATOM_INITIALIZED))
|
||||
// Unrolled CHECK_TICK setup to let us enable/disable mapload based off source
|
||||
if(TICK_CHECK)
|
||||
clear_tracked_initalize(mapload_source)
|
||||
stoplag()
|
||||
if(mapload_source)
|
||||
set_tracked_initalized(INITIALIZATION_INNEW_MAPLOAD, mapload_source)
|
||||
PROFILE_INIT_ATOM_BEGIN()
|
||||
InitAtom(A, TRUE, mapload_arg)
|
||||
PROFILE_INIT_ATOM_END(A)
|
||||
else
|
||||
#ifdef TESTING
|
||||
count = 0
|
||||
#endif
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/map_loader_stop()
|
||||
initialized = old_initialized
|
||||
for(var/atom/A as anything in world)
|
||||
if(!(A.flags & ATOM_INITIALIZED))
|
||||
PROFILE_INIT_ATOM_BEGIN()
|
||||
InitAtom(A, FALSE, mapload_arg)
|
||||
PROFILE_INIT_ATOM_END(A)
|
||||
#ifdef TESTING
|
||||
++count
|
||||
#endif
|
||||
if(TICK_CHECK)
|
||||
clear_tracked_initalize(mapload_source)
|
||||
stoplag()
|
||||
if(mapload_source)
|
||||
set_tracked_initalized(INITIALIZATION_INNEW_MAPLOAD, mapload_source)
|
||||
|
||||
#ifdef TESTING
|
||||
testing("Initialized [count] atoms")
|
||||
#endif
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/map_loader_begin(source)
|
||||
set_tracked_initalized(INITIALIZATION_INSSATOMS, source)
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/map_loader_stop(source)
|
||||
clear_tracked_initalize(source)
|
||||
|
||||
/// Returns the source currently modifying SSatom's init behavior
|
||||
/datum/controller/subsystem/atoms/proc/get_initialized_source()
|
||||
var/state_length = length(initialized_state)
|
||||
if(!state_length)
|
||||
return null
|
||||
return initialized_state[state_length][1]
|
||||
|
||||
/// Use this to set initialized to prevent error states where the old initialized is overridden, and we end up losing all context
|
||||
/// Accepts a state and a source, the most recent state is used, sources exist to prevent overriding old values accidentally
|
||||
/datum/controller/subsystem/atoms/proc/set_tracked_initalized(state, source)
|
||||
if(!length(initialized_state))
|
||||
base_initialized = initialized
|
||||
initialized_state += list(list(source, state))
|
||||
initialized = state
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/clear_tracked_initalize(source)
|
||||
if(!length(initialized_state))
|
||||
return
|
||||
for(var/i in length(initialized_state) to 1 step -1)
|
||||
if(initialized_state[i][1] == source)
|
||||
initialized_state.Cut(i, i+1)
|
||||
break
|
||||
|
||||
if(!length(initialized_state))
|
||||
initialized = base_initialized
|
||||
base_initialized = INITIALIZATION_INNEW_REGULAR
|
||||
return
|
||||
initialized = initialized_state[length(initialized_state)][2]
|
||||
|
||||
/// Returns TRUE if anything is currently being initialized
|
||||
/datum/controller/subsystem/atoms/proc/initializing_something()
|
||||
return length(initialized_state) > 1
|
||||
|
||||
/datum/controller/subsystem/atoms/Recover()
|
||||
initialized = SSatoms.initialized
|
||||
if(initialized == INITIALIZATION_INNEW_MAPLOAD)
|
||||
InitializeAtoms()
|
||||
old_initialized = SSatoms.old_initialized
|
||||
initialized_state = SSatoms.initialized_state
|
||||
BadInitializeCalls = SSatoms.BadInitializeCalls
|
||||
|
||||
/datum/controller/subsystem/atoms/proc/InitLog()
|
||||
@@ -115,20 +177,23 @@ SUBSYSTEM_DEF(atoms)
|
||||
. += "Path : [path] \n"
|
||||
var/fails = BadInitializeCalls[path]
|
||||
if(fails & BAD_INIT_DIDNT_INIT)
|
||||
. += "- Didn't call atom/Initialize()\n"
|
||||
. += "- Didn't call atom/Initialize(mapload)\n"
|
||||
if(fails & BAD_INIT_NO_HINT)
|
||||
. += "- Didn't return an Initialize hint\n"
|
||||
if(fails & BAD_INIT_QDEL_BEFORE)
|
||||
. += "- Qdel'd in New()\n"
|
||||
. += "- Qdel'd before Initialize proc ran\n"
|
||||
if(fails & BAD_INIT_SLEPT)
|
||||
. += "- Slept during Initialize()\n"
|
||||
|
||||
/// Prepares an atom to be deleted once the atoms SS is initialized.
|
||||
/datum/controller/subsystem/atoms/proc/prepare_deletion(atom/target)
|
||||
if (initialized == INITIALIZATION_INNEW_REGULAR)
|
||||
// Atoms SS has already completed, just kill it now.
|
||||
qdel(target)
|
||||
else
|
||||
queued_deletions += WEAKREF(target)
|
||||
|
||||
/datum/controller/subsystem/atoms/Shutdown()
|
||||
var/initlog = InitLog()
|
||||
if(initlog)
|
||||
text2file(initlog, "[GLOB.log_directory]-initialize.log")
|
||||
|
||||
#undef BAD_INIT_QDEL_BEFORE
|
||||
#undef BAD_INIT_DIDNT_INIT
|
||||
#undef BAD_INIT_SLEPT
|
||||
#undef BAD_INIT_NO_HINT
|
||||
|
||||
Reference in New Issue
Block a user