From 012cc023529c479563a2acb6d8a6f9e5f88e6713 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Wed, 11 Sep 2024 12:46:34 -0700 Subject: [PATCH] Streamlining --- FakeeDeck/Class/FakeDeck.cs | 35 +++++++++++++ FakeeDeck/{ => Class}/HttpServer.cs | 23 +++------ FakeeDeck/Class/YamlConfig.cs | 77 ----------------------------- FakeeDeck/FakeeDeck.csproj | 4 ++ FakeeDeck/Program.cs | 6 +-- FakeeDeck/configuration.yaml | 26 ++-------- 6 files changed, 49 insertions(+), 122 deletions(-) create mode 100644 FakeeDeck/Class/FakeDeck.cs rename FakeeDeck/{ => Class}/HttpServer.cs (94%) delete mode 100644 FakeeDeck/Class/YamlConfig.cs diff --git a/FakeeDeck/Class/FakeDeck.cs b/FakeeDeck/Class/FakeDeck.cs new file mode 100644 index 0000000..9b0fb89 --- /dev/null +++ b/FakeeDeck/Class/FakeDeck.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using YamlDotNet.Serialization.NamingConventions; +using YamlDotNet.Serialization; +using System.Diagnostics; +using System.IO; +using System.Text.Json; +using System.Reflection; +using FakeeDeck.ButtonType; + +namespace FakeeDeck.Class +{ + internal class FakeDeck + { + public FakeDeck() + { + HttpServer server = new HttpServer(); + + foreach (var stratogem in HelldiversTwoMacro.stratogems) + { + server.pageData += HelldiversTwoMacro.getButton(stratogem.Key); + } + + foreach (var control in MediaMacro.mediaControls) + { + server.pageData += MediaMacro.getButton(control.Key); + } + + server.serv(); + } + } +} diff --git a/FakeeDeck/HttpServer.cs b/FakeeDeck/Class/HttpServer.cs similarity index 94% rename from FakeeDeck/HttpServer.cs rename to FakeeDeck/Class/HttpServer.cs index 8a9e622..7a0d73d 100644 --- a/FakeeDeck/HttpServer.cs +++ b/FakeeDeck/Class/HttpServer.cs @@ -18,7 +18,7 @@ using System.Xml.Linq; using System.Reflection; using System.Text.Json; -namespace FakeeDeck +namespace FakeeDeck.Class { internal class HttpServer { @@ -117,9 +117,9 @@ namespace FakeeDeck " " + " " + ""; - public static string pageData = ""; + public string pageData = ""; - public static async Task HandleIncomingConnections() + public async Task HandleIncomingConnections() { bool runServer = true; @@ -192,18 +192,8 @@ namespace FakeeDeck } } - public static void serv() + public void serv() { - foreach (var stratogem in HelldiversTwoMacro.stratogems) - { - pageData += HelldiversTwoMacro.getButton(stratogem.Key); - } - - foreach (var control in MediaMacro.mediaControls) - { - pageData += MediaMacro.getButton(control.Key); - } - // Create a Http server and start listening for incoming connections listener = new HttpListener(); listener.Prefixes.Add(url); @@ -282,7 +272,6 @@ namespace FakeeDeck resp.OutputStream.Write(buffer, 0, nbytes); input.Close(); resp.OutputStream.Flush(); - resp.StatusCode = (int)HttpStatusCode.OK; } catch (Exception ex) @@ -296,10 +285,10 @@ namespace FakeeDeck } } - private static async Task servViewResponseAsync(HttpListenerRequest req, HttpListenerResponse resp) + private async Task servViewResponseAsync(HttpListenerRequest req, HttpListenerResponse resp) { string disableSubmit = false ? "disabled" : ""; - byte[] data = Encoding.UTF8.GetBytes(String.Format((pageHeader + pageData + pageFooter), pageViews, disableSubmit)); + byte[] data = Encoding.UTF8.GetBytes(string.Format(pageHeader + this.pageData + pageFooter, pageViews, disableSubmit)); resp.ContentType = "text/html"; resp.ContentEncoding = Encoding.UTF8; resp.ContentLength64 = data.LongLength; diff --git a/FakeeDeck/Class/YamlConfig.cs b/FakeeDeck/Class/YamlConfig.cs deleted file mode 100644 index 1269d1b..0000000 --- a/FakeeDeck/Class/YamlConfig.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Reflection.Metadata.Ecma335; -using System.Text; -using System.Text.Json; -using System.Threading.Tasks; -using static System.Runtime.InteropServices.JavaScript.JSType; - -namespace FakeeDeck.Class -{ - internal class YamlConfig - { - public Dictionary Data { get; private set; } - public YamlConfig(string path) - { - Data = new Dictionary(); - ParseYamlFile(File.ReadAllLines(path)); - } - private Dictionary ParseYamlFile(string[] lines, int levelStart = 0, int linesParsedPreviuse = 0) - { - Dictionary tempObject = new Dictionary(); - - int linesParsed = 0; - - if ((linesParsed + linesParsedPreviuse) <= lines.Length) - { - int level = 0; - int iterator = 0; - foreach (string line in lines) - { - level = line.Count(x => x == '\t'); - - linesParsed++; - if (string.IsNullOrEmpty(line)) - { - continue; - } - - Console.WriteLine(level + ">"+ levelStart + " level " + linesParsedPreviuse + " Lines handeled previouselly '" + line + "'"); - if ((level > levelStart || levelStart == 0)) - { - if (line.Contains(":")) - { - Console.WriteLine("Key:Value Pair"); - iterator = 0; - string[] array = line.Split(":"); - string key = array[0].Trim('\t'); - dynamic value = ""; - - if (array.Length > 1 && !string.IsNullOrEmpty(array[1])) - { - Console.WriteLine("Strict Value Found: " + array[1]); - tempObject.Add(key,array[1]); - } - else if (array.Length == 2 && string.IsNullOrEmpty(array[1]) && lines[linesParsed..lines.Length].Length > 0) - { - tempObject.Add(key, ParseYamlFile(lines[linesParsed..lines.Length], level, linesParsed)); - } - } else - { - iterator++; - tempObject.Add(iterator,line); - } - } else if (level < levelStart) - { - Console.WriteLine("Level DOwns"); - } - } - } - - Debug.WriteLine(JsonSerializer.Serialize(tempObject)); - return tempObject; - } - } -} diff --git a/FakeeDeck/FakeeDeck.csproj b/FakeeDeck/FakeeDeck.csproj index 37c5875..9983fff 100644 --- a/FakeeDeck/FakeeDeck.csproj +++ b/FakeeDeck/FakeeDeck.csproj @@ -11,6 +11,10 @@ + + + + PreserveNewest diff --git a/FakeeDeck/Program.cs b/FakeeDeck/Program.cs index 62f84fe..54648cc 100644 --- a/FakeeDeck/Program.cs +++ b/FakeeDeck/Program.cs @@ -4,8 +4,4 @@ using System.Diagnostics; using System.Text.Json; -YamlConfig parser = new YamlConfig("configuration.yaml"); -var result = parser.Data; -Console.WriteLine(JsonSerializer.Serialize(result)); - -//HttpServer.serv(); +FakeDeck fakeDeck = new FakeDeck(); \ No newline at end of file diff --git a/FakeeDeck/configuration.yaml b/FakeeDeck/configuration.yaml index 3af38fe..b769e4c 100644 --- a/FakeeDeck/configuration.yaml +++ b/FakeeDeck/configuration.yaml @@ -5,26 +5,6 @@ server: pages: - page: helldivers, buttons: - - button: b_1, - name: test, - type: keyboard, - url: https://www.reddit.com/r/kde/comments/fen7lj/plasmoid_for_managing_docker_containers/, - action: - - 0x31 - - 0x31 - - button: b_2, - name: test, - type: keyboard, - url: https://www.reddit.com/r/kde/comments/fen7lj/plasmoid_for_managing_docker_containers/, - action: - - 0x31 - - 0x31 - - page: media, - buttons: - - button: b_1, - name: test, - type: keyboard, - url: https://www.reddit.com/, - action: - - 0x31 - - 0x31 \ No newline at end of file + - button: reinforce, + name: reinforce, + type: HelldiversTwoMacro, \ No newline at end of file