Repository navigation
feat: first stab at adding cross-platform support for the console/input handler #12
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
base: main
Are you sure you want to change the base?
Changes from all commits
ea199d1
4c6a83d
06b2b49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| using System.Collections.Concurrent; | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using System.Text; | ||
|
|
@@ -77,16 +79,42 @@ internal static void Start(GameBase game) | |
| Socket.Listen(10); | ||
| LoggerThread.Start(); | ||
|
|
||
| StartLoggerProcess(); | ||
| } | ||
|
|
||
| private static void StartLoggerProcess() | ||
| { | ||
| var logger = Path.Combine(AppContext.BaseDirectory, OperatingSystem.IsWindows() ? "ConsoleEngine.Logger.exe" : "ConsoleEngine.Logger"); | ||
|
|
||
| _loggerProcess = new Process(); | ||
| _loggerProcess.StartInfo.FileName = "ConsoleEngine.Logger.exe"; | ||
| _loggerProcess.StartInfo.UseShellExecute = true; | ||
| _loggerProcess.Start(); | ||
| if (OperatingSystem.IsMacOS()) | ||
| { | ||
| // The game owns the current terminal, open the logger in a new Terminal window. | ||
| _loggerProcess.StartInfo.FileName = "open"; | ||
| _loggerProcess.StartInfo.ArgumentList.Add("-a"); | ||
| _loggerProcess.StartInfo.ArgumentList.Add("Terminal"); | ||
| _loggerProcess.StartInfo.ArgumentList.Add(logger); | ||
| } | ||
| else | ||
| { | ||
| _loggerProcess.StartInfo.FileName = logger; | ||
| _loggerProcess.StartInfo.UseShellExecute = true; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: on Linux the logger runs inside the game's own terminal. When
On Linux it would be safer to start it in a terminal emulator ( |
||
| } | ||
|
|
||
| try | ||
| { | ||
| _loggerProcess.Start(); | ||
| } | ||
| catch (Exception) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: if the logger fails to start, the failure is ignored and messages pile up. When
Before this change the failure at least surfaced. Consider setting a flag in the |
||
| { | ||
| // No way to open a new console window here (e.g. linux without a desktop). Run ConsoleEngine.Logger manually to see the log. | ||
| } | ||
| } | ||
|
|
||
| internal static void Stop() | ||
| { | ||
| _running = false; | ||
| _loggerConnection.Dispose(); | ||
| _loggerConnection?.Dispose(); | ||
| Socket.Dispose(); | ||
| Messages.Clear(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <LangVersion>12</LangVersion> | ||
| <RootNamespace>ConsoleEngine.CrossPlatform</RootNamespace> | ||
| <Configurations>Release;Debug</Configurations> | ||
| <Platforms>AnyCPU</Platforms> | ||
| <TreatWarningsAsErrors>True</TreatWarningsAsErrors> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\ConsoleEngine.Abstractions\ConsoleEngine.Abstractions.csproj" /> | ||
| </ItemGroup> | ||
| </Project> | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the platform choice is made in two places.
OperatingSystem.IsWindows()picks the Win32 or cross-platform implementation both here and inInput.cs, where it creates the staticHandler. A small factory (for examplePlatform.CreateConsoleHandler(...)andPlatform.CreateInputHandler()) would keep the decision in one place. That would make it easier to add a backend, or to force the ANSI renderer on Windows Terminal, without the two places getting out of sync.