private static void CallOnLoadMethods([NotNull] List <Assembly> modAssemblies) { ModLogger.WriteLine("Calling OnLoad methods"); foreach (Assembly modAssembly in modAssemblies) { if (modAssembly == null) { continue; } foreach (Type type in modAssembly.GetTypes()) { if (type == null) { continue; } try { MethodInfo onLoad = type.GetMethod("OnLoad", new Type[0]); onLoad?.Invoke(null, new object[0]); } catch (Exception e) { if (e is TargetInvocationException && e.InnerException != null) { e = e.InnerException; } string message = "OnLoad method failed for type " + type.FullName + " of mod " + modAssembly.GetName().Name; ModLogger.Error.WriteLine(message); ModLogger.Error.WriteLine(e); throw new ModLoadingException(message + ExceptionToString(e)); } } } }
private static DirectoryInfo GetModsDirectory() { DirectoryInfo dataDir = new DirectoryInfo(Application.dataPath); DirectoryInfo oniBaseDirectory; if (Application.platform == RuntimePlatform.OSXPlayer) { oniBaseDirectory = dataDir.Parent?.Parent; } else if (Application.platform == RuntimePlatform.LinuxPlayer) { oniBaseDirectory = dataDir.Parent; } else { oniBaseDirectory = dataDir.Parent; } ModLogger.WriteLine("Path to mods is: " + Path.Combine(oniBaseDirectory?.FullName, "Mods")); return(new DirectoryInfo(Path.Combine(oniBaseDirectory?.FullName, "Mods"))); }
public static void Start() { // Patch in Mod Loader helpers HarmonyInstance.Create("ONI-ModLoader")?.PatchAll(Assembly.GetExecutingAssembly()); // Load mods DirectoryInfo modsDir = GetModsDirectory(); List <FileInfo> files = modsDir.GetFiles("*.dll").ToList(); foreach (DirectoryInfo modDirectory in modsDir.GetDirectories()) { DirectoryInfo[] sub = modDirectory?.GetDirectories(); DirectoryInfo assmeblies = sub?.FirstOrDefault(x => x != null && x.Name.Contains(AssemblyDir)); if (assmeblies != null) { foreach (FileInfo file in assmeblies.GetFiles("*.dll")) { files.Add(file); } } } try { DependencyGraph dependencyGraph = LoadModAssemblies(files); List <Assembly> sortedAssemblies = dependencyGraph?.TopologicalSort(); ApplyHarmonyPatches(sortedAssemblies); CallOnLoadMethods(sortedAssemblies); ModLogger.WriteLine("All mods successfully loaded!"); } catch (ModLoadingException mle) { failureMessage = mle.Message; } }
/// <summary> /// mono inject entry /// </summary> /// <param name="args"></param> public static void Injector(string[] args) { AppDomain currentDomain = AppDomain.CurrentDomain; string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string managedFolder = Environment.GetEnvironmentVariable("DOORSTOP_MANAGED_FOLDER_DIR"); //bool isWaitingForDebugger = Environment.GetEnvironmentVariable("WAITING_FOR_DEBUGGER") == "FALSE" ? false : true; LoadDependency(Assembly.GetCallingAssembly()); var loadedAssemblies = new Dictionary <string, Assembly>(); currentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnAssemblyLoadEventHandler); AppDomain.CurrentDomain.AssemblyResolve += (sender, arg) => { if (depends.TryGetValue(arg.Name, out Assembly loadedAssembly)) { return(loadedAssembly); } return(null); }; ModLogger.Debug("Waiting for Assembly-CSharp loaded."); }
private static List <Assembly> CheckForDependences(List <Assembly> modAssemblies) { ModLogger.WriteLine("CheckForDependences"); List <string> failedMods = new List <string>(); List <Assembly> loadableMods = new List <Assembly>(); foreach (Assembly modAssembly in modAssemblies) { if (modAssembly == null) { continue; } try { ModLogger.WriteLine("\tChecking " + modAssembly.FullName); bool correct = true; foreach (AssemblyName referenced in modAssembly.GetReferencedAssemblies()) { ModLogger.WriteLine("\t\t" + referenced.FullName); try { Assembly dependence = Assembly.ReflectionOnlyLoad(referenced.FullName); if (dependence == null) { correct = false; ModLogger.Error.WriteLine("\tCannot find dependence: " + referenced.FullName); continue; } } catch (Exception ex) { ModLogger.WriteLine("\t\t\tChecking embedded dependences:"); correct = CheckForEmbeddedDependence(referenced.Name, modAssembly); if (!correct) { ModLogger.Error.WriteLine("\tCannot find dependence: " + referenced.FullName); ModLogger.Error.WriteLine(ex); continue; } } } if (correct) { loadableMods.Add(modAssembly); } else { failedMods.Add(modAssembly.GetName().ToString()); } } catch (Exception e) { failedMods.Add(modAssembly.GetName() + ExceptionToString(e)); ModLogger.Error.WriteLine("Patching mod " + modAssembly.GetName() + " failed!"); ModLogger.Error.WriteLine(e); } } return(loadableMods); }