Exemple #1
0
        public bool loadDat(string filename, UInt32 signature)
        {
            FileStream fileStream = new FileStream(filename, FileMode.Open);
            try
            {
                using (BinaryReader reader = new BinaryReader(fileStream))
                {
                    UInt32 datSignature = reader.ReadUInt32();
                    if (signature != 0 && datSignature != signature)
                    {
                        return false;
                    }

                    //get max id
                    UInt16 itemCount = reader.ReadUInt16();
                    UInt16 creatureCount = reader.ReadUInt16();
                    UInt16 effectCount = reader.ReadUInt16();
                    UInt16 distanceCount = reader.ReadUInt16();

                    UInt16 minclientID = 100; //items starts at 100
                    UInt16 maxclientID = itemCount;

                    UInt16 id = minclientID;
                    while (id <= maxclientID)
                    {
                        SpriteItem item = new SpriteItem();
                        item.id = id;
                        items[id] = item;

                        // read the options until we find 0xff
                        byte optbyte;
                        do
                        {
                            optbyte = reader.ReadByte();
                            //Trace.WriteLine(String.Format("{0:X}", optbyte));

                            switch (optbyte)
                            {
                                case 0x00: //groundtile
                                    {
                                        item.groundSpeed = reader.ReadUInt16();
                                        item.type = ItemType.Ground;
                                    } break;

                                case 0x01: //all OnTop - CLIP item
                                    {
                                        item.alwaysOnTop = true;
                                        item.alwaysOnTopOrder = 1;
                                    } break;

                                case 0x02: //can walk trough (open doors, arces, bug pen fence) | BOTTOM item
                                    {
                                        item.alwaysOnTop = true;
                                        item.alwaysOnTopOrder = 2;
                                    } break;

                                case 0x03: //can walk trough | TOP item
                                    {
                                        item.alwaysOnTop = true;
                                        item.alwaysOnTopOrder = 3;
                                    } break;

                                case 0x04: //container
                                    {
                                        item.type = ItemType.Container;
                                    } break;

                                case 0x05: //stackable
                                    {
                                        item.isStackable = true;
                                        break;
                                    }

                                case 0x06: //force use
                                    {
                                        //item.forceUse = true;
                                    } break;

                                case 0x07: //useable
                                    {
                                        item.hasUseWith = true;
                                    } break;

                                case 0x08: //read/write-able
                                    {
                                        item.isReadable = true;
                                        //item.isWriteable = true;
                                        item.maxReadWriteChars = reader.ReadUInt16();
                                    } break;

                                case 0x09: //readable
                                    {
                                        item.isReadable = true;
                                        item.maxReadChars = reader.ReadUInt16();
                                    } break;

                                case 0x0A: //fluid containers
                                    {
                                        item.type = ItemType.Fluid;
                                    } break;

                                case 0x0B: //splashes
                                    {
                                        item.type = ItemType.Splash;
                                    } break;

                                case 0x0C: //blocks solid objects (creatures, walls etc)
                                    {
                                        item.blockObject = true;
                                    } break;

                                case 0x0D: //not moveable
                                    {
                                        item.isMoveable = false;
                                    } break;

                                case 0x0E: //blocks missiles (walls, magic wall etc)
                                    {
                                        item.blockProjectile = true;
                                    } break;

                                case 0x0F: //blocks pathfind algorithms (monsters)
                                    {
                                        item.blockPathFind = true;
                                    } break;

                                case 0x10: //blocks monster movement (flowers, parcels etc)
                                    {
                                        item.isPickupable = true;
                                    } break;

                                case 0x11: //hangable objects (wallpaper etc)
                                    {
                                        item.isHangable = true;
                                    } break;

                                case 0x12: //horizontal wall
                                    {
                                        item.isHorizontal = true;
                                    } break;

                                case 0x13: //vertical wall
                                    {
                                        item.isVertical = true;
                                    } break;

                                case 0x14: //rotatable
                                    {
                                        item.isRotatable = true;
                                    } break;

                                case 0x15: //light info
                                    {
                                        item.lightLevel = reader.ReadUInt16();
                                        item.lightColor = reader.ReadUInt16();
                                    } break;

                                case 0x16: //dont hide
                                    {
                                        //item.dontHide = true;
                                    } break;

                                case 0x17: //translucent
                                    {
                                        //item.translucent = true;
                                    } break;

                                case 0x18: //shift
                                    {
                                        //item.displaced = true;
                                        UInt16 displacementX = reader.ReadUInt16();
                                        //item.displacementX = displacementX;
                                        UInt16 displacementY = reader.ReadUInt16();
                                        //item.displacementY = displacementY;
                                    } break;

                                case 0x19:
                                    {
                                        item.hasHeight = true;
                                        UInt16 height = reader.ReadUInt16();
                                        //item.height = height;
                                    } break;

                                case 0x1A: //lying object
                                    {
                                        //item.lyingObject = true;
                                    } break;

                                case 0x1B: //animate
                                    {
                                        //item.animateWhenIdle = true;
                                    } break;

                                case 0x1C: //minimap color
                                    {
                                        item.minimapColor = reader.ReadUInt16();
                                        break;
                                    }

                                case 0x1D: //in-game help info
                                    {
                                        UInt16 opt = reader.ReadUInt16();
                                        if(opt == 1112)
                                        {
                                            item.isReadable = true;
                                        }
                                    } break;

                                case 0x1E: //full tile
                                    {
                                        //item.fullTile = true;
                                    } break;

                                case 0x1F: //look through (borders)
                                    {
                                        item.lookThrough = true;
                                    } break;

                                case 0x20: //cloth
                                    {
                                        //item.isCloth = true;
                                        UInt16 slot = reader.ReadUInt16();
                                        //item.clothSlot = slot;
                                    } break;

                                case 0xFF: //end of attributes
                                    {
                                    } break;

                                default:
                                    {
                                        Trace.WriteLine(String.Format("Plugin: Error while parsing, unknown optbyte {0:X}", optbyte));
                                        return false;
                                    }
                            }
                        } while (optbyte != 0xFF);

                        item.width = reader.ReadByte();
                        item.height = reader.ReadByte();
                        if ((item.width > 1) || (item.height > 1))
                        {
                            reader.BaseStream.Position++;
                        }

                        item.frames = reader.ReadByte();
                        item.xdiv = reader.ReadByte();
                        item.ydiv = reader.ReadByte();
                        item.zdiv = reader.ReadByte();
                        item.animationLength = reader.ReadByte();
                        item.isAnimation = item.animationLength > 1;

                        item.numSprites =
                            (UInt32)item.width * (UInt32)item.height *
                            (UInt32)item.frames *
                            (UInt32)item.xdiv * (UInt32)item.ydiv * item.zdiv *
                            (UInt32)item.animationLength;

                        // Read the sprite ids
                        for (UInt32 i = 0; i < item.numSprites; ++i)
                        {
                            UInt16 spriteId = reader.ReadUInt16();
                            Sprite sprite;
                            if (!sprites.TryGetValue(spriteId, out sprite))
                            {
                                sprite = new Sprite();
                                sprite.id = spriteId;
                                sprites[spriteId] = sprite;
                            }
                            item.spriteList.Add(sprite);
                        }
                        ++id;
                    }
                }
            }
            finally
            {
                fileStream.Close();
            }

            return true;
        }
        private void showSpriteCandidates(SpriteItem spriteItem)
        {
            tableLayoutPanelCandidates.Visible = true;

            //list with the top 5 results
            List<KeyValuePair<double, OtbItem>> signatureList = new List<KeyValuePair<double, OtbItem>>();

            foreach (OtbItem cmpItem in items)
            {
                if (cmpItem.type == ItemType.Deprecated)
                {
                    continue;
                }

                SpriteItem cmpSpriteItem;
                if(!currentPlugin.Instance.Items.TryGetValue(cmpItem.spriteId, out cmpSpriteItem))
                {
                    continue;
                }

                double similarity = ImageUtils.CompareSignature(spriteItem.spriteSignature, cmpSpriteItem.spriteSignature);

                foreach (KeyValuePair<double, OtbItem> kvp in signatureList)
                {
                    if (similarity < kvp.Key)
                    {
                        //TODO: Use isEqual aswell to match against attributes.
                        signatureList.Remove(kvp);
                        break;
                    }
                }

                if (signatureList.Count < 5)
                {
                    KeyValuePair<double, OtbItem> kvp = new KeyValuePair<double, OtbItem>(similarity, cmpItem);
                    signatureList.Add(kvp);
                }
            }

            signatureList.Sort(
                delegate(KeyValuePair<double, OtbItem> item1, KeyValuePair<double, OtbItem> item2)
                {
                    return item1.Key.CompareTo(item2.Key);
                });

            //those with lowest value are the closest match
            int index = 0;
            foreach (KeyValuePair<double, OtbItem> kvp in signatureList)
            {
                PictureBox box = (PictureBox)tableLayoutPanelCandidates.GetControlFromPosition(index, 0);
                box.Tag = kvp.Value;

                SpriteItem spriteCandidateItem;
                if (currentPlugin.Instance.Items.TryGetValue(kvp.Value.spriteId, out spriteCandidateItem))
                {
                    drawSprite(box, spriteCandidateItem);
                }
                ++index;
            }
        }
        private Bitmap getBitmap(SpriteItem spriteItem)
        {
            int Width = 32;
            int Height = 32;

            if (spriteItem.width > 1 || spriteItem.height > 1)
            {
                Width = 64;
                Height = 64;
            }

            Bitmap canvas = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(canvas);

            //draw sprite
            for (int frame = 0; frame < spriteItem.frames; frame++)
            {
                for (int cy = 0; cy < spriteItem.height; ++cy)
                {
                    for (int cx = 0; cx < spriteItem.width; ++cx)
                    {
                        int frameIndex = cx + cy * spriteItem.width + frame * spriteItem.width * spriteItem.height;
                        Bitmap bmp = ImageUtils.getBitmap(spriteItem.getRGBData(frameIndex), PixelFormat.Format24bppRgb, 32, 32);

                        if (canvas.Width == 32)
                        {
                            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                        }
                        else
                        {
                            g.DrawImage(bmp, new Rectangle(Math.Max(32 - cx * 32, 0), Math.Max(32 - cy * 32, 0), bmp.Width, bmp.Height));
                        }
                    }
                }
            }

            g.Save();
            return canvas;
        }
        private void drawSprite(PictureBox picturBox, SpriteItem spriteItem)
        {
            Bitmap canvas = new Bitmap(64, 64, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(canvas))
            {
                g.FillRectangle(new SolidBrush(Color.FromArgb(0x11, 0x11, 0x11)), 0, 0, canvas.Width, canvas.Height);
                g.Save();
            }

            drawSprite(ref canvas, spriteItem);

            Bitmap newImage = new Bitmap(64, 64, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(newImage))
            {
                g.DrawImage(canvas, new Point((canvas.Width > 32 ? 0 : 32), (canvas.Height > 32 ? 0 : 32)));
                g.Save();
            }

            newImage.MakeTransparent(Color.FromArgb(0x11, 0x11, 0x11));
            picturBox.Image = newImage;
        }
        private void drawSprite(ref Bitmap canvas, SpriteItem spriteItem)
        {
            Graphics g = Graphics.FromImage(canvas);

            //draw sprite
            for (int frame = 0; frame < spriteItem.frames; frame++)
            {
                for (int cy = 0; cy < spriteItem.height; ++cy)
                {
                    for (int cx = 0; cx < spriteItem.width; ++cx)
                    {
                        int frameIndex = cx + cy * spriteItem.width + frame * spriteItem.width * spriteItem.height;
                        Bitmap bmp = ImageUtils.getBitmap(spriteItem.getRGBData(frameIndex), PixelFormat.Format24bppRgb, 32, 32);

                        if (canvas.Width == 32)
                        {
                            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                        }
                        else
                        {
                            g.DrawImage(bmp, new Rectangle(Math.Max(32 - cx * 32, 0), Math.Max(32 - cy * 32, 0), bmp.Width, bmp.Height));
                        }
                    }
                }
            }

            g.Save();
        }
