Streamlining

This commit is contained in:
Václav Španinger 2024-09-11 12:46:34 -07:00
parent 41aa2b7c7b
commit 012cc02352
6 changed files with 49 additions and 122 deletions

View File

@ -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();
}
}
}

View File

@ -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
" <script src=\"StaticFiles/app.js\"></script>" +
" </body>" +
"</html>";
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;

View File

@ -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<string, dynamic> Data { get; private set; }
public YamlConfig(string path)
{
Data = new Dictionary<string, dynamic>();
ParseYamlFile(File.ReadAllLines(path));
}
private Dictionary<dynamic, dynamic> ParseYamlFile(string[] lines, int levelStart = 0, int linesParsedPreviuse = 0)
{
Dictionary<dynamic, dynamic> tempObject = new Dictionary<dynamic, dynamic>();
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;
}
}
}

View File

@ -11,6 +11,10 @@
<Folder Include="StaticFiles\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="YamlDotNet" Version="16.1.0" />
</ItemGroup>
<ItemGroup>
<None Update="configuration.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View File

@ -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();

View File

@ -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
- button: reinforce,
name: reinforce,
type: HelldiversTwoMacro,