Created
July 20, 2015 18:01
-
-
Save Rene-Sackers/320decb8586991faeb25 to your computer and use it in GitHub Desktop.
A self-destruct for Cities: Skylines mods, as it fails to call "OnReleased" when a mod gets reloaded.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ICities; | |
using System; | |
using System.Linq; | |
using System.Reflection; | |
namespace SelfDestructor | |
{ | |
public static class SelfDestruct | |
{ | |
private static bool SelfDestructExecuted = false; | |
public static void SelfDestructCall(ILoadingExtension currentInstance) | |
{ | |
if (currentInstance == null || SelfDestructExecuted) return; | |
SelfDestructExecuted = true; | |
currentInstance.OnLevelUnloading(); | |
currentInstance.OnReleased(); | |
} | |
public static void DestructOldInstances(ILoadingExtension currentInstance) | |
{ | |
var targetType = typeof(SelfDestruct); | |
var currentAssemblyName = Assembly.GetAssembly(targetType).GetName().Name; | |
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == currentAssemblyName); | |
foreach (var assembly in assemblies) | |
{ | |
var oldInstance = assembly.GetTypes().Where(t => t.Name == targetType.Name).FirstOrDefault(); // Get old instance. | |
if (oldInstance == null || oldInstance == targetType) continue; // Skip current assembly. | |
var selfDestructMethod = oldInstance.GetMethod("SelfDestructCall"); // Find self destruct. | |
if (selfDestructMethod == null) continue; // No self destruct. | |
selfDestructMethod.Invoke(null, new object[] { currentInstance }); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment