internal static void Initialize() { try { string basedir = Path.Combine(Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MelonLoader"), "Dependencies"), "SupportModules"); string filepath = null; if (Imports.IsIl2CppGame()) { filepath = Path.Combine(basedir, "MelonLoader.Support.Il2Cpp.dll"); } else { if (File.Exists(Path.Combine(Imports.GetAssemblyDirectory(), "UnityEngine.CoreModule.dll"))) { filepath = Path.Combine(basedir, "MelonLoader.Support.Mono.dll"); } else { if (IsOldUnity()) { filepath = Path.Combine(basedir, "MelonLoader.Support.Mono.Pre2017.2.dll"); } else { filepath = Path.Combine(basedir, "MelonLoader.Support.Mono.Pre2017.dll"); } } } if (File.Exists(filepath)) { byte[] data = File.ReadAllBytes(filepath); if (data.Length > 0) { assembly = Assembly.Load(data); if (!assembly.Equals(null)) { type = assembly.GetType("MelonLoader.Support.Main"); if (!type.Equals(null)) { MethodInfo method = type.GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static); if (!method.Equals(null)) { supportModule = (ISupportModule)method.Invoke(null, new object[0]); } } } } } else { MelonModLogger.LogError("Unable to load Support Module! Support Module is Missing!"); MelonModLogger.Log("------------------------------"); } } catch (Exception e) { MelonModLogger.LogError("Unable to load Support Module!\n" + e.ToString()); MelonModLogger.Log("------------------------------"); } }
private static void OnApplicationStart() { if (Imports.IsIl2CppGame()) { Assembly_CSharp = Assembly.Load("Assembly-CSharp"); UnhollowerSupport.Initialize(); } SupportModule.Initialize(); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Unity " + UnityVersion); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Name: " + CurrentGameAttribute.GameName); MelonModLogger.Log("Developer: " + CurrentGameAttribute.Developer); MelonModLogger.Log("Type: " + (Imports.IsIl2CppGame() ? "Il2Cpp" : (Imports.IsOldMono() ? "Mono" : "MonoBleedingEdge"))); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Using v" + BuildInfo.Version + " Open-Beta"); MelonModLogger.Log("------------------------------"); LoadMods(); if (Mods.Count > 0) { for (int i = 0; i < Mods.Count; i++) { MelonMod mod = Mods[i]; if (mod != null) { MelonModLogger.Log(mod.InfoAttribute.Name + (!string.IsNullOrEmpty(mod.InfoAttribute.Version) ? (" v" + mod.InfoAttribute.Version) : "") + (!string.IsNullOrEmpty(mod.InfoAttribute.Author) ? (" by " + mod.InfoAttribute.Author) : "") + (!string.IsNullOrEmpty(mod.InfoAttribute.DownloadLink) ? (" (" + mod.InfoAttribute.DownloadLink + ")") : "") ); if (Imports.IsDebugMode()) { MelonModLogger.Log("Preload: " + mod.IsPreload.ToString()); } MelonModLogger.LogModStatus((mod.GameAttributes.Any()) ? (mod.IsUniversal ? 0 : 1) : 2); MelonModLogger.Log("------------------------------"); } } for (int i = 0; i < Mods.Count; i++) { MelonMod mod = Mods[i]; if (mod != null) { try { mod.OnApplicationStart(); } catch (Exception ex) { MelonModLogger.LogModError(ex.ToString(), mod.InfoAttribute.Name); } } } } else { MelonModLogger.Log("No Mods Loaded!"); MelonModLogger.Log("------------------------------"); } }
public static void SaveConfig() { foreach (KeyValuePair <string, Dictionary <string, PrefDesc> > prefsInSection in prefs) { foreach (KeyValuePair <string, PrefDesc> pref in prefsInSection.Value) { pref.Value.Value = pref.Value.ValueEdited; ModPrefsController.SetString(prefsInSection.Key, pref.Key, pref.Value.Value); } } Main.OnModSettingsApplied(); MelonModLogger.Log("Config Saved!"); }
private static void OnApplicationStart() { if (!HasGeneratedAssembly) { return; } if (Imports.IsIl2CppGame()) { if (IsVRChat) { Assembly_CSharp = Assembly.Load("Assembly-CSharp"); } UnhollowerSupport.Initialize(); } SupportModule.Initialize(); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Unity " + UnityVersion); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Name: " + CurrentGameAttribute.GameName); MelonModLogger.Log("Developer: " + CurrentGameAttribute.Developer); MelonModLogger.Log("Type: " + (Imports.IsIl2CppGame() ? "Il2Cpp" : (Imports.IsOldMono() ? "Mono" : "MonoBleedingEdge"))); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Using v" + BuildInfo.Version + " Open-Beta"); MelonModLogger.Log("------------------------------"); LoadDLLs(); if (Plugins.Count > 0) { for (int i = 0; i < Plugins.Count; i++) { MelonPlugin plugin = Plugins[i]; if (plugin != null) { MelonModLogger.Log(plugin.InfoAttribute.Name + (!string.IsNullOrEmpty(plugin.InfoAttribute.Version) ? (" v" + plugin.InfoAttribute.Version) : "") + (!string.IsNullOrEmpty(plugin.InfoAttribute.Author) ? (" by " + plugin.InfoAttribute.Author) : "") + (!string.IsNullOrEmpty(plugin.InfoAttribute.DownloadLink) ? (" (" + plugin.InfoAttribute.DownloadLink + ")") : "") ); MelonModLogger.LogDLLStatus(plugin.Compatibility); MelonModLogger.Log("------------------------------"); } } Plugins = TempPlugins; } if (Plugins.Count <= 0) { MelonModLogger.Log("No Plugins Loaded!"); MelonModLogger.Log("------------------------------"); } if (Mods.Count > 0) { for (int i = 0; i < Mods.Count; i++) { MelonMod mod = Mods[i]; if (mod != null) { MelonModLogger.Log(mod.InfoAttribute.Name + (!string.IsNullOrEmpty(mod.InfoAttribute.Version) ? (" v" + mod.InfoAttribute.Version) : "") + (!string.IsNullOrEmpty(mod.InfoAttribute.Author) ? (" by " + mod.InfoAttribute.Author) : "") + (!string.IsNullOrEmpty(mod.InfoAttribute.DownloadLink) ? (" (" + mod.InfoAttribute.DownloadLink + ")") : "") ); MelonModLogger.LogDLLStatus(mod.Compatibility); MelonModLogger.Log("------------------------------"); } } Mods.RemoveAll((MelonMod mod) => (mod.Compatibility >= MelonBase.MelonCompatibility.INCOMPATIBLE)); DependencyGraph <MelonMod> .TopologicalSort(Mods, mod => mod.InfoAttribute.Name); } if (Mods.Count <= 0) { MelonModLogger.Log("No Mods Loaded!"); MelonModLogger.Log("------------------------------"); } if ((Plugins.Count > 0) || (Mods.Count > 0)) { AddUnityDebugLog(); } if (Plugins.Count > 0) { HashSet <MelonPlugin> failedPlugins = new HashSet <MelonPlugin>(); for (int i = 0; i < Plugins.Count; i++) { MelonPlugin plugin = Plugins[i]; if (plugin != null) { try { InitializeModOrPlugin(plugin); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), plugin.InfoAttribute.Name); failedPlugins.Add(plugin); } } } Plugins.RemoveAll(plugin => failedPlugins.Contains(plugin)); } if (Mods.Count > 0) { HashSet <MelonMod> failedMods = new HashSet <MelonMod>(); for (int i = 0; i < Mods.Count; i++) { MelonMod mod = Mods[i]; if (mod != null) { try { InitializeModOrPlugin(mod); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); failedMods.Add(mod); } } } Mods.RemoveAll(mod => failedMods.Contains(mod)); } if ((Plugins.Count <= 0) && (Mods.Count <= 0)) { SupportModule.Destroy(); } }
private static void LoadDLLs(bool plugins = false) { string searchdir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, (plugins ? "Plugins" : "Mods")); if (!Directory.Exists(searchdir)) { Directory.CreateDirectory(searchdir); } else { // DLL string[] files = Directory.GetFiles(searchdir, "*.dll"); if (files.Length > 0) { for (int i = 0; i < files.Count(); i++) { string file = files[i]; if (!string.IsNullOrEmpty(file)) { if (plugins) { if ((Imports.IsDevPluginsOnly() && !file.EndsWith("-dev.dll")) || (!Imports.IsDevPluginsOnly() && file.EndsWith("-dev.dll"))) { continue; } } else { if ((Imports.IsDevModsOnly() && !file.EndsWith("-dev.dll")) || (!Imports.IsDevModsOnly() && file.EndsWith("-dev.dll"))) { continue; } } try { LoadAssembly(File.ReadAllBytes(file), plugins, file); } catch (Exception e) { MelonModLogger.LogError("Unable to load " + file + ":\n" + e.ToString()); MelonModLogger.Log("------------------------------"); } } } } // ZIP string[] zippedFiles = Directory.GetFiles(searchdir, "*.zip"); if (zippedFiles.Length > 0) { for (int i = 0; i < zippedFiles.Count(); i++) { string file = zippedFiles[i]; if (!string.IsNullOrEmpty(file)) { try { using (var fileStream = File.OpenRead(file)) { using (var zipInputStream = new ZipInputStream(fileStream)) { ZipEntry entry; while ((entry = zipInputStream.GetNextEntry()) != null) { string filename = Path.GetFileName(entry.Name); if (string.IsNullOrEmpty(filename) || !filename.EndsWith(".dll")) { continue; } if (plugins) { if ((Imports.IsDevPluginsOnly() && !filename.EndsWith("-dev.dll")) || (!Imports.IsDevPluginsOnly() && filename.EndsWith("-dev.dll"))) { continue; } } else { if ((Imports.IsDevModsOnly() && !filename.EndsWith("-dev.dll")) || (!Imports.IsDevModsOnly() && filename.EndsWith("-dev.dll"))) { continue; } } using (var unzippedFileStream = new MemoryStream()) { int size = 0; byte[] buffer = new byte[4096]; while (true) { size = zipInputStream.Read(buffer, 0, buffer.Length); if (size > 0) { unzippedFileStream.Write(buffer, 0, size); } else { break; } } LoadAssembly(unzippedFileStream.ToArray(), plugins, (file + "/" + filename)); } } } } } catch (Exception e) { MelonModLogger.LogError("Unable to load " + file + ":\n" + e.ToString()); MelonModLogger.Log("------------------------------"); } } } } } }
private static void LoadMods(bool preload = false) { string modDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("Mods", (preload ? "PRELOAD" : ""))); if (!Directory.Exists(modDirectory)) { Directory.CreateDirectory(modDirectory); } else { // DLL string[] files = Directory.GetFiles(modDirectory, "*.dll", SearchOption.TopDirectoryOnly); if (files.Length > 0) { for (int i = 0; i < files.Count(); i++) { string file = files[i]; if (!string.IsNullOrEmpty(file)) { try { byte[] data = File.ReadAllBytes(file); if (data.Length > 0) { LoadAssembly(data, preload); } else { MelonModLogger.LogError("Unable to load " + file); MelonModLogger.Log("------------------------------"); } } catch (Exception e) { MelonModLogger.LogError("Unable to load " + file + ":\n" + e.ToString()); MelonModLogger.Log("------------------------------"); } } } } // ZIP /* * string[] zippedFiles = Directory.GetFiles(modDirectory, "*.zip", SearchOption.TopDirectoryOnly); * if (zippedFiles.Length > 0) * { * for (int i = 0; i < zippedFiles.Count(); i++) * { * string file = zippedFiles[i]; * if (!string.IsNullOrEmpty(file)) * { * try * { * using (var fileStream = File.OpenRead(file)) * { * using (var zipInputStream = new ZipInputStream(fileStream)) * { * ZipEntry entry; * while ((entry = zipInputStream.GetNextEntry()) != null) * { * if ((Path.GetFileName(entry.Name).Length <= 0) || !Path.GetFileName(entry.Name).EndsWith(".dll")) * continue; * * using (var unzippedFileStream = new MemoryStream()) * { * int size = 0; * byte[] buffer = new byte[4096]; * while (true) * { * size = zipInputStream.Read(buffer, 0, buffer.Length); * if (size > 0) * unzippedFileStream.Write(buffer, 0, size); * else * break; * } * LoadAssembly(unzippedFileStream.ToArray(), preload); * } * } * } * } * } * catch (Exception e) * { * MelonModLogger.LogError("Unable to load " + file + ":\n" + e.ToString()); * MelonModLogger.Log("------------------------------"); * } * } * } * } */ } }
private static void Initialize() { CurrentGameAttribute = new MelonModGameAttribute(Imports.GetCompanyName(), Imports.GetProductName()); if (Imports.IsIl2CppGame()) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); IsVRChat = CurrentGameAttribute.IsGame("VRChat", "VRChat"); IsBoneworks = CurrentGameAttribute.IsGame("Stress Level Zero", "BONEWORKS"); } if (!Imports.IsDebugMode() #if !DEBUG && Environment.CommandLine.Contains("--melonloader.console") #endif ) { MelonModLogger.consoleEnabled = true; Console.Create(); } MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Unity " + Imports.GetUnityVersion()); MelonModLogger.Log("Developer: " + CurrentGameAttribute.Developer); MelonModLogger.Log("GameName: " + CurrentGameAttribute.GameName); MelonModLogger.Log("------------------------------"); MelonModLogger.Log("Using v" + BuildInfo.Version + " Open-Beta"); MelonModLogger.Log("------------------------------"); if (Imports.IsIl2CppGame()) { MelonModLogger.Log("Initializing NET_SDK..."); NET_SDK.SDK.Initialize(); MelonModLogger.Log("------------------------------"); } bool no_mods = false; string modDirectory = Path.Combine(Environment.CurrentDirectory, "Mods"); if (!Directory.Exists(modDirectory)) { Directory.CreateDirectory(modDirectory); no_mods = true; } else { string[] files = Directory.GetFiles(modDirectory, "*.dll"); if (files.Length > 0) { foreach (string s in files) { if (!File.Exists(s) || !s.EndsWith(".dll", true, null)) { return; } try { byte[] data = File.ReadAllBytes(s); if (data.Length > 0) { Assembly a = Assembly.Load(data); if (a != null) { LoadModsFromAssembly(a); } else { MelonModLogger.LogError("Unable to load " + s); } } else { MelonModLogger.LogError("Unable to load " + s); } } catch (Exception e) { MelonModLogger.LogError("Unable to load " + s + ":\n" + e.ToString()); } MelonModLogger.Log("------------------------------"); } if (Mods.Count() <= 0) { no_mods = true; } } else { no_mods = true; } } if (no_mods) { MelonModLogger.Log("No Mods Loaded!"); MelonModLogger.Log("------------------------------"); } else { MelonModComponent.Create(); } }
private static void LoadModsFromAssembly(Assembly assembly) { MelonModInfoAttribute modInfoAttribute = assembly.GetCustomAttribute(typeof(MelonModInfoAttribute)) as MelonModInfoAttribute; if ((modInfoAttribute != null) && (modInfoAttribute.ModType != null) && modInfoAttribute.ModType.IsSubclassOf(typeof(MelonMod))) { MelonModLogger.Log(modInfoAttribute.Name + (!string.IsNullOrEmpty(modInfoAttribute.Version) ? (" v" + modInfoAttribute.Version) : "") + (!string.IsNullOrEmpty(modInfoAttribute.Author) ? (" by " + modInfoAttribute.Author) : "") + (!string.IsNullOrEmpty(modInfoAttribute.DownloadLink) ? (" (" + modInfoAttribute.DownloadLink + ")") : "")); bool should_continue = false; bool isUniversal = false; MelonModGameAttribute[] modGameAttributes = assembly.GetCustomAttributes(typeof(MelonModGameAttribute), true) as MelonModGameAttribute[]; int modGameAttributes_Count = modGameAttributes.Length; if (modGameAttributes_Count > 0) { for (int i = 0; i < modGameAttributes_Count; i++) { MelonModGameAttribute modGameAttribute = modGameAttributes[i]; if (CurrentGameAttribute.IsCompatible(modGameAttribute)) { isUniversal = CurrentGameAttribute.IsCompatibleBecauseUniversal(modGameAttribute); should_continue = true; break; } } } else { isUniversal = true; should_continue = true; } if (should_continue) { try { MelonMod modInstance = Activator.CreateInstance(modInfoAttribute.ModType) as MelonMod; if (modInstance != null) { modInstance.IsUniversal = isUniversal; modInstance.InfoAttribute = modInfoAttribute; if (modGameAttributes_Count > 0) { modInstance.GameAttributes = modGameAttributes; } else { modInstance.GameAttributes = null; } Mods.Add(modInstance); MelonModLogger.LogModStatus((modGameAttributes_Count > 0) ? (isUniversal ? 0 : 1) : 2); } else { MelonModLogger.LogError("Unable to load Mod in " + assembly.GetName() + "! Failed to Create Instance!"); } } catch (Exception e) { MelonModLogger.LogError("Unable to load Mod in " + assembly.GetName() + "! " + e.ToString()); } } else { MelonModLogger.LogModStatus(3); } } }