Example #1
0
        public override bool getValue(IoBuffer ioBuffer, System.Type type, byte flag, out object value)
        {
            value = null;

            // 对象解析
            value = Activator.CreateInstance(type);
            IList list = (IList)value;

            if (list == null)
            {
                Debuger.LogError("类型不是list:{0}", type.Name);
                return(false);
            }

            //获取元素类型
            CollectionDef def = ProtocolCoder.instance.getCollectionDef(type);

            if (def == null)
            {
                Debuger.LogError("list定义{0}不存在", type.FullName);

                return(false);
            }
            Type elementType = def.elementType;

            // 取出元素个数
            int len;

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

            BeginParentLog("List", len);
            bool needNewLine = elementType.IsClass && typeof(string) != elementType;

            for (int i = 0; i < len; i++)
            {
                if (needNewLine)
                {
                    AddLogNewLine("");
                }
                object e;
                if (!ProtocolCoder.instance.Decode(ioBuffer, elementType, out e))
                {
                    return(false);
                }

                list.Add(e);
                AddLog(",");//加个分隔符
            }
            EndParentLog();

            return(true);
        }
Example #2
0
        public override bool setValue(IoBuffer ioBuffer, object value)
        {
            byte  flag = Types.COLLECTION;
            IList l    = (IList)value;

            //获取元素类型
            CollectionDef def = ProtocolCoder.instance.getCollectionDef(value.GetType());

            if (def == null)
            {
                if (ProtocolCoder.CanRegister(value.GetType()))
                {
                    ProtocolCoder.instance.Register(value.GetType());
                }
                def = ProtocolCoder.instance.getCollectionDef(value.GetType());
                if (def == null)
                {
                    Debuger.LogError("list定义{0}不存在", value.GetType().FullName);
                    return(false);
                }
            }

            // #### 0000
            ioBuffer.Write(flag);
            putVarInt32(ioBuffer, l.Count);
            BeginParentLog("List", l.Count);
            bool needNewLine = def.elementType.IsClass && typeof(string) != def.elementType;

            for (int i = 0; i < l.Count; i++)
            {
                if (needNewLine)
                {
                    AddLogNewLine("");
                }
                if (!ProtocolCoder.instance.Encode(ioBuffer, l[i]))
                {
                    return(false);
                }
                AddLog(",");//加个分隔符
            }
            EndParentLog();

            return(true);
        }
        public static CollectionDef valueOf(Type type)
        {
            CollectionDef t = new CollectionDef();

            t.code = type.GetHashCode();
            t.type = type;
            MethodInfo m = type.GetMethod("get_Item");//list的索引器的返回值就是对应类型

            if (m == null)
            {
                Debuger.LogError("找不到索引器 不能确定类型:" + type.Name);
                return(null);
            }
            t.elementType = m.ReturnType;
            if (ProtocolCoder.CanRegister(t.elementType))
            {
                ProtocolCoder.instance.Register(t.elementType);
            }
            return(t);
        }
Example #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);
                }
            }
        }