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
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
namespace RegexBot;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains essential instance configuration for this bot including Discord connection settings, service configuration,
|
|
|
|
|
/// and command-line options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class InstanceConfig {
|
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-07-23 06:41:49 +00:00
|
|
|
|
public string? SqlHost { get; }
|
|
|
|
|
public string? SqlDatabase { get; }
|
|
|
|
|
public string SqlUsername { get; }
|
|
|
|
|
public string SqlPassword { 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-07-21 01:55:08 +00:00
|
|
|
|
internal InstanceConfig() {
|
2022-07-23 06:41:49 +00:00
|
|
|
|
var args = CommandLineParameters.Parse(Environment.GetCommandLineArgs());
|
|
|
|
|
var path = args?.ConfigFile ?? Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)
|
|
|
|
|
+ Path.DirectorySeparatorChar + "." + Path.DirectorySeparatorChar + "instance.json";
|
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
|
|
|
|
|
|
|
|
|
SqlHost = ReadConfKey<string>(conf, nameof(SqlHost), false);
|
|
|
|
|
SqlDatabase = ReadConfKey<string?>(conf, nameof(SqlDatabase), false);
|
|
|
|
|
SqlUsername = ReadConfKey<string>(conf, nameof(SqlUsername), true);
|
|
|
|
|
SqlPassword = ReadConfKey<string>(conf, nameof(SqlPassword), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Command line options
|
|
|
|
|
/// </summary>
|
|
|
|
|
class CommandLineParameters {
|
|
|
|
|
[Option('c', "config", Default = null,
|
|
|
|
|
HelpText = "Custom path to instance configuration. Defaults to instance.json in bot directory.")]
|
|
|
|
|
public string ConfigFile { get; set; } = null!;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Command line arguments parsed here. Depending on inputs, the program can exit here.
|
|
|
|
|
/// </summary>
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|