2017-12-08 21:57:35 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Noikoio.RegexBot.ConfigItem;
|
2018-03-10 06:17:55 +00:00
|
|
|
|
using Noikoio.RegexBot.Module.ModCommands.Commands;
|
2017-12-08 21:57:35 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
2018-03-10 06:17:55 +00:00
|
|
|
|
namespace Noikoio.RegexBot.Module.ModCommands
|
2017-12-08 21:57:35 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents ModTools configuration within one server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class ConfigItem
|
|
|
|
|
{
|
2018-03-10 06:17:55 +00:00
|
|
|
|
private readonly ReadOnlyDictionary<string, Command> _cmdInstances;
|
2017-12-19 06:05:37 +00:00
|
|
|
|
|
2018-03-10 06:17:55 +00:00
|
|
|
|
public ReadOnlyDictionary<string, Command> Commands => _cmdInstances;
|
2017-12-08 21:57:35 +00:00
|
|
|
|
|
2018-03-10 06:17:55 +00:00
|
|
|
|
public ConfigItem(CommandListener instance, JToken inconf)
|
2017-12-08 21:57:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (inconf.Type != JTokenType.Object)
|
|
|
|
|
{
|
|
|
|
|
throw new RuleImportException("Configuration for this section is invalid.");
|
|
|
|
|
}
|
|
|
|
|
var config = (JObject)inconf;
|
2017-12-19 06:05:37 +00:00
|
|
|
|
|
2018-03-10 06:17:55 +00:00
|
|
|
|
// Command instance creation
|
|
|
|
|
var commands = new Dictionary<string, Command>(StringComparer.OrdinalIgnoreCase);
|
2017-12-08 21:57:35 +00:00
|
|
|
|
var commandconf = config["Commands"];
|
|
|
|
|
if (commandconf != null)
|
|
|
|
|
{
|
|
|
|
|
if (commandconf.Type != JTokenType.Object)
|
|
|
|
|
{
|
|
|
|
|
throw new RuleImportException("CommandDefs is not properly defined.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var def in commandconf.Children<JProperty>())
|
|
|
|
|
{
|
|
|
|
|
string label = def.Name;
|
2018-03-10 06:17:55 +00:00
|
|
|
|
var cmd = Command.CreateInstance(instance, def);
|
|
|
|
|
if (commands.ContainsKey(cmd.Trigger))
|
2017-12-08 21:57:35 +00:00
|
|
|
|
throw new RuleImportException(
|
|
|
|
|
$"{label}: 'command' value must not be equal to that of another definition. " +
|
2018-03-10 06:17:55 +00:00
|
|
|
|
$"Given value is being used for \"{commands[cmd.Trigger].Label}\".");
|
2017-12-08 21:57:35 +00:00
|
|
|
|
|
2018-03-10 06:17:55 +00:00
|
|
|
|
commands.Add(cmd.Trigger, cmd);
|
2017-12-08 21:57:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-10 06:17:55 +00:00
|
|
|
|
_cmdInstances = new ReadOnlyDictionary<string, Command>(commands);
|
2017-12-08 21:57:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|