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

Commit 91efbe8

Browse files
committed
Add FileLogger and add Command Summary
1 parent 0a19ea0 commit 91efbe8

22 files changed

Lines changed: 297 additions & 129 deletions

Rocket.API/Commands/ICommand.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,20 @@ public interface ICommand
4141
string[] Aliases { get; }
4242

4343
/// <summary>
44-
/// The description of the command.
45-
/// <para>
46-
/// <b>This proprty can return null</b> but its advised to always include a description for the command.
47-
/// </para>
44+
/// The command summary.
45+
/// <para><b>This proprty must not return null.</b>.</para>
4846
/// </summary>
4947
/// <example>
5048
/// <c>"This command heals you or someone else."</c>
5149
/// </example>
50+
string Summary { get; }
51+
52+
/// <summary>
53+
/// The full description of the command.
54+
/// <para>
55+
/// <b>This proprty can return null</b>.
56+
/// </para>
57+
/// </summary>
5258
string Description { get; }
5359

5460
/// <summary>
@@ -72,6 +78,7 @@ public interface ICommand
7278
/// <summary>
7379
/// The command syntax will be shown to the <see cref="ICommandCaller" /> when the command was not used correctly.
7480
/// <para>An output for the above example could be "/heal [player] &lt;amount&gt;".</para>
81+
/// <para>The syntax should not contain sub command usage.</para>
7582
/// <para><b>This property must never return null.</b></para>
7683
/// </summary>
7784
/// <remarks>

Rocket.ConsoleImplementation/ConsoleCommandCaller.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using System;
22
using Rocket.API.Commands;
3+
using Rocket.API.Logging;
34
using Rocket.API.Permissions;
5+
using Rocket.Core.Logging;
46

