Skip to content

Commit

Permalink
Storage (#30)
Browse files Browse the repository at this point in the history
* storage helper (save/load data)
* no longer needed to pass modbehaviour to asset helper
  • Loading branch information
amazingalek authored Dec 27, 2019
1 parent fd34e32 commit 29c701b
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 135 deletions.
8 changes: 4 additions & 4 deletions OWML.Common/IModAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace OWML.Common
{
public interface IModAssets
{
IModAsset<GameObject> Load3DObject(IModBehaviour modBehaviour, string objectFilename, string imageFilename);
IModAsset<MeshFilter> LoadMesh(IModBehaviour modBehaviour, string objectFilename);
IModAsset<MeshRenderer> LoadTexture(IModBehaviour modBehaviour, string imageFilename);
IModAsset<AudioSource> LoadAudio(IModBehaviour modBehaviour, string audioFilename);
IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename);
IModAsset<MeshFilter> LoadMesh(string objectFilename);
IModAsset<MeshRenderer> LoadTexture(string imageFilename);
IModAsset<AudioSource> LoadAudio(string audioFilename);
}
}
1 change: 0 additions & 1 deletion OWML.Common/IModBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace OWML.Common
{
public interface IModBehaviour
{
IModManifest ModManifest { get; set; }
Coroutine StartCoroutine(IEnumerator routine);
}
}
2 changes: 2 additions & 0 deletions OWML.Common/IModHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public interface IModHelper
IModEvents Events { get; }
IHarmonyHelper HarmonyHelper { get; }
IModAssets Assets { get; }
IModStorage Storage { get; }
IModManifest Manifest { get; }
}
}
8 changes: 8 additions & 0 deletions OWML.Common/IModStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OWML.Common
{
public interface IModStorage
{
T Load<T>(string filename);
void Save<T>(T obj, string filename);
}
}
1 change: 1 addition & 0 deletions OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="IModConsole.cs" />
<Compile Include="IModLogger.cs" />
<Compile Include="IModManifest.cs" />
<Compile Include="IModStorage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
32 changes: 17 additions & 15 deletions OWML.ModHelper.Assets/ModAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,60 @@ namespace OWML.ModHelper.Assets
public class ModAssets : IModAssets
{
private readonly IModConsole _console;
private readonly IModManifest _manifest;
private readonly ObjImporter _objImporter;

public ModAssets(IModConsole console)
public ModAssets(IModConsole console, IModManifest manifest)
{
_console = console;
_manifest = manifest;
_objImporter = new ObjImporter();
}

public IModAsset<GameObject> Load3DObject(IModBehaviour modBehaviour, string objectFilename, string imageFilename)
public IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename)
{
var objectPath = modBehaviour.ModManifest.FolderPath + objectFilename;
var imagePath = modBehaviour.ModManifest.FolderPath + imageFilename;
var objectPath = _manifest.FolderPath + objectFilename;
var imagePath = _manifest.FolderPath + imageFilename;
_console.WriteLine("Loading object from " + objectPath);

var go = new GameObject();
var modAsset = go.AddComponent<ObjectAsset>();

modBehaviour.StartCoroutine(LoadMesh(modAsset, objectPath));
modBehaviour.StartCoroutine(LoadTexture(modAsset, imagePath));
modAsset.StartCoroutine(LoadMesh(modAsset, objectPath));
modAsset.StartCoroutine(LoadTexture(modAsset, imagePath));

return modAsset;
}

public IModAsset<MeshFilter> LoadMesh(IModBehaviour modBehaviour, string objectFilename)
public IModAsset<MeshFilter> LoadMesh(string objectFilename)
{
var objectPath = modBehaviour.ModManifest.FolderPath + objectFilename;
var objectPath = _manifest.FolderPath + objectFilename;
_console.WriteLine("Loading mesh from " + objectPath);

var go = new GameObject();
var modAsset = go.AddComponent<MeshAsset>();

modBehaviour.StartCoroutine(LoadMesh(modAsset, objectPath));
modAsset.StartCoroutine(LoadMesh(modAsset, objectPath));

return modAsset;
}

public IModAsset<MeshRenderer> LoadTexture(IModBehaviour modBehaviour, string imageFilename)
public IModAsset<MeshRenderer> LoadTexture(string imageFilename)
{
var imagePath = modBehaviour.ModManifest.FolderPath + imageFilename;
var imagePath = _manifest.FolderPath + imageFilename;
_console.WriteLine("Loading texture from " + imagePath);

var go = new GameObject();
var modAsset = go.AddComponent<TextureAsset>();

modBehaviour.StartCoroutine(LoadTexture(modAsset, imagePath));
modAsset.StartCoroutine(LoadTexture(modAsset, imagePath));

return modAsset;
}

