Example #1
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 #2
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 #3
0
        private static void LoadPluginFromAssembly(Assembly assembly, string filelocation = null)
        {
            MelonPluginInfoAttribute pluginInfoAttribute = assembly.GetCustomAttributes(false).FirstOrDefault(x => (x.GetType() == typeof(MelonPluginInfoAttribute))) as MelonPluginInfoAttribute;

            if ((pluginInfoAttribute != null) && (pluginInfoAttribute.SystemType != null) && pluginInfoAttribute.SystemType.IsSubclassOf(typeof(MelonPlugin)))
            {
                bool isCompatible = false;
                bool isUniversal  = false;
                bool hasAttribute = true;
                MelonPluginGameAttribute[] pluginGameAttributes = assembly.GetCustomAttributes(typeof(MelonPluginGameAttribute), true) as MelonPluginGameAttribute[];
                int pluginGameAttributes_Count = pluginGameAttributes.Length;
                if (pluginGameAttributes_Count > 0)
                {
                    for (int i = 0; i < pluginGameAttributes_Count; i++)
                    {
                        MelonPluginGameAttribute pluginGameAttribute = pluginGameAttributes[i];
                        if (CurrentGameAttribute.IsCompatible(pluginGameAttribute))
                        {
                            isCompatible = true;
                            isUniversal  = CurrentGameAttribute.IsCompatibleBecauseUniversal(pluginGameAttribute);
                            break;
                        }
                    }
                }
                else
                {
                    hasAttribute = false;
                }
                try
                {
                    MelonPlugin pluginInstance = Activator.CreateInstance(pluginInfoAttribute.SystemType) as MelonPlugin;
                    if (pluginInstance != null)
                    {
                        pluginInstance.InfoAttribute = pluginInfoAttribute;
                        if (pluginGameAttributes_Count > 0)
                        {
                            pluginInstance.GameAttributes = pluginGameAttributes;
                        }
                        else
                        {
                            pluginInstance.GameAttributes = null;
                        }
                        pluginInstance.Location      = filelocation;
                        pluginInstance.Compatibility = (isUniversal ? MelonBase.MelonCompatibility.UNIVERSAL : (isCompatible ? MelonBase.MelonCompatibility.COMPATIBLE : (!hasAttribute ? MelonBase.MelonCompatibility.NOATTRIBUTE : MelonBase.MelonCompatibility.INCOMPATIBLE)));
                        if (pluginInstance.Compatibility < MelonBase.MelonCompatibility.INCOMPATIBLE)
                        {
                            pluginInstance.Assembly = assembly;
                        }
                        Plugins.Add(pluginInstance);
                    }
                    else
                    {
                        MelonModLogger.LogError("Unable to load Plugin in " + assembly.GetName() + "! Failed to Create Instance!");
                    }
                }
                catch (Exception e) { MelonModLogger.LogError("Unable to load Plugin in " + assembly.GetName() + "! " + e.ToString()); }
            }
        }
Example #4
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()
#if !DEBUG
                && Imports.IsConsoleEnabled()
#endif
                )
            {
                Console.Enabled = true;
                Console.Create();
            }

            if (!Imports.IsIl2CppGame() || AssemblyGenerator.Main.Initialize())
            {
                HasGeneratedAssembly = true;
            }
            else
            {
                Imports.UNLOAD_MELONLOADER();
            }

            if (HasGeneratedAssembly)
            {
                LoadDLLs(true);
                if (Plugins.Count > 0)
                {
                    HashSet <MelonPlugin> failedPlugins = new HashSet <MelonPlugin>();
                    TempPlugins = Plugins.Where(plugin => (plugin.Compatibility < MelonBase.MelonCompatibility.INCOMPATIBLE)).ToList();
                    DependencyGraph <MelonPlugin> .TopologicalSort(TempPlugins, plugin => plugin.InfoAttribute.Name);

                    for (int i = 0; i < TempPlugins.Count; i++)
                    {
                        MelonPlugin plugin = TempPlugins[i];
                        if (plugin != null)
                        {
                            try { plugin.OnPreInitialization(); } catch (Exception ex) { MelonModLogger.LogDLLError(ex.ToString(), plugin.InfoAttribute.Name); failedPlugins.Add(plugin); }
                        }
                    }
                    TempPlugins.RemoveAll(plugin => failedPlugins.Contains(plugin));
                }
            }
        }
        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 #6
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();
            }
        }