### 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" }
```
85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2023 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: RegionByNameConverter.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 RegionByNameConverter : JsonConverter<Region>
|
|
{
|
|
public override Region Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
string name = null;
|
|
Map map = null;
|
|
|
|
if (reader.TokenType != JsonTokenType.StartObject)
|
|
{
|
|
throw new JsonException("Invalid json for RegionByName");
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
reader.Read();
|
|
if (reader.TokenType == JsonTokenType.EndObject)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (reader.TokenType != JsonTokenType.PropertyName)
|
|
{
|
|
throw new JsonException("Invalid json for RegionByName");
|
|
}
|
|
|
|
string property = reader.GetString()?.ToLower();
|
|
|
|
if (property != "name" && property != "map")
|
|
{
|
|
continue;
|
|
}
|
|
|
|
reader.Read();
|
|
if (reader.TokenType != JsonTokenType.String)
|
|
{
|
|
throw new JsonException($"Value for {property} must be a string");
|
|
}
|
|
|
|
if (property == "name")
|
|
{
|
|
name = reader.GetString();
|
|
}
|
|
else
|
|
{
|
|
map = Map.Parse(reader.GetString());
|
|
}
|
|
}
|
|
|
|
if (name == null || map == null)
|
|
{
|
|
throw new JsonException("Invalid json for RegionByName");
|
|
}
|
|
|
|
return Region.Find(name, map);
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, Region value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStartObject();
|
|
writer.WriteString("Name", value.Name);
|
|
writer.WriteString("Map", value.Map.Name);
|
|
writer.WriteEndObject();
|
|
}
|
|
}
|