Exemple #6
0
        public bool loadDat(string filename, UInt32 signature)
        {
            FileStream fileStream = new FileStream(filename, FileMode.Open);
            try
            {
                using (BinaryReader reader = new BinaryReader(fileStream))
                {
                    UInt32 datSignature = reader.ReadUInt32();
                    if (signature != 0 && datSignature != signature)
                    {
                        Trace.WriteLine(String.Format("Plugin: Bad signature, dat signature is {0} and signature is {0}", datSignature, signature));
                        return false;
                    }

                    //get max id
                    UInt16 itemCount = reader.ReadUInt16();
                    Trace.WriteLine(String.Format("Plugin: itemCount is {0}", itemCount));
                    UInt16 creatureCount = reader.ReadUInt16();
                    UInt16 effectCount = reader.ReadUInt16();
                    UInt16 distanceCount = reader.ReadUInt16();

                    UInt16 minclientID = 100; //items starts at 100
                    UInt16 maxclientID = itemCount;

                    UInt16 id = minclientID;
                    while (id <= maxclientID)
                    {
                        SpriteItem item = new SpriteItem();
                        item.id = id;
                        items[id] = item;

                        // read the options until we find 0xff
                        byte optbyte;
                        do
                        {
                            optbyte = reader.ReadByte();
                            //Trace.WriteLine(String.Format("{0:X}", optbyte));

                            switch (optbyte)
                            {
                                case 0x00: //groundtile
                                    {
                                        item.groundSpeed = reader.ReadUInt16();
                                        item.type = ItemType.Ground;
                                    } break;

                                case 0x01: //all OnTop
                                    {
                                        item.alwaysOnTop = true;
                                        item.alwaysOnTopOrder = 1;
                                    } break;

                                case 0x02: //can walk trough (open doors, arces, bug pen fence)
                                    {
                                        item.alwaysOnTop = true;
                                        item.alwaysOnTopOrder = 2;
                                    } break;

                                case 0x03: //can walk trough (arces)
                                    {
                                        item.alwaysOnTop = true;
                                        item.alwaysOnTopOrder = 3;
                                    } break;

                                case 0x04: //container
                                    {
                                        item.type = ItemType.Container;
                                    } break;

                                case 0x05: //stackable
                                    {
                                        item.isStackable = true;
                                        break;
                                    }

                                case 0x06: //unknown
                                    {
                                    } break;

                                case 0x07: //useable
                                    {
                                        item.hasUseWith = true;
                                    } break;

                                case 0x08: //read/write-able
                                    {
                                        item.isReadable = true;
                                        //item.isWriteable = true;
                                        item.maxReadWriteChars = reader.ReadUInt16();
                                    } break;

                                case 0x09: //readable
                                    {
                                        item.isReadable = true;
                                        item.maxReadChars = reader.ReadUInt16();
                                    } break;

                                case 0x0A: //fluid containers
                                    {
                                        item.type = ItemType.Fluid;
                                    } break;

                                case 0x0B: //splashes
                                    {
                                        item.type = ItemType.Splash;
                                    } break;

                                case 0x0C: //blocks solid objects (creatures, walls etc)
                                    {
                                        item.blockObject = true;
                                    } break;

                                case 0x0D: //not moveable
                                    {
                                        item.isMoveable = false;
                                    } break;

                                case 0x0E: //blocks missiles (walls, magic wall etc)
                                    {
                                        item.blockProjectile = true;
                                    } break;

                                case 0x0F: //blocks pathfind algorithms (monsters)
                                    {
                                        item.blockPathFind = true;
                                    } break;

                                case 0x10: // no move animation
                                    {
                                        // item.noMoveAnimation = true;
                                    } break;

                                case 0x11: //blocks monster movement (flowers, parcels etc)
                                    {
                                        item.isPickupable = true;
                                    } break;

                                case 0x12: //hangable objects (wallpaper etc)
                                    {
                                        item.isHangable = true;
                                    } break;

                                case 0x13: //horizontal wall
                                    {
                                        item.isHorizontal = true;
                                    } break;

                                case 0x14: //vertical wall
                                    {
                                        item.isVertical = true;
                                    } break;

                                case 0x15: //rotatable
                                    {
                                        item.isRotatable = true;
                                    } break;

                                case 0x16: //light info
                                    {
                                        item.lightLevel = reader.ReadUInt16();
                                        item.lightColor = reader.ReadUInt16();
                                    } break;

                                case 0x17: //unknown
                                    {
                                    } break;

                                case 0x18: //changes floor
                                    {
                                    } break;

                                case 0x19: //unknown
                                    {
                                        reader.BaseStream.Seek(4, SeekOrigin.Current);
                                    } break;

                                case 0x1A:
                                    {
                                        item.hasHeight = true;
                                        UInt16 height = reader.ReadUInt16();
                                    } break;

                                case 0x1B: //unknown
                                    {
                                    } break;

                                case 0x1C: //unknown
                                    {
                                    } break;

                                case 0x1D: //minimap color
                                    {
                                        item.minimapColor = reader.ReadUInt16();
                                        break;
                                    }

                                case 0x1E: //in-game help info
                                    {
                                        UInt16 opt = reader.ReadUInt16();
                                        if (opt == 1112)
                                        {
                                            item.isReadable = true;
                                        }
                                    } break;

                                case 0x1F: //full tile
                                    {
                                        item.walkStack = true;
                                    } break;

                                case 0x20: //look through (borders)
                                    {
                                        item.lookThrough = true;
                                    } break;

                                case 0x21: //unknown
                                    {
                                        reader.ReadUInt16();
                                    } break;

                                case 0x22: //market
                                    {
                                        reader.ReadUInt16(); // category
                                        item.wareId = reader.ReadUInt16(); // trade as
                                        reader.ReadUInt16(); // show as
                                        var size = reader.ReadUInt16();
                                        item.name = new string(reader.ReadChars(size));

                                        reader.ReadUInt16(); // profession
                                        reader.ReadUInt16(); // level
                                    }
                                    break;

                                case 0x23: //Default Action
                                    {
                                        reader.ReadUInt16();
                                    } break;

                                case 0xFE: //Usable
                                    {
                                    } break;

                                case 0xFF: //end of attributes
                                    {
                                    } break;

                                default:
                                    {
                                        Trace.WriteLine(String.Format("Plugin: Error while parsing, unknown optbyte 0x{0:X} at id {1}", optbyte, id));
                                        return true;
                                    }
                            }
                        } while (optbyte != 0xFF);

                        item.width = reader.ReadByte();
                        item.height = reader.ReadByte();
                        if ((item.width > 1) || (item.height > 1))
                        {
                            reader.BaseStream.Position++;
                        }

                        item.frames = reader.ReadByte();
                        item.xdiv = reader.ReadByte();
                        item.ydiv = reader.ReadByte();
                        item.zdiv = reader.ReadByte();
                        item.animationLength = reader.ReadByte();
                        if (item.animationLength > 1)
                        {
                            item.isAnimation = true;
                            reader.ReadByte();
                            reader.ReadInt32();
                            reader.ReadByte();
                            for (int i = 0; i < item.animationLength; i++)
                            {
                                reader.ReadUInt32();
                                reader.ReadUInt32();
                            }
                        }

                        item.numSprites =
                            (UInt32)item.width * (UInt32)item.height *
                            (UInt32)item.frames *
                            (UInt32)item.xdiv * (UInt32)item.ydiv * item.zdiv *
                            (UInt32)item.animationLength;

                        // Read the sprite ids
                        for (UInt32 i = 0; i < item.numSprites; ++i)
                        {
                            var spriteId = reader.ReadUInt32();
                            Sprite sprite;
                            if (!sprites.TryGetValue(spriteId, out sprite))
                            {
                                sprite = new Sprite();
                                sprite.id = spriteId;
                                sprites[spriteId] = sprite;
                            }
                            item.spriteList.Add(sprite);
                        }
                        ++id;
                    }
                }
            }
            finally
            {
                fileStream.Close();
            }
            return true;
        }