Skip to content

Commit

Permalink
Changed the way mods are loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
solidDoWant authored and solidDoWant committed Jan 13, 2017
1 parent af4e678 commit 3af2c3a
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 8 deletions.
38 changes: 33 additions & 5 deletions ModBase.cs
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();
}
}
82 changes: 82 additions & 0 deletions Modloader.cs
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);
}
}
}
}
}
8 changes: 8 additions & 0 deletions PlanetbaseFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="GameStateTitleReplacement.cs" />
<Compile Include="ModBase.cs" />
<Compile Include="LoadGameTitleButton.cs" />
<Compile Include="Modloader.cs" />
<Compile Include="NewGameTitleButton.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QuitTitleButton.cs" />
Expand All @@ -80,6 +81,13 @@
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PostBuildEvent>copy $(TargetDir)$(TargetFileName) "T:\Steam Files\steamapps\common\Planetbase\Planetbase_Data\Managed"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
37 changes: 34 additions & 3 deletions Utils.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
using Planetbase;
using System;
using System.IO;
using UnityEngine;

namespace PlanetbaseFramework
{
public class Utils
{
public static Texture2D LoadPNG(string filePath)
public static Texture2D LoadPNG(ModBase mod, string filename)
{
string path = mod.ModName + filename;
return LoadPNGFromFile(path);
}

public static Texture2D[] LoadAllPNG(ModBase mod, string subfolder = null)
{
string[] files = null;
if(subfolder == null)
{
files = Directory.GetFiles(mod.ModPath, "*.png");
} else if(Directory.Exists(mod.ModPath + subfolder + Path.DirectorySeparatorChar.ToString()))
{
files = Directory.GetFiles(mod.ModPath + subfolder + Path.DirectorySeparatorChar.ToString());
}
else
{
Debug.Log("Could not load PNG files from invalid folder " + mod.ModPath + subfolder + Path.DirectorySeparatorChar.ToString());
throw new Exception("Could not load PNG files from invalid folder " + mod.ModPath + subfolder + Path.DirectorySeparatorChar.ToString());
}

Texture2D[] loadedFiles = new Texture2D[files.Length];
for(int i = 0; i < files.Length; i++)
{
loadedFiles[i] = LoadPNG(mod, subfolder + Path.DirectorySeparatorChar.ToString() + files[i]);
}

return loadedFiles;
}

public static Texture2D LoadPNGFromFile(string AbsolutePath)
{
Texture2D tex = null;
byte[] fileData;

if (File.Exists(filePath))
if (File.Exists(AbsolutePath))
{
fileData = File.ReadAllBytes(filePath);
fileData = File.ReadAllBytes(AbsolutePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}
Expand Down

0 comments on commit 3af2c3a

Please sign in to comment.