-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathConsoleManager.cs
More file actions
86 lines (75 loc) · 2.71 KB
/
ConsoleManager.cs
File metadata and controls
86 lines (75 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WDBXEditor.Storage;
namespace WDBXEditor.ConsoleHandler
{
public static class ConsoleManager
{
public static bool ConsoleMode { get; set; } = false;
public static Dictionary<string, HandleCommand> CommandHandlers = new Dictionary<string, HandleCommand>();
public delegate void HandleCommand(string[] args);
public static void ConsoleMain(string[] args)
{
Database.LoadDefinitions().Wait();
if (CommandHandlers.ContainsKey(args[0].ToLower()))
InvokeHandler(args[0], args.Skip(1).ToArray());
}
public static bool InvokeHandler(string command, params string[] args)
{
try
{
command = command.ToLower();
if (CommandHandlers.ContainsKey(command))
{
CommandHandlers[command].Invoke(args);
return true;
}
else
return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("");
return false;
}
}
public static void LoadCommandDefinitions()
{
//Argument commands
//DefineCommand("-console", ConsoleManager.LoadConsoleMode);
DefineCommand("-export", ConsoleCommands.ExportArgCommand);
DefineCommand("-sqldump", ConsoleCommands.SqlDumpArgCommand);
DefineCommand("-extract", ConsoleCommands.ExtractCommand);
DefineCommand("-import", ConsoleCommands.ImportArgCommand);
}
/// <summary>
/// Converts args into keyvalue pair
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static Dictionary<string, string> ParseCommand(string[] args)
{
Dictionary<string, string> keyvalues = new Dictionary<string, string>();
for (int i = 0; i < args.Length; i++)
{
if (i == args.Length - 1)
break;
string key = args[i].ToLower();
string value = args[++i];
if (value[0] == '"' && value[value.Length - 1] == '"')
value = value.Substring(1, value.Length - 2);
keyvalues.Add(key, value);
}
return keyvalues;
}
private static void DefineCommand(string command, HandleCommand handler)
{
CommandHandlers[command.ToLower()] = handler;
}
}
}