Exemplo n.º 1
0
        public bool ReplaceSprite(Bitmap newBitmap, uint replaceId)
        {
            if (newBitmap == null)
            {
                throw new ArgumentNullException("newBitmap");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling || newBitmap.Width != Sprite.DefaultSize || newBitmap.Height != Sprite.DefaultSize || replaceId == 0 || replaceId > this.Count)
            {
                return false;
            }

            Sprite newSprite = new Sprite(replaceId, this.transparency);
            Sprite replacedSprite = null;

            if (this.sprites.ContainsKey(replaceId))
            {
                replacedSprite = this.sprites[replaceId];
                this.sprites[replaceId] = newSprite;
            }
            else
            {
                replacedSprite = this.ReadSprite(replaceId);
                this.sprites.Add(replaceId, newSprite);
            }

            this.Changed = true;

            if (this.StorageChanged != null)
            {
                this.StorageChanged(this, new SpriteListChangedArgs(new Sprite[] { replacedSprite }, StorageChangeType.Replace));
            }

            return true;
        }
Exemplo n.º 2
0
        public bool ReplaceSprite(Sprite newSprite, uint replaceId)
        {
            if (newSprite == null)
            {
                throw new ArgumentNullException("newSprite");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling || replaceId == 0 || replaceId > this.Count)
            {
                return false;
            }

            newSprite.ID = replaceId;
            newSprite.Transparent = this.transparency;

            Sprite replacedSprite = null;

            if (this.sprites.ContainsKey(replaceId))
            {
                replacedSprite = this.sprites[replaceId];
                this.sprites[replaceId] = newSprite;
            }
            else
            {
                replacedSprite = this.ReadSprite(replaceId);
                this.sprites.Add(replaceId, newSprite);
            }

            this.Changed = true;

            if (this.StorageChanged != null)
            {
                this.StorageChanged(this, new SpriteListChangedArgs(new Sprite[] { replacedSprite }, StorageChangeType.Replace));
            }

            return true;
        }
Exemplo n.º 3
0
        public bool ReplaceSprite(Sprite newSprite)
        {
            if (newSprite == null)
            {
                throw new ArgumentNullException("newSprite");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling)
            {
                return this.ReplaceSprite(newSprite, newSprite.ID);
            }

            return false;
        }
Exemplo n.º 4
0
        public bool AddSprites(Bitmap[] sprites)
        {
            if (sprites == null)
            {
                throw new ArgumentNullException("sprites");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling || sprites.Length == 0)
            {
                return false;
            }

            List<Sprite> changedSprites = new List<Sprite>();

            foreach (Bitmap bitmap in sprites)
            {
                if (bitmap == null || bitmap.Width != Sprite.DefaultSize || bitmap.Height != Sprite.DefaultSize)
                {
                    continue;
                }

                if (this.IsFull)
                {
                    throw new Exception("The limit of sprites was reached.");
                }

                uint id = ++this.Count;
                Sprite sprite = new Sprite(id, this.transparency);
                this.sprites.Add(id, sprite);
                changedSprites.Add(sprite);
            }

            if (changedSprites.Count != 0)
            {
                this.Changed = true;

                if (this.StorageChanged != null)
                {
                    this.StorageChanged(this, new SpriteListChangedArgs(changedSprites.ToArray(), StorageChangeType.Add));
                }

                return true;
            }

            return false;
        }
Exemplo n.º 5
0
        public void Dispose()
        {
            this.Disposed = true;

            if (!this.Loaded)
            {
                return;
            }

            if (this.stream != null)
            {
                this.stream.Dispose();
                this.stream = null;
                this.reader.Dispose();
                this.reader = null;
            }

            this.FilePath = null;
            this.sprites.Clear();
            this.sprites = null;
            this.rawSpriteCount = 0;
            this.Count = 0;
            this.transparency = false;
            this.Changed = false;
            this.Loaded = false;
            this.Compiling = false;
            this.blankSprite = null;

            if (this.StorageDisposed != null)
            {
                this.StorageDisposed(this, new EventArgs());
            }
        }
Exemplo n.º 6
0
        public ThingData GetThingData(ushort id, ThingCategory category, bool singleFrameGroup)
        {
            ThingType thing = this.Things.GetThing(id, category);
            if (thing == null)
            {
                return null;
            }

            if (singleFrameGroup)
            {
                thing = ThingType.ToSingleFrameGroup(thing);
            }

            SpriteGroup spriteGroups = new SpriteGroup();

            Console.WriteLine(thing.FrameGroupCount);

            for (byte i = 0; i < thing.FrameGroupCount; i++)
            {
                FrameGroupType groupType = (FrameGroupType)i;
                FrameGroup frameGroup = thing.GetFrameGroup(groupType);
                int length = frameGroup.SpriteIDs.Length;
                Sprite[] sprites = new Sprite[length];

                for (int s = 0; s < length; s++)
                {
                    sprites[s] = this.Sprites.GetSprite(frameGroup.SpriteIDs[s]);
                }

                spriteGroups.Add(groupType, sprites);
            }

            return new ThingData(thing, spriteGroups);
        }
