Skip to content

Commit f4b039f

Browse files
fix: Nullable config (#769)
* fix: Nullable config * swap * fix: Skill issue * feat: Add Quaternion * remove LogInfo --------- Co-authored-by: @Someone <45270312+Someone-193@users.noreply.github.com>
1 parent d3dd12a commit f4b039f

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

EXILED/Exiled.Loader/Features/Configs/CustomConverters/ColorConverter.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ namespace Exiled.Loader.Features.Configs.CustomConverters
1414

1515
using Exiled.API.Features;
1616
using Exiled.API.Features.Pools;
17-
1817
using UnityEngine;
19-
2018
using YamlDotNet.Core;
2119
using YamlDotNet.Core.Events;
2220
using YamlDotNet.Serialization;
@@ -36,13 +34,20 @@ public bool Accepts(Type type)
3634
/// <inheritdoc cref="IYamlTypeConverter" />
3735
public object ReadYaml(IParser parser, Type type)
3836
{
39-
Type baseType = Nullable.GetUnderlyingType(type) ?? type;
37+
Type baseType = Nullable.GetUnderlyingType(type);
38+
39+
bool isNullable = true;
40+
if (baseType == null)
41+
{
42+
baseType = type;
43+
isNullable = false;
44+
}
4045

4146
if (parser.TryConsume(out Scalar scalar))
4247
{
4348
if (string.IsNullOrEmpty(scalar.Value) || scalar.Value.Equals("null", StringComparison.OrdinalIgnoreCase))
4449
{
45-
if (Nullable.GetUnderlyingType(type) != null)
50+
if (isNullable)
4651
return null;
4752

4853
Log.Error($"Cannot assign null to non-nullable type {baseType.FullName}.");
@@ -83,7 +88,11 @@ public object ReadYaml(IParser parser, Type type)
8388
}
8489
}
8590

86-
object color = Activator.CreateInstance(type, coordinates.ToArray());
91+
object color;
92+
if (isNullable)
93+
color = Activator.CreateInstance(type, Activator.CreateInstance(baseType, coordinates.ToArray()));
94+
else
95+
color = Activator.CreateInstance(type, coordinates.ToArray());
8796

8897
ListPool<object>.Pool.Return(coordinates);
8998

EXILED/Exiled.Loader/Features/Configs/CustomConverters/VectorsConverter.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ namespace Exiled.Loader.Features.Configs.CustomConverters
1414

1515
using Exiled.API.Features;
1616
using Exiled.API.Features.Pools;
17-
1817
using UnityEngine;
19-
2018
using YamlDotNet.Core;
2119
using YamlDotNet.Core.Events;
2220
using YamlDotNet.Serialization;
@@ -30,19 +28,26 @@ public sealed class VectorsConverter : IYamlTypeConverter
3028
public bool Accepts(Type type)
3129
{
3230
Type baseType = Nullable.GetUnderlyingType(type) ?? type;
33-
return baseType == typeof(Vector2) || baseType == typeof(Vector3) || baseType == typeof(Vector4);
31+
return baseType == typeof(Vector2) || baseType == typeof(Vector3) || baseType == typeof(Vector4) || baseType == typeof(Quaternion);
3432
}
3533

3634
/// <inheritdoc cref="IYamlTypeConverter" />
3735
public object ReadYaml(IParser parser, Type type)
3836
{
39-
Type baseType = Nullable.GetUnderlyingType(type) ?? type;
37+
Type baseType = Nullable.GetUnderlyingType(type);
38+
39+
bool isNullable = true;
40+
if (baseType == null)
41+
{
42+
isNullable = false;
43+
baseType = type;
44+
}
4045

4146
if (parser.TryConsume(out Scalar scalar))
4247
{
4348
if (string.IsNullOrEmpty(scalar.Value) || scalar.Value.Equals("null", StringComparison.OrdinalIgnoreCase))
4449
{
45-
if (Nullable.GetUnderlyingType(type) != null)
50+
if (isNullable)
4651
return null;
4752

4853
Log.Error($"Cannot assign null to non-nullable type {baseType.FullName}.");
@@ -75,7 +80,11 @@ public object ReadYaml(IParser parser, Type type)
7580
coordinates.Add(coordinate);
7681
}
7782

78-
object vector = Activator.CreateInstance(baseType, coordinates.ToArray());
83+
object vector;
84+
if (isNullable)
85+
vector = Activator.CreateInstance(type, Activator.CreateInstance(baseType, coordinates.ToArray()));
86+
else
87+
vector = Activator.CreateInstance(type, coordinates.ToArray());
7988

8089
ListPool<object>.Pool.Return(coordinates);
8190

@@ -111,6 +120,13 @@ public void WriteYaml(IEmitter emitter, object value, Type type)
111120
coordinates["z"] = vector4.z;
112121
coordinates["w"] = vector4.w;
113122
}
123+
else if (value is Quaternion quaternion)
124+
{
125+
coordinates["x"] = quaternion.x;
126+
coordinates["y"] = quaternion.y;
127+
coordinates["z"] = quaternion.z;
128+
coordinates["w"] = quaternion.w;
129+
}
114130

115131
emitter.Emit(new MappingStart());
116132

0 commit comments

Comments
 (0)