Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit 36ad9bf

Browse files
committed
Add CommandRocket
1 parent 929e0d2 commit 36ad9bf

10 files changed

Lines changed: 105 additions & 16 deletions

File tree

Rocket.API/Commands/ICommand.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@ namespace Rocket.API.Commands
44
{
55
public interface ICommand
66
{
7+
string Name { get; }
8+
string Description { get; }
9+
string Permission { get; }
10+
711
/* todo: not used yet */
812
List<ICommand> ChildCommands { get; }
913

1014
string GetSyntax(ICommandContext context);
11-
12-
string Description { get; }
13-
14-
List<string> Aliases { get; }
15-
1615
string GetHelpText(ICommandContext context);
1716

18-
string Name { get; }
17+
List<string> Aliases { get; }
1918

20-
string Permission { get; }
19+
bool SupportsCaller(ICommandCaller caller);
2120

2221
void Execute(ICommandContext context);
2322

24-
bool SupportsCaller(ICommandCaller caller);
2523
}
2624
}

Rocket.API/Commands/ICommandContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Rocket.API.Commands
44
{
55
public interface ICommandContext
66
{
7+
ICommandContext ParentCommandContext { get; }
8+
79
string CommandPrefix { get; }
810

911
string CommandAlias { get; }

Rocket.API/Plugin/IPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IPlugin : IEventEmitter
88
string WorkingDirectory { get; }
99

1010
void Load();
11-
1211
void Unload();
12+
void Reload();
1313
}
1414
}

Rocket.Core/Commands/CommandContext.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ namespace Rocket.Core.Commands
55
{
66
public class CommandContext : ICommandContext
77
{
8-
public CommandContext(IDependencyContainer container, ICommandCaller caller, string commandPrefix, ICommand command, string commandAlias, string[] parameters)
8+
public CommandContext(IDependencyContainer container,
9+
ICommandCaller caller,
10+
string commandPrefix,
11+
ICommand command,
12+
string commandAlias,
13+
string[] parameters,
14+
ICommandContext parentCommandContext)
915
{
1016
Container = container;
1117
Caller = caller;
1218
Command = command;
1319
CommandAlias = commandAlias;
20+
ParentCommandContext = parentCommandContext;
1421
Parameters = new CommandParameters(parameters);
1522
CommandPrefix = commandPrefix;
1623
}
1724

1825
public ICommand Command { get; internal set; }
1926
public ICommandCaller Caller { get; }
27+
public ICommandContext ParentCommandContext { get; }
2028
public string CommandPrefix { get; }
2129
public string CommandAlias { get; }
2230
public ICommandParameters Parameters { get; }

Rocket.Core/Commands/DefaultCommandHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public bool HandleCommand(ICommandCaller caller, string commandLine, string pref
2525
commandLine = commandLine.Trim();
2626
string[] args = commandLine.Split(' ');
2727

28-
CommandContext context = new CommandContext(container.CreateChildContainer(), caller, prefix, null, args[0], args.Skip(1).ToArray());
28+
CommandContext context = new CommandContext(container.CreateChildContainer(),
29+
caller, prefix, null,
30+
args[0], args.Skip(1).ToArray(), null);
2931

3032
ICommand target = GetCommand(context);
3133
if (target == null)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Rocket.API.Commands;
4+
using Rocket.API.Permissions;
5+
using Rocket.API.Plugin;
6+
7+
namespace Rocket.Core.Commands.RocketCommands
8+
{
9+
public class CommandRocket : ICommand
10+
{
11+
public List<ICommand> ChildCommands => null;
12+
public string GetSyntax(ICommandContext context)
13+
{
14+
return context.Parameters.Length == 0 ? "<reload>" : "";
15+
}
16+
17+
public string Description => "Manage Rocket";
18+
public List<string> Aliases => null;
19+
20+
public string GetHelpText(ICommandContext context)
21+
{
22+
if (context.Parameters.Length == 0)
23+
return Description;
24+
25+
string cmd = context.Parameters.Get<string>(1).ToLower();
26+
if (cmd == "reload")
27+
return "Reloads all configs.";
28+
29+
return "Unknown sub command.";
30+
}
31+
32+
public string Name => "Rocket";
33+
public string Permission => "Rocket.ManageRocket";
34+
35+
public void Execute(ICommandContext context)
36+
{
37+
if (context.Parameters.Length == 0)
38+
throw new CommandParameterMismatchException();
39+
40+
string cmd = context.Parameters.Get<string>(1).ToLower();
41+
switch (cmd)
42+
{
43+
case "reload":
44+
{
45+
var permissions = context.Container.Get<IPermissionProvider>();
46+
permissions.Reload();
47+
48+
foreach (var plugin in context.Container.Get<IPluginManager>())
49+
{
50+
plugin.Reload();
51+
}
52+
53+
context.Caller.SendMessage("Reload completed.", ConsoleColor.DarkGreen);
54+
return;
55+
}
56+
}
57+
58+
throw new CommandWrongUsageException();
59+
}
60+
61+
public bool SupportsCaller(ICommandCaller caller)
62+
{
63+
return true;
64+
}
65+
}
66+
}

Rocket.Core/Plugins/Plugin.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ protected Plugin(string name, IDependencyContainer container)
4747
protected IImplementation Implementation => Container.Get<IImplementation>();
4848

4949
public void Load()
50+
{
51+
Load(false);
52+
}
53+
54+
public void Load(bool isReload)
5055
{
5156
WorkingDirectory = Path.Combine(Path.Combine(Implementation.WorkingDirectory, "Plugins"), Name);
5257

@@ -81,7 +86,7 @@ public void Load()
8186
}, DefaultTranslations);
8287
}
8388

84-
OnLoad();
89+
OnLoad(isReload);
8590
IsAlive = true;
8691

8792
if (EventManager != null)
@@ -109,10 +114,17 @@ public void Unload()
109114
}
110115
}
111116

112-
public bool IsAlive { get; internal set; }
117+
public void Reload()
118+
{
119+
Unload();
120+
Configuration?.Reload();
121+
Translations?.Reload();
122+
Load(true);
123+
}
113124

114-
protected virtual void OnLoad() { }
125+
public bool IsAlive { get; internal set; }
115126

127+
protected virtual void OnLoad(bool isReload) { }
116128
protected virtual void OnUnload() { }
117129

118130
public void Subscribe(IEventListener listener)

Rocket.Core/Rocket.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Commands\CommandParameterMismatchException.cs" />
6262
<Compile Include="Commands\RocketCommands\CommandPermission.cs" />
6363
<Compile Include="Commands\RocketCommands\CommandPermissionGroup.cs" />
64+
<Compile Include="Commands\RocketCommands\CommandRocket.cs" />
6465
<Compile Include="Logging\ProxyLogger.cs" />
6566
<Compile Include="Permissions\NotEnoughPermissionsException.cs" />
6667
<Compile Include="Configuration\ConfigurationNotLoadedException.cs" />

Rocket.Tests/TestPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Task<bool> TestEventingWithName()
5050
return promise.Task;
5151
}
5252

53-
protected override void OnLoad()
53+
protected override void OnLoad(bool isReload)
5454
{
5555
Logger.LogInformation("Hello World (From plugin)");
5656
}

examples/HelloWorldPlugin/HelloWorldPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public HelloWorldPlugin(IDependencyContainer container, ILogger logger) : base("
2323
this.logger = logger;
2424
}
2525

26-
protected override void OnLoad()
26+
protected override void OnLoad(bool isReload)
2727
{
2828
logger.LogInformation("Hello world!");
2929
}

0 commit comments

Comments
 (0)