ModernUO/Projects/UOContent/Engines/Spawners/Commands/EditSpawnerCommand.cs
Kamron Batman 0d1fffd9fc
fix: Fixes spawners, adds convertpremiumspawners, exportspawners, and replaces with neruns distro (#751)
### Additions
* Adds `[exportspawners <relative json file path to distro>`
  * If no path is provided, it will be saved to the `Data\Spawns` folder with the current timestamp as the file name.
  * Supports global, facet, region, multi, and area
* Fixes client disconnect for bad spawner generation command.
* Moves spawner commands to their own folder.
* Adds GUIDs for spawners. This allows for easy replacement during import to reduce duplication.
* Allows exporting the name of the spawner.
* Fixes globbing when using [generatespawners
* Adds [convertpremiumspawners to convert Premium Spawners (from Neruns distro)

Possibly in .NET 6 serializing JSON will be more performant using JSON nodes.


### Screenshots
![Screen_Shot_2021-08-31_at_10 20 06_PM](https://user-images.githubusercontent.com/3953314/131618495-cf7e3ae5-58e5-4f86-9d3c-3cc49906d9fc.png)
![Screen_Shot_2021-08-31_at_10 21 08_PM](https://user-images.githubusercontent.com/3953314/131618502-c39cc368-2266-47a5-9b87-e5ffa108b875.png)
![Screen_Shot_2021-08-31_at_10 13 29_PM](https://user-images.githubusercontent.com/3953314/131618512-f9807f1b-76e4-4503-989d-42324798fbf5.png)
2021-09-03 21:52:44 -07:00

134 lines
4.8 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: EditSpawnerCommand.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections.Generic;
using Server.Commands.Generic;
using static Server.Types;
namespace Server.Engines.Spawners
{
public class EditSpawnCommand : BaseCommand
{
public static void Initialize()
{
TargetCommands.Register(new EditSpawnCommand());
}
public EditSpawnCommand()
{
AccessLevel = AccessLevel.GameMaster;
Supports = CommandSupport.Complex | CommandSupport.Simple;
Commands = new[] { "EditSpawner" };
ObjectTypes = ObjectTypes.Items;
Usage = "EditSpawner <type> <arguments> set <properties> where <properties>";
Description = "Modifies spawners arguments and properties for the given type";
ListOptimized = true;
}
public override void ExecuteList(CommandEventArgs e, List<object> list)
{
var args = e.Arguments;
if (args.Length <= 1)
{
LogFailure(Usage);
return;
}
if (list.Count == 0)
{
LogFailure("No matching objects found.");
return;
}
var name = args[0];
var type = AssemblyHandler.FindTypeByName(name);
if (!IsEntity(type))
{
LogFailure("No type with that name was found.");
return;
}
var argSpan = e.ArgString.AsSpan(name.Length + 1);
var setIndex = argSpan.InsensitiveIndexOf("set ");
var where = argSpan.InsensitiveIndexOf("where ");
ReadOnlySpan<char> props = null, findmatch = null;
if (setIndex > -1 || where > -1)
{
var start = setIndex + 4;
var len = where > -1 ? where : argSpan.Length;
findmatch = argSpan.Slice(len < argSpan.Length ? len + 6 : argSpan.Length);
if (setIndex > -1)
{
props = argSpan[start..^len];
argSpan = argSpan[..setIndex];
}
else
{
argSpan = argSpan[..^len];
}
}
var argStr = argSpan.ToString().DefaultIfNullOrEmpty(null);
var propsStr = props.ToString().DefaultIfNullOrEmpty(null);
var whereStr = findmatch.ToString().DefaultIfNullOrEmpty(null);
e.Mobile.SendMessage("Updating spawners...");
foreach (var obj in list)
{
if (obj is BaseSpawner spawner)
{
UpdateSpawner(spawner, name, argStr, propsStr, whereStr);
}
}
e.Mobile.SendMessage("Update completed.");
}
public static void UpdateSpawner(BaseSpawner spawner, string name, string arguments, string properties, string find = null)
{
foreach (var entry in spawner.Entries)
{
// TODO: Should cache spawn type on the entry
if (!entry.SpawnedName.InsensitiveEquals(name))
{
continue;
}
var found = find != null ? entry.Properties?.LastIndexOf(find, StringComparison.OrdinalIgnoreCase) : null;
var shouldUpdate = find == null || found > -1 &&
(found + find.Length == entry.Properties.Length ||
char.IsWhiteSpace(entry.Properties[(int)found + find.Length]));
if (shouldUpdate)
{
if (arguments != null)
{
entry.Parameters = arguments;
}
entry.Properties = properties;
}
}
}
}
}