public IModAsset<AudioSource> LoadAudio(IModBehaviour modBehaviour, string audioFilename)
public IModAsset<AudioSource> LoadAudio(string audioFilename)
{
var audioPath = modBehaviour.ModManifest.FolderPath + audioFilename;
var audioPath = _manifest.FolderPath + audioFilename;
_console.WriteLine("Loading audio from " + audioPath);

var go = new GameObject();
Expand All @@ -68,7 +70,7 @@ public IModAsset<AudioSource> LoadAudio(IModBehaviour modBehaviour, string audio
var loadAudioFrom = audioFilename.EndsWith(".mp3")
? LoadAudioFromMp3(modAsset, audioPath)
: LoadAudioFromWav(modAsset, audioPath);
modBehaviour.StartCoroutine(loadAudioFrom);
modAsset.StartCoroutine(loadAudioFrom);

return modAsset;
}
Expand Down
4 changes: 1 addition & 3 deletions OWML.ModHelper/ModBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ namespace OWML.ModHelper
public class ModBehaviour : MonoBehaviour, IModBehaviour
{
public static IModHelper ModHelper;
public IModManifest ModManifest { get; set; }

public void Init(IModHelper modHelper, IModManifest manifest)
public void Init(IModHelper modHelper)
{
ModHelper = modHelper;
ModManifest = manifest;
DontDestroyOnLoad(gameObject);
}

Expand Down
6 changes: 5 additions & 1 deletion OWML.ModHelper/ModHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ public class ModHelper : IModHelper
public IModEvents Events { get; }
public IHarmonyHelper HarmonyHelper { get; }
public IModAssets Assets { get; }
public IModStorage Storage { get; }
public IModManifest Manifest { get; }

public ModHelper(IModConfig config, IModLogger logger, IModConsole console, IModEvents events, IHarmonyHelper harmonyHelper, IModAssets assets)
public ModHelper(IModConfig config, IModLogger logger, IModConsole console, IModEvents events, IHarmonyHelper harmonyHelper, IModAssets assets, IModStorage storage, IModManifest manifest)
{
Config = config;
Logger = logger;
Console = console;
Events = events;
HarmonyHelper = harmonyHelper;
Assets = assets;
Storage = storage;
Manifest = manifest;
}

}
Expand Down
30 changes: 30 additions & 0 deletions OWML.ModHelper/ModStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.IO;
using Newtonsoft.Json;
using OWML.Common;

namespace OWML.ModHelper
{
public class ModStorage : IModStorage
{
private readonly IModManifest _manifest;

public ModStorage(IModManifest manifest)
{
_manifest = manifest;
}

public T Load<T>(string filename)
{
var path = _manifest.FolderPath + filename;
var json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<T>(json);
}

public void Save<T>(T obj, string filename)
{
var path = _manifest.FolderPath + filename;
var json = JsonConvert.SerializeObject(obj);
File.WriteAllText(path, json);
}
}
}
1 change: 1 addition & 0 deletions OWML.ModHelper/OWML.ModHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="ModHelper.cs" />
<Compile Include="ModLogger.cs" />
<Compile Include="ModManifest.cs" />
<Compile Include="ModStorage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions OWML.ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using OWML.Common;
using OWML.ModHelper;
using OWML.ModHelper.Assets;
using OWML.ModHelper.Events;
using UnityEngine;

Expand Down Expand Up @@ -32,10 +31,8 @@ public static void LoadMods()
var console = new ModConsole(config);
var harmonyHelper = new HarmonyHelper(logger, console);
var events = new ModEvents(harmonyHelper);
var assets = new ModAssets(console);
var helper = new ModHelper.ModHelper(config, logger, console, events, harmonyHelper, assets);
var modFinder = new ModFinder(config);
var owo = new Owo(helper, modFinder);
var owo = new Owo(modFinder, logger, console, config, harmonyHelper, events);
owo.LoadMods();
SecondaryLog("Loaded mods");
}
Expand Down
63 changes: 40 additions & 23 deletions OWML.ModLoader/Owo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,97 @@
using System.Reflection;
using OWML.Common;
using OWML.ModHelper;
using OWML.ModHelper.Assets;
using UnityEngine;

