forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminCommandHandler.cs
More file actions
78 lines (67 loc) · 2.31 KB
/
AdminCommandHandler.cs
File metadata and controls
78 lines (67 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Reflection;
using log4net;
using L2dotNET.Attributes;
using L2dotNET.Commands;
using L2dotNET.model.player;
using L2dotNET.Utility;
namespace L2dotNET.Handlers
{
public class AdminCommandHandler : IAdminCommandHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(AdminCommandHandler));
private readonly SortedList<string, AAdminCommand> _commands = new SortedList<string, AAdminCommand>();
private static volatile AdminCommandHandler _instance;
private static readonly object SyncRoot = new object();
public static AdminCommandHandler Instance
{
get
{
if (_instance != null)
return _instance;
lock (SyncRoot)
{
if (_instance == null)
_instance = new AdminCommandHandler();
}
return _instance;
}
}
public void Initialize()
{
IEnumerable<Type> typelist = Utilz.GetTypesInNamespace(Assembly.GetExecutingAssembly(), "L2dotNET.Commands.Admin");
foreach (Type t in typelist)
Register(Activator.CreateInstance(t));
Log.Info($"AdminAccess: loaded {_commands.Count} commands.");
}
public void Request(L2Player admin, string alias)
{
string cmd = alias;
if (alias.Contains(" "))
cmd = alias.Split(' ')[0];
if (!_commands.ContainsKey(cmd))
{
admin.SendMessage($"Command {cmd} not exists.");
admin.SendActionFailed();
return;
}
AAdminCommand processor = _commands[cmd];
try
{
processor.Use(admin, alias);
}
catch (Exception sss)
{
admin.SendMessage("Probably syntax eror.");
Log.Error(sss);
}
}
public void Register(object processor)
{
CommandAttribute attribute =
(CommandAttribute) processor.GetType().GetCustomAttribute(typeof(CommandAttribute));
_commands.Add(attribute.CommandName,(AAdminCommand) processor);
}
}
}