From 3af2c3a0db24c9c66656956cdaee1c95c0dc8ee4 Mon Sep 17 00:00:00 2001 From: solidDoWant Date: Thu, 12 Jan 2017 23:37:23 -0600 Subject: [PATCH] Changed the way mods are loaded --- ModBase.cs | 38 +++++++++++++++--- Modloader.cs | 82 ++++++++++++++++++++++++++++++++++++++ PlanetbaseFramework.csproj | 8 ++++ Utils.cs | 37 +++++++++++++++-- 4 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 Modloader.cs diff --git a/ModBase.cs b/ModBase.cs index 851db59..fc3ab55 100644 --- a/ModBase.cs +++ b/ModBase.cs @@ -1,20 +1,48 @@ using Planetbase; +using System; using System.IO; +using UnityEngine; namespace PlanetbaseFramework { public abstract class ModBase { - public ModBase() + public ModBase(string ModName) { - StringList.loadStringsFromFolder(Util.getFilesFolder() + Path.DirectorySeparatorChar.ToString() + "Mods" + Path.DirectorySeparatorChar.ToString() + GetModName(), GetModName(), StringList.mStrings); - UnityEngine.Debug.Log((object)"Loading " + GetModName()); + this.ModName = ModName; + try + { + StringList.loadStringsFromFolder(this.ModPath, this.ModName, StringList.mStrings); + } + catch (Exception e) + { + Debug.Log("Couldn't load string file for the mod " + this.ModName + " because of exception: " + e.ToString() + ": " + e.Message); + } + } + + public string ModName + { + get; set; + } + + public static string BasePath + { + get + { + return Util.getFilesFolder() + Path.DirectorySeparatorChar.ToString() + "Mods" + Path.DirectorySeparatorChar.ToString(); + } + } + + public string ModPath + { + get + { + return BasePath + this.ModName + Path.DirectorySeparatorChar.ToString(); + } } public abstract void Init(); public abstract void Update(); - - public abstract string GetModName(); } } diff --git a/Modloader.cs b/Modloader.cs new file mode 100644 index 0000000..890731d --- /dev/null +++ b/Modloader.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace PlanetbaseFramework +{ + public class Modloader + { + public static List modList = new List(); + public static void loadMods() + { + if (Directory.Exists(ModBase.BasePath)) + { + string[] files = Directory.GetFiles(ModBase.BasePath, "*.dll"); + foreach (string file in files) + { + Type[] types = Assembly.LoadFile(file).GetTypes(); + foreach(Type type in types) + { + if (typeof(ModBase).IsAssignableFrom(type)) + { + 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); + } + } + 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); + } + } + } + } +} diff --git a/PlanetbaseFramework.csproj b/PlanetbaseFramework.csproj index ded0eac..e6b111c 100644 --- a/PlanetbaseFramework.csproj +++ b/PlanetbaseFramework.csproj @@ -68,6 +68,7 @@ + @@ -80,6 +81,13 @@ + + + + + + copy $(TargetDir)$(TargetFileName) "T:\Steam Files\steamapps\common\Planetbase\Planetbase_Data\Managed" +