fix: Drastically simplifies regions (#1400)
### Summary
- [X] Gets rid of the Dtos
- [X] Simplifies the json serializer registration
- [X] Adds a custom `RegionByName` JsonConverter that can look up other regions that have already been registered.
### BREAKING CHANGE
1. **Child regions must appear after their parents in the JSON file**
2. In the regions json file, _"Parent"_ can no longer be just a string. It must be an object that includes the map.
- ```json
"Parent": { "Name": "Britain", "Map": "Felucca" }
```
This commit is contained in:
parent
734d20cbaf
commit
0a9bfb0558
47 changed files with 542 additions and 470 deletions
|
|
@ -22,7 +22,6 @@ using System.Text.Json.Serialization;
|
|||
using System.Text.Json.Serialization.Metadata;
|
||||
using Server.Json;
|
||||
using Server.Logging;
|
||||
using Server.Utilities;
|
||||
|
||||
namespace Server;
|
||||
|
||||
|
|
@ -30,10 +29,7 @@ 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 JsonDerivedType[] _derivedTypes = { new(typeof(Region), nameof(Region)) };
|
||||
|
||||
private static JsonSerializerOptions _options = new(JsonConfig.DefaultOptions)
|
||||
{
|
||||
|
|
@ -44,7 +40,7 @@ public static class RegionJsonSerializer
|
|||
{
|
||||
static typeInfo =>
|
||||
{
|
||||
if (typeInfo.Type != typeof(RegionJsonDto))
|
||||
if (typeInfo.Type != typeof(Region))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
@ -55,26 +51,39 @@ public static class RegionJsonSerializer
|
|||
typeInfo.PolymorphismOptions.DerivedTypes.Add(_derivedTypes[i]);
|
||||
}
|
||||
},
|
||||
static typeInfo =>
|
||||
{
|
||||
if (!typeInfo.Type.IsAssignableTo(typeof(Region)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
typeInfo.OnDeserialized = o =>
|
||||
{
|
||||
if (o is Region region && Core.Expansion >= region.MinExpansion && Core.Expansion <= region.MaxExpansion)
|
||||
{
|
||||
region.Register();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static void Register<TDto, TRegion>()
|
||||
where TDto : RegionJsonDto, new() where TRegion : Region
|
||||
public static void Register<TRegion>() where TRegion : Region
|
||||
{
|
||||
for (var i = 0; i < _derivedTypes.Length; i++)
|
||||
{
|
||||
if (_derivedTypes[i].DerivedType == typeof(TDto))
|
||||
if (_derivedTypes[i].DerivedType == typeof(TRegion))
|
||||
{
|
||||
throw new Exception(
|
||||
$"Type '{typeof(TDto)}' has already been registered for serialization with the region loader."
|
||||
$"Type '{typeof(TRegion)}' 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);
|
||||
_derivedTypes[^1] = new JsonDerivedType(typeof(TRegion), typeof(TRegion).Name);
|
||||
}
|
||||
|
||||
internal static void LoadRegions()
|
||||
|
|
@ -85,19 +94,17 @@ public static class RegionJsonSerializer
|
|||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
|
||||
var regions = JsonConfig.Deserialize<List<RegionJsonDto>>(path, _options);
|
||||
var regions = JsonConfig.Deserialize<List<Region>>(path, _options);
|
||||
if (regions == null)
|
||||
{
|
||||
throw new JsonException($"Failed to deserialize {path}.");
|
||||
}
|
||||
|
||||
var count = 0;
|
||||
foreach (var dto in regions)
|
||||
foreach (var region in regions)
|
||||
{
|
||||
if (Core.Expansion >= dto.MinExpansion && Core.Expansion <= dto.MaxExpansion)
|
||||
if (Core.Expansion >= region.MinExpansion && Core.Expansion <= region.MaxExpansion)
|
||||
{
|
||||
var region = dto.ToRegion();
|
||||
region.Register();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
|
@ -111,43 +118,4 @@ public static class RegionJsonSerializer
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue