Format code

This commit is contained in:
Wojtek Figat
2023-03-13 20:20:10 +01:00
parent d2a0438b71
commit 6f304040b0

View File

@@ -53,6 +53,7 @@ namespace Flax.Build.Plugins
internal const string NetworkRpc = "NetworkRpc";
private const string Thunk1 = "INetworkSerializable_Serialize";
private const string Thunk2 = "INetworkSerializable_Deserialize";
private static readonly Dictionary<string, InBuildSerializer> _inBuildSerializers = new Dictionary<string, InBuildSerializer>()
{
{ "System.Boolean", new InBuildSerializer("WriteBoolean", "ReadBoolean") },
@@ -127,6 +128,7 @@ namespace Flax.Build.Plugins
{
fields = structInfo.Fields;
}
bool useReplication = false, useRpc = false;
if (fields != null)
{
@@ -139,6 +141,7 @@ namespace Flax.Build.Plugins
}
}
}
if (properties != null)
{
foreach (var propertyInfo in properties)
@@ -150,6 +153,7 @@ namespace Flax.Build.Plugins
}
}
}
if (functions != null)
{
foreach (var functionInfo in functions)
@@ -161,6 +165,7 @@ namespace Flax.Build.Plugins
}
}
}
if (useReplication)
{
typeInfo.SetTag(NetworkReplicated, string.Empty);
@@ -171,6 +176,7 @@ namespace Flax.Build.Plugins
OnGenerateCppTypeSerialize(buildData, typeInfo, contents, fields, properties, true);
OnGenerateCppTypeSerialize(buildData, typeInfo, contents, fields, properties, false);
}
if (useRpc)
{
typeInfo.SetTag(NetworkRpc, string.Empty);
@@ -293,6 +299,7 @@ namespace Flax.Build.Plugins
OnGenerateCppTypeSerializeData(buildData, typeInfo, contents, fieldInfo.Type, $"obj.{fieldInfo.Name}", serialize);
}
}
if (properties != null)
{
foreach (var propertyInfo in properties)
@@ -308,6 +315,7 @@ namespace Flax.Build.Plugins
}
}
}
contents.AppendLine(" }");
contents.AppendLine();
}
@@ -326,6 +334,7 @@ namespace Flax.Build.Plugins
if (apiType != null)
return IsRawPOD(buildData, apiType);
}
return false;
}
@@ -405,6 +414,7 @@ namespace Flax.Build.Plugins
// Register generated serializer functions
contents.AppendLine($" NetworkReplicator::AddSerializer(ScriptingTypeHandle({typeNameNative}::TypeInitializer), {typeNameInternal}Internal::INetworkSerializable_Serialize, {typeNameInternal}Internal::INetworkSerializable_Deserialize);");
}
if (rpcTag != null)
{
// Register generated RPCs
@@ -413,6 +423,7 @@ namespace Flax.Build.Plugins
{
functions = classInfo.Functions;
}
if (functions != null)
{
foreach (var functionInfo in functions)
@@ -437,7 +448,6 @@ namespace Flax.Build.Plugins
// Generate C# wrapper functions to serialize/deserialize type directly from managed code
OnGenerateCSharpTypeSerialize(buildData, typeInfo, contents, indent, true);
OnGenerateCSharpTypeSerialize(buildData, typeInfo, contents, indent, false);
}
private void OnGenerateCSharpTypeSerialize(Builder.BuildData buildData, ApiTypeInfo typeInfo, StringBuilder contents, string indent, bool serialize)
@@ -463,6 +473,7 @@ namespace Flax.Build.Plugins
contents.Append(indent).AppendLine($" throw new NotImplementedException(\"Not supported native structure with references used in managed code for replication.\");");
}
}
contents.Append(indent).AppendLine("}");
}
@@ -503,6 +514,7 @@ namespace Flax.Build.Plugins
if (file.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
searchDirectories.Add(Path.GetDirectoryName(file));
}
foreach (var e in searchDirectories)
assemblyResolver.AddSearchDirectory(e);
@@ -554,6 +566,7 @@ namespace Flax.Build.Plugins
}
}
}
var isNetworkReplicated = false;
foreach (FieldDefinition f in type.Fields)
{
@@ -562,6 +575,7 @@ namespace Flax.Build.Plugins
isNetworkReplicated = true;
break;
}
foreach (PropertyDefinition p in type.Properties)
{
if (!p.HasAttribute(NetworkReplicatedAttribute))
@@ -569,6 +583,7 @@ namespace Flax.Build.Plugins
isNetworkReplicated = true;
break;
}
if (type.IsValueType)
{
if (isINetworkSerializable)
@@ -597,6 +612,7 @@ namespace Flax.Build.Plugins
modified = true;
}
}
if (failed)
throw new Exception($"Failed to generate network replication for assembly {assemblyPath}");
if (!modified)
@@ -650,6 +666,7 @@ namespace Flax.Build.Plugins
il.Emit(OpCodes.Newobj, module.ImportReference(serializeFuncCtor));
il.Emit(OpCodes.Call, module.ImportReference(addSerializer));
}
foreach (var e in methodRPCs)
{
// NetworkReplicator.AddRPC(typeof(<type>), "<name>", <name>_Execute, <isServer>, <isClient>, <channel>);
@@ -664,6 +681,7 @@ namespace Flax.Build.Plugins
il.Emit(OpCodes.Ldc_I4, e.Channel);
il.Emit(OpCodes.Call, module.ImportReference(addRPC));
}
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ret);
c.Methods.Add(m);
@@ -810,17 +828,20 @@ namespace Flax.Build.Plugins
failed = true;
return;
}
if (property.SetMethod == null)
{
MonoCecil.CompilationError($"Missing setter method for property '{property.Name}' of type {valueType.FullName} in {type.FullName} for automatic replication.", property);
failed = true;
return;
}
if (property.GetMethod.IsVirtual)
propertyGetOpCode = OpCodes.Callvirt;
if (property.SetMethod.IsVirtual)
propertySetOpCode = OpCodes.Callvirt;
}
ModuleDefinition module = type.Module;
TypeDefinition valueTypeDef = valueType.Resolve();
if (_inBuildSerializers.TryGetValue(valueType.FullName, out var serializer))
@@ -843,6 +864,7 @@ namespace Flax.Build.Plugins
il.Emit(OpCodes.Ldarg_1);
m = networkStreamType.GetMethod(serializer.ReadMethod);
}
il.Emit(OpCodes.Callvirt, module.ImportReference(m));
if (!serialize)
{
@@ -1222,12 +1244,14 @@ namespace Flax.Build.Plugins
failed = true;
return;
}
if (method.IsVirtual)
{
MonoCecil.CompilationError($"Not supported virtual RPC method '{method.FullName}'.", method);
failed = true;
return;
}
ModuleDefinition module = type.Module;
var voidType = module.TypeSystem.Void;
if (method.ReturnType != voidType)
@@ -1236,12 +1260,14 @@ namespace Flax.Build.Plugins
failed = true;
return;
}
if (method.IsStatic)
{
MonoCecil.CompilationError($"Not supported static RPC method '{method.FullName}'.", method);
failed = true;
return;
}
var methodRPC = new MethodRPC();
methodRPC.Type = type;
methodRPC.Method = method;
@@ -1252,6 +1278,7 @@ namespace Flax.Build.Plugins
methodRPC.IsClient = (bool)attribute.ConstructorArguments[1].Value;
methodRPC.Channel = (int)attribute.ConstructorArguments[2].Value;
}
methodRPC.IsServer = (bool)attribute.GetFieldValue("Server", methodRPC.IsServer);
methodRPC.IsClient = (bool)attribute.GetFieldValue("Client", methodRPC.IsClient);
methodRPC.Channel = (int)attribute.GetFieldValue("Channel", methodRPC.Channel);
@@ -1261,12 +1288,14 @@ namespace Flax.Build.Plugins
failed = true;
return;
}
if (!methodRPC.IsServer && !methodRPC.IsClient)
{
MonoCecil.CompilationError($"Network RPC {method.Name} in {type.FullName} needs to have Server or Client specifier.", method);
failed = true;
return;
}
module.GetType("System.IntPtr", out var intPtrType);
module.GetType("FlaxEngine.Object", out var scriptingObjectType);
var fromUnmanagedPtr = scriptingObjectType.Resolve().GetMethod("FromUnmanagedPtr");
@@ -1306,6 +1335,7 @@ namespace Flax.Build.Plugins
failed = true;
return;
}
var parameterType = parameter.ParameterType;
il.Body.Variables.Add(new VariableDefinition(parameterType));
}
@@ -1324,6 +1354,7 @@ namespace Flax.Build.Plugins
{
il.Emit(OpCodes.Ldloc, argsStart + i);
}
il.Emit(OpCodes.Callvirt, method);
il.Emit(OpCodes.Nop);
@@ -1418,7 +1449,6 @@ namespace Flax.Build.Plugins
il.InsertBefore(ilStart, tmp);
//il.InsertBefore(ilStart, il.Create(OpCodes.Ret));
il.InsertBefore(ilStart, il.Create(OpCodes.Br, jumpBodyEnd));
}
// if (client && networkMode == NetworkManagerMode.Server) return;