generates OnSerialize/OnDeserialize for SyncLists
Exemplo n.º 1
0
        private static bool CheckSyncList(TypeDefinition td)
        {
            if (!td.IsClass)
            {
                return(false);
            }

            var didWork = false;

            // are ANY parent classes SyncListStruct
            var parent = td.BaseType;

            while (parent != null)
            {
                if (parent.FullName.StartsWith(SyncListType.FullName, StringComparison.Ordinal))
                {
                    SyncListProcessor.Process(td);
                    didWork = true;
                    break;
                }

                if (parent.FullName.StartsWith(SyncSetType.FullName, StringComparison.Ordinal))
                {
                    SyncListProcessor.Process(td);
                    didWork = true;
                    break;
                }

                if (parent.FullName.StartsWith(SyncDictionaryType.FullName, StringComparison.Ordinal))
                {
                    SyncDictionaryProcessor.Process(td);
                    didWork = true;
                    break;
                }

                try
                {
                    parent = parent.Resolve().BaseType;
                }
                catch (AssemblyResolutionException)
                {
                    // this can happen for pluins.
                    //Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
                    break;
                }
            }

            // check for embedded types
            foreach (var embedded in td.NestedTypes)
            {
                didWork |= CheckSyncList(embedded);
            }

            return(didWork);
        }
Exemplo n.º 2
0
        static bool WeaveSyncObject(TypeDefinition td)
        {
            if (!td.IsClass)
            {
                return(false);
            }

            bool modified = false;

            // are ANY parent classes SyncListStruct
            TypeReference parent = td.BaseType;

            while (parent != null)
            {
                if (parent.FullName.StartsWith(SyncListType.FullName, StringComparison.Ordinal))
                {
                    SyncListProcessor.Process(td);
                    modified = true;
                    break;
                }
                if (parent.FullName.StartsWith(SyncSetType.FullName, StringComparison.Ordinal))
                {
                    SyncListProcessor.Process(td);
                    modified = true;
                    break;
                }
                if (parent.FullName.StartsWith(SyncDictionaryType.FullName, StringComparison.Ordinal))
                {
                    SyncDictionaryProcessor.Process(td);
                    modified = true;
                    break;
                }
                try
                {
                    parent = parent.Resolve().BaseType;
                }
                catch (AssemblyResolutionException)
                {
                    // this can happen for pluins.
                    //Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
                    break;
                }
            }

            // check for embedded types
            foreach (TypeDefinition embedded in td.NestedTypes)
            {
                modified |= WeaveSyncObject(embedded);
            }

            return(modified);
        }
Exemplo n.º 3
0
        static bool WeaveSyncObject(TypeDefinition td)
        {
            bool modified = false;

            // ignore generic classes
            // we can not process generic classes
            // we give error if a generic syncObject is used in NetworkBehaviour
            if (td.HasGenericParameters)
            {
                return(false);
            }

            // ignore abstract classes
            // we dont need to process abstract classes because classes that
            // inherit from them will be processed instead

            // We cant early return with non classes or Abstract classes
            // because we still need to check for embeded types
            if (td.IsClass || !td.IsAbstract)
            {
                if (td.IsDerivedFrom(WeaverTypes.SyncListType))
                {
                    SyncListProcessor.Process(td, WeaverTypes.SyncListType);
                    modified = true;
                }
                else if (td.IsDerivedFrom(WeaverTypes.SyncSetType))
                {
                    SyncListProcessor.Process(td, WeaverTypes.SyncSetType);
                    modified = true;
                }
                else if (td.IsDerivedFrom(WeaverTypes.SyncDictionaryType))
                {
                    SyncDictionaryProcessor.Process(td);
                    modified = true;
                }
            }

            // check for embedded types
            foreach (TypeDefinition embedded in td.NestedTypes)
            {
                modified |= WeaveSyncObject(embedded);
            }

            return(modified);
        }
Exemplo n.º 4
0
        static bool WeaveSyncObject(TypeDefinition td)
        {
            if (!td.IsClass)
            {
                return(false);
            }

            // ignore generic classes
            // we can not process generic classes
            // we give error if a generic syncObject is used in NetworkBehaviour
            if (td.HasGenericParameters)
            {
                return(false);
            }

            bool modified = false;

            if (td.IsDerivedFrom(SyncListType))
            {
                SyncListProcessor.Process(td);
                modified = true;
            }
            else if (td.IsDerivedFrom(SyncSetType))
            {
                SyncListProcessor.Process(td);
                modified = true;
            }
            else if (td.IsDerivedFrom(SyncDictionaryType))
            {
                SyncDictionaryProcessor.Process(td);
                modified = true;
            }

            // check for embedded types
            foreach (TypeDefinition embedded in td.NestedTypes)
            {
                modified |= WeaveSyncObject(embedded);
            }

            return(modified);
        }
