diff --git a/App.config b/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BalatroMessager.csproj b/BalatroMessager.csproj new file mode 100644 index 0000000..911d98a --- /dev/null +++ b/BalatroMessager.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {83ED0515-B7E5-4F63-96E2-D7B650DB1DA3} + Exe + BalatroMessager + BalatroMessager + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/BalatroMessager.sln b/BalatroMessager.sln new file mode 100644 index 0000000..88aac68 --- /dev/null +++ b/BalatroMessager.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34714.143 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BalatroMessager", "BalatroMessager.csproj", "{83ED0515-B7E5-4F63-96E2-D7B650DB1DA3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {83ED0515-B7E5-4F63-96E2-D7B650DB1DA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83ED0515-B7E5-4F63-96E2-D7B650DB1DA3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83ED0515-B7E5-4F63-96E2-D7B650DB1DA3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83ED0515-B7E5-4F63-96E2-D7B650DB1DA3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DBA5E491-F0A2-471F-96A1-7D5E98CFD38F} + EndGlobalSection +EndGlobal diff --git a/BalatroMessager.sln.DotSettings b/BalatroMessager.sln.DotSettings new file mode 100644 index 0000000..405a03b --- /dev/null +++ b/BalatroMessager.sln.DotSettings @@ -0,0 +1,3 @@ + + True + True \ No newline at end of file diff --git a/LogCat.cs b/LogCat.cs new file mode 100644 index 0000000..1729325 --- /dev/null +++ b/LogCat.cs @@ -0,0 +1,75 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BalatroMessager +{ + public class LogCat + { + private readonly TcpListener _listener; + + public LogCat(int port) + { + _listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, port)); + } + + public async Task Listen(CancellationToken token) + { + try + { + _listener.Start(); + var buffer = new byte[1024]; + while (!token.IsCancellationRequested) + { + Console.WriteLine($"[LogCat] {_listener.LocalEndpoint} Listening..."); + while (!_listener.Pending()) + await Task.Delay(100, token); + + using (var client = _listener.AcceptTcpClient()) + using (var stream = client.GetStream()) + { + Console.WriteLine("[LogCat] Connected. Receiving..."); + try + { + while (!token.IsCancellationRequested) + { + var len = await stream.ReadAsync(buffer, 0, buffer.Length, token); + if (len == 0) + { + Console.WriteLine("[LogCat] Disconnected."); + break; + } + Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, len)); + } + } + catch (SocketException ex) + { + Console.WriteLine("[LogCat] Socket Error occurred while listening. Code: {0}\n{1}", ex.ErrorCode, ex); + } + catch (Exception ex) + { + Console.WriteLine("[LogCat] {0} Error occurred while listening: {1}", ex.GetType().Name, ex.Message); + } + + try + { + client.Close(); + } + catch (Exception ex) + { + Console.WriteLine("[LogCat] close throw exception: {0}", ex); + } + } + } + } + finally + { + _listener.Stop(); + Console.WriteLine("[LogCat] Stopped."); + } + } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..56e6c10 --- /dev/null +++ b/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace BalatroMessager +{ + internal class Program + { + private const int DebugMessagePort = 12345; + + private static readonly CancellationTokenSource Cts = new CancellationTokenSource(); + static async Task Main(string[] args) + { + Console.CancelKeyPress += Console_CancelKeyPress; + try + { + var logCat = new LogCat(DebugMessagePort); + await logCat.Listen(Cts.Token); + } + catch (TaskCanceledException) + { + // Ignored. + } + catch (Exception ex) + { + Console.WriteLine(ex); + Console.ReadKey(); + } + } + + private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) + { + e.Cancel = true; + Cts.Cancel(); + } + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..32c3bb5 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("BalatroMessager")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BalatroMessager")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("83ed0515-b7e5-4f63-96e2-d7b650db1da3")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]