Example #1
0
 public static void OnApplicationQuit()
 {
     if (Plugins.Count > 0)
     {
         for (int i = 0; i < Plugins.Count; i++)
         {
             MelonPlugin plugin = Plugins[i];
             if (plugin != null)
             {
                 try { plugin.OnApplicationQuit(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), plugin.InfoAttribute.Name); }
             }
         }
     }
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnApplicationQuit(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
     if ((Plugins.Count > 0) || (Mods.Count() > 0))
     {
         ModPrefs.SaveConfig();
     }
     Harmony.HarmonyInstance.UnpatchAllInstances();
     Imports.UNLOAD_MELONLOADER();
     if (Imports.IsQuitFix())
     {
         Process.GetCurrentProcess().Kill();
     }
 }
Example #2
0
 public static void OnModSettingsApplied()
 {
     if (Plugins.Count > 0)
     {
         for (int i = 0; i < Plugins.Count; i++)
         {
             MelonPlugin plugin = Plugins[i];
             if (plugin != null)
             {
                 try { plugin.OnModSettingsApplied(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), plugin.InfoAttribute.Name); }
             }
         }
     }
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnModSettingsApplied(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
 }
Example #3
0
        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("------------------------------");
            }
        }
Example #4
0
        private static void LoadModFromAssembly(Assembly assembly, string filelocation = null)
        {
            MelonModInfoAttribute modInfoAttribute = assembly.GetCustomAttributes(false).FirstOrDefault(x => (x.GetType() == typeof(MelonModInfoAttribute))) as MelonModInfoAttribute;

            if ((modInfoAttribute != null) && (modInfoAttribute.SystemType != null) && modInfoAttribute.SystemType.IsSubclassOf(typeof(MelonMod)))
            {
                bool isCompatible = false;
                bool isUniversal  = false;
                bool hasAttribute = true;
                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))
                        {
                            isCompatible = true;
                            isUniversal  = CurrentGameAttribute.IsCompatibleBecauseUniversal(modGameAttribute);
                            break;
                        }
                    }
                }
                else
                {
                    hasAttribute = false;
                }
                try
                {
                    MelonMod modInstance = Activator.CreateInstance(modInfoAttribute.SystemType) as MelonMod;
                    if (modInstance != null)
                    {
                        modInstance.InfoAttribute = modInfoAttribute;
                        if (modGameAttributes_Count > 0)
                        {
                            modInstance.GameAttributes = modGameAttributes;
                        }
                        else
                        {
                            modInstance.GameAttributes = null;
                        }
                        modInstance.Location      = filelocation;
                        modInstance.Compatibility = (isUniversal ? MelonBase.MelonCompatibility.UNIVERSAL : (isCompatible ? MelonBase.MelonCompatibility.COMPATIBLE : (!hasAttribute ? MelonBase.MelonCompatibility.NOATTRIBUTE : MelonBase.MelonCompatibility.INCOMPATIBLE)));
                        if (modInstance.Compatibility < MelonBase.MelonCompatibility.INCOMPATIBLE)
                        {
                            modInstance.Assembly = assembly;
                        }
                        Mods.Add(modInstance);
                    }
                    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()); }
            }
        }
Example #5
0
 internal static void OnLevelWasInitialized(int level)
 {
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnLevelWasInitialized(level); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
 }
