commit ef662f1d1e1a5ca2e760ebab275186921de48eb5 Author: NorbiPeti Date: Mon Apr 19 23:53:41 2021 +0200 Create basic web server to receive commands diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4069955 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ + +.idea +TBConsole.sln.DotSettings.user diff --git a/TBConsole.sln b/TBConsole.sln new file mode 100644 index 0000000..776832c --- /dev/null +++ b/TBConsole.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TBConsole", "TBConsole\TBConsole.csproj", "{9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9B1B4559-5C22-4F75-B29B-2E1E1E761B1A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/TBConsole/TBConsole.csproj b/TBConsole/TBConsole.csproj new file mode 100644 index 0000000..25a29f4 --- /dev/null +++ b/TBConsole/TBConsole.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp3.1 + Exe + + + + + + + + + ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\GamecraftModdingAPI.dll + + + ..\..\GamecraftModdingAPI\GamecraftModdingAPI\bin\Debug\net472\IllusionPlugin.dll + + + + diff --git a/TBConsole/TBConsoleMod.cs b/TBConsole/TBConsoleMod.cs new file mode 100644 index 0000000..be0c7e4 --- /dev/null +++ b/TBConsole/TBConsoleMod.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; +using System.Net; +using System.Reflection; +using IllusionPlugin; + +namespace TBConsole +{ + public class TBConsoleMod : IEnhancedPlugin + { + public override string Name => "TBConsole"; + public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString(); + + private WebServer _server; + public override void OnApplicationStart() + { + GamecraftModdingAPI.Main.Init(); + _server = new WebServer(CommandReceived); + _server.Start(); + } + + public string CommandReceived(string command) + { + return $"Got it: {command}"; + } + + public override void OnApplicationQuit() + { + _server.Stop(); + GamecraftModdingAPI.Main.Shutdown(); + } + + public static void Main(string[] args) + { + var mod = new TBConsoleMod(); + mod._server = new WebServer(mod.CommandReceived); + mod._server.Start(); + Console.ReadLine(); + } + } +} \ No newline at end of file diff --git a/TBConsole/WebServer.cs b/TBConsole/WebServer.cs new file mode 100644 index 0000000..edba93a --- /dev/null +++ b/TBConsole/WebServer.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using System.Net; + +namespace TBConsole +{ + public class WebServer + { + private bool _running; + private readonly HttpListener _listener = new HttpListener(); + private Func _receiver; + public WebServer(Func receiver) => _receiver = receiver; + + public void Start() + { + _running = true; + KeepListening(); + } + + public void Stop() + { + _running = false; + _listener.Stop(); + } + + private async void KeepListening() + { + _listener.Prefixes.Add("http://localhost:8019/"); + _listener.Start(); + while (_running) + { + var context = await _listener.GetContextAsync(); + string resp = _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync()); + var sw = new StreamWriter(context.Response.OutputStream); + await sw.WriteLineAsync(resp); + sw.Close(); + } + } + } +} \ No newline at end of file