Exemple #1
0
 private string _DoPrintField(ProtoFiled field, int tab)
 {
     if (field.FieldType == typeof(object))
     {
         return("\n" + _DoPrint((AnyProto)field.FieldValue, tab));
     }
     else
     {
         return(" " + field.FieldValue.ToString());
     }
 }
Exemple #2
0
        private AnyProto _DoParse(byte[] data, uint start, uint len, bool log = true)
        {
            try
            {
                AnyProto anyProto = new AnyProto();

                uint index = start;
                while (len - index > 0)
                {
                    ProtoFiled field = new ProtoFiled();
                    uint       value = 0;
                    uint       used  = 0;

                    if (!ReadVarint32(out value, out used, data, index, len))
                    {
                        if (log)
                        {
                            LogError("ReadVarint32 Failed");
                        }
                        return(null);
                    }

                    index      += used;
                    field.Index = value >> 3;
                    uint nWireType = value & 0x7;
                    if (nWireType > (uint)WireType.WIRETYPE_FIXED32)
                    {
                        if (log)
                        {
                            LogError("WireType > WIRETYPE_FIXED32");
                        }
                        return(null);
                    }

                    WireType type = (WireType)nWireType;

                    switch (type)
                    {
                    case WireType.WIRETYPE_VARINT:
                    {
                        ulong vvalue = 0;
                        if (!ReadVarint64(out vvalue, out used, data, index, len))
                        {
                            if (log)
                            {
                                LogError("ReadVarint64 Failed");
                            }
                            return(null);
                        }
                        index += used;

                        long lvalue = (long)vvalue;
                        if (lvalue > int.MaxValue)
                        {
                            field.FieldType  = typeof(long);
                            field.FieldValue = lvalue;
                        }
                        else
                        {
                            field.FieldType  = typeof(int);
                            field.FieldValue = (int)vvalue;
                        }
                    }
                    break;

                    case WireType.WIRETYPE_FIXED32:
                    {
                        int rvalue = 0;
                        rvalue  = (int)data[index];
                        rvalue |= ((int)data[index + 1]) << 8;
                        rvalue |= ((int)data[index + 2]) << 16;
                        rvalue |= ((int)data[index + 3]) << 24;

                        field.FieldType  = typeof(int);
                        field.FieldValue = rvalue;

                        index += 4;
                    }
                    break;

                    case WireType.WIRETYPE_FIXED64:
                    {
                        long rvalue = 0;
                        rvalue  = (long)data[index];
                        rvalue |= ((long)data[index + 1]) << 8;
                        rvalue |= ((long)data[index + 2]) << 16;
                        rvalue |= ((long)data[index + 3]) << 24;
                        rvalue |= ((long)data[index + 4]) << 32;
                        rvalue |= ((long)data[index + 5]) << 40;
                        rvalue |= ((long)data[index + 6]) << 48;
                        rvalue |= ((long)data[index + 7]) << 56;

                        field.FieldType  = typeof(long);
                        field.FieldValue = rvalue;

                        index += 8;
                    }
                    break;

                    case WireType.WIRETYPE_LENGTH_DELIMITED:
                    {
                        if (!ReadVarint32(out value, out used, data, index, len))
                        {
                            if (log)
                            {
                                LogError("ReadVarint32 Failed");
                            }
                            return(null);
                        }
                        index += used;

                        if (index + value > len)
                        {
                            //Core.Debug.LogError("used data beyond data size");
                            value = (uint)data.Length - index;
                            return(null);
                        }

                        AnyProto tryProto = _DoParse(data, index, index + value, false);
                        if (tryProto != null)
                        {
                            index += value;

                            field.FieldType  = typeof(object);
                            field.IsObject   = true;
                            field.FieldValue = tryProto;
                        }
                        else
                        {
                            if (index + value > data.Length)
                            {
                                //Core.Debug.LogError("used data beyond data size");
                                value = (uint)data.Length - index;
                                return(null);
                            }
                            var getValue = Encoding.UTF8.GetString(data, (int)index, (int)value);
                            index += value;

                            field.FieldType  = typeof(string);
                            field.FieldValue = getValue;
                        }
                    }
                    break;

                    case WireType.WIRETYPE_START_GROUP:
                        break;

                    case WireType.WIRETYPE_END_GROUP:
                        break;

                    default:
                        if (log)
                        {
                            LogError("unexcept");
                        }
                        break;
                    }

                    bool added = false;
                    foreach (var fld in anyProto.Fields)
                    {
                        if (fld.Index == field.Index)
                        {
                            List <ProtoFiled> reptead = null;
                            if (!fld.Repeated)
                            {
                                fld.Repeated = true;
                                reptead      = new List <ProtoFiled>();
                                var nfld = new ProtoFiled();
                                nfld.FieldType  = fld.FieldType;
                                nfld.FieldValue = fld.FieldValue;
                                nfld.Index      = fld.Index;
                                nfld.Repeated   = false;
                                reptead.Add(nfld);
                                fld.FieldValue = reptead;
                            }
                            else
                            {
                                reptead = (List <ProtoFiled>)fld.FieldValue;
                            }

                            reptead.Add(field);
                            added = true;
                            break;
                        }
                    }
                    if (!added)
                    {
                        anyProto.Fields.Add(field);
                    }
                }

                return(anyProto);
            }
            catch (System.Exception e)
            {
                //Core.Debug.LogError(e);
                return(null);
            }
        }