Initial Commit

This commit is contained in:
SnapixLP | Tim G.
2025-05-19 09:16:34 +02:00
commit c214be937c
39 changed files with 3710 additions and 0 deletions

63
DX86/Modules/InputBox.cs Normal file
View File

@@ -0,0 +1,63 @@
namespace DX86.Modules;
public class InputBox
{
private MessageSender ms;
public InputBox(MessageSender ms)
{
this.ms = ms;
ms.Log("[InputBox] InputBox initialized.");
}
public string ShowAsync(string title, List<string> lines)
{
ms.SaveOnly(true);
Console.Clear();
string message = "";
List<int> longestLine = new List<int>();
int lineNumber = 0;
foreach (var line in lines)
{
longestLine.Add(line.Length);
lineNumber++;
}
longestLine.Add(title.Length + 4);
int minLength = 50;
int maxLength = longestLine.Max();
if (maxLength < minLength)
{
maxLength = minLength;
}
string titleBar = new string('-', maxLength);
Console.WriteLine("+ {0} +", titleBar);
Console.WriteLine("| » {0}{1} |", title, new string(' ', (maxLength - title.Length) - 4));
Console.WriteLine("+ {0} +", titleBar);
for (int i = 0; i < lines.Count; i++)
{
Console.WriteLine("| {0}{1} |", lines[i], new string(' ', maxLength - longestLine[i]));
}
Console.WriteLine("+ {0} +", titleBar);
Console.WriteLine("| {0} |", new string(' ', maxLength));
Console.WriteLine("+ {0} +", titleBar);
Console.SetCursorPosition(2, 4+lines.Count);
string output = Console.ReadLine();
ms.SaveOnly(false);
ms.SendHistory();
return output;
}
}

View File

@@ -0,0 +1,158 @@
namespace DX86.Modules;
/*
* Copyright 2024 SnapixLP
*/
public class ItemSelector
{
private MessageSender ms;
public ItemSelector(MessageSender ms)
{
this.ms = ms;
ms.Log("[ItemSelector] ItemSelector initialized");
}
private string listTitle = "";
public void SetTitle(string title)
{
listTitle = title;
}
public string SelectItemFromList(string[] items)
{
bool noerror = true;
while (noerror)
{
try
{
Console.Clear();
ms.SaveOnly(true);
int cursorPosition = 1;
int listPosition = 1;
bool running = true;
while (running)
{
GenerateList(listTitle, items, cursorPosition, listPosition);
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
switch (keyInfo.Key)
{
case ConsoleKey.UpArrow:
cursorPosition -= 1;
break;
case ConsoleKey.DownArrow:
cursorPosition += 1;
break;
case ConsoleKey.W:
listPosition -= 1;
break;
case ConsoleKey.S:
listPosition += 1;
break;
case ConsoleKey.Enter:
running = false;
break;
case ConsoleKey.F1:
MessageBox mbox = new MessageBox(ms);
mbox.ShowAsync("Help", [
"Use the Arrow keys to select the items from the list.",
"Press enter to select the items from the list.",
"Press F1 to see this message.",
]);
break;
}
if (cursorPosition > items.Length)
{
cursorPosition = 1;
listPosition = 1;
}
if (cursorPosition < 1)
{
cursorPosition = items.Length;
listPosition = items.Length - 4;
}
if (listPosition > 1 && cursorPosition - listPosition == 0)
{
listPosition -= 1;
}
if (listPosition < items.Length - 4 && cursorPosition - listPosition == 4)
{
listPosition += 1;
}
Console.Clear();
}
ms.SaveOnly(false);
Console.Clear();
ms.SendHistory();
noerror = false;
return items[cursorPosition - 1];
}
catch (Exception e)
{
MessageBox messageBox = new MessageBox(ms);
ms.Error("[ItemSelector] [SelectItemFromList()] An Error Occured");
ms.Error(e.Message);
messageBox.ShowAsync("An error Occured | ItemSelector.cs at SelectItemFromList()", [e.Message, "", "Please try again..."]);
}
}
return "";
}
public static void GenerateList(string title, string[] items, int cursorPostion, int listPosition)
{
//Console.WriteLine($"{cursorPostion}, {listPosition}, {items.Length}, {cursorPostion - listPosition}, {listPosition < items.Length - 4}");;
string outMessage = "";
string headBar = "";
for (int i = 1; i <= title.Length; i++)
headBar += "=";
string titleSpace = "";
if (headBar.Length - (items[cursorPostion - 1].Length + 14) < 0)
{
// make int from negative to positive
int adjustedLength = Math.Abs(headBar.Length - (items[cursorPostion - 1].Length + 14));
for (int i = 1; i <= adjustedLength; i++)
{
headBar += "=";
titleSpace += " ";
}
}
string spaces = "";
for (int i = 1; i <= headBar.Length - (items[cursorPostion - 1].Length + 14); i++)
spaces += " ";
outMessage += $"[ {headBar} ]\n";
outMessage += ($"[ {title}{titleSpace} ]\n");
outMessage += ($"[ Current Item: {items[cursorPostion-1]} [{cursorPostion}/{items.Length}]{spaces} ]\n");
outMessage += ($"[ Press \"F1\" for help ]\n");
outMessage += "\n";
Console.WriteLine(outMessage);
int b = listPosition - 2;
for (int i = listPosition - 1; i < items.Length && i <= b + 5; i++)
{
if(i == cursorPostion - 1)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("[" + listPosition + "] " + items[i]);
Console.ResetColor();
listPosition++;
}
else
{
Console.WriteLine("[" + listPosition + "] " + items[i]);
listPosition++;
}
}
}
}

