Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (90 loc) · 3.49 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (90 loc) · 3.49 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
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);
SocketAPI.SocketAPIServer server = SocketAPI.SocketAPIServer.shared;
_ = 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();
}
}
}