/// <summary> /// Unloads the given GadgetMod. /// Cannot fully release the mod's RAM usage, as the old instance of the mod assembly that is stored in RAM cannot be fully removed. /// WARNING: Will trigger an immediate garbage collection, causing a short freeze! /// </summary> public static void UnloadMod(GadgetMod mod) { try { if (mod.Name == "GadgetCore") { throw new InvalidOperationException(GadgetMods.GetModByAssembly(Assembly.GetCallingAssembly()).Name + " seriously just tried to unload GadgetCore... Really?"); } List <GadgetMod> modsToUnload = mod.LoadedGadgets.SelectMany(x => Gadgets.LoadOrderTree.Find(x).FlattenUniqueByBreadth().Where(y => y != x)).Where(x => x != null).Select(x => x.Mod).Distinct().ToList(); bool wasBatchLoading = BatchLoading; BatchLoading = true; foreach (GadgetMod modToUnload in modsToUnload) { UnloadModInternal(modToUnload); } mod.Unload(); GC.Collect(); BatchLoading = wasBatchLoading; DisableQueuedGadgets(); Gadgets.SortGadgets(); } catch (Exception e) { Logger.LogError("Error unloading " + mod.Name + ": " + e); } }
private static void UnloadModInternal(GadgetMod mod) { foreach (GadgetMod dependency in GadgetMods.ListAllMods().Where(x => x.ModDependencies.Contains(mod.Name))) { if (dependency.Name != "GadgetCore") { UnloadModInternal(mod); } } GadgetMods.UnregisterMod(mod); GadgetCore.LoadedAssemblies.Remove(mod.Assembly.GetName().Name); mod.Unload(); }