namespace OWML.ModLoader
{
internal class Owo
{
private readonly IModHelper _helper;
private readonly IModFinder _modFinder;
private readonly IModLogger _logger;
private readonly IModConsole _console;
private readonly IModConfig _config;
private readonly IHarmonyHelper _harmonyHelper;
private readonly IModEvents _events;

public Owo(IModHelper helper, IModFinder modFinder)
public Owo(IModFinder modFinder, IModLogger logger, IModConsole console, IModConfig config, IHarmonyHelper harmonyHelper, IModEvents events)
{
_helper = helper;
_modFinder = modFinder;
_logger = logger;
_console = console;
_config = config;
_harmonyHelper = harmonyHelper;
_events = events;
}

public void LoadMods()
{
_helper.Logger.Log($"{nameof(Owo)}: {nameof(LoadMods)}");
if (_helper.Config.Verbose)
_logger.Log($"{nameof(Owo)}: {nameof(LoadMods)}");
if (_config.Verbose)
{
Application.logMessageReceived += OnLogMessageReceived;
}
var manifests = _modFinder.GetManifests();
foreach (var manifest in manifests)
{
LoadMod(manifest);
var helper = CreateModHelper(manifest);
LoadMod(helper);
}
}

private IModHelper CreateModHelper(IModManifest manifest)
{
var assets = new ModAssets(_console, manifest);
var storage = new ModStorage(manifest);
return new ModHelper.ModHelper(_config, _logger, _console, _events, _harmonyHelper, assets, storage, manifest);
}

private void OnLogMessageReceived(string message, string stackTrace, LogType type)
{
_helper.Logger.Log($"{type}: {message}");
_logger.Log($"{type}: {message}");
if (type == LogType.Error || type == LogType.Exception)
{
_helper.Console.WriteLine($"{type}: {message}");
_console.WriteLine($"{type}: {message}");
}
}

private void LoadMod(IModManifest manifest)
private void LoadMod(IModHelper helper)
{
var modType = LoadModType(manifest);
var modType = LoadModType(helper.Manifest);
if (modType == null)
{
_helper.Logger.Log("Mod type is null, skipping");
_logger.Log("Mod type is null, skipping");
return;
}
_helper.Logger.Log($"Loading {manifest.UniqueName} ({manifest.Version})...");
_helper.Logger.Log("Adding mod behaviour...");
var go = new GameObject(manifest.UniqueName);
_logger.Log($"Loading {helper.Manifest.UniqueName} ({helper.Manifest.Version})...");
_logger.Log("Adding mod behaviour...");
var go = new GameObject(helper.Manifest.UniqueName);
var mod = (ModBehaviour)go.AddComponent(modType);
_helper.Logger.Log("Added! Initializing...");
mod.Init(_helper, manifest);
_helper.Console.WriteLine($"Loaded {manifest.UniqueName} ({manifest.Version}).");
_helper.Logger.Log($"Loaded {manifest.UniqueName} ({manifest.Version}).");
_logger.Log("Added! Initializing...");
mod.Init(helper);
_console.WriteLine($"Loaded {helper.Manifest.UniqueName} ({helper.Manifest.Version}).");
_logger.Log($"Loaded {helper.Manifest.UniqueName} ({helper.Manifest.Version}).");
}

private Type LoadModType(IModManifest manifest)
{
if (!manifest.Enabled)
{
_helper.Logger.Log($"{manifest.UniqueName} is disabled");
_logger.Log($"{manifest.UniqueName} is disabled");
return null;
}
_helper.Logger.Log("Loading assembly: " + manifest.AssemblyPath);
_logger.Log("Loading assembly: " + manifest.AssemblyPath);
var assembly = Assembly.LoadFile(manifest.AssemblyPath);
_helper.Logger.Log($"Loaded {assembly.FullName}");
_logger.Log($"Loaded {assembly.FullName}");
try
{
return assembly.GetTypes().FirstOrDefault(x => x.IsSubclassOf(typeof(ModBehaviour)));
}
catch (Exception ex)
{
_helper.Logger.Log($"Error while trying to get {typeof(ModBehaviour)}: {ex.Message}");
_helper.Console.WriteLine($"Error while trying to get {typeof(ModBehaviour)}: {ex.Message}");
_logger.Log($"Error while trying to get {typeof(ModBehaviour)}: {ex.Message}");
_console.WriteLine($"Error while trying to get {typeof(ModBehaviour)}: {ex.Message}");
return null;
}
}
Expand Down
4 changes: 4 additions & 0 deletions OWML.Nuget/OWML.Nuget.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@
<None Include="OWML.Nuget.nuspec" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
Loading

0 comments on commit 29c701b

Please sign in to comment.