fix: Fixes region child level (#1508)

This commit is contained in:
Kamron Batman 2023-09-19 10:35:50 -07:00 committed by GitHub
parent 2a3e048b86
commit 38557342f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 35 deletions

View file

@ -205,7 +205,7 @@ public class Region : IComparable<Region>
public int Priority { get; }
public int ChildLevel { get; }
public int ChildLevel { get; internal set; }
public bool Registered { get; private set; }

View file

@ -31,43 +31,50 @@ public static class RegionJsonSerializer
private static JsonDerivedType[] _derivedTypes = { new(typeof(Region), nameof(Region)) };
public static IJsonTypeInfoResolver RegionJsonTypeInfoResolver => new DefaultJsonTypeInfoResolver()
{
Modifiers =
{
static typeInfo =>
{
if (typeInfo.Type != typeof(Region))
{
return;
}
typeInfo.PolymorphismOptions = new JsonPolymorphismOptions();
for (var i = 0; i < _derivedTypes.Length; i++)
{
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)
{
// Set the child level since it is not part of the JSON data, and our constructor skips setting parent.
if (region.Parent != null)
{
region.ChildLevel = region.Parent.ChildLevel + 1;
}
region.Register();
}
};
}
}
};
private static JsonSerializerOptions _options = new(JsonConfig.DefaultOptions)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers =
{
static typeInfo =>
{
if (typeInfo.Type != typeof(Region))
{
return;
}
typeInfo.PolymorphismOptions = new JsonPolymorphismOptions();
for (var i = 0; i < _derivedTypes.Length; i++)
{
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();
}
};
}
}
}
TypeInfoResolver = RegionJsonTypeInfoResolver,
};
public static void Register<TRegion>() where TRegion : Region

View file

@ -125,6 +125,7 @@ public class Sector
if (regRect.Region == region)
{
m_RegionRects.RemoveAt(i);
break;
}
}