57
namespace Rocket.ConsoleImplementation
68
{
79
public class ConsoleCommandCaller : IConsoleCommandCaller
810
{
11+
private readonly ILogger logger;
12+
13+
public ConsoleCommandCaller(ILogger logger)
14+
{
15+
ConsoleLogger.SkipTypeFromLogging(GetType());
16+
this.logger = logger;
17+
}
18+
919
public int CompareTo(object obj) => throw new NotImplementedException();
1020

1121
public int CompareTo(IIdentifiable other) => throw new NotImplementedException();
@@ -22,10 +32,7 @@ public class ConsoleCommandCaller : IConsoleCommandCaller
2232

2333
public void SendMessage(string message, ConsoleColor? color = null, params object[] bindings)
2434
{
25-
ConsoleColor tmp = Console.ForegroundColor;
26-
Console.ForegroundColor = color ?? tmp;
27-
Console.WriteLine(message, bindings);
28-
Console.ForegroundColor = tmp;
35+
logger.LogInformation(message, color, bindings);
2936
}
3037

3138
public string ToString(string format, IFormatProvider formatProvider) => Id;

Rocket.ConsoleImplementation/ConsoleImplementation.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using System.Reflection;
55
using Rocket.API;
66
using Rocket.API.Commands;
7+
using Rocket.API.Logging;
78
using Rocket.API.Plugins;
9+
using Rocket.Core.Logging;
810

911
namespace Rocket.ConsoleImplementation
1012
{
1113
public class ConsoleImplementation : IImplementation
1214
{
13-
private readonly ConsoleCommandCaller consoleCommandCaller = new ConsoleCommandCaller();
15+
private ConsoleCommandCaller consoleCommandCaller;
1416

1517
public IEnumerable<string> Capabilities => new List<string>();
1618
public string Name => "ConsoleHost";
@@ -19,14 +21,16 @@ public class ConsoleImplementation : IImplementation
1921

2022
public void Init(IRuntime runtime)
2123
{
22-
23-
Console.WriteLine("Loading...");
2424
runtime.Container.Resolve<IPluginManager>().Init();
2525
ICommandHandler cmdHandler = runtime.Container.Resolve<ICommandHandler>();
2626

2727
Directory.SetCurrentDirectory(WorkingDirectory);
2828

29-
Console.WriteLine("Loaded; type \"help\" for help or \"exit\" to exit.");
29+
ILogger logger = runtime.Container.Resolve<ILogger>();
30+
consoleCommandCaller = new ConsoleCommandCaller(logger);
31+
32+
logger.LogInformation("Loaded; type \"help\" for help or \"exit\" to exit.");
33+
3034
Console.Write(">");
3135

3236
string line;

Rocket.Core/Commands/CommandAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ public class CommandAttribute : Attribute
77
{
88
public string Name { get; set; }
99

10-
public string Description { get; set; }
10+
public string Summary { get; set; }
1111

1212
public string Permission { get; set; }
1313

1414
public string Syntax { get; set; }
15+
16+
public string Description { get; set; }
1517
}
1618
}

Rocket.Core/Commands/CommandAttributeWrapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private string BuildSyntaxFromMethod()
4747
public MethodBase Method { get; set; }
4848

4949
public string Name { get; }
50+
public string Summary => Attribute?.Summary;
5051
public string Description => Attribute?.Description;
5152
public string Permission => Attribute?.Permission;
5253
public string Syntax { get; }

Rocket.Core/Commands/DefaultCommandHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Linq;
55
using Rocket.API.Commands;
66
using Rocket.API.DependencyInjection;
7+
using Rocket.API.Logging;
78
using Rocket.API.Permissions;
9+
using Rocket.Core.Logging;
810
using Rocket.Core.Permissions;
911

1012
namespace Rocket.Core.Commands
@@ -26,6 +28,7 @@ public bool HandleCommand(ICommandCaller caller, string commandLine, string pref
2628
string[] args = commandLine.Split(' ');
2729

2830
IDependencyContainer contextContainer = container.CreateChildContainer();
31+
contextContainer.Resolve<ILogger>().LogInformation($"{caller.Name} executed command: \"{commandLine}\"");
2932

3033
CommandContext context = new CommandContext(contextContainer,
3134
caller, prefix, null,

Rocket.Core/Commands/RocketCommands/CommandHelp.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class CommandHelp : ICommand
1010
{
1111
public string Name => "Help";
1212
public string[] Aliases => new[] { "h" };
13-
public string Description => "Provides help for all or a specific command";
13+
public string Summary => "Provides help for all or a specific command.";
14+
public string Description => null;
1415
public string Permission => "Rocket.Help";
1516
public string Syntax => "[command] [1. sub command] [2. sub command] [...]";
1617
public ISubCommand[] ChildCommands => null;
@@ -51,6 +52,9 @@ public void Execute(ICommandContext context)
5152

5253
context.Caller.SendMessage(GetCommandUsage(cmd, prefix), ConsoleColor.Blue);
5354

55+
if(cmd.Description != null)
56+
context.Caller.SendMessage(cmd.Description, ConsoleColor.Cyan);
57+
5458
var childCommands =
5559
(cmd.ChildCommands?.Cast<ICommand>().ToList() ?? new List<ICommand>())
5660
.Where(c => HasAccess(c, context.Caller, permissionProvider))
@@ -60,10 +64,9 @@ public void Execute(ICommandContext context)
6064
if (childCommands.Count == 0)
6165
return;
6266

63-
context.Caller.SendMessage("Sub commands:", ConsoleColor.DarkBlue);
6467
foreach (var subCmd in childCommands)
6568
{
66-
context.Caller.SendMessage(GetCommandUsage(subCmd, rootPrefix + cmd.Name + " "), ConsoleColor.Cyan);
69+
context.Caller.SendMessage(GetCommandUsage(subCmd, rootPrefix + cmd.Name.ToLower() + " "), ConsoleColor.Blue);
6770
}
6871

6972
return;
@@ -87,7 +90,7 @@ public bool HasAccess(ICommand command, ICommandCaller caller, IPermissionProvid
8790

8891
public string GetCommandUsage(ICommand command, string prefix)
8992
{
90-
return prefix + command.Name + (string.IsNullOrEmpty(command.Syntax) ? "" : " " + command.Syntax) + (string.IsNullOrEmpty(command.Description) ? "" : ": " + command.Description);
93+
return prefix + command.Name.ToLower() + (string.IsNullOrEmpty(command.Syntax) ? "" : " " + command.Syntax) + (string.IsNullOrEmpty(command.Summary) ? "" : ": " + command.Summary);
9194
}
9295
}
9396
}

Rocket.Core/Commands/RocketCommands/CommandMigrateConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class CommandMigrateConfig : ICommand
1212
{
1313
public string Name => "MigrateConfig";
1414
public string[] Aliases => null;
15-
public string Description => "Migrates configs from one type to another.";
15+
public string Summary => "Migrates configs from one type to another.";
16+
public string Description => null;
1617
public string Permission => "Rocket.MigrateConfig";
1718
public string Syntax => "[<from type> <to type> <path>]";
1819
public ISubCommand[] ChildCommands { get; }

Rocket.Core/Commands/RocketCommands/CommandPermission.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public class CommandPermission : ICommand
1010
{
1111
public string Name => "Permission";
1212
public string Permission => "Rocket.Permissions.ManagePermissions";
13-
public string Syntax => "<add/remove/reload>";
14-
public string Description => "Manages rocket permissions";
13+
public string Syntax => "";
14+
public string Summary => "Manages rocket permissions.";
15+
public string Description => null;
1516

1617
public ISubCommand[] ChildCommands => new ISubCommand[]
1718
{new PermissionSubCommandAdd(), new PermissionSubCommandRemove(), new PermissionSubCommandReload()};
@@ -29,7 +30,8 @@ public void Execute(ICommandContext context)
2930
public abstract class PermissionSubCommandUpdate : ISubCommand
3031
{
3132
public abstract string Name { get; }
32-
public abstract string Description { get; }
33+
public abstract string Summary { get; }
34+
public string Description => null;
3335
public abstract string Permission { get; }
3436
public string Syntax => "<[p]layer/[g]roup> [target] [permission]";
3537

@@ -93,7 +95,7 @@ protected abstract void UpdatePermission(ICommandCaller caller, IPermissionProvi
9395
public class PermissionSubCommandAdd : PermissionSubCommandUpdate
9496
{
9597
public override string Name => "Add";
96-
public override string Description => "Adds a permission to a group or player";
98+
public override string Summary => "Adds a permission to a group or player.";
9799
public override string Permission => "Rocket.Permissions.ManagePermissions.Add";
98100
public override string[] Aliases => new[] {"a", "+"};
99101

@@ -111,7 +113,7 @@ protected override void UpdatePermission(ICommandCaller caller, IPermissionProvi
111113
public class PermissionSubCommandRemove : PermissionSubCommandUpdate
112114
{
113115
public override string Name => "Remove";
114-
public override string Description => "Removes permission from a group or player";
116+
public override string Summary => "Removes permission from a group or player.";
115117
public override string Permission => "Rocket.Permissions.ManagePermissions.Remove";
116118
public override string[] Aliases => new[] {"r", "-"};
117119

@@ -130,7 +132,8 @@ protected override void UpdatePermission(ICommandCaller caller, IPermissionProvi
130132
public class PermissionSubCommandReload : ISubCommand
131133
{
132134
public string Name => "Reload";
133-
public string Description => "Reloads permissions";
135+
public string Summary => "Reloads permissions.";
136+
public string Description => null;
134137
public string Permission => "Rocket.Permissions.ManagePermissions.Reload";
135138
public string Syntax => "";
136139
public ISubCommand[] ChildCommands => null;

Rocket.Core/Commands/RocketCommands/CommandPermissionGroup.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ namespace Rocket.Core.Commands.RocketCommands
88
{
99
public class CommandPermissionGroup : ICommand
1010
{
11-
public string Syntax => "<add/remove> <player> <group>";
11+
public string Syntax => "";
1212

1313
public ISubCommand[] ChildCommands => new ISubCommand[]
1414
{new PermissionGroupSubCommandAdd(), new PermissionGroupSubCommandRemove()};
1515

16-
public string Description => "Manages permission groups";
16+
public string Summary => "Manages permission groups.";
17+
public string Description => null;
1718

1819
public string[] Aliases => new[] {"PG"};
1920

@@ -31,7 +32,8 @@ public void Execute(ICommandContext context)
3132
public abstract class PermissionGroupSubCommandUpdate : ISubCommand
3233
{
3334
public abstract string Name { get; }
34-
public abstract string Description { get; }
35+
public abstract string Summary { get; }
36+
public string Description => null;
3537
public abstract string Permission { get; }
3638
public string Syntax => "<player> <group>";
3739

@@ -69,7 +71,7 @@ protected abstract void UpdateGroup(ICommandCaller caller, IPermissionProvider p
6971
public class PermissionGroupSubCommandAdd : PermissionGroupSubCommandUpdate
7072
{
7173
public override string Name => "Add";
72-
public override string Description => "Adds a player to a permission group";
74+
public override string Summary => "Adds a player to a permission group.";
7375
public override string Permission => "Rocket.Permissions.ManageGroups.Add";
7476
public override string[] Aliases => new[] {"a", "+"};
7577

@@ -87,7 +89,7 @@ protected override void UpdateGroup(ICommandCaller caller, IPermissionProvider p
8789
public class PermissionGroupSubCommandRemove : PermissionGroupSubCommandUpdate
8890
{
8991
public override string Name => "Remove";
90-
public override string Description => "Removes a player from a permission group";
92+
public override string Summary => "Removes a player from a permission group.";
9193
public override string Permission => "Rocket.Permissions.ManageGroups.Remove";
9294
public override string[] Aliases => new[] {"r", "-"};
9395

0 commit comments

Comments
 (0)