2017-06-23 19:31:47 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Noikoio.RegexBot
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configuration loader
|
|
|
|
|
/// </summary>
|
2017-07-26 22:36:59 +00:00
|
|
|
|
class Configuration
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
public const string LogPrefix = "Config";
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
private readonly RegexBot _bot;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
private readonly string _configPath;
|
2017-11-01 20:56:06 +00:00
|
|
|
|
private DatabaseConfig _dbConfig;
|
2017-07-26 22:36:59 +00:00
|
|
|
|
private ServerConfig[] _servers;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
// The following values do not change on reload:
|
2017-06-23 19:31:47 +00:00
|
|
|
|
private string _botToken;
|
|
|
|
|
private string _currentGame;
|
|
|
|
|
|
|
|
|
|
public string BotUserToken => _botToken;
|
|
|
|
|
public string CurrentGame => _currentGame;
|
2017-11-01 20:56:06 +00:00
|
|
|
|
public DatabaseConfig Database => _dbConfig;
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
public ServerConfig[] Servers => _servers;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
public Configuration(RegexBot bot)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
_bot = bot;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
var dsc = Path.DirectorySeparatorChar;
|
|
|
|
|
_configPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
|
|
|
|
|
+ dsc + "settings.json";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<JObject> LoadFile()
|
|
|
|
|
{
|
|
|
|
|
var Log = Logger.GetLogger(LogPrefix);
|
|
|
|
|
JObject pcfg;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var ctxt = File.ReadAllText(_configPath);
|
|
|
|
|
pcfg = JObject.Parse(ctxt);
|
|
|
|
|
return pcfg;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
await Log("Config file not found! Check bot directory for settings.json file.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
|
{
|
|
|
|
|
await Log("Could not access config file. Check file permissions.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (JsonReaderException jex)
|
|
|
|
|
{
|
|
|
|
|
await Log("Failed to parse JSON.");
|
|
|
|
|
await Log(jex.GetType().Name + " " + jex.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-26 22:36:59 +00:00
|
|
|
|
/// Loads essential, unchanging values needed for bot startup. Returns false on failure.
|
2017-06-23 19:31:47 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public bool LoadInitialConfig()
|
|
|
|
|
{
|
|
|
|
|
var lt = LoadFile();
|
|
|
|
|
lt.Wait();
|
|
|
|
|
JObject conf = lt.Result;
|
|
|
|
|
if (conf == null) return false;
|
|
|
|
|
|
|
|
|
|
_botToken = conf["bot-token"]?.Value<string>();
|
|
|
|
|
if (String.IsNullOrWhiteSpace(_botToken))
|
|
|
|
|
{
|
|
|
|
|
Logger.GetLogger(LogPrefix)("Error: Bot token not defined. Cannot continue.").Wait();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_currentGame = conf["playing"]?.Value<string>();
|
|
|
|
|
|
2017-11-01 20:56:06 +00:00
|
|
|
|
_dbConfig = new DatabaseConfig(conf["database"]);
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
return true;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reloads the server portion of the configuration file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>False on failure. Specific reasons will have been sent to log.</returns>
|
|
|
|
|
public async Task<bool> ReloadServerConfig()
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
var config = await LoadFile();
|
|
|
|
|
if (config == null) return false;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
return await ProcessServerConfig(config);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a json object containing bot configuration into data usable by this program.
|
|
|
|
|
/// On success, updates the Servers values and returns true. Returns false on failure.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task<bool> ProcessServerConfig(JObject conf)
|
|
|
|
|
{
|
|
|
|
|
var Log = Logger.GetLogger(LogPrefix);
|
|
|
|
|
if (!conf["servers"].HasValues)
|
|
|
|
|
{
|
|
|
|
|
await Log("Error: No server configurations are defined.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
List<ServerConfig> newservers = new List<ServerConfig>();
|
2017-06-23 19:31:47 +00:00
|
|
|
|
await Log("Reading server configurations...");
|
|
|
|
|
foreach (JObject sconf in conf["servers"].Children<JObject>())
|
|
|
|
|
{
|
|
|
|
|
// Server name
|
2017-10-21 20:14:51 +00:00
|
|
|
|
//if (sconf["id"] == null || sconf["id"].Type != JTokenType.Integer))
|
|
|
|
|
if (sconf["id"] == null)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
2017-10-21 20:14:51 +00:00
|
|
|
|
await Log("Error: Server ID is missing within definition.");
|
2017-06-23 19:31:47 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-10-21 20:14:51 +00:00
|
|
|
|
ulong sid = sconf["id"].Value<ulong>();
|
|
|
|
|
string sname = sconf["name"]?.Value<string>();
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
2017-10-21 20:14:51 +00:00
|
|
|
|
var SLog = Logger.GetLogger(LogPrefix + "/" + (sname ?? sid.ToString()));
|
2017-06-23 19:31:47 +00:00
|
|
|
|
|
|
|
|
|
// Load server moderator list
|
|
|
|
|
EntityList mods = new EntityList(sconf["moderators"]);
|
|
|
|
|
if (sconf["moderators"] != null) await SLog("Moderator " + mods.ToString());
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
2017-11-12 03:12:24 +00:00
|
|
|
|
// Load module configurations
|
|
|
|
|
Dictionary<BotModule, object> customConfs = new Dictionary<BotModule, object>();
|
|
|
|
|
foreach (var item in _bot.Modules)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
var attr = item.GetType().GetTypeInfo()
|
|
|
|
|
.GetMethod("ProcessConfiguration").GetCustomAttribute<ConfigSectionAttribute>();
|
|
|
|
|
if (attr == null)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await SLog("No additional configuration for " + item.Name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var section = sconf[attr.SectionName];
|
|
|
|
|
if (section == null)
|
|
|
|
|
{
|
|
|
|
|
await SLog("Additional configuration not defined for " + item.Name);
|
|
|
|
|
continue;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await SLog("Loading additional configuration for " + item.Name);
|
|
|
|
|
object result;
|
2017-06-23 19:31:47 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
result = await item.ProcessConfiguration(section);
|
|
|
|
|
}
|
|
|
|
|
catch (RuleImportException ex)
|
2017-06-23 19:31:47 +00:00
|
|
|
|
{
|
2017-07-26 22:36:59 +00:00
|
|
|
|
await SLog($"{item.Name} failed to load configuration: " + ex.Message);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
|
|
|
|
customConfs.Add(item, result);
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:36:59 +00:00
|
|
|
|
|
2017-06-23 19:31:47 +00:00
|
|
|
|
// Switch to using new data
|
|
|
|
|
List<Tuple<Regex, string[]>> rulesfinal = new List<Tuple<Regex, string[]>>();
|
2017-11-12 03:12:24 +00:00
|
|
|
|
newservers.Add(new ServerConfig(sid, mods, new ReadOnlyDictionary<BotModule, object>(customConfs)));
|
2017-06-23 19:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_servers = newservers.ToArray();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|