Beispiel #1
0
        protected static bool InitializeMod(ModBase mod)
        {
            try
            {
                mod.Init();
                ModList.Add(mod);
                Debug.Log($"Loaded mod \"{mod.ModName}\"");
            }
            catch (Exception e)
            {
                Debug.Log($"Error initializing mod \"{mod.ModName}\"");
                Utils.LogException(e);
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Called by the game manager on startup to load in mods
        /// </summary>
        public static void LoadMods()
        {
            Debug.Log("Loading mods...");

            var modDLLs = new List <string>();

            modDLLs.Add(Assembly.GetExecutingAssembly().Location);

            if (Directory.Exists(ModBase.BasePath))
            {
                modDLLs.AddRange(Directory.GetFiles(ModBase.BasePath, "*.dll"));
            }
            else
            {
                Debug.Log($"Mod directory does not exist, creating at \"{ModBase.BasePath}\"");
                Directory.CreateDirectory(ModBase.BasePath);

                //Create the planetbase mod folder and extract the assets
            }

            Debug.Log($"Found {modDLLs.Count} mods");

            foreach (var file in modDLLs)
            {
                Type[] types;
                try
                {
                    types = Assembly.LoadFile(file).GetTypes();
                }
                catch (ReflectionTypeLoadException e)
                {
                    Utils.LogException(e);
                    foreach (var loaderException in e.LoaderExceptions)
                    {
                        Utils.LogException(loaderException);
                    }

                    Debug.Log(
                        "************************ Note to modders: If you're seeing this exception, you probably are using a a post .Net 2.0.5.0 function.\r\n" +
                        "For convenience I've made it so you can use mods compiled after 2.0.5.0, however modern features are not available. ************************"
                        );

                    continue;
                }
                catch (Exception e)
                {
                    Utils.LogException(e);
                    continue;
                }

                foreach (var type in types)
                {
                    //Skip if the type isn't a mod
                    if (!typeof(ModBase).IsAssignableFrom(type) || type.IsAbstract || !type.IsPublic ||
                        Attribute.IsDefined(type, typeof(ModLoaderIgnoreAttribute)))
                    {
                        continue;
                    }

                    var typeName = type.Name;

                    Debug.Log($"Loading mod \"{typeName}\" from file \"{file}\"");

                    ModBase mod = null;
                    try
                    {
                        mod = Activator.CreateInstance(type) as ModBase;
                    }
                    catch (Exception e)
                    {
                        Debug.Log(
                            $"Error loading mod from file: \"{file}\" of type: \"{typeName}\". Exception thrown:");
                        Utils.LogException(e);
                    }

                    if (mod != null)
                    {
                        var modName = mod.ModName;

                        try
                        {
                            mod.Init();
                            ModList.Add(mod);
                            Debug.Log($"Loaded mod \"{modName}\"");
                        }
                        catch (Exception e)
                        {
                            Debug.Log(
                                $"Error initializing mod \"{modName}\" from file: \"{file}\" of type: {typeName}"
                                );
                            Utils.LogException(e);
                        }
                    }
                    else
                    {
                        Debug.Log($"Failed to load mod \"{typeName}\" from file \"{file}\"");
                    }
                }
            }

            Debug.Log($"Successfully loaded {modDLLs.Count} mods");
        }