ModernUO/Projects/UOContent/Commands/GenCommands.cs
Kamron Batman fffda53263
fix: Adds command help, webpage, and fixes issues with other commands (#1669)
### Summary

- Fixes `[AdvancedSearch` being accessible by players 😱 
- Adds `[GenCommands` to generate the same commands html page on https://muo.gg/commands.
- Fixes `[helpinfo` so all commands properly show up!

> [!WARNING]  
> ### Developer Warning:
> Commands must now be registered in the `Configure` bootup phase.
> If a command is not registered early enough, it may not be available to systems like [helpinfo
> that cache their information.

> [!NOTE]  
> ### Developer Note:
> Various commands related to generating content have been changed to _Developer_ and above access level.

### Screenshots
<img width="673" alt="image" src="https://github.com/modernuo/ModernUO/assets/3953314/b105b5c9-5eb4-4ace-93ff-1bfb31e7132f">

<img width="547" alt="image" src="https://github.com/modernuo/ModernUO/assets/3953314/e97487e8-47a5-4aa7-89cc-9fe3deda584d">
2024-02-10 00:19:19 -08:00

72 lines
2.4 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using Server.Commands.Generic;
using Server.Json;
using Server.Utilities;
namespace Server.Commands;
public static class GenCommands
{
public static void Configure()
{
CommandSystem.Register("GenCommands", AccessLevel.Developer, GenCommandsWebpage_OnCommand);
}
[Usage("GenCommands [<command>]")]
[Aliases("GenCmdWeb")]
[Description("Generates a webpage to the web folder with a list of all commands.")]
private static void GenCommandsWebpage_OnCommand(CommandEventArgs e)
{
var allCommands = HelpInfo.SortedHelpInfo;
var allModifiers = BaseCommandImplementor.Implementors;
Dictionary<string, BaseCommandImplementor> modifiersByAccessor = [];
foreach (var modifier in allModifiers)
{
modifiersByAccessor[modifier.Accessors[0]] = modifier;
}
Dictionary<AccessLevel, List<CommandInfo>> commandsByAccessLevel = [];
foreach (var command in allCommands)
{
if (!commandsByAccessLevel.TryGetValue(command.AccessLevel, out var list))
{
commandsByAccessLevel[command.AccessLevel] = list = [];
}
list.Add(command);
}
// Serialization options with camel case naming policy
var options = JsonConfig.GetOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
// Write modifiers as JSON. All keys must be lowercase.
var modifiersJson = JsonSerializer.Serialize(
modifiersByAccessor,
options
);
// Write commands as JSON. All keys must be lowercase.
var commandsJson = JsonSerializer.Serialize(
commandsByAccessLevel,
options
);
var template = File.ReadAllText(Path.Combine(Core.BaseDirectory, "Data", "commands.html"))
.Replace("<!-- COMMANDS -->", commandsJson.EscapeHtml())
.Replace("<!-- MODIFIERS -->", modifiersJson.EscapeHtml());
// Write the new file to the web folder
var webFolder = Path.Combine(Core.BaseDirectory, "web");
PathUtility.EnsureDirectory(webFolder);
var webPath = Path.Combine(webFolder, "commands.html");
File.WriteAllText(webPath, template, Encoding.UTF8);
e.Mobile.SendMessage($"Commands webpage generated at {webPath}.");
}
}