Skip to content

Commit

Permalink
fix: Fixes codegenned spawners and adds RunUO spawner import support (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kamronbatman authored Feb 17, 2024
1 parent cc064a2 commit d0d7be8
Show file tree
Hide file tree
Showing 12 changed files with 568 additions and 522 deletions.
24 changes: 24 additions & 0 deletions Projects/Server/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ public ConstructibleAttribute() :
public AccessLevel AccessLevel { get; set; }
}

[AttributeUsage(AttributeTargets.Method)]
public class UsageAttribute : Attribute
{
public UsageAttribute(string usage) => Usage = usage;

public string Usage { get; }
}

[AttributeUsage(AttributeTargets.Method)]
public class DescriptionAttribute : Attribute
{
public DescriptionAttribute(string description) => Description = description;

public string Description { get; }
}

[AttributeUsage(AttributeTargets.Method)]
public class AliasesAttribute : Attribute
{
public AliasesAttribute(params string[] aliases) => Aliases = aliases;

public string[] Aliases { get; }
}

[AttributeUsage(AttributeTargets.Property)]
public class CommandPropertyAttribute : Attribute
{
Expand Down
38 changes: 37 additions & 1 deletion Projects/Server/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Server.Logging;

namespace Server;

Expand Down Expand Up @@ -125,6 +128,8 @@ public int Compare(CommandInfo a, CommandInfo b)

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);
Expand Down Expand Up @@ -194,7 +199,38 @@ public static string[] Split(string value)

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)
Expand Down
1 change: 1 addition & 0 deletions Projects/Server/Json/JsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static JsonSerializerOptions GetOptions(params JsonConverterFactory[] con
options.Converters.Add(new IPEndPointConverterFactory());
options.Converters.Add(new TypeConverterFactory());
options.Converters.Add(new WorldLocationConverterFactory());
options.Converters.Add(new TextDefinitionConverterFactory());

for (var i = 0; i < converters.Length; i++)
{
Expand Down
28 changes: 0 additions & 28 deletions Projects/UOContent/Commands/Attributes.cs

This file was deleted.

18 changes: 8 additions & 10 deletions Projects/UOContent/Commands/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static void Configure()
Register("Where", AccessLevel.Counselor, Where_OnCommand);

Register("AutoPageNotify", AccessLevel.Counselor, APN_OnCommand);
Register("APN", AccessLevel.Counselor, APN_OnCommand);

Register("Animate", AccessLevel.GameMaster, Animate_OnCommand);

Expand All @@ -47,12 +46,8 @@ public static void Configure()
Register("Client", AccessLevel.Counselor, Client_OnCommand);

Register("SMsg", AccessLevel.Counselor, StaffMessage_OnCommand);
Register("SM", AccessLevel.Counselor, StaffMessage_OnCommand);
Register("S", AccessLevel.Counselor, StaffMessage_OnCommand);

Register("BCast", AccessLevel.GameMaster, BroadcastMessage_OnCommand);
Register("BC", AccessLevel.GameMaster, BroadcastMessage_OnCommand);
Register("B", AccessLevel.GameMaster, BroadcastMessage_OnCommand);

Register("Bank", AccessLevel.GameMaster, Bank_OnCommand);

Expand Down Expand Up @@ -736,14 +731,16 @@ public static void Help_OnCommand(CommandEventArgs e)
}
}

[Usage("SMsg <text>"), Aliases("S", "SM")]
[Usage("SMsg <text>")]
[Aliases("S", "SM")]
[Description("Broadcasts a message to all online staff.")]
public static void StaffMessage_OnCommand(CommandEventArgs e)
{
BroadcastMessage(AccessLevel.Counselor, e.Mobile.SpeechHue, $"[{e.Mobile.Name}] {e.ArgString}");
}

[Usage("BCast <text>"), Aliases("B", "BC")]
[Usage("BCast <text>")]
[Aliases("B", "BC")]
[Description("Broadcasts a message to everyone online.")]
public static void BroadcastMessage_OnCommand(CommandEventArgs e)
{
Expand All @@ -764,7 +761,8 @@ public static void BroadcastMessage(AccessLevel ac, int hue, string message)
}
}

[Usage("AutoPageNotify"), Aliases("APN")]
[Usage("AutoPageNotify")]
[Aliases("APN")]
[Description("Toggles your auto-page-notify status.")]
public static void APN_OnCommand(CommandEventArgs e)
{
Expand All @@ -782,8 +780,8 @@ public static void APN_OnCommand(CommandEventArgs e)
}
}

[Usage("Animate <action> <frameCount> <repeatCount> <forward> <repeat> <delay>"),
Description("Makes your character do a specified animation.")]
[Usage("Animate <action> <frameCount> <repeatCount> <forward> <repeat> <delay>")]
[Description("Makes your character do a specified animation.")]
public static void Animate_OnCommand(CommandEventArgs e)
{
if (e.Length == 6)
Expand Down
33 changes: 16 additions & 17 deletions Projects/UOContent/Engines/Spawners/BaseSpawner.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.Json;
using ModernUO.Serialization;
Expand Down Expand Up @@ -216,11 +215,8 @@ public void Remove(ISpawnable spawn)

if (spawn != null)
{
Spawned.TryGetValue(spawn, out var entry);

entry?.Spawned.Remove(spawn);

Spawned.Remove(spawn);
Spawned.Remove(spawn, out var entry);
entry?.RemoveFromSpawned(spawn);
}

if (_running && !IsFull && _timer?.Running == false)
Expand Down Expand Up @@ -419,12 +415,7 @@ public virtual bool OnDefragSpawn(ISpawnable spawned, bool remove)
};
}

if (remove)
{
Spawned.Remove(spawned);
}

return remove;
return remove && Spawned.Remove(spawned);
}

public void OnTick()
Expand Down Expand Up @@ -683,7 +674,7 @@ out var toSet
if (entity is Mobile m)
{
Spawned.Add(m, entry);
entry.Spawned.Add(m);
entry.AddToSpawned(m);

var loc = m is BaseVendor ? Location : GetSpawnPosition(m, map);

Expand Down Expand Up @@ -712,7 +703,7 @@ out var toSet
else if (entity is Item item)
{
Spawned.Add(item, entry);
entry.Spawned.Add(item);
entry.AddToSpawned(item);

var loc = GetSpawnPosition(item, map);

Expand Down Expand Up @@ -884,8 +875,6 @@ public override void OnDelete()

private void Deserialize(IGenericReader reader, int version)
{
Spawned = new Dictionary<ISpawnable, SpawnerEntry>();

_guid = reader.ReadGuid();
_returnOnDeactivate = reader.ReadBool();
_entries = new List<SpawnerEntry>(reader.ReadInt());
Expand All @@ -910,9 +899,19 @@ private void Deserialize(IGenericReader reader, int version)
_end = _running ? reader.ReadDeltaTime() : Core.Now;
}

[AfterDeserialization(false)]
[AfterDeserialization]
private void AfterDeserialization()
{
Spawned = new Dictionary<ISpawnable, SpawnerEntry>();

foreach (var entry in Entries)
{
foreach (var spawned in entry.Spawned)
{
Spawned.Add(spawned, entry);
}
}

if (_running && _end > Core.Now)
{
DoTimer(_end - Core.Now);
Expand Down
Loading

0 comments on commit d0d7be8

Please sign in to comment.