コード例 #1
0
ファイル: EnumProxy.cs プロジェクト: tongxingyang/TanksBattle
        public override bool setValue(IoBuffer ioBuffer, object value)
        {
            byte flag = Types.ENUM;

            EnumDef def = ProtocolCoder.instance.getEnumDef(value.GetType());

            if (def == null)
            {
                ProtocolCoder.instance.Register(value.GetType());
                def = ProtocolCoder.instance.getEnumDef(value.GetType());
                if (def == null)
                {
                    Debuger.LogError("找不到枚举的预定义:{0}", value.GetType().Name);
                    return(false);
                }
            }

            ioBuffer.Write(flag);
            //名字哈希值
            ioBuffer.Write(def.code);
            //枚举对应的数值
            putVarInt32(ioBuffer, (int)value);
            //Debuger.Log("{0} code:{1} value:{2}",value,def.code,(int)value);
            AddLog(value);
            return(true);
        }
コード例 #2
0
ファイル: EnumDef.cs プロジェクト: tongxingyang/TanksWar
        public static EnumDef valueOf(Type type)
        {
            EnumDef e = new EnumDef();

            if (!type.IsEnum)
            {
                Debuger.LogError("逻辑错误。{0}不是枚举", type.Name);
                return(null);
            }
            e.code  = type.Name.GetHashCode();
            e.type  = type;
            e.names = System.Enum.GetNames(type);
            Array arr = System.Enum.GetValues(type);

            e.values = new Enum[arr.Length];
            for (int i = 0; i < arr.Length; ++i)
            {
                e.values[i] = (Enum)arr.GetValue(i);
                // Debuger.LogError("" + (int)(object)(e.values[i]));
            }
            return(e);
        }
コード例 #3
0
ファイル: EnumProxy.cs プロジェクト: tongxingyang/TanksBattle
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            if (ioBuffer.ReadSize < 4)
            {
                Debuger.LogError("可读字节长度不足" + 4);
                return(false);
            }

            int code = ioBuffer.ReadInt32();

            //类型的获取
            EnumDef def = ProtocolCoder.instance.getEnumDef(code);

            if (def == null)
            {
                Debuger.LogError("枚举定义{0}不存在", type.Name);
                return(false);
            }

            if (def.type != type)
            {
                Debuger.LogError("枚举类型不匹配。EnumDef:{0} Type:", def.type.Name, type.Name);
                return(false);
            }

            // 枚举值
            int valNum;

            if (!readVarInt32(ioBuffer, out valNum))
            {
                return(false);
            }

            value = System.Enum.ToObject(type, valNum);
            AddLog(value);
            return(true);
        }
コード例 #4
0
        public void Register(System.Type clz)
        {
            if (clz.IsEnum)
            {
                if (m_enumDefs.Get(clz) == null)
                {
                    EnumDef def = EnumDef.valueOf(clz);
                    if (def == null)
                    {
                        return;
                    }
                    m_enumIdxs.Add(def.code, def);
                    m_enumDefs.Add(clz, def);
                    m_log.AppendFormat("Protocol注册了枚举:{0} code:{1} \n", clz.Name, def.code);
                }
            }
            else if (!clz.IsClass)
            {
                Debuger.LogError("不能注册的类型:{0}", clz.Name);
            }
            else if (clz.IsArray)
            {
                System.Type elemType = clz.GetElementType();
                if (ProtocolCoder.CanRegister(elemType))
                {
                    ProtocolCoder.instance.Register(elemType);
                }
            }
            else if (clz.GetInterface("System.Collections.IList") != null)
            {
                if (m_collectionDefs.Get(clz) == null)
                {
                    CollectionDef def = CollectionDef.valueOf(clz);
                    if (def == null)
                    {
                        return;
                    }
                    m_collectionIdxs.Add(def.code, def);
                    m_collectionDefs.Add(clz, def);
                    m_log.AppendFormat("Protocol注册了list:{0} code:{1} element:{2} \n", clz.Name, def.code, def.elementType.Name);
                }
            }
            else if (clz.GetInterface("System.Collections.IDictionary") != null)
            {
                if (m_mapDefs.Get(clz) == null)
                {
                    MapDef def = MapDef.valueOf(clz);
                    if (def == null)
                    {
                        return;
                    }
                    m_mapIdxs.Add(def.code, def);
                    m_mapDefs.Add(clz, def);
                    m_log.AppendFormat("Protocol注册了map:{0} code:{1} key:{2} value:{3} \n", clz.Name, def.code, def.keyType.Name, def.valueType.Name);
                }
            }
            else
            {
                if (m_typeDefs.Get(clz) == null)
                {
                    if (!IsObjectType(clz))
                    {
                        Debuger.LogError("不能序列化的类型,不能序列化模板类型、c#原生类型、继承自其他类的类型:{0}", clz.Name);
                        return;
                    }
                    TimeCheck check = new TimeCheck();
                    TypeDef   def   = TypeDef.valueOf(clz, m_typeDefs);
                    if (def == null)
                    {
                        return;
                    }
                    m_typeIdxs.Add(def.code, def);

                    m_log.AppendFormat("Protocol注册了类:{0} code:{1} 耗时:{2} \n", clz.Name, def.code, check.delayMS);
                }
            }
        }