Simplify reader registration

This commit is contained in:
Paul Pacheco 2020-09-28 12:33:32 -05:00
parent 2dc280d292
commit 7aa47f14be

View File

@ -28,20 +28,13 @@ public static MethodReference GetReadFunc(TypeReference variableReference, int r
return foundFunc;
}
MethodDefinition newReaderFunc;
// Arrays are special, if we resolve them, we get teh element type,
// so the following ifs might choke on it for scriptable objects
// or other objects that require a custom serializer
// thus check if it is an array and skip all the checks.
if (variableReference.IsArray)
{
newReaderFunc = GenerateArrayReadFunc(variableReference, recursionCount);
if (newReaderFunc != null)
{
RegisterReadFunc(variableReference.FullName, newReaderFunc);
}
return newReaderFunc;
return GenerateArrayReadFunc(variableReference, recursionCount);
}
TypeDefinition variableDefinition = variableReference.Resolve();
@ -89,33 +82,23 @@ public static MethodReference GetReadFunc(TypeReference variableReference, int r
if (variableDefinition.IsEnum)
{
newReaderFunc = GenerateEnumReadFunc(variableReference);
return GenerateEnumReadFunc(variableReference);
}
else if (variableDefinition.Is(typeof(ArraySegment<>)))
{
newReaderFunc = GenerateArraySegmentReadFunc(variableReference, recursionCount);
return GenerateArraySegmentReadFunc(variableReference, recursionCount);
}
else if (variableDefinition.Is(typeof(List<>)))
{
newReaderFunc = GenerateListReadFunc(variableReference, recursionCount);
}
else
{
newReaderFunc = GenerateClassOrStructReadFunction(variableReference, recursionCount);
return GenerateListReadFunc(variableReference, recursionCount);
}
if (newReaderFunc == null)
{
Weaver.Error($"{variableReference.Name} is not a supported type", variableReference);
return null;
}
RegisterReadFunc(variableReference.FullName, newReaderFunc);
return newReaderFunc;
return GenerateClassOrStructReadFunction(variableReference, recursionCount);
}
static void RegisterReadFunc(string name, MethodDefinition newReaderFunc)
static void RegisterReadFunc(TypeReference typeReference, MethodDefinition newReaderFunc)
{
readFuncs[name] = newReaderFunc;
readFuncs[typeReference.FullName] = newReaderFunc;
Weaver.WeaveLists.generatedReadFunctions.Add(newReaderFunc);
Weaver.WeaveLists.ConfirmGeneratedCodeClass();
@ -305,6 +288,8 @@ private static MethodDefinition GenerateReaderFunction(TypeReference variable)
readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, WeaverTypes.Import<Mirror.NetworkReader>()));
readerFunc.Body.InitLocals = true;
RegisterReadFunc(variable, readerFunc);
return readerFunc;
}