Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using UnityEngine;
- namespace PlanetbaseFramework
- {
- public class Modloader
- {
- public static List<ModBase> ModList = new List<ModBase>();
- public static void loadMods()
- {
- Debug.Log("Loading mod \"Planetbase Framework\"");
- ModBase frameworkMod = new FrameworkMod();
- ModList.Add(frameworkMod);
- frameworkMod.Init();
- Debug.Log("Loaded mod \"Planetbase Framework\"");
- if (Directory.Exists(ModBase.BasePath))
- {
- string[] files = Directory.GetFiles(ModBase.BasePath, "*.dll");
- foreach (string file in files)
- {
- Type[] types;
- try
- {
- types = Assembly.LoadFile(file).GetTypes();
- }
- catch (ReflectionTypeLoadException e)
- {
- Debug.LogException(e);
- foreach (Exception loaderException in e.LoaderExceptions)
- {
- Debug.LogException(loaderException);
- }
- continue;
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- continue;
- }
- foreach(Type type in types)
- {
- if (typeof(ModBase).IsAssignableFrom(type) && !type.IsAbstract && type.IsPublic)
- {
- Debug.Log("Loading mod \"" + type.Name.ToString() + "\" 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: " + type.Name.ToString() + ". Exception thrown:");
- Debug.Log(e.ToString() + ": " + e.Message);
- }
- if (mod != null)
- {
- try
- {
- mod.Init();
- ModList.Add(mod);
- Debug.Log("Loaded mod \"" + type.Name.ToString() + "\"");
- }
- catch (Exception e)
- {
- Debug.Log("Error initializing mod from file: " + file + " of type: " + type.Name.ToString() + ". Exception thrown:");
- Debug.Log(e.ToString() + ": " + e.Message);
- Debug.Log("Stacktrace: ");
- Debug.Log(e.StackTrace);
- }
- }
- else
- {
- Debug.Log("Failed to load mod \"" + type.Name.ToString() + "\" from file \"" + file + "\"");
- }
- }
- }
- }
- }
- else
- {
- Directory.CreateDirectory(ModBase.BasePath);
- }
- }
- public static void updateMods()
- {
- foreach(ModBase mod in ModList)
- {
- try
- {
- mod.Update();
- }
- catch (Exception e)
- {
- Debug.Log("Error updating mod " + mod.ModName + ". Exception thrown:");
- Debug.Log(e.ToString() + ": " + e.Message);
- Debug.Log("Stacktrace: ");
- Debug.Log(e.StackTrace);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement