Skip to content

Commit

Permalink
cleanup and output (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingalek authored Dec 28, 2019
1 parent 40804dd commit f5eef66
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 55 deletions.
10 changes: 0 additions & 10 deletions OWML.Common/IModBehaviour.cs

This file was deleted.

1 change: 0 additions & 1 deletion OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<Compile Include="IHarmonyHelper.cs" />
<Compile Include="IModAssets.cs" />
<Compile Include="IModAsset.cs" />
<Compile Include="IModBehaviour.cs" />
<Compile Include="IModConfig.cs" />
<Compile Include="IModEvents.cs" />
<Compile Include="IModFinder.cs" />
Expand Down
7 changes: 0 additions & 7 deletions OWML.ModHelper.Events/HarmonyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void AddPrefix<T>(string methodName, Type patchType, string patchMethodNa
var prefix = patchType.GetAnyMethod(patchMethodName);
if (prefix == null)
{
_logger.Log("prefix is null");
_console.WriteLine("prefix is null");
return;
}
Expand All @@ -33,7 +32,6 @@ public void AddPostfix<T>(string methodName, Type patchType, string patchMethodN
var postfix = patchType.GetAnyMethod(patchMethodName);
if (postfix == null)
{
_logger.Log("postfix is null");
_console.WriteLine("postfix is null");
return;
}
Expand All @@ -50,7 +48,6 @@ public void Transpile<T>(string methodName, Type patchType, string patchMethodNa
var patchMethod = patchType.GetAnyMethod(patchMethodName);
if (patchMethod == null)
{
_logger.Log("patchMethod is null");
_console.WriteLine("patchMethod is null");
return;
}
Expand All @@ -70,7 +67,6 @@ private void Patch<T>(string methodName, MethodInfo prefix, MethodInfo postfix,
}
catch (Exception ex)
{
_logger.Log($"Exception while creating harmony instance: {ex}");
_console.WriteLine($"Exception while creating harmony instance: {ex}");
return;
}
Expand All @@ -83,13 +79,11 @@ private void Patch<T>(string methodName, MethodInfo prefix, MethodInfo postfix,
}
catch (Exception ex)
{
_logger.Log($"Exception while getting method {methodName} of {targetType.Name}: {ex}");
_console.WriteLine($"Exception while getting method {methodName} of {targetType.Name}: {ex}");
return;
}
if (original == null)
{
_logger.Log("original is null");
_console.WriteLine("original is null");
return;
}
Expand All @@ -103,7 +97,6 @@ private void Patch<T>(string methodName, MethodInfo prefix, MethodInfo postfix,
}
catch (Exception ex)
{
_logger.Log($"Exception while patching {targetType.Name}: {ex}");
_console.WriteLine($"Exception while patching {targetType.Name}: {ex}");
}
}
Expand Down
1 change: 0 additions & 1 deletion OWML.ModHelper.Events/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static void SetValue(this object obj, string name, object value)
if (property != null)
{
property.SetValue(obj, value, null);
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion OWML.ModHelper/ModBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OWML.ModHelper
{
public class ModBehaviour : MonoBehaviour, IModBehaviour
public class ModBehaviour : MonoBehaviour
{
public static IModHelper ModHelper;

Expand Down
5 changes: 4 additions & 1 deletion OWML.ModHelper/ModConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ namespace OWML.ModHelper
public class ModConsole : IModConsole
{
private readonly FileStream _writer;
private readonly IModLogger _logger;

public ModConsole(IModConfig config)
public ModConsole(IModConfig config, IModLogger logger)
{
_logger = logger;
_writer = File.Open(config.OutputFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}

public void WriteLine(string s)
{
_logger.Log(s);
var bytes = Encoding.UTF8.GetBytes(s + Environment.NewLine);
_writer.Write(bytes, 0, bytes.Length);
_writer.Flush();
Expand Down
38 changes: 10 additions & 28 deletions OWML.ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,23 @@ namespace OWML.ModLoader
public class ModLoader
{
private static readonly string ConfigPath = $"{Application.dataPath}/Managed/OWML.Config.json";
private static readonly string SecondaryLogPath = $"{Application.dataPath}/Resources/OWML.Output.txt";

public static void LoadMods()
{
SecondaryLog($"In {nameof(ModLoader)}.{nameof(LoadMods)}!");
SecondaryLog("Getting config...");
var config = GetConfig();
if (config == null)
{
SecondaryLog("Config is null");
return;
}
SecondaryLog("Got config!");
SecondaryLog("Loading mods...");
try
{
var logger = new ModLogger(config);
var console = new ModConsole(config);
var harmonyHelper = new HarmonyHelper(logger, console);
var events = new ModEvents(harmonyHelper);
var modFinder = new ModFinder(config);
var owo = new Owo(modFinder, logger, console, config, harmonyHelper, events);
owo.LoadMods();
SecondaryLog("Loaded mods");
}
catch (Exception ex)
{
SecondaryLog("Error while loading mods: " + ex);
}
var logger = new ModLogger(config);
logger.Log("Got config!");
var console = new ModConsole(config, logger);
console.WriteLine("Mod loader has been initialized.");
var harmonyHelper = new HarmonyHelper(logger, console);
var events = new ModEvents(harmonyHelper);
var modFinder = new ModFinder(config);
var owo = new Owo(modFinder, logger, console, config, harmonyHelper, events);
owo.LoadMods();
}

private static IModConfig GetConfig()
Expand All @@ -49,17 +37,11 @@ private static IModConfig GetConfig()
var json = File.ReadAllText(ConfigPath);
return JsonConvert.DeserializeObject<ModConfig>(json);
}
catch (Exception ex)
catch (Exception)
{
SecondaryLog("Error while loading config: " + ex);
return null;
}
}

private static void SecondaryLog(string s)
{
File.AppendAllText(SecondaryLogPath, $"{DateTime.Now}: {s}{Environment.NewLine}");
}

}
}
18 changes: 12 additions & 6 deletions OWML.ModLoader/Owo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public Owo(IModFinder modFinder, IModLogger logger, IModConsole console, IModCon

public void LoadMods()
{
_logger.Log($"{nameof(Owo)}: {nameof(LoadMods)}");
if (_config.Verbose)
{
_console.WriteLine("Verbose mod is enabled");
Application.logMessageReceived += OnLogMessageReceived;
}
var manifests = _modFinder.GetManifests();
Expand Down Expand Up @@ -69,11 +69,18 @@ private void LoadMod(IModHelper helper)
_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);
_logger.Log("Added! Initializing...");
mod.Init(helper);
try
{
var mod = (ModBehaviour)go.AddComponent(modType);
_logger.Log("Added! Initializing...");
mod.Init(helper);
}
catch (Exception ex)
{
_console.WriteLine($"Error while adding/initializing {helper.Manifest.UniqueName}: {ex}");
return;
}
_console.WriteLine($"Loaded {helper.Manifest.UniqueName} ({helper.Manifest.Version}).");
_logger.Log($"Loaded {helper.Manifest.UniqueName} ({helper.Manifest.Version}).");
}

private Type LoadModType(IModManifest manifest)
Expand All @@ -92,7 +99,6 @@ private Type LoadModType(IModManifest manifest)
}
catch (Exception ex)
{
_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

0 comments on commit f5eef66

Please sign in to comment.