Skip to content

Commit

Permalink
Merge pull request #330 from amazingalek/logging-overhaul
Browse files Browse the repository at this point in the history
* New OWML config (available in OWML menu): Debug mode.
* When Debug mode is enabled, *everything* is written to the console (in blue), including what was previously only written to log files and all Unity messages.
* Deprecated Logger.Log and ModConsole.OwmlConsole, use ModHelper.Console instead.
  • Loading branch information
amazingalek authored Dec 22, 2020
2 parents b72f3a0 + 55507f7 commit 0c33d48
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 164 deletions.
2 changes: 2 additions & 0 deletions src/OWML.Common/Interfaces/IOwmlConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface IOwmlConfig

bool BlockInput { get; set; }

bool DebugMode { get; set; }

int SocketPort { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/OWML.Common/Interfaces/Menus/IModMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ public interface IModMenu
IModSeparator AddSeparator(IModSeparator separator);

IModSeparator AddSeparator(IModSeparator separator, int index);

IModSeparator GetSeparator(string title);

object GetInputValue(string key);

T GetInputValue<T>(string key);

void SetInputValue(string key, object value);

void SelectFirst();
Expand Down
4 changes: 3 additions & 1 deletion src/OWML.Common/MessageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public enum MessageType

Quit = 5,

Fatal = 6
Fatal = 6,

Debug = 7
}
}
3 changes: 3 additions & 0 deletions src/OWML.Common/OwmlConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class OwmlConfig : IOwmlConfig
[JsonProperty("combinationsBlockInput")]
public bool BlockInput { get; set; }

[JsonProperty("debugMode")]
public bool DebugMode { get; set; }

[JsonIgnore]
public string DataPath => $"{GamePath}/OuterWilds_Data";

Expand Down
1 change: 1 addition & 0 deletions src/OWML.Launcher/OWML.DefaultConfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"gamePath": "C:/Program Files/Epic Games/OuterWilds",
"debugMode": false,
"combinationsBlockInput": false,
"socketPort": 0
}
2 changes: 1 addition & 1 deletion src/OWML.Launcher/OWML.Manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Alek",
"name": "OWML",
"uniqueName": "Alek.OWML",
"version": "1.1.2",
"version": "1.1.3",
"description": "The mod loader and mod framework for Outer Wilds",
"minGameVersion": "1.0.7.0",
"maxGameVersion": "1.0.7.481"
Expand Down
2 changes: 1 addition & 1 deletion src/OWML.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ private static IModManifest GetOwmlManifest() =>
private static IModConsole CreateConsoleWriter(IOwmlConfig owmlConfig, IModManifest owmlManifest, bool hasConsolePort) =>
hasConsolePort
? new ModSocketOutput(owmlConfig, owmlManifest, null, new ModSocket(owmlConfig), new ProcessHelper())
: (IModConsole)new OutputWriter();
: (IModConsole)new OutputWriter(owmlConfig);
}
}
1 change: 1 addition & 0 deletions src/OWML.Logging/ConsoleUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void WriteByType(MessageType type, string line)
MessageType.Message => ConsoleColor.Gray,
MessageType.Info => ConsoleColor.Cyan,
MessageType.Fatal => ConsoleColor.Magenta,
MessageType.Debug => ConsoleColor.Blue,
_ => ConsoleColor.Gray
};

Expand Down
1 change: 1 addition & 0 deletions src/OWML.Logging/ModConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace OWML.Logging
{
public abstract class ModConsole : IModConsole
{
[Obsolete("Use ModHelper.Console instead.")]
public static ModConsole OwmlConsole { get; private set; }

protected readonly IModLogger Logger;
Expand Down
2 changes: 2 additions & 0 deletions src/OWML.Logging/ModLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public ModLogger(IOwmlConfig config, IModManifest manifest)
}
}

[Obsolete("Use ModHelper.Console.WriteLine with messageType = Debug instead.")]
public void Log(string s) =>
LogInternal($"[{_manifest.Name}]: {s}");

[Obsolete("Use ModHelper.Console.WriteLine with messageType = Debug instead.")]
public void Log(params object[] objects) =>
Log(string.Join(" ", objects.Select(o => o.ToString()).ToArray()));

Expand Down
22 changes: 12 additions & 10 deletions src/OWML.Logging/ModSocketOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ public override void WriteLine(params object[] objects)
WriteLine(line, MessageType.Message, GetCallingType(new StackTrace()));
}

public override void WriteLine(string line) =>
public override void WriteLine(string line) =>
WriteLine(line, MessageType.Message, GetCallingType(new StackTrace()));

public override void WriteLine(string line, MessageType type) =>
public override void WriteLine(string line, MessageType type) =>
WriteLine(line, type, GetCallingType(new StackTrace()));

public override void WriteLine(string line, MessageType type, string senderType)
{
Logger?.Log($"{type}: {line}");

var message = new ModSocketMessage
if (type != MessageType.Debug || OwmlConfig.DebugMode)
{
SenderName = Manifest.Name,
SenderType = senderType,
Type = type,
Message = line
};
_socket.WriteToSocket(message);
_socket.WriteToSocket(new ModSocketMessage
{
SenderName = Manifest.Name,
SenderType = senderType,
Type = type,
Message = line
});
}