Exemplo n.º 7
0
        public bool AddSprites(Sprite[] sprites)
        {
            if (sprites == null)
            {
                throw new ArgumentNullException("sprites");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling || sprites.Length == 0)
            {
                return false;
            }

            List<Sprite> changedSprites = new List<Sprite>();

            foreach (Sprite sprite in sprites)
            {
                if (sprite == null)
                {
                    continue;
                }

                if (this.IsFull)
                {
                    throw new Exception("The limit of sprites was reached.");
                }

                uint id = ++this.Count;
                sprite.ID = id;
                sprite.Transparent = this.transparency;
                this.sprites.Add(id, sprite);
                changedSprites.Add(sprite);
            }

            if (changedSprites.Count != 0)
            {
                this.Changed = true;

                if (this.StorageChanged != null)
                {
                    this.StorageChanged(this, new SpriteListChangedArgs(changedSprites.ToArray(), StorageChangeType.Add));
                }

                return true;
            }

            return false;
        }
Exemplo n.º 8
0
        private Sprite ReadSprite(uint id)
        {
            try
            {
                if (id > this.rawSpriteCount)
                {
                    return null;
                }

                if (id == 0)
                {
                    return this.blankSprite;
                }

                // O id 1 no arquivo spr é o endereço 0, então subtraímos
                // o id fornecido e mutiplicamos pela quantidade de bytes
                // de cada endereço.
                this.stream.Position = ((id - 1) * 4) + this.headSize;

                // Lê o endereço do sprite.
                uint spriteAddress = this.reader.ReadUInt32();

                // O endereço 0 representa um sprite em branco,
                // então retornamos um sprite sem a leitura dos dados.
                if (spriteAddress == 0)
                {
                    return new Sprite(id, this.transparency);
                }

                // Posiciona o stream para o endereço do sprite.
                this.stream.Position = spriteAddress;

                // Leitura da cor magenta usada como referência
                // para remover o fundo do sprite.
                this.reader.ReadByte(); // red key color
                this.reader.ReadByte(); // green key color
                this.reader.ReadByte(); // blue key color

                Sprite sprite = new Sprite(id, this.transparency);

                // O tamanho dos pixels compressados.
                ushort pixelDataSize = this.reader.ReadUInt16();
                if (pixelDataSize != 0)
                {
                    sprite.CompressedPixels = this.reader.ReadBytes(pixelDataSize);
                }

                return sprite;
            }
            catch /*(Exception ex)*/
            {
                // TODO ErrorManager.ShowError(ex);
            }

            return null;
        }
Exemplo n.º 9
0
        public bool AddSprite(Sprite sprite)
        {
            if (sprite == null)
            {
                throw new ArgumentNullException("sprite");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling)
            {
                return false;
            }

            uint id = ++this.Count;

            sprite.ID = id;
            sprite.Transparent = this.transparency;

            this.sprites.Add(id, sprite);
            this.Changed = true;

            if (this.StorageChanged != null)
            {
                this.StorageChanged(this, new SpriteListChangedArgs(new Sprite[] { sprite }, StorageChangeType.Add));
            }

            return true;
        }
Exemplo n.º 10
0
        private bool InternalCreate(Core.Version version, ClientFeatures features)
        {
            if (this.Compiling)
            {
                return false;
            }

            if (this.Loaded)
            {
                return true;
            }

            if (features == ClientFeatures.None || features == ClientFeatures.Transparency)
            {
                features |= version.Value >= (ushort)DatFormat.Format_755 ? ClientFeatures.PatternZ : features;
                features |= version.Value >= (ushort)DatFormat.Format_960 ? ClientFeatures.Extended : features;
                features |= version.Value >= (ushort)DatFormat.Format_1050 ? ClientFeatures.FrameDurations : features;
                features |= version.Value >= (ushort)DatFormat.Format_1057 ? ClientFeatures.FrameGroups : features;
            }

            this.Version = version;
            this.ClientFeatures = features;
            this.transparency = (features & ClientFeatures.Transparency) == ClientFeatures.Transparency ? true : false;
            this.headSize = (features & ClientFeatures.Extended) == ClientFeatures.Extended ? HeaderU32 : HeaderU16;
            this.blankSprite = new Sprite(0, this.transparency);
            this.sprites.Add(1, new Sprite(1, this.transparency));
            this.rawSpriteCount = 0;
            this.Count = 1;
            this.Changed = true;
            this.Loaded = true;
            this.Compiling = false;
            this.Disposed = false;
            return true;
        }
