-
Notifications
You must be signed in to change notification settings - Fork 88
Description
Currently the lifecycle for TM:PE is completely obfuscated across multiple classes, it's impossible to see at a glance what is going on through the various stages, and it's getting even more confusing now that we're supporting hot reloads, etc.
In #764 I suggested briefly this: #764 (comment)
I think at some point we should consider creating a
Lifecycle.csthat contains some sort of state engine for the mod lifecycle management (enabed -> loading -> unloading -> disabled) but that's something for a later date.
Kian then added this: #764 (comment)
I have thought of that but I can't see how it would work.
the only thing I can think of is:
- move all code from
OnEnabled/OnDisabled,LoadingExtension.cs, andSerializeDataExtension.cstolifecycle.cs- then all
OnEnabled/OnDisabled,LoadingExtension.cs, andSerializeDataExtension.csdo is to call appropriate functions of lifecycle.cs at appropriate times.So lifeCycle.cs acts as a wrapper for
IUserMod,LoadingExtension, andSerializableDataExtension. A bit ugly if you ask me.The best thing to do is a mod for hot reload to patch CS so that it does not do weird stuff.
Even if we have mod to patch CS to not do weird stuff, we should still have a more coherent way of managing TMPE lifecycle.
Building on Kian's ideas, I suggest:
- Lifecycle class gets stub methods for the various states
TrafficManagerModandLoadingExtension(and anything else relevant) simply calls the applicable lifecycle methods- Rather than filling lifecycle methods with bloated code, we should atomise funcitonality in to methods in some other class
- The lifecycle would then just call those other methods
The result would be a very concise lifecycle class and you could just read it to see exactly what steps happen in each stage of the lifecycle without getting bogged down in reams of code.
For example, enabled/disabled stuff in Lifecycle.cs could look something like (rough mockup):
// comment would note all the ways enabled can be triggered, such as:
// * mod autoenabler mod when user subscribes TMPE
// * enabled via content manager > mods
// * already enabled when game starts
// * hot-reload (can happen in-game, main menu, etc)
public void OnEnabled() {
if (!HotReload) {
LogEnvironmentDetails();
PerformCompatibiltyChecks();
}
}
// comment...
public void OnDisabled() {
RemoveEventListeners();
if (HotReload) {
Patches.Remove();
ModUI.Remove();
//...
}
}Basically, we should be able to see at a glance wtf is going on without having to wade through gargantuan amounts of code.
The TrafficManagerMod.cs would then be like (rough mockup):
public void OnEnabled() {
Lifecycle.OnEnabled();
}
public void OnDisabled() {
Lifecycle.OnDisabled();
}