2016-10-09 12:27:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace Galactic_Colors_Control_Server.Commands
|
|
|
|
|
{
|
|
|
|
|
class Manager
|
|
|
|
|
{
|
|
|
|
|
public static Dictionary<string, ICommand> commands { get; private set; } = new Dictionary<string, ICommand>();
|
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Find all ICommand and add them to commands
|
|
|
|
|
/// </summary>
|
2016-10-09 12:27:08 +00:00
|
|
|
|
public static void Load()
|
|
|
|
|
{
|
2016-10-10 11:41:14 +00:00
|
|
|
|
//C# is magic
|
2016-10-09 12:27:08 +00:00
|
|
|
|
IEnumerable<ICommand> coms = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ICommand)) && x.GetConstructor(Type.EmptyTypes) != null).Select(x => Activator.CreateInstance(x) as ICommand);
|
|
|
|
|
foreach (ICommand com in coms)
|
|
|
|
|
{
|
|
|
|
|
commands.Add(com.Name, com);
|
|
|
|
|
Logger.Write("Added command " + com.GetType().Name, Logger.logType.debug);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute sended command
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args">command with args</param>
|
|
|
|
|
/// <param name="soc">Sender socket</param>
|
|
|
|
|
/// <param name="server">Is server?</param>
|
2016-10-09 12:27:08 +00:00
|
|
|
|
public static void Execute(string[] args, Socket soc = null, bool server = false)
|
|
|
|
|
{
|
|
|
|
|
if (commands.ContainsKey(args[0]))
|
|
|
|
|
{
|
|
|
|
|
ICommand command = commands[args[0]];
|
2016-10-11 10:09:51 +00:00
|
|
|
|
if (CanAccess(command, soc, server))
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-10-11 10:09:51 +00:00
|
|
|
|
if (command.IsClientSide)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-10-11 10:09:51 +00:00
|
|
|
|
Utilities.Return("It's a client side command", soc, server);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (args.Length > command.minArgs)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-10-11 10:09:51 +00:00
|
|
|
|
if (args.Length - 1 <= command.maxArgs)
|
|
|
|
|
{
|
|
|
|
|
command.Execute(args, soc, server);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Utilities.Return("Command " + command.Name + " require at most " + command.minArgs + " argument(s).", soc, server);
|
|
|
|
|
}
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
2016-10-11 10:09:51 +00:00
|
|
|
|
|
2016-10-09 12:27:08 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2016-10-11 10:09:51 +00:00
|
|
|
|
Utilities.Return("Command " + command.Name + " require at least " + command.minArgs + " argument(s).", soc, server);
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-10-09 15:58:14 +00:00
|
|
|
|
Utilities.Return("Unknown command : " + args[0], soc, server);
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-10-09 15:58:14 +00:00
|
|
|
|
Utilities.Return("Unknown command : " + args[0], soc, server);
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check is socket can use specified command
|
|
|
|
|
/// </summary>
|
2016-10-09 12:27:08 +00:00
|
|
|
|
public static bool CanAccess(ICommand command, Socket soc = null, bool server = false)
|
|
|
|
|
{
|
|
|
|
|
if (server)
|
|
|
|
|
{
|
|
|
|
|
return command.IsServer;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (command.IsClient)
|
|
|
|
|
{
|
2016-10-09 15:58:14 +00:00
|
|
|
|
if(!Utilities.IsConnect(soc))
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
return command.IsNoConnect;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|