Exemplo n.º 11
0
        private bool InternalLoad(string path, Core.Version version, ClientFeatures features, bool reloading)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (version == null)
            {
                throw new ArgumentNullException("version");
            }

            if (!File.Exists(path))
            {
                string message = "File not found: {0}"; // TODO: ResourceManager.GetString("Exception.FileNotFound");
                throw new FileNotFoundException(string.Format(message, path), "path");
            }

            if (this.Compiling)
            {
                return false;
            }

            if (this.Loaded)
            {
                if (reloading)
                {
                    this.stream.Close();
                    this.stream = null;
                }
                else
                {
                    return true;
                }
            }

            if (features == ClientFeatures.None || features == ClientFeatures.Transparency)
            {
                features |= version.Value >= (ushort)DatFormat.Format_755 ? ClientFeatures.PatternZ : features;
                features |= version.Value >= (ushort)DatFormat.Format_960 ? ClientFeatures.Extended : features;
                features |= version.Value >= (ushort)DatFormat.Format_1050 ? ClientFeatures.FrameDurations : features;
                features |= version.Value >= (ushort)DatFormat.Format_1057 ? ClientFeatures.FrameGroups : features;
            }

            this.stream = new FileStream(path, FileMode.Open);
            this.reader = new BinaryReader(this.stream);

            uint signature = this.reader.ReadUInt32();
            if (signature != version.SprSignature)
            {
                string message = "Invalid SPR signature. Expected signature is {0:X} and loaded signature is {1:X}.";
                throw new Exception(string.Format(message, version.SprSignature, signature));
            }

            if ((features & ClientFeatures.Extended) == ClientFeatures.Extended)
            {
                this.headSize = HeaderU32;
                this.rawSpriteCount = this.reader.ReadUInt32();
            }
            else
            {
                this.headSize = HeaderU16;
                this.rawSpriteCount = this.reader.ReadUInt16();
            }

            this.FilePath = path;
            this.Version = version;
            this.ClientFeatures = features;
            this.transparency = (features & ClientFeatures.Transparency) == ClientFeatures.Transparency ? true : false;
            this.Count = this.rawSpriteCount;
            this.blankSprite = new Sprite(0, this.transparency);
            this.Changed = false;
            this.Loaded = true;
            this.Disposed = false;
            return true;
        }
Exemplo n.º 12
0
 public void AddRange(Sprite[] sprites)
 {
     this.Items.AddRange(sprites);
 }
Exemplo n.º 13
0
 public void Add(Sprite sprite)
 {
     this.Items.Add(sprite);
 }
Exemplo n.º 14
0
 public SpriteListChangedArgs(Sprite[] changedSprites, StorageChangeType changeType)
 {
     this.ChangedSprites = changedSprites;
     this.ChangeType = changeType;
 }
Exemplo n.º 15
0
        public bool ReplaceSprites(Sprite[] newSprites)
        {
            if (newSprites == null)
            {
                throw new ArgumentNullException("newSprites");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling || newSprites.Length == 0)
            {
                return false;
            }

            List<Sprite> changedSprites = new List<Sprite>();

            foreach (Sprite sprite in newSprites)
            {
                if (sprite == null || sprite.ID == 0 || sprite.ID > this.Count)
                {
                    continue;
                }

                uint id = sprite.ID;
                Sprite replacedSprite = null;

                sprite.Transparent = this.transparency;

                if (this.sprites.ContainsKey(id))
                {
                    replacedSprite = this.sprites[id];
                    this.sprites[id] = sprite;
                }
                else
                {
                    replacedSprite = this.ReadSprite(id);
                    this.sprites.Add(id, sprite);
                }

                changedSprites.Add(replacedSprite);
            }

            if (changedSprites.Count != 0)
            {
                this.Changed = true;

                if (this.StorageChanged != null)
                {
                    this.StorageChanged(this, new SpriteListChangedArgs(changedSprites.ToArray(), StorageChangeType.Replace));
                }

                return true;
            }

            return false;
        }
Exemplo n.º 16
0
        public bool AddSprite(Bitmap bitmap)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap");
            }

            if (this.Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!this.Loaded || this.Compiling || bitmap.Width != Sprite.DefaultSize || bitmap.Height != Sprite.DefaultSize)
            {
                return false;
            }

            if (this.IsFull)
            {
                throw new Exception("The limit of sprites was reached.");
            }

            uint id = ++this.Count;
            Sprite sprite = new Sprite(id, this.transparency);
            sprite.SetBitmap(bitmap);
            this.sprites.Add(id, sprite);
            this.Changed = true;

            if (this.StorageChanged != null)
            {
                this.StorageChanged(this, new SpriteListChangedArgs(new Sprite[] { sprite }, StorageChangeType.Add));
            }

            return true;
        }
Exemplo n.º 17
0
 public Sprite Clone()
 {
     Sprite clone = new Sprite(this.ID, this.transparent);
     clone.CompressedPixels = this.CompressedPixels != null ? (byte[])this.CompressedPixels.Clone() : null;
     return clone;
 }
Exemplo n.º 18
0
        private static ThingData 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 ThingData(thing, spriteGroup);
        }