-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
ServerConfiguration.cs
154 lines (123 loc) · 5.34 KB
/
ServerConfiguration.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Linq;
using SharedLibraryCore.Configuration.Attributes;
using SharedLibraryCore.Configuration.Extensions;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Configuration
{
public class ServerConfiguration : IBaseConfiguration
{
private readonly IList<IRConParser> _rconParsers = new List<IRConParser>();
private IRConParser _selectedParser;
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_IP")]
public string IPAddress { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_PORT")]
public int Port { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_PASSWORD")]
public string Password { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_RULES")]
public string[] Rules { get; set; } = [];
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_AUTO_MESSAGES")]
public string[] AutoMessages { get; set; } = [];
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_PATH")]
[ConfigurationOptional]
public string ManualLogPath { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_RCON_PARSER")]
public string RConParserVersion { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_EVENT_PARSER")]
public string EventParserVersion { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_RESERVED_SLOT")]
public int ReservedSlotNumber { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_GAME_LOG_SERVER")]
[ConfigurationOptional]
public Uri GameLogServerUrl { get; set; }
[LocalizedDisplayName("WEBFRONT_CONFIGURATION_SERVER_CUSTOM_HOSTNAME")]
[ConfigurationOptional]
public string CustomHostname { get; set; }
public IBaseConfiguration Generate()
{
ModifyParsers();
var loc = Utilities.CurrentLocalization.LocalizationIndex;
var shouldTryFindIp = loc["SETUP_SERVER_IP_AUTO"].PromptBool(defaultValue: true);
if (shouldTryFindIp)
{
this.TrySetIpAddress();
Console.WriteLine(loc["SETUP_SERVER_IP_AUTO_RESULT"].FormatExt(IPAddress));
}
else
{
while (string.IsNullOrEmpty(IPAddress))
{
var input = loc["SETUP_SERVER_IP"].PromptString();
IPAddress = input;
}
}
var defaultPort = _selectedParser.Configuration.DefaultRConPort;
Port = loc["SETUP_SERVER_PORT"].PromptInt(null, 1, ushort.MaxValue, defaultPort);
if (!string.IsNullOrEmpty(_selectedParser.Configuration.DefaultInstallationDirectoryHint))
{
var shouldTryFindPassword = loc["SETUP_RCON_PASSWORD_AUTO"].PromptBool(defaultValue: true);
if (shouldTryFindPassword)
{
var passwords = _selectedParser.TryGetRConPasswords();
if (passwords.Length > 1)
{
var (index, _) =
loc["SETUP_RCON_PASSWORD_PROMPT"].PromptSelection(loc["SETUP_RCON_PASSWORD_MANUAL"], null,
passwords.Select(pw =>
$"{pw.Item1}{(string.IsNullOrEmpty(pw.Item2) ? "" : " " + pw.Item2)}")
.ToArray());
if (index > 0)
{
Password = passwords[index - 1].Item1;
}
}
else if (passwords.Length > 0)
{
Password = passwords[0].Item1;
Console.WriteLine(loc["SETUP_RCON_PASSWORD_RESULT"].FormatExt(Password));
}
}
}
if (string.IsNullOrEmpty(Password))
{
Password = loc["SETUP_SERVER_RCON"].PromptString();
}
AutoMessages = [];
Rules = [];
return this;
}
public string Name()
{
return "ServerConfiguration";
}
public void AddRConParser(IRConParser parser)
{
_rconParsers.Add(parser);
}
public void AddEventParser(IEventParser parser)
{
}
public void ModifyParsers()
{
var loc = Utilities.CurrentLocalization.LocalizationIndex;
var parserVersions = _rconParsers.Select(p => p.Name).ToArray();
var (index, parser) = loc["SETUP_SERVER_RCON_PARSER_VERSION"].PromptSelection(parserVersions[0],
null, parserVersions);
if (index < 0)
{
return;
}
_selectedParser = _rconParsers.FirstOrDefault(p => p.Name == parser);
RConParserVersion = _selectedParser?.Name;
EventParserVersion = _selectedParser?.Name;
if (index <= 0 || _rconParsers[index].CanGenerateLogPath)
{
return;
}
Console.WriteLine(loc["SETUP_SERVER_NO_LOG"]);
ManualLogPath = loc["SETUP_SERVER_LOG_PATH"].PromptString();
}
}
}