Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions TLM/TLM/LoadingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,10 @@ public class LoadingExtension : LoadingExtensionBase {
private const string HARMONY_ID = "de.viathinksoft.tmpe";
internal static LoadingExtension Instance = null;

internal bool InGameHotReload { get; private set; } = false;

internal static AppMode currentMode => SimulationManager.instance.m_ManagersWrapper.loading.currentMode;

internal static bool InGame() {
try {
return currentMode == AppMode.Game;
} catch {
return false;
}
}

FastList<ISimulationManager> simManager =>
typeof(SimulationManager).GetField("m_managers", BindingFlags.Static | BindingFlags.NonPublic)
?.GetValue(null) as FastList<ISimulationManager>;


public class Detour {
public MethodInfo OriginalMethod;
public MethodInfo CustomMethod;
Expand Down Expand Up @@ -275,6 +262,10 @@ public override void OnCreated(ILoading loading) {

// SelfDestruct.DestructOldInstances(this);
base.OnCreated(loading);
if(IsGameLoaded) {
// When another mod is detected, OnCreated is called again for god - or CS team - knows what reason!
Log._Debug("Hot reload of another mod detected. Skipping LoadingExtension.OnCreated() ...");
}

Detours = new List<Detour>();
RegisteredManagers = new List<ICustomManager>();
Expand All @@ -284,7 +275,6 @@ public override void OnCreated(ILoading loading) {
RegisterCustomManagers();

Instance = this;
InGameHotReload = InGame();
}

private void RegisterCustomManagers() {
Expand Down
8 changes: 5 additions & 3 deletions TLM/TLM/State/SerializableDataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class SerializableDataExtension
public static bool StateLoading;

public override void OnCreated(ISerializableData serializableData) {
_serializableData = serializableData;

Log._Debug("SerializableDataExtension.OnCreated() called");
if(LoadingExtension.IsGameLoaded) {
Log._Debug("Hot reload of another mod detected. Skipping SerializableDataExtension.OnCreated() ...");
}
_serializableData = serializableData;

if (LoadingExtension.Instance.InGameHotReload) {
if (TrafficManagerMod.Instance.InGameHotReload) {
Log._Debug("HOT RELOAD ...");
OnLoadData();
LoadingExtension.Instance.OnLevelLoaded(LoadMode.LoadGame);
Expand Down
21 changes: 20 additions & 1 deletion TLM/TLM/TrafficManagerMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public class TrafficManagerMod : IUserMod {

public string Description => "Manage your city's traffic";

internal static TrafficManagerMod Instance = null;

internal bool InGameHotReload { get; private set; } = false;

internal static AppMode currentMode => SimulationManager.instance.m_ManagersWrapper.loading.currentMode;

internal static bool InGame() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be better to use SceneManager.GetActiveScene().name == "Game"?

I've been testing that as part of #699 and it's proved extremely reliable. (Note: PR #699 is currently non-functional as the UI stuff is a big mess - I'm waiting to see if kvakvs new Form Builder stuff can be used rather than spending ages messing with raw CO UI)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to mention that this will throw exception when used in menu (mentioned about that earlier)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was not possible from OnCreated()
Now that I moved my stuff into Enable() ... yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have catched the exception so its fine :). I am going to change it to GetActiveScene() because its just cleaner that way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aubergine10 I suspect that SceneManager.GetActiveScene().name == "Game" is not as reliable as we thought... Problem with it is that you will get true inside LoadingExtension.OnCreated()...
It seems that LoadingManager.instance.m_loadingComplete works way better for that case.

try {
return currentMode == AppMode.Game;
}
catch {
return false;
}
}

[UsedImplicitly]
public void OnEnabled() {
Log.InfoFormat(
Expand Down Expand Up @@ -75,6 +90,9 @@ public void OnEnabled() {
Log.InfoFormat("Mono version: {0}", displayName.Invoke(null, null));
}
}

Instance = this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this getting used exaclty? When I look at the code in VS2019, it's not showing any references to it.

I can see down on line 110 (in updated TrafficManagerMod.cs) that it gets set to null in the OnDisabled() method, but other than that it's not getting used.

Was it supposed to be referenced in line 105 in the OnDisabled() method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see
TLM/TLM/State/SerializableDataExtension.cs :29
VersionLabel.cs :14

InGameHotReload = InGame();
}

[UsedImplicitly]
Expand All @@ -84,11 +102,12 @@ public void OnDisabled() {
LocaleManager.eventLocaleChanged -= Translation.HandleGameLocaleChange;
Translation.IsListeningToGameLocaleChanged = false; // is this necessary?

if (LoadingExtension.InGame() && LoadingExtension.Instance != null) {
if (InGame() && LoadingExtension.Instance != null) {
//Hot reload Unloading
LoadingExtension.Instance.OnLevelUnloading();
LoadingExtension.Instance.OnReleased();
}
Instance = null;
}

[UsedImplicitly]
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/UI/MainMenu/VersionLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public override void Start() {
MainMenuPanel.ScaledSize.GetTitlebarHeight());
text = TrafficManagerMod.ModName;

if(LoadingExtension.Instance.InGameHotReload) {
if(TrafficManagerMod.Instance.InGameHotReload) {
// make it easier to Identify Hot reload.
text += " HOT RELOAD " +
Assembly.GetExecutingAssembly().GetName().Version;
Expand Down