View File

@@ -0,0 +1,55 @@
namespace DX86.Modules;
/*
* Copyright 2024 SnapixLP
*/
public class MessageBox
{
private MessageSender ms;
public MessageBox(MessageSender ms)
{
this.ms = ms;
ms.Log("[MessageBox] MessageBox initialized");
}
public async Task<string> ShowAsync(string title, List<string> lines)
{
ms.SaveOnly(true);
Console.Clear();
string message = "";
List<int> longestLine = new List<int>();
int lineNumber = 0;
foreach (var line in lines)
{
longestLine.Add(line.Length);
lineNumber++;
}
longestLine.Add(title.Length + 4);
int maxLength = longestLine.Max();
string titleBar = new string('-', maxLength);
Console.WriteLine("+ {0} +", titleBar);
Console.WriteLine("| » {0}{1} |", title, new string(' ', (maxLength - title.Length) - 4));
Console.WriteLine("+ {0} +", titleBar);
for (int i = 0; i < lines.Count; i++)
{
Console.WriteLine("| {0}{1} |", lines[i], new string(' ', maxLength - longestLine[i]));
}
Console.WriteLine("+ {0} +", titleBar);
Console.Write(" »» Press any key to continue...");
Console.ReadKey();
ms.SaveOnly(false);
ms.SendHistory();
return "OK";
}
}

View File

@@ -0,0 +1,95 @@
namespace DX86.Modules;
/*
* Copyright 2024 SnapixLP
*/
public class MessageSender
{
private string[] messageHistory = {};
private bool saveOnly = false;
private string logfile;
public MessageSender(string logfile)
{
this.logfile = logfile;
if (!File.Exists(logfile))
{
File.Create(logfile).Close();
Log("[MessageSender] Log file created.");
}
Log("[MessageSender] Starting Logoutput to " + logfile);
Log("[MessageSender] MessageSender initialized.");
}
private async Task WriteToFileAsync(string message)
{
try
{
using (StreamWriter writer = new StreamWriter(logfile, true))
{
await writer.WriteLineAsync(message);
}
}
catch (Exception ex)
{
Error($"Failed to write to log file: {ex.Message}");
}
}
public async void Log(string message)
{
Console.ForegroundColor = ConsoleColor.Gray;
this.Send($"[{DateTime.Now}] [INFO ] {message}");
}
public async void Warn(string message)
{
Console.ForegroundColor = ConsoleColor.Yellow;
this.Send($"[{DateTime.Now}] [WARN ] {message}");
Console.ForegroundColor = ConsoleColor.Gray;
}
public async void Error(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
this.Send($"[{DateTime.Now}] [ERROR] {message}");
Console.ForegroundColor = ConsoleColor.Gray;
}
private async void Send(string message)
{
WriteToFileAsync(message);
if (!saveOnly)
{
Console.WriteLine(message);
}
messageHistory = messageHistory.Concat(new[] { message }).ToArray();
if (messageHistory.Length > 50)
{
messageHistory = messageHistory.Skip(1).ToArray();
}
}
public async void SendHistory()
{
int i = 0;
foreach (var s in messageHistory)
{
if (s.Contains("[INFO ]"))
Console.ForegroundColor = ConsoleColor.Gray;
if (s.Contains("[WARN ]"))
Console.ForegroundColor = ConsoleColor.Yellow;
if (s.Contains("[ERROR]"))
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{s}");
Console.ForegroundColor = ConsoleColor.Gray;
}
}
public void SaveOnly(bool newValue)
{
saveOnly = newValue;
}
}