Fixed deserialization of generic types

This commit is contained in:
Simon Lübeß
2024-01-15 01:18:13 +01:00
parent 19d70c94e6
commit 49db3a28c8
3 changed files with 101 additions and 17 deletions
+93
View File
@@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
@@ -65,4 +67,95 @@ public static class TypeExtension
return attribute != null; return attribute != null;
} }
private static ReadOnlySpan<char> ReadTypeName(ReadOnlySpan<char> fullName)
{
for (int i = 0; i < fullName.Length; i++)
{
if (!char.IsLetterOrDigit(fullName[i]) && fullName[i] != '.' && fullName[i] != '+' && fullName[i] != '_')
{
return fullName.Slice(0, i);
}
}
return fullName;
}
public static Type? FindType(string fullName)
{
ReadOnlySpan<char> rest = new ReadOnlySpan<char>();
return FindType(fullName.AsSpan(), ref rest);
}
private static Type? FindType(ReadOnlySpan<char> fullName, ref ReadOnlySpan<char> rest)
{
rest = fullName;
ReadOnlySpan<char> typeName = ReadTypeName(fullName);
rest = rest.Slice(typeName.Length);
if (rest.IsEmpty || rest[0] != '`')
{
// Non generic type, easy!
return GetType(typeName);
}
// Handle generic type
int brackedIndex = fullName.IndexOf('[');
typeName = fullName.Slice(0, brackedIndex);
rest = fullName.Slice(brackedIndex + 1).Trim();
Type? genericType = GetType(typeName);
Console.WriteLine($"Generic Type: {genericType}");
List<Type> arguments = new List<Type>();
while (true)
{
Type? argument = FindType(rest, ref rest);
Console.WriteLine($"Argument: {argument}");
Debug.Assert(argument != null);
arguments.Add(argument!);
rest = rest.TrimStart();
if (rest.TrimStart()[0] == ',')
{
rest = rest.Slice(1);
}
else if (rest.TrimStart()[0] == ']')
{
rest = rest.Slice(1);
break;
}
else
{
break;
}
}
return genericType?.MakeGenericType(arguments.ToArray());
}
private static Type? GetType(ReadOnlySpan<char> fullName)
{
string name = fullName.TrimEnd(']').ToString();
// Non generic type, easy!
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Reverse())
{
Type? type = assembly.GetType(name);
if (type != null)
return type;
}
return null;
}
} }
+4
View File
@@ -15,6 +15,10 @@
<Exec Command="PowerShell ./postbuild.ps1 -sourceDir $(OutDir) -destinationDir &quot;..\GlitchyEditor\resources\scripts&quot;" /> <Exec Command="PowerShell ./postbuild.ps1 -sourceDir $(OutDir) -destinationDir &quot;..\GlitchyEditor\resources\scripts&quot;" />
</Target> </Target>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> <ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\vendor\ImGui.NET\src\ImGui.NET\ImGui.NET.csproj" /> <ProjectReference Include="..\vendor\ImGui.NET\src\ImGui.NET\ImGui.NET.csproj" />
@@ -87,26 +87,13 @@ public class DeserializationObject
} }
} }
} }
public Type? FindType(string fullName)
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Reverse())
{
Type type = assembly.GetType(fullName);
if (type != null)
return type;
}
return null;
}
public Type? GetTypeFromName(string fullName) public Type? GetTypeFromName(string fullName)
{ {
if (_fullNameToType.TryGetValue(fullName, out Type? storedType)) if (_fullNameToType.TryGetValue(fullName, out Type? storedType))
return storedType; return storedType;
Type? type = FindType(fullName); Type? type = TypeExtension.FindType(fullName);
_fullNameToType.Add(fullName, type); _fullNameToType.Add(fullName, type);
@@ -270,7 +257,7 @@ public class DeserializationObject
bool changed = false; bool changed = false;
foreach (FieldInfo field in type.GetFields()) foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{ {
if (!EntitySerializer.SerializeField(field)) if (!EntitySerializer.SerializeField(field))
continue; continue;
@@ -311,7 +298,7 @@ public class DeserializationObject
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e); Log.Exception(e);
} }
return false; return false;