示例#1
0
        public void Serialize(object h)
        {
            pack = new ByteArrayBuilder();
            pack.Add((uint)0);
            Add(h);
            WriteAll();

            long pos = stream.Position;

            pack.Add((uint)pos);
            stream.Seek(0, SeekOrigin.Begin);
            WriteAll();
            stream.Seek(pos, SeekOrigin.Begin);

            ArrayList n = new ArrayList();

            n.AddRange(new int[codes.Count]);
            IDictionaryEnumerator pe = codes.GetEnumerator();

            while (pe.MoveNext())
            {
                n[(int)pe.Value] = (string)pe.Key;
            }
            Add(n);
            WriteAll();
        }
示例#2
0
        public void Add(object o)
        {
            if (o is int)
            {
                pack.Add((byte)0)
                .Add((uint)(int)o);
            }
            else if (o is int[])
            {
                int[] c = (int[])o;
                pack.Add((byte)1).Add((uint)c.Length);
                for (int i = 0; i < c.Length; i++)
                {
                    pack.Add((uint)c[i]);
                }
            }
            else if (o is float)
            {
                pack.Add((byte)2)
                .Add((float)o);
            }
            else if (o is float[])
            {
                float[] fo = (float[])o;
                pack.Add((byte)3)
                .Add((uint)fo.Length)
                .Add(fo);
            }
            else if (o is string)
            {
                pack.Add((byte)4)
                .Add((string)o);
            }
            else if (o is StringBuilder)
            {
                pack.Add((byte)4)
                .Add((o as StringBuilder).ToString());
            }
            else if (o is byte[])
            {
                byte[] bo = o as byte[];
                pack.Add((byte)6)
                .Add((uint)bo.Length)
                .Add(bo);
            }
            else if (o is ArrayList)
            {
                ArrayList h = (ArrayList)o;
                pack.Add((byte)10)
                .Add((uint)h.Count);
                foreach (object oh in h)
                {
                    Add(oh);
                }
            }
            else if (o is Hashtable)
            {
                Hashtable h = (Hashtable)o;

                pack.Add((byte)11)
                .Add((uint)h.Count);

                IDictionaryEnumerator pe = h.GetEnumerator();
                while (pe.MoveNext())
                {
                    if (pe.Key is String)
                    {
                        Add((string)pe.Key);
                    }
                    else
                    {
                        Add((int)pe.Key);
                    }
                    Add(pe.Value);

                    WriteAll();
                }
            }
            else if (o is SpawnData)
            {
                pack.Add((byte)12);
                ((SpawnData)o).Add(this);
                WriteAll();
            }
            else if (o is MapPosition)
            {
                pack.Add((byte)13);
                ((MapPosition)o).Add(this);
                WriteAll();
            }
            else
            {
                throw new FormatException("unknown object " + o.GetType().Name);
            }
        }