Example #1
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 #2
0
        private 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))
                        {
                            object[] attrArray = asm.GetCustomAttributes(typeof(MelonPluginInfoAttribute), false);
                            if ((attrArray.Count() > 0) && (attrArray[0] != null))
                            {
                                MelonPluginInfoAttribute attr = attrArray[0] as MelonPluginInfoAttribute;
                                if (!string.IsNullOrEmpty(attr.Name))
                                {
                                    return("[" + attr.Name.Replace(" ", "_") + "] ");
                                }
                            }
                            attrArray = asm.GetCustomAttributes(typeof(MelonModInfoAttribute), false);
                            if ((attrArray.Count() > 0) && (attrArray[0] != null))
                            {
                                MelonModInfoAttribute attr = attrArray[0] as MelonModInfoAttribute;
                                if (!string.IsNullOrEmpty(attr.Name))
                                {
                                    return("[" + attr.Name.Replace(" ", "_") + "] ");
                                }
                            }
                        }
                    }
                }
            }
            return("");
        }