if (type == MessageType.Fatal)
{
Expand All @@ -50,7 +52,7 @@ public override void WriteLine(string line, MessageType type, string senderType)
}
private void WriteLine(MessageType type, string line, string senderType) =>
WriteLine(line, type, senderType);

private void KillProcess()
{
_socket.Close();
Expand Down
18 changes: 14 additions & 4 deletions src/OWML.Logging/OutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ namespace OWML.Logging
{
public class OutputWriter : IModConsole
{
private readonly IOwmlConfig _config;

public OutputWriter(IOwmlConfig config) =>
_config = config;

[Obsolete("Use WriteLine(string) or WriteLine(string, MessageType) instead.")]
public void WriteLine(params object[] objects) =>
WriteLine(string.Join(" ", objects.Select(o => o.ToString()).ToArray()));

public void WriteLine(string line) =>
WriteLine(line, MessageType.Message);

public void WriteLine(string line, MessageType type) =>
ConsoleUtils.WriteByType(type, line);
public void WriteLine(string line, MessageType type, string senderType) =>
WriteLine($"{senderType}: {line}", type);

public void WriteLine(string line, MessageType type, string senderType) =>
ConsoleUtils.WriteByType(type, $"{senderType}: {line}");
public void WriteLine(string line, MessageType type)
{
if (type != MessageType.Debug || _config.DebugMode)
{
ConsoleUtils.WriteByType(type, line);
}
}
}
}
39 changes: 18 additions & 21 deletions src/OWML.Logging/UnityLogger.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
using System.Linq;
using OWML.Common;
using OWML.Common;
using UnityEngine;

namespace OWML.Logging
{
public class UnityLogger : IUnityLogger
{
private readonly LogType[] _relevantTypes = {
LogType.Error,
LogType.Exception
};

private readonly IModSocket _socket;
private readonly IApplicationHelper _appHelper;
private readonly IModLogger _logger;
private readonly IModConsole _console;
private readonly IOwmlConfig _config;

public UnityLogger(IModSocket socket, IApplicationHelper appHelper, IModLogger logger)
public UnityLogger(IApplicationHelper appHelper, IModConsole console, IOwmlConfig config)
{
_socket = socket;
_appHelper = appHelper;
_logger = logger;
_console = console;
_config = config;
}

public void Start() =>
public void Start() =>
_appHelper.AddLogCallback(OnLogMessageReceived);

private void OnLogMessageReceived(string message, string stackTrace, LogType type)
{
if (!_relevantTypes.Contains(type))
if (type != LogType.Error
&& type != LogType.Exception
&& !_config.DebugMode)
{
return;
}

var line = $"{message}. Stack trace: {stackTrace?.Trim()}";
_logger.Log(line);
_socket.WriteToSocket(new ModSocketMessage

var messageType = type switch
{
Type = MessageType.Error,
Message = line,
SenderName = "Unity",
SenderType = type.ToString()
});
LogType.Error => MessageType.Error,
LogType.Exception => MessageType.Error,
_ => MessageType.Debug
};

_console.WriteLine(line, messageType, "Unity");
}
}
}
10 changes: 4 additions & 6 deletions src/OWML.ModHelper.Events/HarmonyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ namespace OWML.ModHelper.Events
{
public class HarmonyHelper : IHarmonyHelper
{
private readonly IModLogger _logger;
private readonly IModConsole _console;
private readonly IModManifest _manifest;
private readonly IOwmlConfig _owmlConfig;
private readonly HarmonyInstance _harmony;

public HarmonyHelper(IModLogger logger, IModConsole console, IModManifest manifest, IOwmlConfig owmlConfig)
public HarmonyHelper(IModConsole console, IModManifest manifest, IOwmlConfig owmlConfig)
{
_logger = logger;
_console = console;
_manifest = manifest;
_owmlConfig = owmlConfig;
Expand All @@ -28,7 +26,7 @@ private HarmonyInstance CreateInstance()
HarmonyInstance harmony;
try
{
_logger.Log($"Creating harmony instance: {_manifest.UniqueName}");
_console.WriteLine($"Creating harmony instance: {_manifest.UniqueName}", MessageType.Debug);
HarmonyInstance.DEBUG = true;
FileLog.logPath = $"{_owmlConfig.LogsPath}/harmony.log.txt";
harmony = HarmonyInstance.Create(_manifest.UniqueName);
Expand All @@ -51,7 +49,7 @@ private MethodInfo GetMethod<T>(string methodName)
MethodInfo result = null;
try
{
_logger.Log($"Getting method {methodName} of {targetType.Name}");
_console.WriteLine($"Getting method {methodName} of {targetType.Name}", MessageType.Debug);
result = Utils.TypeExtensions.GetAnyMethod(targetType, methodName);
}
catch (Exception ex)
Expand Down Expand Up @@ -129,7 +127,7 @@ private void Patch(MethodBase original, MethodInfo prefix, MethodInfo postfix, M
try
{
_harmony.Patch(original, prefixMethod, postfixMethod, transpilerMethod);
_logger.Log($"Patched {fullName}!");
_console.WriteLine($"Patched {fullName}!", MessageType.Debug);
}
catch (Exception ex)
{
Expand Down
11 changes: 4 additions & 7 deletions src/OWML.ModHelper.Events/ModEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ public class ModEvents : IModEvents

private readonly IHarmonyHelper _harmonyHelper;
private readonly IModConsole _console;
private readonly IModLogger _logger;

public ModEvents(
IModLogger logger,
IModConsole console,
IHarmonyHelper harmonyHelper,
IModPlayerEvents playerEvents,
IModSceneEvents sceneEvents,
IModUnityEvents unityEvents)
{
_logger = logger;
_console = console;
_harmonyHelper = harmonyHelper;
Player = playerEvents;
Expand All @@ -50,13 +47,13 @@ private void OnPatchEvent(MonoBehaviour behaviour, Common.Events ev)
var type = behaviour.GetType();
if (IsSubscribedTo(type, ev))
{
_logger.Log($"Got subscribed event: {ev} of {type.Name}");
_console.WriteLine($"Got subscribed event: {ev} of {type.Name}", MessageType.Debug);
OnEvent?.Invoke(behaviour, ev);
Event?.Invoke(behaviour, ev);
}
else
{
_logger.Log($"Not subscribed to: {ev} of {type.Name}");
_console.WriteLine($"Not subscribed to: {ev} of {type.Name}", MessageType.Debug);
}
}

Expand All @@ -71,7 +68,7 @@ private void SubscribeToEvent<T>(Common.Events ev)
var type = typeof(T);
if (IsSubscribedTo(type, ev))
{
_logger.Log($"Already subscribed to {ev} of {type.Name}");
_console.WriteLine($"Already subscribed to {ev} of {type.Name}", MessageType.Debug);
return;
}
AddToEventList(_subscribedEvents, type, ev);
Expand All @@ -82,7 +79,7 @@ private void PatchEvent<T>(Common.Events ev)
var type = typeof(T);
if (InEventList(PatchedEvents, type, ev))
{
_logger.Log($"Event is already patched: {ev} of {type.Name}");
_console.WriteLine($"Event is already patched: {ev} of {type.Name}", MessageType.Debug);
return;
}
AddToEventList(PatchedEvents, type, ev);
Expand Down
16 changes: 10 additions & 6 deletions src/OWML.ModHelper.Input/ModInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@ public class ModInputHandler : IModInputHandler
private const float Cooldown = 0.05f;
private const float TapDuration = 0.1f;
private const BindingFlags NonPublic = BindingFlags.NonPublic | BindingFlags.Instance;

private readonly HashSet<IModInputCombination> _singlesPressed = new();
private readonly Dictionary<long, HashSet<IModInputCombination>> _comboRegistry = new();
private readonly HashSet<IModInputCombination> _toResetOnNextFrame = new();
private readonly int[] _blockedFrame = new int[ModInputLibrary.MaxUsefulKey];
private readonly int[] _gameBindingCounter = new int[ModInputLibrary.MaxUsefulKey];
private readonly IModLogger _logger;
private readonly IModConsole _console;

private HashSet<IModInputCombination> _currentCombinations = new();
private int _lastSingleUpdate;
private int _lastCombinationUpdate;

public ModInputHandler(IModLogger logger, IModConsole console, IHarmonyHelper patcher, IOwmlConfig owmlConfig, IModEvents events, IModInputTextures inputTextures, IBindingChangeListener listener)

public ModInputHandler(
IModConsole console,
IHarmonyHelper patcher,
IOwmlConfig owmlConfig,
IModEvents events,
IModInputTextures inputTextures,
IBindingChangeListener listener)
{
Instance = this;
_console = console;
_logger = logger;
Textures = inputTextures;

listener.Initialize(this, events);
Expand Down Expand Up @@ -364,7 +368,7 @@ public void UnregisterCombination(IModInputCombination combination)
_console.WriteLine($"Failed to unregister \"{combination.FullName}\": Too long!", MessageType.Error);
return;
case RegistrationCode.AllNormal:
_logger.Log($"Successfully unregistered \"{combination.FullName}\"", MessageType.Success);
_console.WriteLine($"Successfully unregistered \"{combination.FullName}\"", MessageType.Debug);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/OWML.ModHelper.Menus/ModConfigMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override void UpdateUIValues()

protected override void OnSave()
{
ModData.Config.Enabled = (bool)GetInputValue(EnabledTitle);
ModData.Config.Enabled = GetInputValue<bool>(EnabledTitle);
var keys = ModData.Config.Settings.Select(x => x.Key).ToList();
foreach (var key in keys)
{
Expand Down
Loading

0 comments on commit 0c33d48

Please sign in to comment.