Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from EIA485/dev
Browse files Browse the repository at this point in the history
add support for objects and multiple objects to logger 
and fix for neos version 2021.10.17.1326
  • Loading branch information
EIA485 authored Oct 17, 2021
2 parents 8425bd3 + 134c0be commit d763495
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 189 deletions.
91 changes: 46 additions & 45 deletions NeosModLoader/ExecutionHook.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
using FrooxEngine;
using System;

namespace NeosModLoader
{
[ImplementableClass(true)]
class ExecutionHook
{
#pragma warning disable CS0169
// field must exist due to reflective access
private static Type __connectorType;
#pragma warning restore CS0169

static ExecutionHook()
{
try
{
Logger.MsgInternal($"NeosModLoader v{ModLoader.VERSION} starting up!{(Configuration.get().Debug ? " Debug logs will be shown." : "")}");
NeosVersionReset.Initialize();
ModLoader.LoadMods();
}
catch (Exception e) // it's important that this doesn't send exceptions back to Neos
{
Logger.ErrorInternal($"Exception in execution hook!\n{e}");
}
}

// implementation not strictly required, but method must exist due to reflective access
private static DummyConnector InstantiateConnector()
{
return new DummyConnector();
}

// type must match return type of InstantiateConnector()
private class DummyConnector : IConnector
{
public IImplementable Owner { get; private set; }
public void ApplyChanges() { }
public void AssignOwner(IImplementable owner) => Owner = owner;
public void Destroy(bool destroyingWorld) { }
public void Initialize() { }
public void RemoveOwner() => Owner = null;
}
}
}
using FrooxEngine;
using System;

