Exemplo n.º 1
0
        public static long[] ToLongArray(SpObject obj)
        {
            if (obj != null)
            {
                Dictionary <object, SpObject> table = obj.AsTable();
                if (table != null)
                {
                    long[] list  = new long[table.Count];
                    int    index = 0;
                    Dictionary <object, SpObject> .ValueCollection.Enumerator ent = table.Values.GetEnumerator();
                    while (ent.MoveNext())
                    {
                        list[index] = ent.Current.AsLong();
                        index++;
                    }

                    return(list);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public SpObject Decode(SpType type, SpStream mStream)
        {
            if (mStream == null || type == null)
            {
                return(null);
            }

            // buildin type decoding should not be here
            if (mTypeManager.IsBuildinType(type))
            {
                return(null);
            }

            SpObject obj = new SpObject();

            List <int> tags        = new List <int>();
            int        current_tag = 0;

            short fn = mStream.ReadInt16();

            for (short i = 0; i < fn; i++)
            {
                int value = (int)mStream.ReadUInt16();

                if (value == 0)
                {
                    tags.Add(current_tag);
                    current_tag++;
                }
                else
                {
                    if (value % 2 == 0)
                    {
                        SpField f = type.GetFieldByTag(current_tag);
                        if (f == null)
                        {
                            continue;
                        }

                        value = value / 2 - 1;
                        if (f.Type == mTypeManager.Integer)
                        {
                            obj.Insert(f.Name, value);
                        }
                        else if (f.Type == mTypeManager.Boolean)
                        {
                            obj.Insert(f.Name, (value == 0 ? false : true));
                        }
                        else
                        {
                            continue;
                        }

                        current_tag++;
                    }
                    else
                    {
                        //System.Console.WriteLine(string.Format("decode value: {0}",value));
                        current_tag += (value + 1) / 2;
                    }
                }
            }

            for (int c = 0; c < tags.Count; c++)
            {
                int tag = tags[c];

                SpField f = type.GetFieldByTag(tag);
                if (f == null)
                {
                    continue;
                }

                if (f.IsTable)
                {
                    int size = mStream.ReadInt32();
                    if (size == 0)
                    {
                        continue;            //empty table
                    }
                    if (f.Type == mTypeManager.Integer)
                    {
                        byte n     = mStream.ReadByte();
                        int  count = (size - 1) / n;

                        SpObject tbl = new SpObject();
                        for (int i = 0; i < count; i++)
                        {
                            switch (n)
                            {
                            case 4:
                                tbl.Insert(i, mStream.ReadInt32());
                                break;

                            case 8:
                                tbl.Insert(i, mStream.ReadInt64());
                                break;

                            default:
                                return(null);
                            }
                        }

                        obj.Insert(f.Name, tbl);
                    }
                    else if (f.Type == mTypeManager.Boolean)
                    {
                        SpObject tbl = new SpObject();
                        for (int i = 0; i < size; i++)
                        {
                            tbl.Insert(i, mStream.ReadBoolean());
                        }

                        obj.Insert(f.Name, tbl);
                    }
                    else if (f.Type == mTypeManager.String)
                    {
                        SpObject tbl = new SpObject();
                        int      k   = 0;
                        while (size > 0)
                        {
                            int str_len = mStream.ReadInt32();
                            size -= 4;
                            tbl.Insert(k, Encoding.UTF8.GetString(mStream.ReadBytes(str_len), 0, str_len));
                            k++;
                            size -= str_len;
                        }

                        obj.Insert(f.Name, tbl);
                    }
                    else if (f.Type == null)
                    {
                        // unknown type
                        mStream.ReadBytes(size);
                    }
                    else
                    {
                        SpObject tbl = new SpObject();
                        int      k   = 0;
                        while (size > 0)
                        {
                            int obj_len = mStream.ReadInt32();
                            size -= 4;

                            SpObject o = Decode(f.Type, mStream);
                            if (f.KeyName != null)
                            {
                                tbl.Insert(o.AsTable()[f.KeyName].Value, o);
                            }
                            else
                            {
                                tbl.Insert(k, o);
                            }

                            k++;
                            size -= obj_len;
                        }

                        obj.Insert(f.Name, tbl);
                    }
                }
                else
                {
                    int size = mStream.ReadInt32();

                    if (f.Type == mTypeManager.Integer)
                    {
                        switch (size)
                        {
                        case 4:
                            obj.Insert(f.Name, mStream.ReadInt32());
                            break;

                        case 8:
                            obj.Insert(f.Name, mStream.ReadInt64());
                            break;

                        default:
                            continue;
                        }
                    }
                    else if (f.Type == mTypeManager.Boolean)
                    {
                        // boolean should not be here
                        return(null);
                    }
                    else if (f.Type == mTypeManager.String)
                    {
                        obj.Insert(f.Name, Encoding.UTF8.GetString(mStream.ReadBytes(size), 0, size));
                    }
                    else if (f.Type == null)
                    {
                        // unknown type
                        mStream.ReadBytes(size);
                    }
                    else
                    {
                        obj.Insert(f.Name, Decode(f.Type, mStream));
                    }
                }
            }

            return(obj);
        }