Exemplo n.º 5
0
        ////////////////////////////////////////////////////////////////////////

        void GenerateConstants()
        {
            if (m_Cmds.Count == 0 && m_Rpcs.Count == 0 && m_TargetRpcs.Count == 0 && m_Events.Count == 0 && m_SyncObjects.Count == 0)
            {
                return;
            }

            Weaver.DLog(m_td, "  GenerateConstants ");

            // find static constructor
            MethodDefinition cctor      = null;
            bool             cctorFound = false;

            foreach (MethodDefinition md in m_td.Methods)
            {
                if (md.Name == ".cctor")
                {
                    cctor      = md;
                    cctorFound = true;
                }
            }
            if (cctor != null)
            {
                // remove the return opcode from end of function. will add our own later.
                if (cctor.Body.Instructions.Count != 0)
                {
                    Instruction ret = cctor.Body.Instructions[cctor.Body.Instructions.Count - 1];
                    if (ret.OpCode == OpCodes.Ret)
                    {
                        cctor.Body.Instructions.RemoveAt(cctor.Body.Instructions.Count - 1);
                    }
                    else
                    {
                        Log.Error("No cctor for " + m_td.Name);
                        Weaver.fail = true;
                        return;
                    }
                }
            }
            else
            {
                // make one!
                cctor = new MethodDefinition(".cctor", MethodAttributes.Private |
                                             MethodAttributes.HideBySig |
                                             MethodAttributes.SpecialName |
                                             MethodAttributes.RTSpecialName |
                                             MethodAttributes.Static,
                                             Weaver.voidType);
            }

            // find instance constructor
            MethodDefinition ctor = null;

            foreach (MethodDefinition md in m_td.Methods)
            {
                if (md.Name == ".ctor")
                {
                    ctor = md;

                    var ret = ctor.Body.Instructions[ctor.Body.Instructions.Count - 1];
                    if (ret.OpCode == OpCodes.Ret)
                    {
                        ctor.Body.Instructions.RemoveAt(ctor.Body.Instructions.Count - 1);
                    }
                    else
                    {
                        Weaver.fail = true;
                        Log.Error("No ctor for " + m_td.Name);
                        return;
                    }

                    break;
                }
            }

            if (ctor == null)
            {
                Weaver.fail = true;
                Log.Error("No ctor for " + m_td.Name);
                return;
            }

            ILProcessor ctorWorker  = ctor.Body.GetILProcessor();
            ILProcessor cctorWorker = cctor.Body.GetILProcessor();

            for (int i = 0; i < m_Cmds.Count; ++i)
            {
                GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerCommandDelegateReference, m_CmdInvocationFuncs[i], m_Cmds[i].Name);
            }

            for (int i = 0; i < m_Rpcs.Count; ++i)
            {
                GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerRpcDelegateReference, m_RpcInvocationFuncs[i], m_Rpcs[i].Name);
            }

            for (int i = 0; i < m_TargetRpcs.Count; ++i)
            {
                GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerRpcDelegateReference, m_TargetRpcInvocationFuncs[i], m_TargetRpcs[i].Name);
            }

            for (int i = 0; i < m_Events.Count; ++i)
            {
                GenerateRegisterCommandDelegate(cctorWorker, Weaver.registerEventDelegateReference, m_EventInvocationFuncs[i], m_Events[i].Name);
            }

            foreach (FieldDefinition fd in m_SyncObjects)
            {
                SyncListProcessor.GenerateSyncListInstanceInitializer(ctorWorker, fd);
                SyncObjectProcessor.GenerateSyncObjectInitializer(ctorWorker, fd);
            }

            cctorWorker.Append(cctorWorker.Create(OpCodes.Ret));
            if (!cctorFound)
            {
                m_td.Methods.Add(cctor);
            }

            // finish ctor
            ctorWorker.Append(ctorWorker.Create(OpCodes.Ret));

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