Esempio n. 1
0
        public static MapDef valueOf(Type type)
        {
            MapDef t = new MapDef();

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

            if (m == null)
            {
                Debuger.LogError("找不到索引器 不能确定类型:" + type.Name);
                return(null);
            }

            t.keyType   = m.GetParameters()[0].ParameterType;
            t.valueType = m.ReturnType;

            if (ProtocolCoder.CanRegister(t.keyType))
            {
                ProtocolCoder.instance.Register(t.keyType);
            }
            if (ProtocolCoder.CanRegister(t.valueType))
            {
                ProtocolCoder.instance.Register(t.valueType);
            }

            return(t);
        }
Esempio n. 2
0
        public override bool setValue(IoBuffer ioBuffer, object value)
        {
            byte flag = Types.OBJECT;

            TypeDef def = ProtocolCoder.instance.getTypeDef(value.GetType());

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

            // 类型标记,#### 0000
            ioBuffer.Write(flag);

            //名字哈希值
            ioBuffer.Write(def.code);

            // 字段数量
            putVarInt32(ioBuffer, (int)def.fields.Length);

            //获取当前要写入的位置
            var writePos1 = ioBuffer.WritePos;

            //对象长度字段先占位
            ioBuffer.Write(0);

            BeginParentLog(value.GetType().Name, def.fields.Length);
            //Debuger.Log("{0} code:{1} 字段数:{2}", value, def.code, def.fields.Length);
            // 遍历属性
            for (int i = 0; i < def.fields.Length; i++)
            {
                AddLogNewLine(def.fields[i].Name + ": ");//加个分隔符
                if (!ProtocolCoder.instance.Encode(ioBuffer, def.fields[i].GetValue(value)))
                {
                    return(false);
                }
                AddLog(",");//加个分隔符
            }
            EndParentLog();

            //再次获取当前要写入的位置
            var writePos2 = ioBuffer.WritePos;
            //两个位置相减得到对象的序列化大小
            var objectLen = writePos2 - writePos1;

            //更新序列化大小字段
            ioBuffer.WriteBack(objectLen, writePos1);

            return(true);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public static TypeDef valueOf(Type type, Dictionary<System.Type, TypeDef> typeDefs)
        {
            TypeDef t = new TypeDef();
            t.code = type.Name.GetHashCode();
            t.type = type;
            typeDefs.Add(type, t);
            t.fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);//注意这里不支持私有成员
            
            Type fieldType;
            for (int i = 0; i < t.fields.Length; ++i)
            {
                fieldType =t.fields[i].FieldType;
                if (ProtocolCoder.CanRegister(fieldType))
                    ProtocolCoder.instance.Register(fieldType);
            }
            
            
		    return t;
	    }
Esempio n. 5
0
        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);
        }