-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
solidDoWant
authored and
solidDoWant
committed
Jan 13, 2017
1 parent
af4e678
commit 3af2c3a
Showing
4 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ModBase> modList = new List<ModBase>(); | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters