public static MethodDefinition ProcessRpcInvoke(TypeDefinition td, MethodDefinition md, MethodDefinition rpcCallFunc) { var rpc = new MethodDefinition( RpcPrefix + md.Name, MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig, Weaver.voidType); ILProcessor rpcWorker = rpc.Body.GetILProcessor(); Instruction label = rpcWorker.Create(OpCodes.Nop); NetworkBehaviourProcessor.WriteClientActiveCheck(rpcWorker, md.Name, label, "RPC"); // setup for reader rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); rpcWorker.Append(rpcWorker.Create(OpCodes.Castclass, td)); if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(md, rpcWorker, false)) { return(null); } // invoke actual command function rpcWorker.Append(rpcWorker.Create(OpCodes.Callvirt, rpcCallFunc)); rpcWorker.Append(rpcWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(rpc.Parameters); td.Methods.Add(rpc); return(rpc); }
public static MethodDefinition ProcessEventInvoke(TypeDefinition td, EventDefinition ed) { // find the field that matches the event FieldDefinition eventField = null; foreach (FieldDefinition fd in td.Fields) { if (fd.FullName == ed.FullName) { eventField = fd; break; } } if (eventField == null) { Weaver.DLog(td, "ERROR: no event field?!"); Weaver.fail = true; return(null); } MethodDefinition cmd = new MethodDefinition("InvokeSyncEvent" + ed.Name, MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig, Weaver.voidType); ILProcessor cmdWorker = cmd.Body.GetILProcessor(); Instruction label1 = cmdWorker.Create(OpCodes.Nop); Instruction label2 = cmdWorker.Create(OpCodes.Nop); NetworkBehaviourProcessor.WriteClientActiveCheck(cmdWorker, ed.Name, label1, "Event"); // null event check cmdWorker.Append(cmdWorker.Create(OpCodes.Ldarg_0)); cmdWorker.Append(cmdWorker.Create(OpCodes.Castclass, td)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ldfld, eventField)); cmdWorker.Append(cmdWorker.Create(OpCodes.Brtrue, label2)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ret)); cmdWorker.Append(label2); // setup reader cmdWorker.Append(cmdWorker.Create(OpCodes.Ldarg_0)); cmdWorker.Append(cmdWorker.Create(OpCodes.Castclass, td)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ldfld, eventField)); // read the event arguments MethodReference invoke = Resolvers.ResolveMethod(eventField.FieldType, Weaver.scriptDef, "Invoke"); if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(td, invoke.Resolve(), cmdWorker, false)) { return(null); } // invoke actual event delegate function cmdWorker.Append(cmdWorker.Create(OpCodes.Callvirt, invoke)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(cmd.Parameters); return(cmd); }
/* * // generates code like: * protected static void InvokeCmdCmdThrust(NetworkBehaviour obj, NetworkReader reader) * { * if (!obj.netIdentity.server.active) * { * return; * } * ((ShipControl)obj).CmdThrust(reader.ReadSingle(), (int)reader.ReadPackedUInt32()); * } */ public static MethodDefinition ProcessCommandInvoke(TypeDefinition td, MethodDefinition md, MethodDefinition cmdCallFunc) { MethodDefinition cmd = new MethodDefinition(CmdPrefix + md.Name, MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig, Weaver.voidType); ILProcessor cmdWorker = cmd.Body.GetILProcessor(); Instruction label = cmdWorker.Create(OpCodes.Nop); NetworkBehaviourProcessor.WriteServerActiveCheck(cmdWorker, md.Name, label, "Command"); // setup for reader cmdWorker.Append(cmdWorker.Create(OpCodes.Ldarg_0)); cmdWorker.Append(cmdWorker.Create(OpCodes.Castclass, td)); if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(md, cmdWorker, false)) { return(null); } // invoke actual command function cmdWorker.Append(cmdWorker.Create(OpCodes.Callvirt, cmdCallFunc)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(cmd.Parameters); return(cmd); }
public static MethodDefinition ProcessEventInvoke(TypeDefinition td, EventDefinition ed) { // find the field that matches the event FieldDefinition eventField = null; foreach (var fd in td.Fields) { if (fd.FullName == ed.FullName) { eventField = fd; break; } } if (eventField == null) { Weaver.Error($"{td} not found. Did you declare the event?"); return(null); } var cmd = new MethodDefinition("InvokeSyncEvent" + ed.Name, MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig, Weaver.voidType); var cmdWorker = cmd.Body.GetILProcessor(); var label1 = cmdWorker.Create(OpCodes.Nop); var label2 = cmdWorker.Create(OpCodes.Nop); NetworkBehaviourProcessor.WriteClientActiveCheck(cmdWorker, ed.Name, label1, "Event"); // null event check cmdWorker.Append(cmdWorker.Create(OpCodes.Ldarg_0)); cmdWorker.Append(cmdWorker.Create(OpCodes.Castclass, td)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ldfld, eventField)); cmdWorker.Append(cmdWorker.Create(OpCodes.Brtrue, label2)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ret)); cmdWorker.Append(label2); // setup reader cmdWorker.Append(cmdWorker.Create(OpCodes.Ldarg_0)); cmdWorker.Append(cmdWorker.Create(OpCodes.Castclass, td)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ldfld, eventField)); // read the event arguments var invoke = Resolvers.ResolveMethod(eventField.FieldType, Weaver.CurrentAssembly, "Invoke"); if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(invoke.Resolve(), cmdWorker, false)) { return(null); } // invoke actual event delegate function cmdWorker.Append(cmdWorker.Create(OpCodes.Callvirt, invoke)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(cmd.Parameters); return(cmd); }
public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodDefinition md, MethodDefinition rpcCallFunc) { var rpc = new MethodDefinition(RpcProcessor.RpcPrefix + md.Name, MethodAttributes.Family | MethodAttributes.Static | MethodAttributes.HideBySig, Weaver.voidType); ILProcessor rpcWorker = rpc.Body.GetILProcessor(); Instruction label = rpcWorker.Create(OpCodes.Nop); NetworkBehaviourProcessor.WriteClientActiveCheck(rpcWorker, md.Name, label, "TargetRPC"); // setup for reader rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); rpcWorker.Append(rpcWorker.Create(OpCodes.Castclass, td)); // NetworkConnection parameter is optional bool hasNetworkConnection = HasNetworkConnectionParameter(md); if (hasNetworkConnection) { //client.connection rpcWorker.Append(rpcWorker.Create(OpCodes.Ldarg_0)); rpcWorker.Append(rpcWorker.Create(OpCodes.Call, Weaver.BehaviorConnectionToServerReference)); } // process reader parameters and skip first one if first one is NetworkConnection if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(md, rpcWorker, hasNetworkConnection)) { return(null); } // invoke actual command function rpcWorker.Append(rpcWorker.Create(OpCodes.Callvirt, rpcCallFunc)); rpcWorker.Append(rpcWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(rpc.Parameters); td.Methods.Add(rpc); return(rpc); }