forked from berichan/SysBot.ACNHOrders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
130 lines (110 loc) · 4.56 KB
/
Program.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.IO;
using System.Threading.Tasks;
using System.Text.Json;
using System.Threading;
namespace SysBot.ACNHOrders
{
internal static class Program
{
private const string DefaultConfigPath = "config.json";
private const string DefaultTwitchPath = "twitch.json";
private const string DefaultSocketServerAPIPath = "server.json";
private static async Task Main(string[] args)
{
string configPath;
// Set up logging for Console Window
SysBot.Base.LogUtil.Forwarders.Add(Logger);
static void Logger(string msg, string identity) => Console.WriteLine(GetMessage(msg, identity));
static string GetMessage(string msg, string identity) => $"> [{DateTime.Now:hh:mm:ss}] - {identity}: {msg}";
Console.WriteLine("Starting up...");
if (args.Length > 0)
{
if (args.Length > 1)
{
Console.WriteLine("Too many arguments supplied and will be ignored.");
configPath = DefaultConfigPath;
}
else {
configPath = args[0];
}
}
else {
configPath = DefaultConfigPath;
}
if (!File.Exists(configPath))
{
CreateConfigQuit(configPath);
return;
}
if (!File.Exists(DefaultTwitchPath))
SaveConfig(new TwitchConfig(), DefaultTwitchPath);
if (!File.Exists(DefaultSocketServerAPIPath))
SaveConfig(new SocketAPI.SocketAPIServerConfig(), DefaultSocketServerAPIPath);
var json = File.ReadAllText(configPath);
var config = JsonSerializer.Deserialize<CrossBotConfig>(json);
if (config == null)
{
Console.WriteLine("Failed to deserialize configuration file.");
WaitKeyExit();
return;
}
json = File.ReadAllText(DefaultTwitchPath);
var twitchConfig = JsonSerializer.Deserialize<TwitchConfig>(json);
if (twitchConfig == null)
{
Console.WriteLine("Failed to deserialize twitch configuration file.");
WaitKeyExit();
return;
}
json = File.ReadAllText(DefaultSocketServerAPIPath);
var serverConfig = JsonSerializer.Deserialize<SocketAPI.SocketAPIServerConfig>(json);
if (serverConfig == null)
{
Console.WriteLine("Failed to deserialize Socket API Server configuration file.");
WaitKeyExit();
return;
}
SaveConfig(config, configPath);
SaveConfig(twitchConfig, DefaultTwitchPath);
SaveConfig(serverConfig, DefaultSocketServerAPIPath);
if (serverConfig.EnableDedicatedConnection)
{
SocketAPI.SocketAPIConsoleConnectionConfig? devConsoleConfig = SocketAPI.DedicatedConnection.LoadDevConfigs();
SocketAPI.SocketAPIConsoleConnectionConfig consoleConfig = new();
consoleConfig.IP = config.IP;
consoleConfig.Port = config.Port;
Globals.DedicatedConnection = new(devConsoleConfig ?? consoleConfig);
await Globals.DedicatedConnection.Start();
}
var env = SocketAPI.EnvParser.ParseFile(".env");
SocketAPI.SocketAPIServer server = SocketAPI.SocketAPIServer.shared;
if ((string?)env?["debug"] == "true" && (string?)env?["awaitsysbot"] == "true")
{
Console.WriteLine("SocketAPIServer awaited.");
await server.Start(serverConfig);
}
else
_ = server.Start(serverConfig);
await BotRunner.RunFrom(config, CancellationToken.None, twitchConfig).ConfigureAwait(false);
WaitKeyExit();
}
private static void SaveConfig<T>(T config, string path)
{
var options = new JsonSerializerOptions {WriteIndented = true};
var json = JsonSerializer.Serialize(config, options);
File.WriteAllText(path, json);
}
private static void CreateConfigQuit(string configPath)
{
SaveConfig(new CrossBotConfig {IP = "192.168.0.1", Port = 6000}, configPath);
Console.WriteLine("Created blank config file. Please configure it and restart the program.");
WaitKeyExit();
}
private static void WaitKeyExit()
{
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}