Skip to content

Commit

Permalink
Fix color printing and launch of ConsoleHost
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Oct 1, 2024
1 parent 31ffba7 commit 41f44ef
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
57 changes: 57 additions & 0 deletions MonkeyLoader.ConsoleHost/ConsoleMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MonkeyLoader.ConsoleHost
{
internal static partial class ConsoleMode
{
public static bool IsTerminal { get; private set; }

public static bool EnsureTerminalProcessing()
{
if (!OperatingSystem.IsWindows())
{
IsTerminal = true;

return true;
}

try
{
var handle = GetStdHandle(-11);

// Ensure ENABLE_PROCESSED_OUTPUT and ENABLE_VIRTUAL_TERMINAL_PROCESSING
var success = GetConsoleMode(handle, out var mode);
mode |= 0x0001 | 0x0004;

success &= SetConsoleMode(handle, mode);

IsTerminal = success;
return success;
}
catch (Exception ex)
{
Console.WriteLine("Exception while setting console mode to enable terminal processing!");
Console.WriteLine(ex.ToString());
}

IsTerminal = false;
return false;
}

[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

[LibraryImport("kernel32.dll", SetLastError = true)]
private static partial IntPtr GetStdHandle(int nStdHandle);

[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
}
1 change: 1 addition & 0 deletions MonkeyLoader.ConsoleHost/MonkeyLoader.ConsoleHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<OutDir>../MonkeyLoader/vendor/MonkeyLoader/Tools/ConsoleHost</OutDir>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
31 changes: 28 additions & 3 deletions MonkeyLoader.ConsoleHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace MonkeyLoader.ConsoleHost
{
internal class Program
{
private const char ESC = '\x1b';

private static void Main(string[] args)
{
var appName = args.FirstOrDefault() ?? "MonkeyLoader";
Expand All @@ -12,6 +14,8 @@ private static void Main(string[] args)
using var pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.In);
var reader = new StreamReader(pipeServer);

ConsoleMode.EnsureTerminalProcessing();

Console.Title = $"{appName} - MonkeyLoader Console";
Console.WriteLine("Welcome to MonkeyLoader!");
Console.WriteLine($"Waiting for input on pipe: {pipeName}");
Expand Down Expand Up @@ -42,12 +46,33 @@ private static void Main(string[] args)
try
{
var i = 0;
while (i >= 0)
var isTermSeq = false;

while (true)
{
i = reader.Read();

if (i >= 0)
Console.Write(Convert.ToChar(i));
if (i < 0)
break;

var c = Convert.ToChar(i);

if (!ConsoleMode.IsTerminal)
{
if (c is ESC)
{
isTermSeq = true;
continue;
}

if (isTermSeq && c is 'm')
{
isTermSeq = false;
continue;
}
}

Console.Write(Convert.ToChar(i));
}
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions MonkeyLoader/Logging/ConsoleLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public sealed class ConsoleLoggingHandler : LoggingHandler
public const string REVERSE = "\x1b[7m";
public const string UNDERLINE = "\x1b[4m";
public const string YELLOW = "\x1b[93m";

private static Process? _consoleHostProcess;
private static NamedPipeClientStream? _pipeClient;

Expand Down
3 changes: 3 additions & 0 deletions MonkeyLoader/Logging/LoggingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ private void SetupLoggers()

private void ShouldLogToConsoleChanged(object sender, ConfigKeyChangedEventArgs<bool> configKeyChangedEventArgs)
{
if (_loggingController is null)
return;

if (configKeyChangedEventArgs.NewValue)
ConsoleLoggingHandler.TryConnect();
else
Expand Down

0 comments on commit 41f44ef

Please sign in to comment.