internal static void ReloadModAssembly(object source, FileSystemEventArgs e, bool retrying = false) { if (!File.Exists(e.FullPath)) { return; } Logger.Log(LogLevel.Info, "loader", $"Reloading mod assembly: {e.FullPath}"); QueuedTaskHelper.Do("ReloadModAssembly:" + e.FullPath, () => { EverestModule module = _Modules.FirstOrDefault(m => m.Metadata.DLL == e.FullPath); if (module == null) { return; } AssetReloadHelper.Do($"{Dialog.Clean("ASSETRELOADHELPER_RELOADINGMODASSEMBLY")} {Path.GetFileName(e.FullPath)}", () => { Assembly asm = null; using (FileStream stream = File.OpenRead(e.FullPath)) asm = Relinker.GetRelinkedAssembly(module.Metadata, Path.GetFileNameWithoutExtension(e.FullPath), stream); if (asm == null) { if (!retrying) { // Retry. QueuedTaskHelper.Do("ReloadModAssembly:" + e.FullPath, () => { ReloadModAssembly(source, e, true); }); } return; } ((FileSystemWatcher)source).Dispose(); // be sure to save this module's save data and session before reloading it, so that they are not lost. if (SaveData.Instance != null) { Logger.Log("core", $"Saving save data slot {SaveData.Instance.FileSlot} for {module.Metadata} before reloading"); module.SaveSaveData(SaveData.Instance.FileSlot); if (SaveData.Instance.CurrentSession?.InArea ?? false) { Logger.Log("core", $"Saving session slot {SaveData.Instance.FileSlot} for {module.Metadata} before reloading"); module.SaveSession(SaveData.Instance.FileSlot); } } Unregister(module); LoadModAssembly(module.Metadata, asm); }); AssetReloadHelper.ReloadLevel(); }); }
private static void LoadAutoUpdated(object source, FileSystemEventArgs e) { if (!AutoLoadNewMods) { return; } Logger.Log(LogLevel.Info, "loader", $"Possible new mod container: {e.FullPath}"); QueuedTaskHelper.Do("LoadAutoUpdated:" + e.FullPath, () => AssetReloadHelper.Do($"{Dialog.Clean("ASSETRELOADHELPER_LOADINGNEWMOD")} {Path.GetFileName(e.FullPath)}", () => MainThreadHelper.Do(() => { if (Directory.Exists(e.FullPath)) { LoadDir(e.FullPath); } else if (e.FullPath.EndsWith(".zip")) { LoadZip(e.FullPath); } ((patch_OuiMainMenu)(AssetReloadHelper.ReturnToScene as Overworld)?.GetUI <OuiMainMenu>())?.RebuildMainAndTitle(); }))); }
internal static void ReloadModAssembly(object source, FileSystemEventArgs e, bool retrying = false) { if (!File.Exists(e.FullPath)) { return; } Logger.Log(LogLevel.Info, "loader", $"Reloading mod assembly: {e.FullPath}"); QueuedTaskHelper.Do("ReloadModAssembly:" + e.FullPath, () => { EverestModule module = _Modules.FirstOrDefault(m => m.Metadata.DLL == e.FullPath); if (module == null) { return; } AssetReloadHelper.Do($"Reloading mod assembly: {Path.GetFileName(e.FullPath)}", () => { Assembly asm = null; using (FileStream stream = File.OpenRead(e.FullPath)) asm = Relinker.GetRelinkedAssembly(module.Metadata, stream); if (asm == null) { if (!retrying) { // Retry. QueuedTaskHelper.Do("ReloadModAssembly:" + e.FullPath, () => { ReloadModAssembly(source, e, true); }); } return; } ((FileSystemWatcher)source).Dispose(); Unregister(module); LoadModAssembly(module.Metadata, asm); }); AssetReloadHelper.ReloadLevel(); }); }
/// <summary> /// Register a new EverestModule (mod) dynamically. Invokes LoadSettings and Load. /// </summary> /// <param name="module">Mod to register.</param> public static void Register(this EverestModule module) { lock (_Modules) { _Modules.Add(module); } LuaLoader.Precache(module.GetType().Assembly); bool newStrawberriesRegistered = false; foreach (Type type in module.GetType().Assembly.GetTypesSafe()) { // Search for all entities marked with the CustomEntityAttribute. foreach (CustomEntityAttribute attrib in type.GetCustomAttributes <CustomEntityAttribute>()) { foreach (string idFull in attrib.IDs) { string id; string genName; string[] split = idFull.Split('='); if (split.Length == 1) { id = split[0]; genName = "Load"; } else if (split.Length == 2) { id = split[0]; genName = split[1]; } else { Logger.Log(LogLevel.Warn, "core", $"Invalid number of custom entity ID elements: {idFull} ({type.FullName})"); continue; } id = id.Trim(); genName = genName.Trim(); patch_Level.EntityLoader loader = null; ConstructorInfo ctor; MethodInfo gen; gen = type.GetMethod(genName, new Type[] { typeof(Level), typeof(LevelData), typeof(Vector2), typeof(EntityData) }); if (gen != null && gen.IsStatic && gen.ReturnType.IsCompatible(typeof(Entity))) { loader = (level, levelData, offset, entityData) => (Entity)gen.Invoke(null, new object[] { level, levelData, offset, entityData }); goto RegisterEntityLoader; } ctor = type.GetConstructor(new Type[] { typeof(EntityData), typeof(Vector2), typeof(EntityID) }); if (ctor != null) { loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(new object[] { entityData, offset, new EntityID(levelData.Name, entityData.ID) }); goto RegisterEntityLoader; } ctor = type.GetConstructor(new Type[] { typeof(EntityData), typeof(Vector2) }); if (ctor != null) { loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(new object[] { entityData, offset }); goto RegisterEntityLoader; } ctor = type.GetConstructor(new Type[] { typeof(Vector2) }); if (ctor != null) { loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(new object[] { offset }); goto RegisterEntityLoader; } ctor = type.GetConstructor(_EmptyTypeArray); if (ctor != null) { loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(_EmptyObjectArray); goto RegisterEntityLoader; } RegisterEntityLoader: if (loader == null) { Logger.Log(LogLevel.Warn, "core", $"Found custom entity without suitable constructor / {genName}(Level, LevelData, Vector2, EntityData): {id} ({type.FullName})"); continue; } patch_Level.EntityLoaders[id] = loader; } } // Register with the StrawberryRegistry all entities marked with RegisterStrawberryAttribute. foreach (RegisterStrawberryAttribute attrib in type.GetCustomAttributes <RegisterStrawberryAttribute>()) { List <string> names = new List <string>(); foreach (CustomEntityAttribute nameAttrib in type.GetCustomAttributes <CustomEntityAttribute>()) { foreach (string idFull in nameAttrib.IDs) { string[] split = idFull.Split('='); if (split.Length == 0) { Logger.Log(LogLevel.Warn, "core", $"Invalid number of custom entity ID elements: {idFull} ({type.FullName})"); continue; } names.Add(split[0]); } } if (names.Count == 0) { goto NoDefinedBerryNames; // no customnames? skip out on registering berry } foreach (string name in names) { StrawberryRegistry.Register(type, name, attrib.isTracked, attrib.blocksNormalCollection); newStrawberriesRegistered = true; } } NoDefinedBerryNames: ; // Search for all Entities marked with the CustomEventAttribute. foreach (CustomEventAttribute attrib in type.GetCustomAttributes <CustomEventAttribute>()) { foreach (string idFull in attrib.IDs) { string id; string genName; string[] split = idFull.Split('='); if (split.Length == 1) { id = split[0]; genName = "Load"; } else if (split.Length == 2) { id = split[0]; genName = split[1]; } else { Logger.Log(LogLevel.Warn, "core", $"Invalid number of custom cutscene ID elements: {idFull} ({type.FullName})"); continue; } id = id.Trim(); genName = genName.Trim(); patch_EventTrigger.CutsceneLoader loader = null; ConstructorInfo ctor; MethodInfo gen; gen = type.GetMethod(genName, new Type[] { typeof(EventTrigger), typeof(Player), typeof(string) }); if (gen != null && gen.IsStatic && gen.ReturnType.IsCompatible(typeof(Entity))) { loader = (trigger, player, eventID) => (Entity)gen.Invoke(null, new object[] { trigger, player, eventID }); goto RegisterCutsceneLoader; } ctor = type.GetConstructor(new Type[] { typeof(EventTrigger), typeof(Player), typeof(string) }); if (ctor != null) { loader = (trigger, player, eventID) => (Entity)ctor.Invoke(new object[] { trigger, player, eventID }); goto RegisterCutsceneLoader; } ctor = type.GetConstructor(_EmptyTypeArray); if (ctor != null) { loader = (trigger, player, eventID) => (Entity)ctor.Invoke(_EmptyObjectArray); goto RegisterCutsceneLoader; } RegisterCutsceneLoader: if (loader == null) { Logger.Log(LogLevel.Warn, "core", $"Found custom cutscene without suitable constructor / {genName}(EventTrigger, Player, string): {id} ({type.FullName})"); continue; } patch_EventTrigger.CutsceneLoaders[id] = loader; } } } module.LoadSettings(); module.Load(); if (_ContentLoaded) { module.LoadContent(true); } if (_Initialized) { Tracker.Initialize(); module.Initialize(); Input.Initialize(); if (SaveData.Instance != null) { // we are in a save. we are expecting the save data to already be loaded at this point Logger.Log("core", $"Loading save data slot {SaveData.Instance.FileSlot} for {module.Metadata}"); module.LoadSaveData(SaveData.Instance.FileSlot); if (SaveData.Instance.CurrentSession?.InArea ?? false) { // we are in a level. we are expecting the session to already be loaded at this point Logger.Log("core", $"Loading session slot {SaveData.Instance.FileSlot} for {module.Metadata}"); module.LoadSession(SaveData.Instance.FileSlot, false); } } // Check if the module defines a PrepareMapDataProcessors method. If this is the case, we want to reload maps so that they are applied. // We should also run the map data processors again if new berry types are registered, so that CoreMapDataProcessor assigns them checkpoint IDs and orders. if (newStrawberriesRegistered || module.GetType().GetMethod("PrepareMapDataProcessors", new Type[] { typeof(MapDataFixup) })?.DeclaringType == module.GetType()) { Logger.Log("core", $"Module {module.Metadata} has custom strawberries or map data processors: reloading maps."); AssetReloadHelper.ReloadAllMaps(); } } if (Engine.Instance != null && Engine.Scene is Overworld overworld) { // we already are in the overworld. Register new Ouis real quick! Type[] types = FakeAssembly.GetFakeEntryAssembly().GetTypesSafe(); foreach (Type type in types) { if (typeof(Oui).IsAssignableFrom(type) && !type.IsAbstract && !overworld.UIs.Any(ui => ui.GetType() == type)) { Logger.Log("core", $"Instanciating UI from {module.Metadata}: {type.FullName}"); Oui oui = (Oui)Activator.CreateInstance(type); oui.Visible = false; overworld.Add(oui); overworld.UIs.Add(oui); } } } InvalidateInstallationHash(); EverestModuleMetadata meta = module.Metadata; meta.Hash = GetChecksum(meta); // Audio banks are cached, and as such use the module's hash. We can only ingest those now. if (patch_Audio.AudioInitialized) { patch_Audio.IngestNewBanks(); } Logger.Log(LogLevel.Info, "core", $"Module {module.Metadata} registered."); CheckDependenciesOfDelayedMods(); }