Corpus-Cypher-Executable/cce/App.cs

177 lines
6.8 KiB
C#
Raw Normal View History

2022-04-18 14:53:21 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;
using static System.Console;
using static System.Threading.Thread;
namespace cce {
internal class App {
2022-04-18 22:21:06 +00:00
static internal bool Settings() {
2022-04-18 14:53:21 +00:00
// Finish Settings screen, make it interactable
2022-04-18 22:21:06 +00:00
bool saved = false;
byte reset = 0;
2022-04-19 13:02:35 +00:00
(int, int) since = (0,0);
2022-04-18 22:21:06 +00:00
while (true) {
2022-04-22 13:50:27 +00:00
Title = Program.title + " >>> Settings";
2022-04-19 13:02:35 +00:00
if (since.Item1 >= 4) {
saved = false;
since.Item1 = 0;
}
if (since.Item2 >= 4) {
reset = 0;
since.Item2 = 0;
}
2022-04-18 22:21:06 +00:00
Clear();
AppSettings.Screen(saved, reset);
ConsoleKeyInfo cki;
do {
Write(">>> ");
cki = Console.ReadKey(); WriteLine();
if ((new char[] { '0', '1', 's' }).Contains(char.ToLower(cki.KeyChar))) break;
if (cki.Key == ConsoleKey.Backspace) { AppSettings.Save(); return true; }
WriteLine("Not a valid option");
} while (true);
switch (char.ToLower(cki.KeyChar)) {
case '0':
AppSettings.Reset(out reset);
break;
case '1':
AppSettings.Lgwrite = AppSettings.Lgwrite == 1 ? (byte)0 : (byte)1;
break;
case 's':
AppSettings.Save();
saved = true;
break;
default:
break;
}
2022-04-19 13:02:35 +00:00
since.Item1++;
since.Item2++;
2022-04-18 22:21:06 +00:00
}
2022-04-18 14:53:21 +00:00
}
2022-04-22 13:50:27 +00:00
static internal void Dev() {
if (!File.Exists("dev")) {
BackgroundColor = ConsoleColor.Red;
WriteLine(" \n" +
" Unable to comply, not a dev \n" +
" ");
ResetColor();
return;
}
}
2022-04-18 14:53:21 +00:00
}
2022-04-18 22:21:06 +00:00
internal class AppSettings {
static internal byte Lgwrite = 1;
2022-04-18 14:53:21 +00:00
static internal string Common = "common.json";
static internal string Extended = "extended.json";
2022-04-22 13:50:27 +00:00
static internal string Dictionary = "dictionary.json";
2022-04-18 14:53:21 +00:00
static internal string Settings = "settings.json";
2022-04-18 22:21:06 +00:00
static internal (int, int) top_bottom = (0, 0);
static internal void Check() {
if (!File.Exists(Settings)) File.WriteAllText(Settings, JsonConvert.SerializeObject(AppSettings.ToDict()));
2022-04-18 14:53:21 +00:00
}
2022-04-18 22:21:06 +00:00
static internal Dictionary<string, string> ToDict () {
2022-04-18 14:53:21 +00:00
//Add properties and finish this method
2022-04-18 22:21:06 +00:00
return new Dictionary<string, string>() {
{ "LagWrite", Lgwrite.ToString()},
{ "Common", Common},
{ "Extended", Extended},
{ "Settings", Settings},
};
2022-04-18 14:53:21 +00:00
}
static internal void Update() {
2022-04-18 22:21:06 +00:00
Check();
var settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(Settings));
Lgwrite = byte.Parse(settings["LagWrite"]);
Common = settings["Common"];
Extended = settings["Extended"];
Settings = settings["Settings"];
}
static internal void Save() {
File.WriteAllText(Settings, JsonConvert.SerializeObject(AppSettings.ToDict()));
}
static internal void Screen(bool saved = false, byte reset = 0) {
// │ ─ ┌ ┐ └ ┘
if (saved) {
BackgroundColor = ConsoleColor.Green;
Write("Saved!");
ResetColor();
}
if (reset == 2) {
if (saved) Write(" │ ");
BackgroundColor = ConsoleColor.Blue;
Write("SETTINGS RESET!");
ResetColor();
} else if (reset == 1) {
BackgroundColor = ConsoleColor.Red;
Write("RESET ABORTED!");
ResetColor();
}
WriteLine();
top_bottom.Item1 = CursorTop;
WriteLine(@"┌──────────────────────────────────┐
[1] Laggy Console Write:
[0] Use Default Settings
[S] Save Settings
[S] Settings will save automatically upon return to menu
Press [Backspace] to return to main menu");
top_bottom.Item2 = CursorTop;
if (Lgwrite == 1) BackgroundColor = ConsoleColor.Green;
else BackgroundColor = ConsoleColor.Red;
SetCursorPosition(29, top_bottom.Item1 + 3);
Write(" ");
ResetColor();
SetCursorPosition(0, top_bottom.Item2);
}
static internal void Reset(out byte reset) {
reset = AreYouSure();
if (reset == 1) return;
Lgwrite = DefaultSettings.Lgwrite;
Common = DefaultSettings.Common;
Extended = DefaultSettings.Extended;
Settings = DefaultSettings.Settings;
}
static private byte AreYouSure() {
2022-04-19 12:42:29 +00:00
Title = "Are You Sure?";
2022-04-18 22:21:06 +00:00
SetCursorPosition(0, top_bottom.Item1);
WriteLine(@"
Are You Sure
Press Y to confirm
");
SetCursorPosition(0, top_bottom.Item2);
var cki = ReadKey();
WriteLine();
if (cki.Key == ConsoleKey.Y) return 2;
else return 1;
2022-04-18 14:53:21 +00:00
}
}
2022-04-18 22:21:06 +00:00
internal class DefaultSettings {
static internal byte Lgwrite = 1;
static internal string Common = "common.json";
static internal string Extended = "extended.json";
static internal string Settings = "settings.json";
}
2022-04-18 14:53:21 +00:00
}