Tweaks and Stream Lining

This commit is contained in:
Václav Španinger 2024-09-09 15:38:22 -07:00
parent 8051318d25
commit ff0decdbf6
5 changed files with 321 additions and 110 deletions

View File

@ -8,31 +8,37 @@ namespace FakeeDeck.ButtonType
{
internal class Button
{
public static string getButtonHTML(string icon, string image, string name, string action, Dictionary<string, string> parameters)
public static string getButtonHTML(string icon, string image, string name, string action, Dictionary<string, string> parameters = null)
{
string body = "<div class=\"m-2\">";
string body = "";
body += "<div class=\"m-2\">";
body += "<form style=\"margin-bottom: 0px;\" method=\"post\" action=\"" + action + "\">";
foreach (var parameter in parameters)
if (parameters is not null)
{
body += "<input type=\"hidden\" name=\"" + parameter.Key + "\" value=\"" + parameter.Value + "\">";
foreach (var parameter in parameters)
{
body += "<input type=\"hidden\" name=\"" + parameter.Key + "\" value=\"" + parameter.Value + "\">";
}
}
body += "<button type=\"submit\" value=\"submit\" style=\"background-size: cover; " + (!string.IsNullOrEmpty(image) ? "background-image: url('" + image + "');" : "") + " width: 150px;height: 150px; background-color: aquamarine;\" >";
body += (!string.IsNullOrEmpty(icon) ? "<i class=\"fa-solid " + icon + "\"></i>" : name);
body += "</button>";
body += "</form>";
body += "</div>";
return body;
}
public static string getButton(string Key)
{
return ""; //getButtonHTML(null, "https://docs.itego.cz/uploads/images/system/2022-04/OdRTPJ4iTTInmhdP-jagq7dfjpi2lilfg-imageedit-2-6604933313.gif");
return getButtonHTML(null, "https://docs.itego.cz/uploads/images/system/2022-04/OdRTPJ4iTTInmhdP-jagq7dfjpi2lilfg-imageedit-2-6604933313.gif", "Test", "test/test");
}
public static bool invokeAction(string Key)
{
return false;

View File

@ -1,4 +1,5 @@
using System;
using FakeeDeck.Class;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -14,7 +15,7 @@ namespace FakeeDeck.ButtonType
public static Dictionary<string, uint[]> stratogems = new Dictionary<string, uint[]>
{
{ "reinforce", new uint[] { 0x65, 0x68, 0x62, 0x66, 0x64, 0x68}},
{ "reinforce", new uint[] { KeyboardCode.NUMPAD5, 0x68, 0x62, 0x66, 0x64, 0x68}},
{ "resupply", new uint[] { 0x65, 0x62, 0x62, 0x68, 0x66 }},
//Patriotic Administration Center
{ "anti-material", new uint[] { 0x65, 0x62, 0x64, 0x66, 0x68, 0x62}},

View File

@ -7,7 +7,6 @@ using System.Threading.Tasks;
namespace FakeeDeck.ButtonType
{
internal class KeyboardMacro
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

View File

@ -0,0 +1,201 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeeDeck.Class
{
internal class Keyboard
{
public const uint LBUTTON = 0x01; // Left mouse button
public const uint RBUTTON = 0x02; // Right mouse button
public const uint CANCEL = 0x03; // Control-break processing
public const uint MBUTTON = 0x04; // Middle mouse button (three-button mouse)
public const uint XBUTTON1 = 0x05; // X1 mouse button
public const uint XBUTTON2 = 0x06; // X2 mouse button
public const uint BACK = 0x08; // BACKSPACE key
public const uint TAB = 0x09; // TAB key
public const uint CLEAR = 0x0C; // CLEAR key
public const uint RETURN = 0x0D; // ENTER key
public const uint SHIFT = 0x10; // SHIFT key
public const uint CONTROL = 0x11; // CTRL key
public const uint MENU = 0x12; // ALT key
public const uint PAUSE = 0x13; // PAUSE key
public const uint CAPITAL = 0x14; // CAPS LOCK key
public const uint KANA = 0x15; // IME Kana mode
public const uint HANGUL = 0x15; // IME Hangul mode
public const uint JUNJA = 0x17; // IME Junja mode
public const uint FINAL = 0x18; // IME final mode
public const uint HANJA = 0x19; // IME Hanja mode
public const uint KANJI = 0x19; // IME Kanji mode
public const uint ESCAPE = 0x1B; // ESC key
public const uint CONVERT = 0x1C; // IME convert
public const uint NONCONVERT = 0x1D; // IME nonconvert
public const uint ACCEPT = 0x1E; // IME accept
public const uint MODECHANGE = 0x1F; // IME mode change request
public const uint SPACE = 0x20; // SPACEBAR
public const uint PRIOR = 0x21; // PAGE UP key
public const uint NEXT = 0x22; // PAGE DOWN key
public const uint END = 0x23; // END key
public const uint HOME = 0x24; // HOME key
public const uint LEFT = 0x25; // LEFT ARROW key
public const uint UP = 0x26; // UP ARROW key
public const uint RIGHT = 0x27; // RIGHT ARROW key
public const uint DOWN = 0x28; // DOWN ARROW key
public const uint SELECT = 0x29; // SELECT key
public const uint PRINT = 0x2A; // PRINT key
public const uint EXECUTE = 0x2B; // EXECUTE key
public const uint SNAPSHOT = 0x2C; // PRINT SCREEN key
public const uint INSERT = 0x2D; // INS key
public const uint DELETE = 0x2E; // DEL key
public const uint HELP = 0x2F; // HELP key
public const uint N0 = 0x30; // '0' key
public const uint N1 = 0x31; // '1' key
public const uint N2 = 0x32; // '2' key
public const uint N3 = 0x33; // '3' key
public const uint N4 = 0x34; // '4' key
public const uint N5 = 0x35; // '5' key
public const uint N6 = 0x36; // '6' key
public const uint N7 = 0x37; // '7' key
public const uint N8 = 0x38; // '8' key
public const uint N9 = 0x39; // '9' key
public const uint A = 0x41; // 'A' key
public const uint B = 0x42; // 'B' key
public const uint C = 0x43; // 'C' key
public const uint D = 0x44; // 'D' key
public const uint E = 0x45; // 'E' key
public const uint F = 0x46; // 'F' key
public const uint G = 0x47; // 'G' key
public const uint H = 0x48; // 'H' key
public const uint I = 0x49; // 'I' key
public const uint J = 0x4A; // 'J' key
public const uint K = 0x4B; // 'K' key
public const uint L = 0x4C; // 'L' key
public const uint M = 0x4D; // 'M' key
public const uint N = 0x4E; // 'N' key
public const uint O = 0x4F; // 'O' key
public const uint P = 0x50; // 'P' key
public const uint Q = 0x51; // 'Q' key
public const uint R = 0x52; // 'R' key
public const uint S = 0x53; // 'S' key
public const uint T = 0x54; // 'T' key
public const uint U = 0x55; // 'U' key
public const uint V = 0x56; // 'V' key
public const uint W = 0x57; // 'W' key
public const uint X = 0x58; // 'X' key
public const uint Y = 0x59; // 'Y' key
public const uint Z = 0x5A; // 'Z' key
public const uint LWIN = 0x5B; // Left Windows key (Natural keyboard)
public const uint RWIN = 0x5C; // Right Windows key (Natural keyboard)
public const uint APPS = 0x5D; // Applications key (Natural keyboard)
public const uint SLEEP = 0x5F; // Computer Sleep key
public const uint NUMPAD0 = 0x60; // Numeric keypad '0' key
public const uint NUMPAD1 = 0x61; // Numeric keypad '1' key
public const uint NUMPAD2 = 0x62; // Numeric keypad '2' key
public const uint NUMPAD3 = 0x63; // Numeric keypad '3' key
public const uint NUMPAD4 = 0x64; // Numeric keypad '4' key
public const uint NUMPAD5 = 0x65; // Numeric keypad '5' key
public const uint NUMPAD6 = 0x66; // Numeric keypad '6' key
public const uint NUMPAD7 = 0x67; // Numeric keypad '7' key
public const uint NUMPAD8 = 0x68; // Numeric keypad '8' key
public const uint NUMPAD9 = 0x69; // Numeric keypad '9' key
public const uint MULTIPLY = 0x6A; // Multiply key
public const uint ADD = 0x6B; // Add key
public const uint SEPARATOR = 0x6C; // Separator key
public const uint SUBTRACT = 0x6D; // Subtract key
public const uint DECIMAL = 0x6E; // Decimal key
public const uint DIVIDE = 0x6F; // Divide key
public const uint F1 = 0x70; // F1 key
public const uint F2 = 0x71; // F2 key
public const uint F3 = 0x72; // F3 key
public const uint F4 = 0x73; // F4 key
public const uint F5 = 0x74; // F5 key
public const uint F6 = 0x75; // F6 key
public const uint F7 = 0x76; // F7 key
public const uint F8 = 0x77; // F8 key
public const uint F9 = 0x78; // F9 key
public const uint F10 = 0x79; // F10 key
public const uint F11 = 0x7A; // F11 key
public const uint F12 = 0x7B; // F12 key
public const uint F13 = 0x7C; // F13 key
public const uint F14 = 0x7D; // F14 key
public const uint F15 = 0x7E; // F15 key
public const uint F16 = 0x7F; // F16 key
public const uint F17 = 0x80; // F17 key
public const uint F18 = 0x81; // F18 key
public const uint F19 = 0x82; // F19 key
public const uint F20 = 0x83; // F20 key
public const uint F21 = 0x84; // F21 key
public const uint F22 = 0x85; // F22 key
public const uint F23 = 0x86; // F23 key
public const uint F24 = 0x87; // F24 key
public const uint NUMLOCK = 0x90; // NUM LOCK key
public const uint SCROLL = 0x91; // SCROLL LOCK key
public const uint LSHIFT = 0xA0; // Left SHIFT key
public const uint RSHIFT = 0xA1; // Right SHIFT key
public const uint LCONTROL = 0xA2; // Left CONTROL key
public const uint RCONTROL = 0xA3; // Right CONTROL key
public const uint LMENU = 0xA4; // Left MENU key
public const uint RMENU = 0xA5; // Right MENU key
public const uint BROWSER_BACK = 0xA6; // Browser Back key
public const uint BROWSER_FORWARD = 0xA7; // Browser Forward key
public const uint BROWSER_REFRESH = 0xA8; // Browser Refresh key
public const uint BROWSER_STOP = 0xA9; // Browser Stop key
public const uint BROWSER_SEARCH = 0xAA; // Browser Search key
public const uint BROWSER_FAVORITES = 0xAB; // Browser Favorites key
public const uint BROWSER_HOME = 0xAC; // Browser Start and Home key
public const uint VOLUME_MUTE = 0xAD; // Volume Mute key
public const uint VOLUME_DOWN = 0xAE; // Volume Down key
public const uint VOLUME_UP = 0xAF; // Volume Up key
public const uint MEDIA_NEXT_TRACK = 0xB0; // Next Track key
public const uint MEDIA_PREV_TRACK = 0xB1; // Previous Track key
public const uint MEDIA_STOP = 0xB2; // Stop Media key
public const uint MEDIA_PLAY_PAUSE = 0xB3; // Play/Pause Media key
public const uint LAUNCH_MAIL = 0xB4; // Start Mail key
public const uint LAUNCH_MEDIA_SELECT = 0xB5; // Select Media key
public const uint LAUNCH_APP1 = 0xB6; // Start Application 1 key
public const uint LAUNCH_APP2 = 0xB7; // Start Application 2 key
public const uint OEM_1 = 0xBA; // For the US standard keyboard, the ';:' key
public const uint OEM_PLUS = 0xBB; // For any country/region, the '+' key
public const uint OEM_COMMA = 0xBC; // For any country/region, the ',' key
public const uint OEM_MINUS = 0xBD; // For any country/region, the '-' key
public const uint OEM_PERIOD = 0xBE; // For any country/region, the '.' key
public const uint OEM_2 = 0xBF; // For the US standard keyboard, the '/?' key
public const uint OEM_3 = 0xC0; // For the US standard keyboard, the '`~' key
public const uint OEM_4 = 0xDB; // For the US standard keyboard, the '[{' key
public const uint OEM_5 = 0xDC; // For the US standard keyboard, the '\|' key
public const uint OEM_6 = 0xDD; // For the US standard keyboard, the ']}' key
public const uint OEM_7 = 0xDE; // For the US standard keyboard, the 'single-quote/double-quote' key
public const uint OEM_8 = 0xDF; // Used for miscellaneous characters; it can vary by keyboard.
public const uint OEM_AX = 0xE1; // OEM specific
public const uint OEM_102 = 0xE2; // The angle bracket key or the backslash key on the RT 102-key keyboard
public const uint ICO_HELP = 0xE3; // Help key on ICO
public const uint ICO_00 = 0xE4; // 00 key on ICO
public const uint PROCESSKEY = 0xE5; // IME PROCESS key
public const uint ICO_CLEAR = 0xE6; // Clear key on ICO
public const uint PACKET = 0xE7; // Used to pass Unicode characters as if they were keystrokes.
public const uint OEM_RESET = 0xE9; // OEM specific
public const uint OEM_JUMP = 0xEA; // OEM specific
public const uint OEM_PA1 = 0xEB; // OEM specific
public const uint OEM_PA2 = 0xEC; // OEM specific
public const uint OEM_PA3 = 0xED; // OEM specific
public const uint OEM_WSCTRL = 0xEE; // OEM specific
public const uint OEM_CUSEL = 0xEF; // OEM specific
public const uint OEM_ATTN = 0xF0; // OEM specific
public const uint OEM_FINISH = 0xF1; // OEM specific
public const uint OEM_COPY = 0xF2; // OEM specific
public const uint OEM_AUTO = 0xF3; // OEM specific
public const uint OEM_ENLW = 0xF4; // OEM specific
public const uint OEM_BACKTAB = 0xF5; // OEM specific
public const uint ATTN = 0xF6; // Attn key
public const uint CRSEL = 0xF7; // CrSel key
public const uint EXSEL = 0xF8; // ExSel key
public const uint EREOF = 0xF9; // Erase EOF key
public const uint PLAY = 0xFA; // Play key
public const uint ZOOM = 0xFB; // Zoom key
public const uint NONAME = 0xFC; // Reserved
public const uint PA1 = 0xFD; // PA1 key
public const uint OEM_CLEAR = 0xFE; // Clear key
}
}

View File

@ -141,80 +141,23 @@ namespace FakeeDeck
Console.WriteLine(req.UserAgent);
Console.WriteLine();
//Parse Port Parameters
Dictionary<string, string> postParams = new Dictionary<string, string>();
if (req.HttpMethod == "POST")
{
using (var reader = new StreamReader(req.InputStream, req.ContentEncoding))
//Parse Port Parameters
Dictionary<string, string> postParams = parsePostRequestParameters(req);
// If `shutdown` url requested w/ POST, then shutdown the server after serving the page
if (req.Url.AbsolutePath == "/shutdown")
{
string postData = reader.ReadToEnd();
var parsedData = HttpUtility.ParseQueryString(postData);
foreach (string key in parsedData)
{
postParams[key] = parsedData[key];
}
Console.WriteLine("Shutdown requested");
runServer = false;
}
}
// If `shutdown` url requested w/ POST, then shutdown the server after serving the page
if ((req.HttpMethod == "POST") && (req.Url.AbsolutePath == "/shutdown"))
{
Console.WriteLine("Shutdown requested");
runServer = false;
}
// If `shutdown` url requested w/ POST, then shutdown the server after serving the page
if ((req.HttpMethod == "POST") && (req.Url.AbsolutePath.StartsWith("/button")))
{
try
{
string module = req.Url.AbsolutePath.Replace("/button", "");
Console.WriteLine("Call module " + module);
callButtonAction(module, postParams);
resp.StatusCode = (int)HttpStatusCode.OK;
}
catch (Exception ex)
{
byte[] errorData = Encoding.UTF8.GetBytes(ex.Message);
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = errorData.LongLength;
resp.StatusCode = (int)HttpStatusCode.InternalServerError;
await resp.OutputStream.WriteAsync(errorData, 0, errorData.Length);
}
finally
{
resp.Close();
}
continue;
}
if (req.Url.AbsolutePath.Contains("."))
{
string filename = Path.Combine("./", req.Url.AbsolutePath.Substring(1));
if (File.Exists(filename))
else if (req.Url.AbsolutePath.StartsWith("/button"))
{
try
{
Stream input = new FileStream(filename, FileMode.Open);
string mime;
resp.ContentType = mimeTypes.TryGetValue(Path.GetExtension(filename), out mime)
? mime
: "application/octet-stream";
resp.ContentLength64 = input.Length;
resp.AddHeader("Date", DateTime.Now.ToString("r"));
resp.AddHeader("Last-Modified", File.GetLastWriteTime(filename).ToString("r"));
byte[] buffer = new byte[1024 * 32];
int nbytes;
while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
resp.OutputStream.Write(buffer, 0, nbytes);
input.Close();
resp.OutputStream.Flush();
string module = req.Url.AbsolutePath.Replace("/button", "");
Console.WriteLine("Call module " + module);
callButtonAction(module, postParams);
resp.StatusCode = (int)HttpStatusCode.OK;
}
catch (Exception ex)
@ -233,26 +176,19 @@ namespace FakeeDeck
continue;
}
}
// Make sure we don't increment the page views counter if `favicon.ico` is requested
if (req.Url.AbsolutePath != "/favicon.ico")
else if (req.HttpMethod == "GET")
{
pageViews += 1;
if (req.Url.AbsolutePath.Contains("."))
{
await servFileResponseAsync(req, resp);
}
else
{
await servViewResponseAsync(req, resp);
}
resp.Close();
continue;
}
// Write the response info
string disableSubmit = !runServer ? "disabled" : "";
byte[] data = Encoding.UTF8.GetBytes(String.Format((pageHeader + pageData + pageFooter), pageViews, disableSubmit));
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
// Write out to the response stream (asynchronously), then close it
await resp.OutputStream.WriteAsync(data, 0, data.Length);
resp.Close();
}
}
@ -282,16 +218,23 @@ namespace FakeeDeck
listener.Close();
}
private static void callButtonAction(string module, Dictionary<string, string> postParams)
private static void callButtonAction(string module, Dictionary<string, string> postParams)
{
string cleanClass = module.Trim('/');
string cleanClass = "FakeeDeck.ButtonType." + module.Trim('/');
Type? buttonClass = Type.GetType(cleanClass, true);
if (buttonClass is null)
return;
MethodInfo? method = buttonClass.GetMethod("invokeAction");
if (method is null)
return;
Type buttonClass = Type.GetType("FakeeDeck.ButtonType." + cleanClass, true);
MethodInfo method = buttonClass.GetMethod("invokeAction");
ParameterInfo[] pars = method.GetParameters();
List<object> parameters = new List<object>();
Console.WriteLine(module);
foreach (ParameterInfo p in pars)
{
if (p == null)
@ -299,24 +242,85 @@ namespace FakeeDeck
continue;
}
Console.WriteLine(p.Name);
Console.WriteLine(postParams[p.Name]);
if (p.Name != null && postParams.ContainsKey(p.Name))
{
parameters.Insert(p.Position, postParams[p.Name]);
Console.WriteLine(postParams[p.Name]);
}
else if (p.IsOptional && p.DefaultValue != null)
{
parameters.Insert(p.Position, p.DefaultValue);
}
}
Console.WriteLine(JsonSerializer.Serialize(parameters));
Console.WriteLine(method);
Console.WriteLine(method.Invoke(null, parameters.ToArray()).ToString());
_ = method.Invoke(null, [.. parameters]).ToString();
}
private static async Task servFileResponseAsync(HttpListenerRequest req, HttpListenerResponse resp)
{
string filename = Path.Combine("./", req.Url.AbsolutePath.Substring(1));
if (!File.Exists(filename))
{
resp.StatusCode = (int)HttpStatusCode.NotFound;
resp.Close();
}
try
{
Stream input = new FileStream(filename, FileMode.Open);
string mime;
resp.ContentType = mimeTypes.TryGetValue(Path.GetExtension(filename), out mime)
? mime
: "application/octet-stream";
resp.ContentLength64 = input.Length;
resp.AddHeader("Date", DateTime.Now.ToString("r"));
resp.AddHeader("Last-Modified", File.GetLastWriteTime(filename).ToString("r"));
byte[] buffer = new byte[1024 * 32];
int nbytes;
while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
resp.OutputStream.Write(buffer, 0, nbytes);
input.Close();
resp.OutputStream.Flush();
resp.StatusCode = (int)HttpStatusCode.OK;
}
catch (Exception ex)
{
byte[] errorData = Encoding.UTF8.GetBytes(ex.Message);
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = errorData.LongLength;
resp.StatusCode = (int)HttpStatusCode.InternalServerError;
await resp.OutputStream.WriteAsync(errorData, 0, errorData.Length);
}
}
private static async Task servViewResponseAsync(HttpListenerRequest req, HttpListenerResponse resp)
{
string disableSubmit = false ? "disabled" : "";
byte[] data = Encoding.UTF8.GetBytes(String.Format((pageHeader + pageData + pageFooter), pageViews, disableSubmit));
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
await resp.OutputStream.WriteAsync(data, 0, data.Length);
}
private static Dictionary<string, string> parsePostRequestParameters(HttpListenerRequest req)
{
Dictionary<string, string> postParams = new Dictionary<string, string>();
using (var reader = new StreamReader(req.InputStream, req.ContentEncoding))
{
string postData = reader.ReadToEnd();
var parsedData = HttpUtility.ParseQueryString(postData);
foreach (string key in parsedData)
{
postParams[key] = parsedData[key];
}
}
return postParams;
}
}
}