Example #6
0
 public static void OnGUI()
 {
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnGUI(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
 }
Example #7
0
 internal static void OnModSettingsApplied()
 {
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnModSettingsApplied(); } catch (Exception ex) { MelonModLogger.LogModError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
 }
Example #8
0
 private static void OnApplicationQuit()
 {
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnApplicationQuit(); } catch (Exception ex) { MelonModLogger.LogModError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
     ModPrefs.SaveConfig();
     Harmony.HarmonyInstance.UnpatchAllInstances();
 }
Example #9
0
        private static void Initialize()
        {
            if (string.IsNullOrEmpty(AppDomain.CurrentDomain.BaseDirectory))
            {
                ((AppDomainSetup)typeof(AppDomain).GetProperty("SetupInformationNoCopy", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(AppDomain.CurrentDomain, new object[0])).ApplicationBase = Imports.GetGameDirectory();
            }
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;

            CurrentGameAttribute = new MelonModGameAttribute(Imports.GetCompanyName(), Imports.GetProductName());
            UnityVersion         = GetUnityFileVersion();

            if (Imports.IsIl2CppGame())
            {
                IsVRChat    = CurrentGameAttribute.IsGame("VRChat", "VRChat");
                IsBoneworks = CurrentGameAttribute.IsGame("Stress Level Zero", "BONEWORKS");
            }

            if (!Imports.IsDebugMode())
            {
                Console.Enabled = true;
                Console.Create();
            }

            if (Imports.IsIl2CppGame() && !AssemblyGenerator.Main.Initialize())
            {
                Imports.UNLOAD_MELONLOADER(true);
            }
            else
            {
                LoadMods(true);
                if (Mods.Count > 0)
                {
                    for (int i = 0; i < Mods.Count; i++)
                    {
                        MelonMod mod = Mods[i];
                        if (mod != null)
                        {
                            try { mod.OnPreInitialization(); } catch (Exception ex) { MelonModLogger.LogModError(ex.ToString(), mod.InfoAttribute.Name); }
                        }
                    }
                }
            }
        }
Example #10
0
 public static void OnUpdate()
 {
     SceneHandler.CheckForSceneChange();
     if (Imports.IsIl2CppGame() && IsVRChat)
     {
         VRChat_CheckUiManager();
     }
     if (Mods.Count() > 0)
     {
         for (int i = 0; i < Mods.Count; i++)
         {
             MelonMod mod = Mods[i];
             if (mod != null)
             {
                 try { mod.OnUpdate(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); }
             }
         }
     }
 }
Example #11
0
        internal static string GetNameSection()
        {
            StackTrace st = new StackTrace(2, true);
            StackFrame sf = st.GetFrame(0);

            if (sf != null)
            {
                MethodBase method = sf.GetMethod();
                if (!method.Equals(null))
                {
                    Type methodClassType = method.DeclaringType;
                    if (!methodClassType.Equals(null))
                    {
                        Assembly asm = methodClassType.Assembly;
                        if (!asm.Equals(null))
                        {
                            MelonPlugin plugin = MelonHandler.Plugins.Find(x => (x.Assembly == asm));
                            if (plugin != null)
                            {
                                if (!string.IsNullOrEmpty(plugin.Info.Name))
                                {
                                    return("[" + plugin.Info.Name.Replace(" ", "_") + "] ");
                                }
                            }
                            else
                            {
                                MelonMod mod = MelonHandler.Mods.Find(x => (x.Assembly == asm));
                                if (mod != null)
                                {
                                    if (!string.IsNullOrEmpty(mod.Info.Name))
                                    {
                                        return("[" + mod.Info.Name.Replace(" ", "_") + "] ");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return("");
        }
Example #12
0
 private static void VRChat_CheckUiManager()
 {
     if (ShouldCheckForUiManager)
     {
         if (VRCUiManager == null)
         {
             VRCUiManager = Assembly_CSharp.GetType("VRCUiManager");
         }
         if (VRCUiManager != null)
         {
             if (VRCUiManager_Instance == null)
             {
                 VRCUiManager_Instance = VRCUiManager.GetProperty("field_Protected_Static_VRCUiManager_0");
             }
             if (VRCUiManager_Instance != null)
             {
                 object returnval = VRCUiManager_Instance.GetValue(null, new object[0]);
                 if (returnval != null)
                 {
                     ShouldCheckForUiManager = false;
                     if (Mods.Count() > 0)
                     {
                         for (int i = 0; i < Mods.Count; i++)
                         {
                             MelonMod mod = Mods[i];
                             if (mod != null)
                             {
                                 try { mod.VRChat_OnUiManagerInit(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), mod.InfoAttribute.Name); }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #13
0
        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();
            }
        }
Example #14
0
        private static void LoadModFromAssembly(Assembly assembly, bool isPlugin = false)
        {
            MelonModInfoAttribute modInfoAttribute = assembly.GetCustomAttributes(false).FirstOrDefault(x => (x.GetType() == typeof(MelonModInfoAttribute))) as MelonModInfoAttribute;

            if ((modInfoAttribute != null) && (modInfoAttribute.ModType != null) && modInfoAttribute.ModType.IsSubclassOf(typeof(MelonMod)))
            {
                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.IsPlugin      = isPlugin;
                            modInstance.InfoAttribute = modInfoAttribute;
                            if (modGameAttributes_Count > 0)
                            {
                                modInstance.GameAttributes = modGameAttributes;
                            }
                            else
                            {
                                modInstance.GameAttributes = null;
                            }
                            modInstance.ModAssembly = assembly;
                            Harmony.HarmonyInstance.Create(assembly.FullName).PatchAll(assembly);
                            Mods.Add(modInstance);
                        }
                        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);
                }
            }
        }
Example #15
0
        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);
                }
            }
        }