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>
|
2018-03-17 20:51:09 +00:00
|
|
|
|
/// Contains a server's ModCommands configuration.
|
2017-12-08 21:57:35 +00:00
|
|
|
|
/// </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-22 06:27:19 +00:00
|
|
|
|
public ConfigItem(ModCommands 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.");
|
|
|
|
|
}
|
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);
|
2018-03-16 05:00:34 +00:00
|
|
|
|
foreach (var def in inconf.Children<JProperty>())
|
2017-12-08 21:57:35 +00:00
|
|
|
|
{
|
2018-03-16 05:00:34 +00:00
|
|
|
|
string label = def.Name;
|
|
|
|
|
var cmd = Command.CreateInstance(instance, def);
|
|
|
|
|
if (commands.ContainsKey(cmd.Trigger))
|
|
|
|
|
throw new RuleImportException(
|
|
|
|
|
$"{label}: 'command' value must not be equal to that of another definition. " +
|
|
|
|
|
$"Given value is being used for \"{commands[cmd.Trigger].Label}\".");
|
2017-12-08 21:57:35 +00:00
|
|
|
|
|
2018-03-16 05:00:34 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|