Example #1
0
        ///////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// 从流中读取lua消息
        /// </summary>
        /// <param name="idMsg">配置表中的消息索引</param>
        /// <returns></returns>
        public luaMessage readLuaMessage(object idMsg)
        {
            luaMessage msg = (luaMessage)luaMessageCache.createTypeInstance(idMsg);

            if (msg == null)
            {
                return(null);
            }

            int           fieldCount = msg.fieldsCount();
            fieldDataInfo fieldData  = null;

            for (int idx = 1; idx <= fieldCount; idx++)
            {
                fieldData = (fieldDataInfo)msg[idx];
                if (fieldData == null)
                {
                    continue;
                }

                WireType      wireType       = fieldData.descriptor.wireType;
                ProtoTypeCode typeCode       = fieldData.descriptor.typeCode;
                string        nestedTypeName = fieldData.descriptor.nestedTypeName;

                if (tryReadFieldHeader(idx, wireType))
                {
                    //创建值字段
                    if (!fieldData.appendValue())
                    {
                        continue;
                    }

                    if (fieldData.descriptor.isRepeated())
                    {
                        do
                        {
                            var obj = readLuaObject(typeCode, wireType, nestedTypeName);
                            fieldData.Value.addChild(obj);
                        } while (tryReadFieldHeader(idx, wireType));
                    }
                    else
                    {
                        fieldData.Value.valueL = readLuaObject(typeCode, wireType, nestedTypeName);
                    }
                }
            }

            return(msg);
        }
Example #2
0
        /// <summary>
        /// 读取lua消息
        /// </summary>
        internal static luaMessage readLuaMessage(object idMsg, LuaTable luaMsgTbl)
        {
            if (luaMsgTbl == null)
            {
                return(null);
            }

            luaMessage msg = (luaMessage)luaMessageCache.createTypeInstance(idMsg);

            if (msg == null)
            {
                return(null);
            }

            //按照索引顺序读取lua表中对应字段的值
            int           count     = msg.fieldsCount();
            fieldDataInfo fieldData = null;

            for (int i = 1; i <= count; i++)
            {
                fieldData = (fieldDataInfo)msg[i];
                if (fieldData == null)
                {
                    continue;
                }

                //检查lua表中对应字段的值
                string name = fieldData.descriptor.name;
                if (luaMsgTbl[name] == null)
                {
                    continue;
                }

                //创建值字段
                if (!fieldData.appendValue())
                {
                    continue;
                }

                fieldDescriptor des = fieldData.descriptor;
                if (des.isRepeated())
                {
                    LuaTable repeatedTbl = (LuaTable)luaMsgTbl[name];
                    for (int j = 1; j <= repeatedTbl.length(); j++)
                    {
                        object subValue = luaType2CType(des.typeCode, repeatedTbl[j]);
                        if (subValue == null)
                        {
                            continue;
                        }

                        //嵌入类型
                        if (des.haveNestedType())
                        {
                            object subMsg = readLuaMessage(des.nestedTypeName, (LuaTable)subValue);
                            if (subMsg != null)
                            {
                                fieldData.Value.addChild(subMsg);
                            }
                        }
                        else
                        {
                            fieldData.Value.addChild(subValue);
                        }
                    }
                }
                else
                {
                    object value = luaType2CType(des.typeCode, luaMsgTbl[name]);
                    if (value == null)
                    {
                        continue;
                    }

                    //嵌入类型
                    if (des.haveNestedType())
                    {
                        fieldData.Value.valueL = readLuaMessage(des.nestedTypeName, (LuaTable)value);
                    }
                    else
                    {
                        fieldData.Value.valueL = value;
                    }
                }
            }

            return(msg);
        }