2022-07-23 06:41:49 +00:00
|
|
|
|
using CommandLine;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
namespace RegexBot;
|
2022-12-05 00:30:50 +00:00
|
|
|
|
class Configuration {
|
2018-05-06 20:09:17 +00:00
|
|
|
|
/// <summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// Token used for Discord authentication.
|
2018-05-06 20:09:17 +00:00
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
internal string BotToken { get; }
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// List of assemblies to load, by file. Paths are always relative to the bot directory.
|
|
|
|
|
/// </summary>
|
2022-07-21 03:34:29 +00:00
|
|
|
|
internal IReadOnlyList<string> Assemblies { get; }
|
2018-05-11 06:13:00 +00:00
|
|
|
|
|
2022-12-05 00:30:50 +00:00
|
|
|
|
public JObject ServerConfigs { get; }
|
|
|
|
|
|
|
|
|
|
// SQL properties:
|
|
|
|
|
public string? Host { get; }
|
|
|
|
|
public string? Database { get; }
|
|
|
|
|
public string Username { get; }
|
|
|
|
|
public string Password { get; }
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets up instance configuration object from file and command line parameters.
|
|
|
|
|
/// </summary>
|
2022-12-05 00:30:50 +00:00
|
|
|
|
internal Configuration() {
|
2022-07-23 06:41:49 +00:00
|
|
|
|
var args = CommandLineParameters.Parse(Environment.GetCommandLineArgs());
|
2022-12-05 00:30:50 +00:00
|
|
|
|
var path = args?.ConfigFile!;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
JObject conf;
|
|
|
|
|
try {
|
|
|
|
|
var conftxt = File.ReadAllText(path);
|
|
|
|
|
conf = JObject.Parse(conftxt);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
string pfx;
|
|
|
|
|
if (ex is JsonException) pfx = "Unable to parse configuration: ";
|
|
|
|
|
else pfx = "Unable to access configuration: ";
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
throw new Exception(pfx + ex.Message, ex);
|
|
|
|
|
}
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-07-23 06:41:49 +00:00
|
|
|
|
BotToken = ReadConfKey<string>(conf, nameof(BotToken), true);
|
2018-06-05 00:15:18 +00:00
|
|
|
|
|
2022-07-21 03:34:29 +00:00
|
|
|
|
try {
|
|
|
|
|
Assemblies = Common.Utilities.LoadStringOrStringArray(conf[nameof(Assemblies)]).AsReadOnly();
|
|
|
|
|
} catch (ArgumentNullException) {
|
|
|
|
|
Assemblies = Array.Empty<string>();
|
|
|
|
|
} catch (ArgumentException) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
throw new Exception($"'{nameof(Assemblies)}' is not properly specified in configuration.");
|
2018-05-06 20:09:17 +00:00
|
|
|
|
}
|
2022-07-23 06:41:49 +00:00
|
|
|
|
|
2024-06-09 04:07:35 +00:00
|
|
|
|
var dbconf = (conf["DatabaseOptions"]?.Value<JObject>())
|
|
|
|
|
?? throw new Exception("Database settings were not specified in configuration.");
|
2022-12-05 00:30:50 +00:00
|
|
|
|
Host = ReadConfKey<string>(dbconf, nameof(Host), false);
|
|
|
|
|
Database = ReadConfKey<string?>(dbconf, nameof(Database), false);
|
|
|
|
|
Username = ReadConfKey<string>(dbconf, nameof(Username), true);
|
|
|
|
|
Password = ReadConfKey<string>(dbconf, nameof(Password), true);
|
|
|
|
|
|
|
|
|
|
ServerConfigs = conf["Servers"]?.Value<JObject>();
|
|
|
|
|
if (ServerConfigs == null) throw new Exception("No server configurations were specified.");
|
2022-07-23 06:41:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static T? ReadConfKey<T>(JObject jc, string key, [DoesNotReturnIf(true)] bool failOnEmpty) {
|
|
|
|
|
if (jc.ContainsKey(key)) return jc[key]!.Value<T>();
|
|
|
|
|
if (failOnEmpty) throw new Exception($"'{key}' must be specified in the instance configuration.");
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CommandLineParameters {
|
2022-12-05 00:30:50 +00:00
|
|
|
|
[Option('c', "config", Default = "config.json")]
|
|
|
|
|
public string? ConfigFile { get; set; } = null;
|
2022-07-23 06:41:49 +00:00
|
|
|
|
|
|
|
|
|
public static CommandLineParameters? Parse(string[] args) {
|
|
|
|
|
CommandLineParameters? result = null;
|
|
|
|
|
|
|
|
|
|
new Parser(settings => {
|
|
|
|
|
settings.IgnoreUnknownArguments = true;
|
|
|
|
|
settings.AutoHelp = false;
|
|
|
|
|
settings.AutoVersion = false;
|
|
|
|
|
}).ParseArguments<CommandLineParameters>(args)
|
|
|
|
|
.WithParsed(p => result = p)
|
|
|
|
|
.WithNotParsed(e => { /* ignore */ });
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-05-06 20:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|