Exemple #1
0
        private static ObjectData DecodeV1(BinaryReader reader)
        {
            reader.BaseStream.Position = 0;

            Console.WriteLine(reader.ReadUInt16());

            ushort nameLength = reader.ReadUInt16();

            byte[]        buffer      = reader.ReadBytes(nameLength);
            string        categoryStr = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            ThingCategory category    = ThingCategory.Invalid;

            switch (categoryStr)
            {
            case "item":
                category = ThingCategory.Item;
                break;

            case "outfit":
                category = ThingCategory.Outfit;
                break;

            case "effect":
                category = ThingCategory.Effect;
                break;

            case "missile":
                category = ThingCategory.Missile;
                break;
            }

            ThingType thing = new ThingType(category);

            if (!ThingTypeSerializer.ReadProperties(thing, DatFormat.Format_1010, reader))
            {
                return(null);
            }

            FrameGroup group = new FrameGroup();

            group.Width  = reader.ReadByte();
            group.Height = reader.ReadByte();

            if (group.Width > 1 || group.Height > 1)
            {
                group.ExactSize = reader.ReadByte();
            }
            else
            {
                group.ExactSize = Sprite.DefaultSize;
            }

            group.Layers   = reader.ReadByte();
            group.PatternX = reader.ReadByte();
            group.PatternY = reader.ReadByte();
            group.PatternZ = reader.ReadByte();
            group.Frames   = reader.ReadByte();

            if (group.Frames > 1)
            {
                group.IsAnimation    = true;
                group.AnimationMode  = AnimationMode.Asynchronous;
                group.LoopCount      = 0;
                group.StartFrame     = 0;
                group.FrameDurations = new FrameDuration[group.Frames];

                for (byte i = 0; i < group.Frames; i++)
                {
                    group.FrameDurations[i] = new FrameDuration(category);
                }
            }

            int totalSprites = group.GetTotalSprites();

            if (totalSprites > 4096)
            {
                throw new Exception("The ThingData has more than 4096 sprites.");
            }

            group.SpriteIDs = new uint[totalSprites];
            SpriteGroup spriteGroup = new SpriteGroup();

            Sprite[] sprites = new Sprite[totalSprites];

            for (int i = 0; i < totalSprites; i++)
            {
                uint spriteID = reader.ReadUInt32();
                group.SpriteIDs[i] = spriteID;

                uint dataSize = reader.ReadUInt32();
                if (dataSize > Sprite.PixelsDataSize)
                {
                    throw new Exception("Invalid sprite data size.");
                }

                byte[] pixels = reader.ReadBytes((int)dataSize);

                Sprite sprite = new Sprite(spriteID, true);
                sprite.SetPixelsARGB(pixels);
                sprites[i] = sprite;
            }

            thing.SetFrameGroup(FrameGroupType.Default, group);
            spriteGroup.Add(FrameGroupType.Default, sprites);
            return(new ObjectData(thing, spriteGroup));
        }
Exemple #2
0
        private static byte[] EncodeV1(ObjectData data)
        {
            using (FlagsBinaryWriter writer = new FlagsBinaryWriter(new MemoryStream()))
            {
                // write client version
                writer.Write((ushort)DatFormat.Format_1010);

                // write category
                string category = string.Empty;
                switch (data.Category)
                {
                case ThingCategory.Item:
                    category = "item";
                    break;

                case ThingCategory.Outfit:
                    category = "outfit";
                    break;

                case ThingCategory.Effect:
                    category = "effect";
                    break;

                case ThingCategory.Missile:
                    category = "missile";
                    break;
                }

                writer.Write((ushort)category.Length);
                writer.Write(Encoding.UTF8.GetBytes(category));

                if (!ThingTypeSerializer.WriteProperties(data.ThingType, DatFormat.Format_1010, writer))
                {
                    return(null);
                }

                FrameGroup group = data.GetFrameGroup(FrameGroupType.Default);

                writer.Write(group.Width);
                writer.Write(group.Height);

                if (group.Width > 1 || group.Height > 1)
                {
                    writer.Write(group.ExactSize);
                }

                writer.Write(group.Layers);
                writer.Write(group.PatternX);
                writer.Write(group.PatternY);
                writer.Write(group.PatternZ);
                writer.Write(group.Frames);

                Sprite[] sprites = data.Sprites[FrameGroupType.Default];
                for (int i = 0; i < sprites.Length; i++)
                {
                    Sprite sprite = sprites[i];
                    byte[] pixels = sprite.GetARGBPixels();
                    writer.Write((uint)sprite.ID);
                    writer.Write((uint)pixels.Length);
                    writer.Write(pixels);
                }

                return(LZMACoder.Compress(((MemoryStream)writer.BaseStream).ToArray()));
            }
        }