Handle non-interactive terminal environments gracefully

When MCC runs in non-interactive terminals (e.g. CI runners, IDE
embedded shells, piped input), several Console APIs throw exceptions
because there is no real console attached.

Changes:
- Program.cs: Wrap Console.KeyAvailable / Console.ReadKey in
  HandleFailure() with try-catch so MCC does not crash on startup
  failure in headless environments.
- Chunk.cs: Wrap Console.BufferWidth / BufferHeight in try-catch
  with fallback values (120x50) to prevent exceptions when rendering
  chunk maps without a console buffer.
- Map.cs: Same treatment for the map rendering path - use safe
  fallback values when Console.BufferWidth/Height are unavailable.
- ReplayHandler.cs: Replace Array.Reverse() (returns void in newer
  .NET) with .AsEnumerable().Reverse() to fix compilation with
  .NET 10 SDK where the void return breaks the fluent chain.

Made-with: Cursor
This commit is contained in:
BruceChen 2026-03-19 00:11:13 +08:00
parent 494be0930b
commit ff1c570a78
4 changed files with 21 additions and 12 deletions

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@ -344,8 +344,11 @@ namespace MinecraftClient.ChatBots
private static void RenderInConsole(McMap map)
{
StringBuilder sb = new();
int consoleWidth = Math.Max(Console.BufferWidth, Settings.Config.Main.Advanced.MinTerminalWidth) / 2;
int consoleHeight = Math.Max(Console.BufferHeight, Settings.Config.Main.Advanced.MinTerminalHeight) - 1;
int safeBufWidth, safeBufHeight;
try { safeBufWidth = Console.BufferWidth; } catch { safeBufWidth = 120; }
try { safeBufHeight = Console.BufferHeight; } catch { safeBufHeight = 50; }
int consoleWidth = Math.Max(safeBufWidth, Settings.Config.Main.Advanced.MinTerminalWidth) / 2;
int consoleHeight = Math.Max(safeBufHeight, Settings.Config.Main.Advanced.MinTerminalHeight) - 1;
int scaleX = (map.Width + consoleWidth - 1) / consoleWidth;
int scaleY = (map.Height + consoleHeight - 1) / consoleHeight;
int scale = Math.Max(scaleX, scaleY);

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Text;
using Brigadier.NET;
using Brigadier.NET.Builder;
@ -100,11 +100,15 @@ namespace MinecraftClient.Commands
sb.AppendLine(string.Format(Translations.cmd_chunk_chunk_pos, markChunkX, markChunkZ)); ;
}
int consoleHeight = Math.Max(Math.Max(Console.BufferHeight, Settings.Config.Main.Advanced.MinTerminalHeight) - 2, 25);
int safeHeight;
int safeWidth;
try { safeHeight = Console.BufferHeight; } catch { safeHeight = 50; }
try { safeWidth = Console.BufferWidth; } catch { safeWidth = 120; }
int consoleHeight = Math.Max(Math.Max(safeHeight, Settings.Config.Main.Advanced.MinTerminalHeight) - 2, 25);
if (consoleHeight % 2 == 0)
--consoleHeight;
int consoleWidth = Math.Max(Math.Max(Console.BufferWidth, Settings.Config.Main.Advanced.MinTerminalWidth) / 2, 17);
int consoleWidth = Math.Max(Math.Max(safeWidth, Settings.Config.Main.Advanced.MinTerminalWidth) / 2, 17);
if (consoleWidth % 2 == 0)
--consoleWidth;

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@ -768,8 +768,10 @@ namespace MinecraftClient
if (!String.IsNullOrEmpty(errorMessage))
{
ConsoleIO.Reset();
while (Console.KeyAvailable)
Console.ReadKey(true);
try {
while (Console.KeyAvailable)
Console.ReadKey(true);
} catch { }
ConsoleIO.WriteLine(errorMessage);
if (disconnectReason.HasValue)

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -239,8 +239,8 @@ namespace MinecraftClient.Protocol
// format: timestamp + packetLength + RawPacket
List<byte> line = new();
int nowTime = Convert.ToInt32((lastPacketTime - recordStartTime).TotalMilliseconds);
line.AddRange(BitConverter.GetBytes((Int32)nowTime).Reverse().ToArray());
line.AddRange(BitConverter.GetBytes((Int32)rawPacket.Count).Reverse().ToArray());
line.AddRange(BitConverter.GetBytes((Int32)nowTime).AsEnumerable().Reverse().ToArray());
line.AddRange(BitConverter.GetBytes((Int32)rawPacket.Count).AsEnumerable().Reverse().ToArray());
line.AddRange(rawPacket.ToArray());
// Write out to the file
recordStream!.Write(line.ToArray());