Skip to content

Commit

Permalink
Add resonite log to console monkey
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Oct 3, 2024
1 parent 4a70210 commit c9a97fa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions MonkeyLoader.Resonite.Integration/Locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"MonkeyLoader.GamePacks.Resonite.ModLocaleFallbackDriver.Description": "Ensures that mods' locale messages show up as the (English) fallback value for users without the mod adding them.",
"MonkeyLoader.GamePacks.Resonite.ModSettingStandaloneFacet.Description": "Allows creating working standalone facets from MonkeyLoader Mod Settings items.",
"MonkeyLoader.GamePacks.Resonite.SettingsDataFeedInjector.Description": "Adds the MonkeyLoader category to the Settings.",
"MonkeyLoader.GamePacks.Resonite.ResoniteLogToConsole.Description": "Sends the FrooxEngine UniLog messages to the MonkeyLoader Console when available.",

"Tooltip.Slot.ResetPosition": "Resets the relative Position of Slot [{Name}] to [0; 0; 0].",
"Tooltip.Slot.ParentUnderWorldRoot": "Reparents the Slot [{Name}] from [{Parent}] to the world root while keeping its global transform.",
Expand Down
63 changes: 63 additions & 0 deletions MonkeyLoader.Resonite.Integration/ResoniteLogToConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Elements.Core;
using MonkeyLoader.Logging;
using MonkeyLoader.Patching;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MonkeyLoader.Resonite
{
internal sealed class ResoniteLogToConsole : Monkey<ResoniteLogToConsole>
{
public override bool CanBeDisabled => true;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => [];

protected override bool OnLoaded()
{
UniLog.OnError += message => ToConsoleLoggingHandler(LoggingLevel.Error, message);
UniLog.OnWarning += message => ToConsoleLoggingHandler(LoggingLevel.Warn, message);
UniLog.OnLog += message => ToConsoleLoggingHandler(LoggingLevel.Info, message);

return true;
}

private static void ToConsoleLoggingHandler(LoggingLevel level, string message)
{
Task.Run(() =>
{
// Log when enabled, Console is open, level is supported, and message isn't from the ModLoader
if (!Enabled || !ConsoleLoggingHandler.Instance.Connected || !Logger.ShouldLog(level) || message.Length <= 25 || message.Contains("[MonkeyLoader"))
return;

string Producer()
{
var fpsIndex = message.IndexOf(" FPS)");
if (fpsIndex >= 0)
{
var openIndex = message.LastIndexOf('(', fpsIndex - 4);
message = message[openIndex..];
}

return message.TrimEnd('\r', '\n');
}

switch (level)
{
case LoggingLevel.Error:
ConsoleLoggingHandler.Instance.Error(Producer);
break;

case LoggingLevel.Warn:
ConsoleLoggingHandler.Instance.Warn(Producer);
break;

default:
ConsoleLoggingHandler.Instance.Info(Producer);
break;
}
});
}
}
}

0 comments on commit c9a97fa

Please sign in to comment.