namespace NeosModLoader
{
[ImplementableClass(true)]
class ExecutionHook
{
#pragma warning disable CS0169
// field must exist due to reflective access
private static Type __connectorType;
private static Type __connectorTypes;
#pragma warning restore CS0169

static ExecutionHook()
{
try
{
Logger.MsgInternal($"NeosModLoader v{ModLoader.VERSION} starting up!{(Configuration.get().Debug ? " Debug logs will be shown." : "")}");
NeosVersionReset.Initialize();
ModLoader.LoadMods();
}
catch (Exception e) // it's important that this doesn't send exceptions back to Neos
{
Logger.ErrorInternal($"Exception in execution hook!\n{e}");
}
}

// implementation not strictly required, but method must exist due to reflective access
private static DummyConnector InstantiateConnector()
{
return new DummyConnector();
}

// type must match return type of InstantiateConnector()
private class DummyConnector : IConnector
{
public IImplementable Owner { get; private set; }
public void ApplyChanges() { }
public void AssignOwner(IImplementable owner) => Owner = owner;
public void Destroy(bool destroyingWorld) { }
public void Initialize() { }
public void RemoveOwner() => Owner = null;
}
}
}
221 changes: 127 additions & 94 deletions NeosModLoader/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,127 @@
using BaseX;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace NeosModLoader
{
internal class Logger
{
internal static void DebugExternal(string message)
{
if (Configuration.get().Debug)
{
LogInternal(LogType.DEBUG, message, SourceFromStackTrace());
}
}

internal static void DebugInternal(string message)
{
if (Configuration.get().Debug)
{
LogInternal(LogType.DEBUG, message);
}
}

internal static void MsgExternal(string message)
{
LogInternal(LogType.INFO, message, SourceFromStackTrace());
}

internal static void MsgInternal(string message)
{
LogInternal(LogType.INFO, message);
}

internal static void WarnExternal(string message)
{
LogInternal(LogType.WARN, message, SourceFromStackTrace());
}

internal static void WarnInternal(string message)
{
LogInternal(LogType.WARN, message);
}

internal static void ErrorExternal(string message)
{
LogInternal(LogType.ERROR, message, SourceFromStackTrace());
}

internal static void ErrorInternal(string message)
{
LogInternal(LogType.ERROR, message);
}

private static void LogInternal(string logTypePrefix, string message, string source = null)
{
if (source == null)
{
UniLog.Log($"{logTypePrefix}[NeosModLoader] {message}");
}
else
{
UniLog.Log($"{logTypePrefix}[NeosModLoader/{source}] {message}");
}
}

private static string SourceFromStackTrace()
{
Dictionary<Assembly, NeosMod> loadedMods = ModLoader.LoadedMods;
// skip three frames: SourceFromStackTrace(), MsgExternal(), Msg()
StackTrace stackTrace = new StackTrace(3);
for (int i = 0; i < stackTrace.FrameCount; i++)
{
Assembly assembly = stackTrace.GetFrame(i).GetMethod().DeclaringType.Assembly;
NeosMod mod;
if (loadedMods.TryGetValue(assembly, out mod))
{
return mod.Name;
}
}
return null;
}

private sealed class LogType
{
public readonly static string DEBUG = "[DEBUG]";
public readonly static string INFO = "[INFO] ";
public readonly static string WARN = "[WARN] ";
public readonly static string ERROR = "[ERROR]";
}
}
}
using BaseX;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace NeosModLoader
{
internal class Logger
{
internal static void DebugExternal(string message)
{
if (Configuration.get().Debug)
{
LogInternal(LogType.DEBUG, message, SourceFromStackTrace());
}
}

internal static void DebugInternal(string message)
{
if (Configuration.get().Debug)
{
LogInternal(LogType.DEBUG, message);
}
}
internal static void DebugList(object[] messages)
{
string Source = SourceFromStackTrace();
foreach (object element in messages)
{
LogInternal(LogType.DEBUG, element.ToString(), Source);
}
}

internal static void MsgExternal(string message)
{
LogInternal(LogType.INFO, message, SourceFromStackTrace());
}

internal static void MsgInternal(string message)
{
LogInternal(LogType.INFO, message);
}
internal static void MsgList(object[] messages)
{
string Source = SourceFromStackTrace();
foreach (object element in messages)
{
LogInternal(LogType.INFO, element.ToString(), Source);
}
}

internal static void WarnExternal(string message)
{
LogInternal(LogType.WARN, message, SourceFromStackTrace());
}

internal static void WarnInternal(string message)
{
LogInternal(LogType.WARN, message);
}
internal static void WarnList(object[] messages)
{
string Source = SourceFromStackTrace();
foreach (object element in messages)
{
LogInternal(LogType.WARN, element.ToString(), Source);
}
}

internal static void ErrorExternal(string message)
{
LogInternal(LogType.ERROR, message, SourceFromStackTrace());
}

internal static void ErrorInternal(string message)
{
LogInternal(LogType.ERROR, message);
}

internal static void ErrorList(object[] messages)
{
string Source = SourceFromStackTrace();
foreach (object element in messages)
{
LogInternal(LogType.ERROR, element.ToString(), Source);
}
}

private static void LogInternal(string logTypePrefix, string message, string source = null)
{
if (source == null)
{
UniLog.Log($"{logTypePrefix}[NeosModLoader] {message}");
}
else
{
UniLog.Log($"{logTypePrefix}[NeosModLoader/{source}] {message}");
}
}

private static string SourceFromStackTrace()
{
Dictionary<Assembly, NeosMod> loadedMods = ModLoader.LoadedMods;
// skip three frames: SourceFromStackTrace(), MsgExternal(), Msg()
StackTrace stackTrace = new StackTrace(3);
for (int i = 0; i < stackTrace.FrameCount; i++)
{
Assembly assembly = stackTrace.GetFrame(i).GetMethod().DeclaringType.Assembly;
NeosMod mod;
if (loadedMods.TryGetValue(assembly, out mod))
{
return mod.Name;
}
}
return null;
}

private sealed class LogType
{
public readonly static string DEBUG = "[DEBUG]";
public readonly static string INFO = "[INFO] ";
public readonly static string WARN = "[WARN] ";
public readonly static string ERROR = "[ERROR]";
}
}
}
38 changes: 23 additions & 15 deletions NeosModLoader/NeosMod.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
namespace NeosModLoader
{
public abstract class NeosMod
{
public static void Debug(string message) => Logger.DebugExternal(message);
public static void Msg(string message) => Logger.MsgExternal(message);
public static void Warn(string message) => Logger.WarnExternal(message);
public static void Error(string message) => Logger.ErrorExternal(message);
public virtual void OnEngineInit() { }
public abstract string Name { get; }
public abstract string Author { get; }
public abstract string Version { get; }
public virtual string Link { get; }
}
}
namespace NeosModLoader
{
public abstract class NeosMod
{
public static void Debug(string message) => Logger.DebugExternal(message);
public static void Debug(object message) => Logger.DebugExternal(message.ToString());
public static void Debug(params object[] messages) => Logger.DebugList(messages);
public static void Msg(string message) => Logger.MsgExternal(message);
public static void Msg(object message) => Logger.MsgExternal(message.ToString());
public static void Msg(params object[] messages) => Logger.MsgList(messages);
public static void Warn(string message) => Logger.WarnExternal(message);
public static void Warn(object message) => Logger.WarnExternal(message.ToString());
public static void Warn(params object[] messages) => Logger.WarnList(messages);
public static void Error(string message) => Logger.ErrorExternal(message);
public static void Error(object message) => Logger.ErrorExternal(message.ToString());
public static void Error(params object[] messages) => Logger.ErrorList(messages);
public virtual void OnEngineInit() { }
public abstract string Name { get; }
public abstract string Author { get; }
public abstract string Version { get; }
public virtual string Link { get; }
}
}
Loading

0 comments on commit d763495

Please sign in to comment.