fix: Fixes codegenned spawners and adds RunUO spawner import support (#1682)

> [!IMPORTANT]  
> This code change includes important fixes for all spawners after they were converted to codegen!

> [!NOTE] 
> **Developer Note**
> Usage/Description/Aliases attributes were moved to the core so they can be used by the command registration system.

### Summary
- **Fixes spawners not registering their spawns on world load**
- Changes `[GenerateSpawners` to `[ImportSpawners`
- Removes `ConvertPremiumSpawners`
- Adds Premium spawner import support to `[ImportSpawners`
- Adds RunUO XML import support: https://github.com/ruaduck/xmlspawner/blob/ServUOMaster/XmlSpawner/XmlSpawner%20Core/SpawnerExporter.cs
- Fixed an issue where not manually registering an alias meant it wasn't available as a command
This commit is contained in:
Kamron Batman 2024-02-17 10:46:55 -08:00 committed by GitHub
parent cc064a27a5
commit d0d7be8de0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 568 additions and 522 deletions

View file

@ -15,6 +15,9 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Server.Logging;
namespace Server;
@ -125,6 +128,8 @@ public class CommandInfoSorter : IComparer<CommandInfo>
public static class CommandSystem
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(CommandSystem));
public static string Prefix { get; set; } = "[";
public static Dictionary<string, CommandEntry> Entries { get; } = new(StringComparer.OrdinalIgnoreCase);
@ -194,7 +199,38 @@ public static class CommandSystem
public static void Register(string command, AccessLevel access, CommandEventHandler handler)
{
Entries[command] = new CommandEntry(command, handler, access);
DoRegister(command, access, handler);
var mi = handler.Method;
var aliasesAttr = mi.GetCustomAttribute(typeof(AliasesAttribute), false) as AliasesAttribute;
var aliases = aliasesAttr?.Aliases;
if (aliases == null)
{
return;
}
foreach (var alias in aliases)
{
DoRegister(alias, access, handler);
}
}
private static void DoRegister(string command, AccessLevel accessLevel, CommandEventHandler handler)
{
ref var commandEntry = ref CollectionsMarshal.GetValueRefOrAddDefault(Entries, command, out var exists);
if (exists)
{
if (commandEntry.AccessLevel == accessLevel && commandEntry.Handler == handler)
{
return;
}
logger.Warning("Command {Command} already registered to {Handler}.", command, commandEntry.Handler.Method.Name);
return;
}
commandEntry = new CommandEntry(command, handler, accessLevel);
}
public static bool Handle(Mobile from, string text, MessageType type = MessageType.Regular)