Skip to content

Instantly share code, notes, and snippets.

@Rene-Sackers
Created July 20, 2015 18:01
Show Gist options
  • Save Rene-Sackers/320decb8586991faeb25 to your computer and use it in GitHub Desktop.
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.
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