protected virtual bool ParseItem(ServerItem item, XElement element) { if (element.Attribute("name") != null) { item.NameXml = element.Attribute("name").Value; } else { Trace.WriteLine($"The item {item.ID} is unnamed."); } return(true); }
public bool Read(string path) { if (!File.Exists(path)) { return false; } try { using (BinaryTreeReader reader = new BinaryTreeReader(path)) { // get root node BinaryReader node = reader.GetRootNode(); if (node == null) { return false; } node.ReadByte(); // first byte of otb is 0 node.ReadUInt32(); // 4 bytes flags, unused byte attr = node.ReadByte(); if ((RootAttribute)attr == RootAttribute.Version) { ushort datalen = node.ReadUInt16(); if (datalen != 140) // 4 + 4 + 4 + 1 * 128 { Trace.WriteLine(String.Format("Size of version header is invalid, updated .otb version?")); return false; } this.Items.MajorVersion = node.ReadUInt32(); // major, file version this.Items.MinorVersion = node.ReadUInt32(); // minor, client version this.Items.BuildNumber = node.ReadUInt32(); // build number, revision node.BaseStream.Seek(128, SeekOrigin.Current); } node = reader.GetChildNode(); if (node == null) { return false; } do { ServerItem item = new ServerItem(); ServerItemGroup itemGroup = (ServerItemGroup)node.ReadByte(); switch (itemGroup) { case ServerItemGroup.None: item.Type = ServerItemType.None; break; case ServerItemGroup.Ground: item.Type = ServerItemType.Ground; break; case ServerItemGroup.Container: item.Type = ServerItemType.Container; break; case ServerItemGroup.Splash: item.Type = ServerItemType.Splash; break; case ServerItemGroup.Fluid: item.Type = ServerItemType.Fluid; break; case ServerItemGroup.Deprecated: item.Type = ServerItemType.Deprecated; break; } ServerItemFlag flags = (ServerItemFlag)node.ReadUInt32(); item.Unpassable = ((flags & ServerItemFlag.Unpassable) == ServerItemFlag.Unpassable); item.BlockMissiles = ((flags & ServerItemFlag.BlockMissiles) == ServerItemFlag.BlockMissiles); item.BlockPathfinder = ((flags & ServerItemFlag.BlockPathfinder) == ServerItemFlag.BlockPathfinder); item.HasElevation = ((flags & ServerItemFlag.HasElevation) == ServerItemFlag.HasElevation); item.ForceUse = ((flags & ServerItemFlag.ForceUse) == ServerItemFlag.ForceUse); item.MultiUse = ((flags & ServerItemFlag.MultiUse) == ServerItemFlag.MultiUse); item.Pickupable = ((flags & ServerItemFlag.Pickupable) == ServerItemFlag.Pickupable); item.Movable = ((flags & ServerItemFlag.Movable) == ServerItemFlag.Movable); item.Stackable = ((flags & ServerItemFlag.Stackable) == ServerItemFlag.Stackable); item.HasStackOrder = ((flags & ServerItemFlag.StackOrder) == ServerItemFlag.StackOrder); item.Readable = ((flags & ServerItemFlag.Readable) == ServerItemFlag.Readable); item.Rotatable = ((flags & ServerItemFlag.Rotatable) == ServerItemFlag.Rotatable); item.Hangable = ((flags & ServerItemFlag.Hangable) == ServerItemFlag.Hangable); item.HookSouth = ((flags & ServerItemFlag.HookSouth) == ServerItemFlag.HookSouth); item.HookEast = ((flags & ServerItemFlag.HookEast) == ServerItemFlag.HookEast); item.AllowDistanceRead = ((flags & ServerItemFlag.AllowDistanceRead) == ServerItemFlag.AllowDistanceRead); item.IgnoreLook = ((flags & ServerItemFlag.IgnoreLook) == ServerItemFlag.IgnoreLook); item.FullGround = ((flags & ServerItemFlag.FullGround) == ServerItemFlag.FullGround); item.IsAnimation = ((flags & ServerItemFlag.IsAnimation) == ServerItemFlag.IsAnimation); while (node.PeekChar() != -1) { ServerItemAttribute attribute = (ServerItemAttribute)node.ReadByte(); ushort datalen = node.ReadUInt16(); switch (attribute) { case ServerItemAttribute.ServerID: item.ID = node.ReadUInt16(); break; case ServerItemAttribute.ClientID: item.ClientId = node.ReadUInt16(); break; case ServerItemAttribute.GroundSpeed: item.GroundSpeed = node.ReadUInt16(); break; case ServerItemAttribute.Name: byte[] buffer = node.ReadBytes(datalen); item.Name = Encoding.UTF8.GetString(buffer, 0, buffer.Length); break; case ServerItemAttribute.SpriteHash: item.SpriteHash = node.ReadBytes(datalen); break; case ServerItemAttribute.MinimaColor: item.MinimapColor = node.ReadUInt16(); break; case ServerItemAttribute.MaxReadWriteChars: item.MaxReadWriteChars = node.ReadUInt16(); break; case ServerItemAttribute.MaxReadChars: item.MaxReadChars = node.ReadUInt16(); break; case ServerItemAttribute.Light: item.LightLevel = node.ReadUInt16(); item.LightColor = node.ReadUInt16(); break; case ServerItemAttribute.StackOrder: item.StackOrder = (TileStackOrder)node.ReadByte(); break; case ServerItemAttribute.TradeAs: item.TradeAs = node.ReadUInt16(); break; default: node.BaseStream.Seek(datalen, SeekOrigin.Current); break; } } if (item.SpriteHash == null && item.Type != ServerItemType.Deprecated) { item.SpriteHash = new byte[16]; } this.Items.Add(item); node = reader.GetNextNode(); } while (node != null); } } catch { return false; } return true; }
private bool EditItem(ServerItem item) { this.CurrentServerItem = null; this.ResetDataBindings(this); this.ResetToolTips(); if (item == null) { return false; } ClientItem clientItem; if (!this.CurrentPlugin.Instance.Items.TryGetValue(item.ClientId, out clientItem)) { return false; } this.pictureBox.ClientItem = clientItem; if (!item.IsCustomCreated && item.SpriteHash != null && clientItem.SpriteHash != null) { this.pictureBox.BackColor = Utils.ByteArrayCompare(item.SpriteHash, clientItem.SpriteHash) ? Color.White : Color.Red; } this.typeCombo.Text = item.Type.ToString(); this.typeCombo.ForeColor = item.Type == clientItem.Type ? Color.Black : Color.Red; this.stackOrderComboBox.Text = item.StackOrder.ToString(); this.stackOrderComboBox.ForeColor = item.StackOrder == clientItem.StackOrder ? Color.Black : Color.Red; this.serverIdLbl.DataBindings.Add("Text", item, "ID"); this.clientIdUpDown.Minimum = this.serverItems.MinId; this.clientIdUpDown.Maximum = (this.CurrentPlugin.Instance.Items.Count + this.serverItems.MinId) - 1; this.clientIdUpDown.DataBindings.Add("Value", clientItem, "ID"); // Attributes this.AddBinding(this.unpassableCheck, "Checked", item, "Unpassable", item.Unpassable, clientItem.Unpassable); this.AddBinding(this.movableCheck, "Checked", item, "Movable", item.Movable, clientItem.Movable); this.AddBinding(this.blockMissilesCheck, "Checked", item, "BlockMissiles", item.BlockMissiles, clientItem.BlockMissiles); this.AddBinding(this.blockPathfinderCheck, "Checked", item, "BlockPathfinder", item.BlockPathfinder, clientItem.BlockPathfinder); this.AddBinding(this.forceUseCheckBox, "Checked", item, "ForceUse", item.ForceUse, clientItem.ForceUse); this.AddBinding(this.useableCheck, "Checked", item, "MultiUse", item.MultiUse, clientItem.MultiUse); this.AddBinding(this.pickupableCheck, "Checked", item, "Pickupable", item.Pickupable, clientItem.Pickupable); this.AddBinding(this.stackableCheck, "Checked", item, "Stackable", item.Stackable, clientItem.Stackable); this.AddBinding(this.readableCheck, "Checked", item, "Readable", item.Readable, clientItem.Readable); this.AddBinding(this.rotatableCheck, "Checked", item, "Rotatable", item.Rotatable, clientItem.Rotatable); this.AddBinding(this.hangableCheck, "Checked", item, "Hangable", item.Hangable, clientItem.Hangable); this.AddBinding(this.hookSouthCheck, "Checked", item, "HookSouth", item.HookSouth, clientItem.HookSouth); this.AddBinding(this.hookEastCheck, "Checked", item, "HookEast", item.HookEast, clientItem.HookEast); this.AddBinding(this.hasElevationCheck, "Checked", item, "HasElevation", item.HasElevation, clientItem.HasElevation); this.AddBinding(this.ignoreLookCheck, "Checked", item, "IgnoreLook", item.IgnoreLook, clientItem.IgnoreLook); this.AddBinding(this.fullGroundCheck, "Checked", item, "FullGround", item.FullGround, clientItem.FullGround); this.AddBinding(this.groundSpeedText, "Text", item, "GroundSpeed", item.GroundSpeed, clientItem.GroundSpeed, true); this.AddBinding(this.lightLevelText, "Text", item, "LightLevel", item.LightLevel, clientItem.LightLevel, true); this.AddBinding(this.lightColorText, "Text", item, "LightColor", item.LightColor, clientItem.LightColor, true); this.AddBinding(this.maxReadCharsText, "Text", item, "MaxReadChars", item.MaxReadChars, clientItem.MaxReadChars, true); this.AddBinding(this.maxReadWriteCharsText, "Text", item, "MaxReadWriteChars", item.MaxReadWriteChars, clientItem.MaxReadWriteChars, true); this.AddBinding(this.minimapColorText, "Text", item, "MinimapColor", item.MinimapColor, clientItem.MinimapColor, true); this.AddBinding(this.wareIdText, "Text", item, "TradeAs", item.TradeAs, clientItem.TradeAs, true); this.AddBinding(this.nameText, "Text", item, "Name", item.Name, clientItem.Name, true); this.candidatesButton.Enabled = false; for (int i = 0; i < this.candidatesTableLayoutPanel.ColumnCount; ++i) { ClientItemView box = (ClientItemView)this.candidatesTableLayoutPanel.GetControlFromPosition(i, 0); box.ClientItem = null; } if (this.PreviousPlugin != null) { ClientItem prevClientItem; if (this.PreviousPlugin.Instance.Items.TryGetValue(item.PreviousClientId, out prevClientItem)) { this.previousPictureBox.ClientItem = prevClientItem; if (prevClientItem.SpriteSignature != null) { // Sprite does not match, use the sprite signature to find possible candidates this.ShowSpriteCandidates(prevClientItem); } } else { this.previousPictureBox.ClientItem = null; } } this.CurrentServerItem = item; return true; }
private void ReloadItem(ServerItem item) { if (!this.Loaded || item == null) { return; } // to avoid problems with events ServerItem tmpItem = this.CurrentServerItem; this.CurrentServerItem = null; ClientItem clientItem; if (this.CurrentPlugin.Instance.Items.TryGetValue(item.ClientId, out clientItem)) { Trace.WriteLine(string.Format("Reloading item id: {0}.", item.ID)); ushort tmpId = item.ID; item.CopyPropertiesFrom(clientItem); item.ID = tmpId; Buffer.BlockCopy(clientItem.SpriteHash, 0, item.SpriteHash, 0, clientItem.SpriteHash.Length); this.CurrentServerItem = tmpItem; } }
private ServerItem CopyItem(ServerItem item) { if (item == null) { return null; } ServerItem copy = new ServerItem(item); copy.SpriteHash = new byte[16]; copy.ClientId = item.ClientId; Buffer.BlockCopy(item.SpriteHash, 0, copy.SpriteHash, 0, copy.SpriteHash.Length); return copy; }
private ServerItem CreateItem(Item item = null) { // create a new otb item ServerItem newItem = new ServerItem(); newItem.ID = (ushort)(this.serverItems.MaxId + 1); if (item != null) { newItem.CopyPropertiesFrom(item); newItem.ClientId = item.ID; newItem.SpriteHash = new byte[16]; Buffer.BlockCopy(item.SpriteHash, 0, newItem.SpriteHash, 0, newItem.SpriteHash.Length); } else { newItem.ClientId = this.serverItems.MinId; newItem.SpriteHash = new byte[16]; newItem.IsCustomCreated = true; } return newItem; }
private bool CompareItem(ServerItem item, bool compareHash) { if (item.Type == ServerItemType.Deprecated) { return true; } ClientItem clientItem; if (this.CurrentPlugin.Instance.Items.TryGetValue(item.ClientId, out clientItem)) { if (compareHash && !Utils.ByteArrayCompare(item.SpriteHash, clientItem.SpriteHash)) { return false; } return item.Equals(clientItem); } return false; }
public bool SelectItem(ServerItem item) { if (!this.Loaded) { return false; } if (this.CurrentServerItem == item) { return true; } int index; if (item == null || (index = this.serverItemListBox.Items.IndexOf(item)) == -1) { this.ResetControls(); return false; } this.EditItem(item); this.editDuplicateItemMenuItem.Enabled = true; this.editReloadItemMenuItem.Enabled = true; this.optionsGroupBox.Enabled = true; this.appearanceGroupBox.Enabled = true; this.serverItemListBox.SelectedIndex = index; return true; }
public bool DuplicateItem(ServerItem item) { if (!this.Loaded || item == null) { return false; } ServerItem copyItem = this.CopyItem(item); copyItem.ID = (ushort)(this.serverItems.MaxId + 1); this.serverItems.Add(copyItem); this.serverItemListBox.Add(copyItem); this.SelectItem(copyItem); this.itemsCountLabel.Text = this.serverItemListBox.Count + " Items"; Trace.WriteLine(string.Format("Duplicated item id {0} to new item id {1}", item.ID, copyItem.ID)); return true; }
public void CreateEmptyOTB(string filePath, SupportedClient client, bool isTemporary = true) { ServerItem item = new ServerItem(); item.SpriteHash = new byte[16]; item.ClientId = 100; item.ID = 100; ServerItemList items = new ServerItemList(); items.MajorVersion = 3; items.MinorVersion = client.OtbVersion; items.BuildNumber = 1; items.ClientVersion = client.Version; items.Add(item); if (!File.Exists(filePath)) { using (File.Create(filePath)) { //// } } OtbWriter writer = new OtbWriter(items); if (writer.Write(filePath)) { this.Open(filePath); this.IsTemporary = isTemporary; this.Saved = !isTemporary; } }
public void Add(ServerItem item) { Items.Add(item); }