Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Resonite log to MonkeyLoader Console monkey #71

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
});
}
}
}
Loading