Пример #1
0
        void RegisterRpcs()
        {
            Weaver.DebugLog(netBehaviourSubclass, "  GenerateConstants ");

            AddToStaticConstructor(netBehaviourSubclass, (worker) =>
            {
                serverRpcProcessor.RegisterServerRpcs(worker);
                clientRpcProcessor.RegisterClientRpcs(worker);
            });
        }
Пример #2
0
        void RegisterRpcs()
        {
            Weaver.DLog(netBehaviourSubclass, "  GenerateConstants ");

            // find static constructor
            MethodDefinition cctor = netBehaviourSubclass.GetMethod(".cctor");

            if (cctor != null)
            {
                // remove the return opcode from end of function. will add our own later.
                if (cctor.Body.Instructions.Count != 0)
                {
                    Instruction retInstr = cctor.Body.Instructions[cctor.Body.Instructions.Count - 1];
                    if (retInstr.OpCode == OpCodes.Ret)
                    {
                        cctor.Body.Instructions.RemoveAt(cctor.Body.Instructions.Count - 1);
                    }
                    else
                    {
                        logger.Error($"{netBehaviourSubclass.Name} has invalid class constructor", cctor);
                        return;
                    }
                }
            }
            else
            {
                // make one!
                cctor = netBehaviourSubclass.AddMethod(".cctor", MethodAttributes.Private |
                                                       MethodAttributes.HideBySig |
                                                       MethodAttributes.SpecialName |
                                                       MethodAttributes.RTSpecialName |
                                                       MethodAttributes.Static);
            }

            ILProcessor cctorWorker = cctor.Body.GetILProcessor();

            serverRpcProcessor.RegisterServerRpcs(cctorWorker);

            clientRpcProcessor.RegisterClientRpcs(cctorWorker);

            cctorWorker.Append(cctorWorker.Create(OpCodes.Ret));

            // in case class had no cctor, it might have BeforeFieldInit, so injected cctor would be called too late
            netBehaviourSubclass.Attributes &= ~TypeAttributes.BeforeFieldInit;
        }