internal static void LogDebug(string message) => L.LogDebug(prefix + message);
/// <summary> /// Loads the mod by searching for assemblies in hollow_knight_Data\Managed\Mods\ /// </summary> public static void LoadMods() { if (Loaded) { return; } Logger.Log("[API] - Trying to load mods"); string path = string.Empty; if (SystemInfo.operatingSystem.Contains("Windows")) { path = Application.dataPath + "\\Managed\\Mods"; } else if (SystemInfo.operatingSystem.Contains("Mac")) { path = Application.dataPath + "/Resources/Data/Managed/Mods/"; } else { Logger.LogWarn($"Operating system of {SystemInfo.operatingSystem} is not known. Unable to load mods."); } if (string.IsNullOrEmpty(path)) { Loaded = true; return; } foreach (string text2 in Directory.GetFiles(path, "*.dll")) { Logger.LogDebug("[API] - Loading assembly: " + text2); try { foreach (Type type in Assembly.LoadFile(text2).GetExportedTypes()) { if (IsSubclassOfRawGeneric(typeof(Mod <>), type)) { Logger.LogDebug("[API] - Trying to instantiate mod<T>: " + type); IMod mod = Activator.CreateInstance(type) as IMod; if (mod == null) { continue; } LoadedMods.Add((Mod)mod); } else if (!type.IsGenericType && type.IsClass && type.IsSubclassOf(typeof(Mod))) { Logger.LogDebug("[API] - Trying to instantiate mod: " + type); Mod mod2 = type.GetConstructor(new Type[0])?.Invoke(new object[0]) as Mod; if (mod2 == null) { continue; } LoadedMods.Add(mod2); } } } catch (Exception ex) { Logger.LogError("[API] - Error: " + ex); _errors.Add(string.Concat(text2, ": FAILED TO LOAD! Check ModLog.txt.")); } } foreach (Mod mod in LoadedMods.OrderBy(x => x.LoadPriority())) { try { LoadMod(mod, false); } catch (Exception ex) { _errors.Add(string.Concat(mod.GetType().Name, ": FAILED TO LOAD! Check ModLog.txt.")); Logger.LogError("[API] - Error: " + ex); } } GameObject gameObject = new GameObject(); _draw = gameObject.AddComponent <ModVersionDraw>(); UnityEngine.Object.DontDestroyOnLoad(gameObject); UpdateModText(); Loaded = true; ModHooks.Instance.SaveGlobalSettings(); }
public static void LoadMods() { if (loaded) { return; } if (!Directory.Exists(MODS_FOLDER)) { Directory.CreateDirectory(MODS_FOLDER); } if (!Directory.Exists($"{MODS_FOLDER}{Path.DirectorySeparatorChar}{RELINK_FOLDER}")) { Directory.CreateDirectory($"{MODS_FOLDER}{Path.DirectorySeparatorChar}{RELINK_FOLDER}"); } DirectoryInfo relinkInfo = new DirectoryInfo($"{MODS_FOLDER}{Path.DirectorySeparatorChar}{RELINK_FOLDER}"); relinkInfo.Attributes |= FileAttributes.Hidden; loadedMods = new List <Mod>(); string ModText = "Underhero Modding API - Version " + ModHooks.API_VERSION; //Init mods from dlls. GetFiles is case-insensitive foreach (string name in Directory.GetFiles(MODS_FOLDER, "*.dll")) { Logger.LogDebug($"[API] Loading mods from assembly: \"{name}\""); try { //Relinker.Combine(stream, Path.GetFileNameWithoutExtension(name)); Assembly asm = Assembly.LoadFile(name); foreach (Type type in asm.GetExportedTypes()) { if (type.IsSubclassOf(typeof(Mod))) { Mod mod = (Mod)Activator.CreateInstance(type); if (mod == null) { Logger.LogWarn($"[API] Could not instantiate mod \"{type}\" from file \"{name}\""); continue; } loadedMods.Add(mod); } } } catch (Exception e) { Logger.LogError($"[API] Failed to load mod: \"{name}\"\n{e}"); } } //Sort can't take a lambda for some reason, gotta use this messier OrderBy implementation loadedMods = loadedMods.OrderBy(mod => { try { return(mod.LoadPriority()); } catch (Exception e) { Logger.LogError($"[API] Failed to get load priority from mod: \"{mod.GetModNameSafe()}\"\n{e}"); return(Mod.DEFAULT_PRIORITY); } }).ToList(); List <Mod> failedInit = new List <Mod>(); foreach (Mod mod in loadedMods) { string name = mod.GetModNameSafe(); Logger.LogDebug($"[API] Attempting to initialize mod \"{name}\""); try { mod.Initialize(); Logger.Log($"[API] Mod \"{name}\" initialized"); ModText += "\n" + mod.GetModName() + " - Version: " + mod.GetVersion(); } catch (Exception e) { Logger.LogError($"[API] Failed to initialize mod \"{name}\"\n{e}"); failedInit.Add(mod); } } loadedMods.RemoveAll(mod => failedInit.Contains(mod)); GameObject gameObject = new GameObject(); _draw = gameObject.AddComponent <ModVersionDraw>(); UnityEngine.Object.DontDestroyOnLoad(gameObject); _draw.drawString = ModText; loaded = true; }
private static void LogDebug(string str) => Logger.LogDebug("[Mantis Gods]: " + str);
public static void LoadMods() { if (loaded) { return; } if (!Directory.Exists(MODS_FOLDER)) { Directory.CreateDirectory(MODS_FOLDER); } if (!Directory.Exists($"{MODS_FOLDER}{Path.DirectorySeparatorChar}{RELINK_FOLDER}")) { Directory.CreateDirectory($"{MODS_FOLDER}{Path.DirectorySeparatorChar}{RELINK_FOLDER}"); } DirectoryInfo relinkInfo = new DirectoryInfo($"{MODS_FOLDER}{Path.DirectorySeparatorChar}{RELINK_FOLDER}"); relinkInfo.Attributes |= FileAttributes.Hidden; loadedMods = new List <Mod>(); //Init mods from dlls. GetFiles is case-insensitive foreach (string name in Directory.GetFiles(MODS_FOLDER, "*.dll")) { Logger.LogDebug($"[API] Loading mods from assembly: \"{name}\""); try { using (FileStream stream = File.OpenRead(Path.GetFullPath(name))) { Assembly asm = Relinker.GetRelinkedAssembly(stream, Path.GetFileNameWithoutExtension(name)); foreach (Type type in asm.GetExportedTypes()) { if (type.IsSubclassOf(typeof(Mod))) { Mod mod = (Mod)Activator.CreateInstance(type); if (mod == null) { Logger.LogWarn($"[API] Could not instantiate mod \"{type}\" from file \"{name}\""); continue; } loadedMods.Add(mod); } } } } catch (Exception e) { Logger.LogError($"[API] Failed to load mod: \"{name}\"\n{e}"); } } //Sort can't take a lambda for some reason, gotta use this messier OrderBy implementation loadedMods = loadedMods.OrderBy(mod => { try { return(mod.LoadPriority()); } catch (Exception e) { Logger.LogError($"[API] Failed to get load priority from mod: \"{mod.GetModNameSafe()}\"\n{e}"); return(Mod.DEFAULT_PRIORITY); } }).ToList(); List <Mod> failedInit = new List <Mod>(); foreach (Mod mod in loadedMods) { string name = mod.GetModNameSafe(); Logger.LogDebug($"[API] Attempting to initialize mod \"{name}\""); try { mod.Init(); Logger.Log($"[API] Mod \"{name}\" initialized"); } catch (Exception e) { Logger.LogError($"[API] Failed to initialize mod \"{name}\"\n{e}"); failedInit.Add(mod); } } loadedMods.RemoveAll(mod => failedInit.Contains(mod)); loaded = true; }