public bool LoadDat(string filename, SupportedClient client, bool extended) { FileStream fileStream = new FileStream(filename, FileMode.Open); try { using (BinaryReader reader = new BinaryReader(fileStream)) { UInt32 datSignature = reader.ReadUInt32(); if (client.DatSignature != datSignature) { string message = "PluginOne: Bad dat signature. Expected signature is {0:X} and loaded signature is {1:X}."; Trace.WriteLine(String.Format(message, client.DatSignature, datSignature)); 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) { ClientItem item = new ClientItem(); item.id = id; items[id] = item; // read the options until we find 0xff ItemFlag flag; do { flag = (ItemFlag)reader.ReadByte(); switch (flag) { case ItemFlag.Ground: item.type = ItemType.Ground; item.groundSpeed = reader.ReadUInt16(); break; case ItemFlag.GroundBorder: item.alwaysOnTop = true; item.alwaysOnTopOrder = 1; break; case ItemFlag.OnBottom: item.alwaysOnTop = true; item.alwaysOnTopOrder = 2; break; case ItemFlag.OnTop: item.alwaysOnTop = true; item.alwaysOnTopOrder = 3; break; case ItemFlag.Container: item.type = ItemType.Container; break; case ItemFlag.Stackable: item.isStackable = true; break; case ItemFlag.ForceUse: break; case ItemFlag.MultiUse: item.multiUse = true; break; case ItemFlag.HasCharges: break; case ItemFlag.Writable: item.isReadable = true; item.maxReadWriteChars = reader.ReadUInt16(); break; case ItemFlag.WritableOnce: item.isReadable = true; item.maxReadChars = reader.ReadUInt16(); break; case ItemFlag.FluidContainer: item.type = ItemType.Fluid; break; case ItemFlag.Fluid: item.type = ItemType.Splash; break; case ItemFlag.IsUnpassable: item.isUnpassable = true; break; case ItemFlag.IsUnmoveable: item.isMoveable = false; break; case ItemFlag.BlockMissiles: item.blockMissiles = true; break; case ItemFlag.BlockPathfinder: item.blockPathfinder = true; break; case ItemFlag.Pickupable: item.isPickupable = true; break; case ItemFlag.Hangable: item.isHangable = true; break; case ItemFlag.IsHorizontal: item.isHorizontal = true; break; case ItemFlag.IsVertical: item.isVertical = true; break; case ItemFlag.Rotatable: item.isRotatable = true; break; case ItemFlag.HasLight: item.lightLevel = reader.ReadUInt16(); item.lightColor = reader.ReadUInt16(); break; case ItemFlag.DontHide: break; case ItemFlag.FloorChange: break; case ItemFlag.HasOffset: reader.ReadUInt16(); // OffsetX reader.ReadUInt16(); // OffsetY break; case ItemFlag.HasElevation: item.hasElevation = true; reader.ReadUInt16(); // Height break; case ItemFlag.Lying: break; case ItemFlag.AnimateAlways: break; case ItemFlag.Minimap: item.minimapColor = reader.ReadUInt16(); break; case ItemFlag.LensHelp: UInt16 opt = reader.ReadUInt16(); if (opt == 1112) { item.isReadable = true; } break; case ItemFlag.FullGround: item.fullGround = true; break; case ItemFlag.IgnoreLook: item.ignoreLook = true; break; case ItemFlag.LastFlag: break; default: Trace.WriteLine(String.Format("PluginOne: Error while parsing, unknown flag 0x{0:X} at id {1}.", flag, id)); return false; } } while (flag != ItemFlag.LastFlag); item.width = reader.ReadByte(); item.height = reader.ReadByte(); if ((item.width > 1) || (item.height > 1)) { reader.BaseStream.Position++; } item.layers = reader.ReadByte(); item.patternX = reader.ReadByte(); item.patternY = reader.ReadByte(); item.patternZ = reader.ReadByte(); item.frames = reader.ReadByte(); item.isAnimation = item.frames > 1; item.numSprites = (UInt32)item.width * item.height * item.layers * item.patternX * item.patternY * item.patternZ * item.frames; // Read the sprite ids for (UInt32 i = 0; i < item.numSprites; ++i) { uint spriteId; if (extended) { spriteId = reader.ReadUInt32(); } else { 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(ClientItem clientItem) { this.candidatesButton.Enabled = true; // list with the top 5 results List <KeyValuePair <double, ServerItem> > signatureList = new List <KeyValuePair <double, ServerItem> >(); foreach (ServerItem cmpItem in this.serverItems) { if (cmpItem.Type == ServerItemType.Deprecated) { continue; } ClientItem cmpClientItem; if (!this.CurrentPlugin.Instance.Items.TryGetValue(cmpItem.ClientId, out cmpClientItem)) { continue; } double similarity = ImageUtils.CompareSignature(clientItem.SpriteSignature, cmpClientItem.SpriteSignature); foreach (KeyValuePair <double, ServerItem> 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, ServerItem> kvp = new KeyValuePair <double, ServerItem>(similarity, cmpItem); signatureList.Add(kvp); } } signatureList.Sort( delegate(KeyValuePair <double, ServerItem> item1, KeyValuePair <double, ServerItem> item2) { return(item1.Key.CompareTo(item2.Key)); }); // those with lowest value are the closest match int index = 0; foreach (KeyValuePair <double, ServerItem> kvp in signatureList) { ClientItemView box = (ClientItemView)candidatesTableLayoutPanel.GetControlFromPosition(index, 0); toolTip.SetToolTip(box, kvp.Value.ClientId.ToString()); box.Tag = kvp.Value; ClientItem candidateItem; if (this.CurrentPlugin.Instance.Items.TryGetValue(kvp.Value.ClientId, out candidateItem)) { box.ClientItem = candidateItem; } ++index; } }
private void ShowSpriteCandidates(ClientItem clientItem) { this.candidatesButton.Enabled = true; // list with the top 5 results List<KeyValuePair<double, ServerItem>> signatureList = new List<KeyValuePair<double, ServerItem>>(); foreach (ServerItem cmpItem in this.serverItems) { if (cmpItem.Type == ServerItemType.Deprecated) { continue; } ClientItem cmpClientItem; if (!this.CurrentPlugin.Instance.Items.TryGetValue(cmpItem.ClientId, out cmpClientItem)) { continue; } double similarity = ImageUtils.CompareSignature(clientItem.SpriteSignature, cmpClientItem.SpriteSignature); foreach (KeyValuePair<double, ServerItem> 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, ServerItem> kvp = new KeyValuePair<double, ServerItem>(similarity, cmpItem); signatureList.Add(kvp); } } signatureList.Sort( delegate(KeyValuePair<double, ServerItem> item1, KeyValuePair<double, ServerItem> item2) { return item1.Key.CompareTo(item2.Key); }); // those with lowest value are the closest match int index = 0; foreach (KeyValuePair<double, ServerItem> kvp in signatureList) { ClientItemView box = (ClientItemView)candidatesTableLayoutPanel.GetControlFromPosition(index, 0); toolTip.SetToolTip(box, kvp.Value.ClientId.ToString()); box.Tag = kvp.Value; ClientItem candidateItem; if (this.CurrentPlugin.Instance.Items.TryGetValue(kvp.Value.ClientId, out candidateItem)) { box.ClientItem = candidateItem; } ++index; } }
public bool LoadDat(string filename, SupportedClient client, bool extended) { using (FileStream fileStream = new FileStream(filename, FileMode.Open)) { BinaryReader reader = new BinaryReader(fileStream); uint datSignature = reader.ReadUInt32(); if (client.DatSignature != datSignature) { string message = "PluginTwo: Bad dat signature. Expected signature is {0:X} and loaded signature is {1:X}."; Trace.WriteLine(String.Format(message, client.DatSignature, datSignature)); return false; } // get max id this.itemCount = reader.ReadUInt16(); reader.ReadUInt16(); // skipping outfits count reader.ReadUInt16(); // skipping effects count reader.ReadUInt16(); // skipping missiles count ushort id = 100; while (id <= this.itemCount) { ClientItem item = new ClientItem(); item.ID = id; this.Items[id] = item; // read the options until we find 0xff ItemFlag flag; do { flag = (ItemFlag)reader.ReadByte(); switch (flag) { case ItemFlag.Ground: item.Type = ServerItemType.Ground; item.GroundSpeed = reader.ReadUInt16(); break; case ItemFlag.GroundBorder: item.HasStackOrder = true; item.StackOrder = TileStackOrder.Border; break; case ItemFlag.OnBottom: item.HasStackOrder = true; item.StackOrder = TileStackOrder.Bottom; break; case ItemFlag.OnTop: item.HasStackOrder = true; item.StackOrder = TileStackOrder.Top; break; case ItemFlag.Container: item.Type = ServerItemType.Container; break; case ItemFlag.Stackable: item.Stackable = true; break; case ItemFlag.ForceUse: break; case ItemFlag.MultiUse: item.MultiUse = true; break; case ItemFlag.Writable: item.Readable = true; item.MaxReadWriteChars = reader.ReadUInt16(); break; case ItemFlag.WritableOnce: item.Readable = true; item.MaxReadChars = reader.ReadUInt16(); break; case ItemFlag.FluidContainer: item.Type = ServerItemType.Fluid; break; case ItemFlag.Fluid: item.Type = ServerItemType.Splash; break; case ItemFlag.IsUnpassable: item.Unpassable = true; break; case ItemFlag.IsUnmoveable: item.Movable = false; break; case ItemFlag.BlockMissiles: item.BlockMissiles = true; break; case ItemFlag.BlockPathfinder: item.BlockPathfinder = true; break; case ItemFlag.Pickupable: item.Pickupable = true; break; case ItemFlag.Hangable: item.Hangable = true; break; case ItemFlag.IsHorizontal: item.HookEast = true; break; case ItemFlag.IsVertical: item.HookSouth = true; break; case ItemFlag.Rotatable: item.Rotatable = true; break; case ItemFlag.HasLight: item.LightLevel = reader.ReadUInt16(); item.LightColor = reader.ReadUInt16(); break; case ItemFlag.DontHide: break; case ItemFlag.Translucent: break; case ItemFlag.HasOffset: reader.ReadUInt16(); // OffsetX reader.ReadUInt16(); // OffsetY break; case ItemFlag.HasElevation: item.HasElevation = true; reader.ReadUInt16(); // Height break; case ItemFlag.Lying: break; case ItemFlag.AnimateAlways: break; case ItemFlag.Minimap: item.MinimapColor = reader.ReadUInt16(); break; case ItemFlag.LensHelp: ushort opt = reader.ReadUInt16(); if (opt == 1112) { item.Readable = true; } break; case ItemFlag.FullGround: item.FullGround = true; break; case ItemFlag.IgnoreLook: item.IgnoreLook = true; break; case ItemFlag.Cloth: reader.ReadUInt16(); break; case ItemFlag.Market: reader.ReadUInt16(); // category item.TradeAs = reader.ReadUInt16(); // trade as reader.ReadUInt16(); // show as ushort nameLength = reader.ReadUInt16(); byte[] buffer = reader.ReadBytes(nameLength); item.Name = Encoding.UTF8.GetString(buffer, 0, buffer.Length); reader.ReadUInt16(); // profession reader.ReadUInt16(); // level break; case ItemFlag.LastFlag: break; default: Trace.WriteLine(String.Format("PluginTwo: Error while parsing, unknown flag 0x{0:X} at id {1}.", flag, id)); return false; } } while (flag != ItemFlag.LastFlag); item.Width = reader.ReadByte(); item.Height = reader.ReadByte(); if ((item.Width > 1) || (item.Height > 1)) { reader.BaseStream.Position++; } item.Layers = reader.ReadByte(); item.PatternX = reader.ReadByte(); item.PatternY = reader.ReadByte(); item.PatternZ = reader.ReadByte(); item.Frames = reader.ReadByte(); item.IsAnimation = item.Frames > 1; item.NumSprites = (uint)item.Width * item.Height * item.Layers * item.PatternX * item.PatternY * item.PatternZ * item.Frames; // Read the sprite ids for (uint i = 0; i < item.NumSprites; ++i) { uint spriteId; if (extended) { spriteId = reader.ReadUInt32(); } else { 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; } } return true; }
private void DrawSprite(PictureBox picturBox, ClientItem clientItem) { 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, clientItem); Bitmap newImage = new Bitmap(64, 64, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(newImage)) { g.DrawImage(canvas, new Point((canvas.Width > spritePixels ? 0 : spritePixels), (canvas.Height > spritePixels ? 0 : spritePixels))); g.Save(); } newImage.MakeTransparent(Color.FromArgb(0x11, 0x11, 0x11)); picturBox.Image = newImage; }
private void DrawSprite(ref Bitmap canvas, ClientItem clientItem) { Graphics g = Graphics.FromImage(canvas); Rectangle rect = new Rectangle(); //draw sprite for (int l = 0; l < clientItem.layers; l++) { for (int h = 0; h < clientItem.height; ++h) { for (int w = 0; w < clientItem.width; ++w) { int frameIndex = w + h * clientItem.width + l * clientItem.width * clientItem.height; Bitmap bmp = ImageUtils.getBitmap(clientItem.GetRGBData(frameIndex), PixelFormat.Format24bppRgb, spritePixels, spritePixels); if (canvas.Width == spritePixels) { rect.X = 0; rect.Y = 0; rect.Width = bmp.Width; rect.Height = bmp.Height; } else { rect.X = Math.Max(spritePixels - w * spritePixels, 0); rect.Y = Math.Max(spritePixels - h * spritePixels, 0); rect.Width = bmp.Width; rect.Height = bmp.Height; } g.DrawImage(bmp, rect); } } } g.Save(); }
private Bitmap GetSpriteBitmap(ClientItem clientItem) { int Width = spritePixels; int Height = spritePixels; if (clientItem.width > 1 || clientItem.height > 1) { Width = spritePixels * 2; Height = spritePixels * 2; } Bitmap canvas = new Bitmap(Width, Height, 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, clientItem); Bitmap newImage = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(newImage)) { g.DrawImage(canvas, new Point(0, 0)); g.Save(); } newImage.MakeTransparent(Color.FromArgb(0x11, 0x11, 0x11)); return newImage; }