fix: Fixes region priority and resolution. (#1254)
- [X] Fixes world location JSON deserializer. - [X] Fixes region resolver and priorities. - [X] Removes DynamicJson from regions. - [X] Adds missing region music.
This commit is contained in:
parent
139be1bc23
commit
18c4459e6b
31 changed files with 3809 additions and 3677 deletions
|
|
@ -1,46 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: JsonDtoConverter.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.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Server.Json;
|
||||
|
||||
public class JsonDtoConverter<TDto, TObject> : JsonConverter<TObject>
|
||||
where TDto : IJsonRootDtoConvertible<TDto, TObject>, new() where TObject : new()
|
||||
{
|
||||
public override TObject Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
if (typeof(TObject) != typeToConvert)
|
||||
{
|
||||
throw new JsonException($"Invalid object type to deserialize for '{typeof(TObject).Name}'");
|
||||
}
|
||||
|
||||
// Deserialize to the DTO
|
||||
var dto = JsonSerializer.Deserialize<TDto>(ref reader, options);
|
||||
|
||||
return TDto.ToObject(dto);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, TObject value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, TDto.FromObject(value), options);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: JsonDtoConverter.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.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Server.Json;
|
||||
|
||||
public class JsonDtoConverterFactory<TDto, TObject> : JsonConverterFactory
|
||||
where TDto : IJsonRootDtoConvertible<TDto, TObject>, new() where TObject : new()
|
||||
{
|
||||
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TObject);
|
||||
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
|
||||
new JsonDtoConverter<TDto, TObject>();
|
||||
}
|
||||
|
|
@ -80,7 +80,6 @@ public class WorldLocationConverter : JsonConverter<WorldLocation>
|
|||
private WorldLocation DeserializeObj(ref Utf8JsonReader reader, JsonSerializerOptions options)
|
||||
{
|
||||
Span<int> data = stackalloc int[3];
|
||||
var count = 0;
|
||||
var hasLoc = false;
|
||||
var hasXYZ = false;
|
||||
var hasMap = false;
|
||||
|
|
@ -150,7 +149,6 @@ public class WorldLocationConverter : JsonConverter<WorldLocation>
|
|||
data[0] = loc.X;
|
||||
data[1] = loc.Y;
|
||||
data[2] = loc.Z;
|
||||
count = 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -160,7 +158,7 @@ public class WorldLocationConverter : JsonConverter<WorldLocation>
|
|||
hasMap = true;
|
||||
}
|
||||
|
||||
if (!hasMap || count != 3)
|
||||
if (!hasMap)
|
||||
{
|
||||
throw new JsonException("WorldLocation must have an x, y, z, and map properties");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: IJsonConvertible.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/>. *
|
||||
*************************************************************************/
|
||||
|
||||
namespace Server.Json;
|
||||
|
||||
public interface IJsonRootDtoConvertible<TSelf, TObject> where TSelf : IJsonRootDtoConvertible<TSelf, TObject> where TObject : new()
|
||||
{
|
||||
public abstract static TSelf FromObject(TObject obj);
|
||||
public abstract static TObject ToObject(TSelf obj);
|
||||
}
|
||||
|
||||
public interface IJsonDtoConvertible<TSelf, TObject> where TSelf : IJsonDtoConvertible<TSelf, TObject> where TObject : new()
|
||||
{
|
||||
public abstract TSelf HydrateDto(TObject obj);
|
||||
public abstract TObject HydrateObject(TObject obj);
|
||||
}
|
||||
|
|
@ -19,7 +19,6 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
|
@ -509,7 +508,7 @@ public static class Core
|
|||
|
||||
TileMatrixLoader.LoadTileMatrix();
|
||||
|
||||
RegionLoader.LoadRegions();
|
||||
RegionJsonSerializer.LoadRegions();
|
||||
World.Load();
|
||||
|
||||
AssemblyHandler.Invoke("Initialize");
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using Server.Json;
|
||||
using Server.Logging;
|
||||
using Server.Network;
|
||||
using Server.Targeting;
|
||||
|
|
@ -78,6 +76,42 @@ public enum MusicName
|
|||
SelimsBar,
|
||||
SerpentIsleCombat_U7,
|
||||
ValoriaShips,
|
||||
TheWanderer,
|
||||
Castle,
|
||||
Festival,
|
||||
Honor,
|
||||
Medieval,
|
||||
BattleOnStones,
|
||||
Docktown,
|
||||
GargoyleQueen,
|
||||
GenericCombat,
|
||||
Holycity,
|
||||
HumanLevel,
|
||||
LoginLoop,
|
||||
NorthernForestBattleonStones,
|
||||
PrimevalLich,
|
||||
QueenPalace,
|
||||
RoyalCity,
|
||||
SlasherVeil,
|
||||
StygianAbyss,
|
||||
StygianDragon,
|
||||
Void,
|
||||
CodexShrine,
|
||||
AnvilStrikeInMinoc,
|
||||
ASkaranLullaby,
|
||||
BlackthornsMarch,
|
||||
DupresNightInTrinsic,
|
||||
FayaxionAndTheSix,
|
||||
FlightOfTheNexus,
|
||||
GalehavenJaunt,
|
||||
JhelomToArms,
|
||||
MidnightInYew,
|
||||
MoonglowSonata,
|
||||
NewMaginciaMarch,
|
||||
NujelmWaltz,
|
||||
SherrysSong,
|
||||
StarlightInBritain,
|
||||
TheVesperMist,
|
||||
NoMusic = 0x1FFF
|
||||
}
|
||||
|
||||
|
|
@ -98,9 +132,16 @@ public class Region : IComparable<Region>
|
|||
{
|
||||
}
|
||||
|
||||
public Region(string name, Map map, params Rectangle3D[] area) : this(name, map, null, area)
|
||||
{
|
||||
}
|
||||
|
||||
public Region(string name, Map map, int priority, params Rectangle3D[] area) : this(name, map, null, area) =>
|
||||
Priority = priority;
|
||||
|
||||
public Region(string name, Map map, Region parent, int priority, params Rectangle3D[] area) : this(name, map, parent, area) =>
|
||||
Priority = priority;
|
||||
|
||||
public Region(string name, Map map, Region parent, params Rectangle2D[] area) : this(
|
||||
name,
|
||||
map,
|
||||
|
|
@ -131,54 +172,6 @@ public class Region : IComparable<Region>
|
|||
}
|
||||
}
|
||||
|
||||
public Region(DynamicJson json, JsonSerializerOptions options)
|
||||
{
|
||||
Map = json.GetProperty("map", options, out Map map) ? map : null;
|
||||
Parent = json.GetProperty("parent", options, out string parent) ? Find(parent, Map) : null;
|
||||
Name = json.GetProperty("name", options, out string name) ? name : null;
|
||||
|
||||
Dynamic = false;
|
||||
|
||||
if (Parent == null)
|
||||
{
|
||||
ChildLevel = 0;
|
||||
Priority = DefaultPriority;
|
||||
}
|
||||
else
|
||||
{
|
||||
ChildLevel = Parent.ChildLevel + 1;
|
||||
Priority = Parent.Priority;
|
||||
}
|
||||
|
||||
Priority = json.GetProperty("priority", options, out int priority) ? priority : Priority;
|
||||
|
||||
Area = json.GetProperty("rects", options, out List<Rectangle3D> rects)
|
||||
? rects.ToArray()
|
||||
: Array.Empty<Rectangle3D>();
|
||||
|
||||
if (Area.Length == 0)
|
||||
{
|
||||
logger.Debug("Empty area for region '{Region}'", this);
|
||||
}
|
||||
|
||||
if (json.GetProperty("go", options, out Point3D go))
|
||||
{
|
||||
GoLocation = go;
|
||||
}
|
||||
else if (Area.Length > 0)
|
||||
{
|
||||
var start = Area[0].Start;
|
||||
var end = Area[0].End;
|
||||
|
||||
var x = start.X + (end.X - start.X) / 2;
|
||||
var y = start.Y + (end.Y - start.Y) / 2;
|
||||
|
||||
GoLocation = new Point3D(x, y, Map?.GetAverageZ(x, y) ?? start.Z + (end.Z - start.Z) / 2);
|
||||
}
|
||||
|
||||
Music = json.GetEnumProperty("music", options, out MusicName music) ? music : DefaultMusic;
|
||||
}
|
||||
|
||||
public static List<Region> Regions { get; } = new();
|
||||
|
||||
public static TimeSpan StaffLogoutDelay { get; set; } = TimeSpan.Zero;
|
||||
|
|
@ -239,6 +232,11 @@ public class Region : IComparable<Region>
|
|||
// This is not optimized. Use sparingly
|
||||
public static Region Find(string name, Map map, bool insensitive = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (insensitive)
|
||||
{
|
||||
name = name.ToLower();
|
||||
|
|
|
|||
61
Projects/Server/Regions/RegionJsonDto.cs
Normal file
61
Projects/Server/Regions/RegionJsonDto.cs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: RegionJsonDto.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.Text.Json.Serialization;
|
||||
using Server.Utilities;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public class RegionJsonDto
|
||||
{
|
||||
[JsonIgnore]
|
||||
public virtual Type RegionType => typeof(Region);
|
||||
|
||||
public Map Map { get; set; }
|
||||
public string? Parent { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int? Priority { get; set; }
|
||||
public Rectangle3D[] Area { get; set; }
|
||||
public Point3D GoLocation { get; set; }
|
||||
public MusicName? Music { get; set; }
|
||||
|
||||
public virtual void FromRegion(Region region)
|
||||
{
|
||||
Map = region.Map;
|
||||
Parent = region.Parent?.Name;
|
||||
Name = region.Name;
|
||||
Priority = region.Priority;
|
||||
Area = region.Area;
|
||||
GoLocation = region.GoLocation;
|
||||
Music = region.Music != region.DefaultMusic ? region.Music : null;
|
||||
}
|
||||
|
||||
public Region ToRegion()
|
||||
{
|
||||
var region = Priority != null
|
||||
? RegionType.CreateInstance<Region>(Name, Map, Region.Find(Parent, Map), Priority, Area)
|
||||
: RegionType.CreateInstance<Region>(Name, Map, Region.Find(Parent, Map), Area);
|
||||
|
||||
HydrateRegion(region);
|
||||
return region;
|
||||
}
|
||||
|
||||
protected virtual void HydrateRegion(Region region)
|
||||
{
|
||||
region.GoLocation = GoLocation;
|
||||
region.Music = Music ?? region.DefaultMusic;
|
||||
}
|
||||
}
|
||||
148
Projects/Server/Regions/RegionJsonSerializer.cs
Normal file
148
Projects/Server/Regions/RegionJsonSerializer.cs
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: RegionJsonSerializer.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 System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using Server.Json;
|
||||
using Server.Logging;
|
||||
using Server.Utilities;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public static class RegionJsonSerializer
|
||||
{
|
||||
private static readonly ILogger logger = LogFactory.GetLogger(typeof(RegionJsonSerializer));
|
||||
|
||||
// Note: This is filled during the `Configure` phase by RegisterRegionForSerialization,
|
||||
// so it is not available to a JsonConverter which might be used during earlier phases.
|
||||
private static Dictionary<Type, Type> _regionToDtoLookup = new() { { typeof(Region), typeof(RegionJsonDto) } };
|
||||
private static JsonDerivedType[] _derivedTypes = { new(typeof(RegionJsonDto), nameof(Region)) };
|
||||
|
||||
private static JsonSerializerOptions _options = new(JsonConfig.DefaultOptions)
|
||||
{
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
|
||||
TypeInfoResolver = new DefaultJsonTypeInfoResolver
|
||||
{
|
||||
Modifiers =
|
||||
{
|
||||
static typeInfo =>
|
||||
{
|
||||
if (typeInfo.Type != typeof(RegionJsonDto))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
typeInfo.PolymorphismOptions = new JsonPolymorphismOptions();
|
||||
for (var i = 0; i < _derivedTypes.Length; i++)
|
||||
{
|
||||
typeInfo.PolymorphismOptions.DerivedTypes.Add(_derivedTypes[i]);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static void RegisterRegionForSerialization<TDto, TRegion>()
|
||||
where TDto : RegionJsonDto, new() where TRegion : Region
|
||||
{
|
||||
for (var i = 0; i < _derivedTypes.Length; i++)
|
||||
{
|
||||
if (_derivedTypes[i].DerivedType == typeof(TDto))
|
||||
{
|
||||
throw new Exception(
|
||||
$"Type '{typeof(TDto)}' has already been registered for serialization with the region loader."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Array.Resize(ref _derivedTypes, _derivedTypes.Length + 1);
|
||||
_derivedTypes[^1] = new JsonDerivedType(typeof(TDto), typeof(TRegion).Name);
|
||||
_regionToDtoLookup[typeof(TRegion)] = typeof(TDto);
|
||||
}
|
||||
|
||||
internal static void LoadRegions()
|
||||
{
|
||||
var path = Path.Join(Core.BaseDirectory, "Data/regions.json");
|
||||
|
||||
logger.Information("Loading regions");
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
|
||||
var regions = JsonConfig.Deserialize<List<RegionJsonDto>>(path, _options);
|
||||
if (regions == null)
|
||||
{
|
||||
throw new JsonException($"Failed to deserialize {path}.");
|
||||
}
|
||||
|
||||
foreach (var dto in regions)
|
||||
{
|
||||
var region = dto.ToRegion();
|
||||
region.Register();
|
||||
}
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
logger.Information(
|
||||
"Loading regions {Status} ({Count} regions) ({Duration:F2} seconds)",
|
||||
"done",
|
||||
regions.Count,
|
||||
stopwatch.Elapsed.TotalSeconds
|
||||
);
|
||||
}
|
||||
|
||||
// Note: This is not thread safe. To make `SerializeRegions` threadsafe, use `[ThreadStatic]` or just create the lists
|
||||
// in the function and take the GC hit.
|
||||
private static List<RegionJsonDto> _regionJsonDtos;
|
||||
|
||||
public static void SerializeRegions(string path, IEnumerable<Region> regions)
|
||||
{
|
||||
_regionJsonDtos ??= new List<RegionJsonDto>();
|
||||
foreach (var region in regions)
|
||||
{
|
||||
if (_regionToDtoLookup.TryGetValue(region.GetType(), out var dtoType))
|
||||
{
|
||||
var dto = dtoType.CreateInstance<RegionJsonDto>();
|
||||
dto.FromRegion(region);
|
||||
_regionJsonDtos.Add(dto);
|
||||
}
|
||||
}
|
||||
|
||||
JsonConfig.Serialize(path, _regionJsonDtos, _options);
|
||||
_regionJsonDtos.Clear();
|
||||
}
|
||||
|
||||
public static string SerializeRegions(IEnumerable<Region> regions)
|
||||
{
|
||||
_regionJsonDtos ??= new List<RegionJsonDto>();
|
||||
foreach (var region in regions)
|
||||
{
|
||||
if (_regionToDtoLookup.TryGetValue(region.GetType(), out var dtoType))
|
||||
{
|
||||
var dto = dtoType.CreateInstance<RegionJsonDto>();
|
||||
dto.FromRegion(region);
|
||||
_regionJsonDtos.Add(dto);
|
||||
}
|
||||
}
|
||||
|
||||
var output = JsonConfig.Serialize(_regionJsonDtos, _options);
|
||||
_regionJsonDtos.Clear();
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,86 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: RegionLoader.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 System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Server.Json;
|
||||
using Server.Logging;
|
||||
using Server.Utilities;
|
||||
|
||||
namespace Server;
|
||||
|
||||
internal static class RegionLoader
|
||||
{
|
||||
private static readonly ILogger logger = LogFactory.GetLogger(typeof(RegionLoader));
|
||||
|
||||
internal static void LoadRegions()
|
||||
{
|
||||
var path = Path.Join(Core.BaseDirectory, "Data/regions.json");
|
||||
|
||||
var failures = new List<string>();
|
||||
var count = 0;
|
||||
|
||||
logger.Information("Loading regions");
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
var regions = JsonConfig.Deserialize<List<DynamicJson>>(path);
|
||||
if (regions == null)
|
||||
{
|
||||
throw new JsonException($"Failed to deserialize {path}.");
|
||||
}
|
||||
|
||||
foreach (var json in regions)
|
||||
{
|
||||
var type = AssemblyHandler.FindTypeByName(json.Type);
|
||||
|
||||
if (type == null || !typeof(Region).IsAssignableFrom(type))
|
||||
{
|
||||
failures.Add($"\tInvalid region type {json.Type}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var region = type.CreateInstance<Region>(json, JsonConfig.DefaultOptions);
|
||||
region?.Register();
|
||||
count++;
|
||||
}
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
if (failures.Count == 0)
|
||||
{
|
||||
logger.Information(
|
||||
"Loading regions {Status} ({Count} regions, {FailureCount} failures) ({Duration:F2} seconds)",
|
||||
"done",
|
||||
count,
|
||||
failures.Count,
|
||||
stopwatch.Elapsed.TotalSeconds
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Warning(
|
||||
$"Loading regions {{Status}} ({{Count}} regions, {{FailureCount}} failures) ({{Duration:F2}} seconds){Environment.NewLine}{{Failures}}",
|
||||
"failed",
|
||||
count,
|
||||
failures.Count,
|
||||
stopwatch.Elapsed.TotalSeconds,
|
||||
failures
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue