public void NBTJsonSerializerTests_DeserializeTest() { ListTag list = new ListTag("list", NBTTagType.INT); list.Add(new IntTag(12345)); list.Add(new IntTag(67890)); CompoundTag subTag = new CompoundTag(); subTag.PutBool("bool", true); subTag.PutByte("byte", 123); CompoundTag tag = new CompoundTag(); tag.PutBool("bool", true); tag.PutByte("byte", 123); tag.PutByteArray("byteArray", ArrayUtils.CreateArray <byte>(200)); tag.PutShort("short", 12345); tag.PutInt("int", 12345678); tag.PutIntArray("intArray", ArrayUtils.CreateArray <int>(200)); tag.PutLong("long", 123456789123456); tag.PutLongArray("longArray", ArrayUtils.CreateArray <long>(200)); tag.PutFloat("float", 12.3456f); tag.PutDouble("double", 12.3456789); tag.PutList(list); tag.PutCompound("com", subTag); JObject json = NBTJsonSerializer.Serialize(tag); Console.WriteLine(NBTJsonSerializer.Serialize(NBTJsonSerializer.Deserialize(json)).ToString()); }
public static CompoundTag ReadZLIBFile(byte[] buffer, ByteOrder order = ByteOrder.Little) { byte[] bytes = buffer; byte[] payload = new byte[0]; using (MemoryStream ms = new MemoryStream(bytes)) { if (ms.ReadByte() != 0x78) { throw new FormatException(); } ms.ReadByte(); using (ZlibStream ds = new ZlibStream(ms, CompressionMode.Decompress, false)) { MemoryStream c = new MemoryStream(); ds.CopyTo(c); payload = c.ToArray(); c.Close(); } } CompoundTag tag = new CompoundTag(); using (NBTStream nbt = new NBTStream(payload, order)) { tag.Read(nbt); return(tag); } }
public void WriteRawFileTest() { ListTag list = new ListTag(Data.NBTTagType.BYTE); list.Name = "listTag"; list.Add(new ByteTag(0xff)); list.Add(new ByteTag(0x00)); list.Add(new ByteTag(0xff)); CompoundTag tag = new CompoundTag(); tag.PutBool("bool", true); tag.PutByte("byte", 0xff); tag.PutShort("short", 0x7fff); tag.PutInt("int", 0x7fffffff); tag.PutLong("long", 0x7fffffffffffffff); tag.PutFloat("float", 0.0001f); tag.PutDouble("double", 0.00000001d); tag.PutString("string", "Hello NBT"); tag.PutByteArray("byte[]", ArrayUtils.CreateArray <byte>(100, 0xff)); tag.PutIntArray("int[]", ArrayUtils.CreateArray <int>(100, 0x7fffffff)); tag.PutLongArray("long[]", ArrayUtils.CreateArray <long>(100, 0x7fffffffffffffff)); tag.PutList(list); NBTIO.WriteRawFile(Path + "\\" + "raw.nbt", tag); }
public override void LoadNBT(CompoundTag nbt) { base.LoadNBT(nbt); this.PlayerCursorInventory.LoadNBT(nbt); this.PlayerEnderChestInventory.LoadNBT(nbt); }
public void NBTIOTests_WriteGZFileTest() { ListTag list = new ListTag("list", NBTTagType.COMPOUND); list.Add(new CompoundTag().PutInt("c1", 123)); list.Add(new CompoundTag().PutLong("c2", 123456)); CompoundTag subTag = new CompoundTag(); subTag.PutBool("bool", true); subTag.PutByte("byte", 123); CompoundTag tag = new CompoundTag(); tag.PutBool("bool", true); tag.PutByte("byte", 123); tag.PutByteArray("byteArray", ArrayUtils.CreateArray <byte>(200)); tag.PutShort("short", 12345); tag.PutInt("int", 12345678); tag.PutIntArray("intArray", ArrayUtils.CreateArray <int>(200)); tag.PutLong("long", 123456789123456); tag.PutLongArray("longArray", ArrayUtils.CreateArray <long>(200)); tag.PutFloat("float", 12.3456f); tag.PutDouble("double", 12.3456789); tag.PutList(list); tag.PutCompound("com", subTag); NBTIO.WriteGZIPFile(Environment.CurrentDirectory + "\\test2.nbt", tag); }
public void NBTIOTests_ReadGZFileTest() { CompoundTag tag = NBTIO.ReadGZIPFile(Environment.CurrentDirectory + "\\test2.nbt"); Console.WriteLine(tag); Console.WriteLine(tag.GetList("list")[0]); }
public void NBTIOTests_Test3() { this.namedTag = new CompoundTag(); this.namedTag.PutList(new ListTag("Attributes", NBTTagType.COMPOUND)); this.namedTag.PutList(new ListTag("Pos", NBTTagType.FLOAT)); this.namedTag.PutList(new ListTag("Rotation", NBTTagType.FLOAT)); this.namedTag.PutInt("PlayerGameMode", 0); this.namedTag.PutInt("PlayerLevel", 0); this.namedTag.PutFloat("PlayerLevelProgress", 0f); if (!this.namedTag.Exist("Inventory")) { ListTag initItems = new ListTag("Inventory", NBTTagType.COMPOUND); for (int i = 0; i < 36; ++i) { initItems.Add(NBTIO.WriteItem(Item.Get(0), i)); } this.namedTag.PutList(initItems); } ListTag items = this.namedTag.GetList("Inventory"); for (int i = 0; i < 36; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); } NBTIO.WriteGZIPFile(Environment.CurrentDirectory + "\\test3.nbt", this.namedTag); Console.WriteLine(NBTIO.ReadGZIPFile(Environment.CurrentDirectory + "\\test3.nbt")); }
protected override void EntityInit(CompoundTag nbt) { base.EntityInit(nbt); this.Attributes.AddAttribute(EntityAttribute.HUNGER); this.Attributes.AddAttribute(EntityAttribute.SATURATION); this.Attributes.AddAttribute(EntityAttribute.EXHAUSTION); this.Attributes.AddAttribute(EntityAttribute.EXPERIENCE); this.Attributes.AddAttribute(EntityAttribute.EXPERIENCE_LEVEL); this.SetFlag(DATA_FLAGS, DATA_FLAG_BREATHING); this.SetFlag(DATA_FLAGS, DATA_FLAG_CAN_CLIMB); this.Inventory = new PlayerInventory(this); this.Inventory.LoadNBT(nbt); this.World = World.GetWorld(nbt.GetString("World")) ?? World.GetMainWorld(); this.SpawnX = nbt.GetInt("SpawnX"); this.SpawnY = nbt.GetInt("SpawnY"); this.SpawnZ = nbt.GetInt("SpawnZ"); this.AdventureSettingsEntry = new AdventureSettingsEntry(); this.GameMode = GameModeExtention.FromIndex(nbt.GetInt("PlayerGameType")); }
public ViewerTab(CompoundTag tag) { InitializeComponent(); NamedTag = (CompoundTag)tag.Clone(); BuildTree(); }
public static void WriteZLIBFile(string fileName, CompoundTag tag, ByteOrder order = ByteOrder.Little) { using (NBTStream stream = new NBTStream(order)) { tag.Write(stream); int sum = 0; using (MemoryStream ms = new MemoryStream()) { ms.WriteByte(0x78); ms.WriteByte(0x01); using (ZlibStream zlib = new ZlibStream(ms, CompressionMode.Compress, true)) { zlib.Write(stream.ToArray(), 0, (int)stream.Length); sum = zlib.Checksum; } byte[] sumBytes = BitConverter.GetBytes(sum); if (BitConverter.IsLittleEndian) { Array.Reverse(sumBytes); } ms.Write(sumBytes, 0, sumBytes.Length); File.WriteAllBytes(fileName, ms.ToArray()); } } }
public Item SetCustomName(string name) { if (name == null || name == "") { this.ClearCustomName(); } else { CompoundTag tag; if (this.HasTags) { tag = this.GetNamedTag(); } else { tag = new CompoundTag(); } if (tag.Exist("display")) { tag.GetCompound("display").PutString("Name", name); } else { tag.PutCompound("display", new CompoundTag("display").PutString("Name", name)); } this.SetNamedTag(tag); } return(this); }
public static CompoundTag ReadZLIBFile(string fileName, NBTEndian endian = NBTEndian.LITTLE_ENDIAN) { byte[] bytes = File.ReadAllBytes(fileName); byte[] payload = new byte[0]; using (MemoryStream ms = new MemoryStream(bytes)) { if (ms.ReadByte() != 0x78) { throw new FormatException(); } ms.ReadByte(); using (ZlibStream ds = new ZlibStream(ms, CompressionMode.Decompress, false)) { MemoryStream c = new MemoryStream(); ds.CopyTo(c); payload = c.ToArray(); c.Close(); } } CompoundTag tag = new CompoundTag(); using (NBTStream nbt = new NBTStream(payload, endian)) { tag.Read(nbt); return(tag); } }
public Item SetLore(params string[] lores) { if (lores == null || lores.Length < 1) { this.ClearLore(); } else { CompoundTag tag; if (this.HasTags) { tag = this.GetNamedTag(); } else { tag = new CompoundTag(); } ListTag list = new ListTag("Lore", NBTTagType.STRING); for (int i = 0; i < lores.Length; ++i) { list.Add(new StringTag(lores[i])); } if (tag.Exist("display")) { tag.GetCompound("display").PutList(list); } else { tag.PutCompound("display", new CompoundTag("display").PutList(list)); } this.SetNamedTag(tag); } return(this); }
public string[] GetLore() { if (!this.HasTags) { return(new string[0]); } CompoundTag tag = this.GetNamedTag(); if (tag.Exist("display")) { return(new string[0]); } CompoundTag display = tag.GetCompound("display"); if (!display.Exist("Lore")) { return(new string[0]); } ListTag lores = display.GetList("Lore"); string[] data = new string[lores.Count]; for (int i = 0; i < lores.Count; ++i) { data[i] = ((StringTag)lores[i]).Data; } return(data); }
/// <summary> /// <see cref="Entity"/> の初期化をします。 /// </summary> /// <param name="nbt"><see cref="Entity"/> のNBTデータ</param> protected virtual void EntityInit(CompoundTag nbt) { ListTag list = nbt.GetList("Pos"); this.X = ((FloatTag)list[0]).Data; this.Y = ((FloatTag)list[1]).Data; this.Z = ((FloatTag)list[2]).Data; list = nbt.GetList("Motion"); this.MotionX = ((FloatTag)list[0]).Data; this.MotionY = ((FloatTag)list[1]).Data; this.MotionZ = ((FloatTag)list[2]).Data; list = nbt.GetList("Rotation"); this.Yaw = ((FloatTag)list[0]).Data; this.Pitch = ((FloatTag)list[1]).Data; this.ResetLastMovements(); this.RecalculateBoundingBox(); this.DataProperties = new EntityMetadataManager(this.EntityID); this.SetDataProperty(new EntityDataLong(DATA_FLAGS, 0)); this.SetDataProperty(new EntityDataShort(DATA_AIR, 400)); this.SetDataProperty(new EntityDataShort(DATA_MAX_AIR, 400)); this.SetDataProperty(new EntityDataString(DATA_NAMETAG, "")); this.SetDataProperty(new EntityDataLong(DATA_LEAD_HOLDER_EID, -1)); this.SetDataProperty(new EntityDataFloat(DATA_SCALE, 1.0f)); this.SetDataProperty(new EntityDataFloat(DATA_BOUNDING_BOX_WIDTH, this.Width)); this.SetDataProperty(new EntityDataFloat(DATA_BOUNDING_BOX_HEIGHT, this.Height)); this.SetFlag(DATA_FLAGS, DATA_FLAG_HAS_COLLISION); this.SetFlag(DATA_FLAGS, DATA_FLAG_GRAVITY); this.Attributes = new EntityAttributeDictionary(this.EntityID); }
public Item SetNamedTag(CompoundTag nbt) { nbt.Name = ""; this.cachedNBT = nbt; this.tags = NBTIO.WriteTag(nbt); return(this); }
public ChunkData(Tuple <int, int> regionPosition, Tuple <int, int> chunkOffset, CompoundTag data) { RegionPosition = regionPosition; ChunkOffset = chunkOffset; Data = data; }
public NBTViewer(CompoundTag tag) { this.InitializeComponent(); this.SetupLangage(); this.dataGridView1.AutoGenerateColumns = true; this._Load(tag); }
public Chunk NBTDeserialize(CompoundTag tag) { CompoundTag level = (CompoundTag)tag["Level"]; int x = level.GetInt("xPos"); int z = level.GetInt("zPos"); SubChunk[] subChunks = ArrayUtils.CreateArray <SubChunk>(16); ListTag sections = level.GetList("Sections"); for (int i = 0; i < sections.Count; ++i) { CompoundTag section = ((CompoundTag)sections[i]); SubChunk subChunk = new SubChunk(); byte y = section.GetByte("Y"); subChunk.BlockDatas = section.GetByteArray("Blocks"); subChunk.MetaDatas = new NibbleArray(section.GetByteArray("Data")); subChunk.SkyLights = new NibbleArray(section.GetByteArray("SkyLight")); subChunk.BlockLigths = new NibbleArray(section.GetByteArray("BlockLight")); subChunks[y] = subChunk; } byte[] biomes = level.GetByteArray("Biomes"); short[] cast = new short[256]; int[] heightMap = level.GetIntArray("HeightMap"); heightMap.CopyTo(cast, 0); Chunk chunk = new Chunk(x, z, subChunks, biomes, cast, level.GetList("Entities"), level.GetList("TileEntities")); chunk.LastUpdate = level.GetLong("LastUpdate"); chunk.InhabitedTime = level.GetLong("InhabitedTime"); chunk.LightPopulated = level.GetByte("LightPopulated") == 1; chunk.TerrainPopulated = level.GetByte("TerrainPopulated") == 1; return(chunk); }
public static byte[] WriteZLIBFile(CompoundTag tag, NBTEndian endian = NBTEndian.LITTLE_ENDIAN) { using (NBTStream stream = new NBTStream(endian)) { tag.Write(stream); int sum = 0; using (MemoryStream ms = new MemoryStream()) { ms.WriteByte(0x78); ms.WriteByte(0x01); using (ZlibStream zlib = new ZlibStream(ms, CompressionMode.Compress, true)) { zlib.Write(stream.ToArray(), 0, (int)stream.Length); sum = zlib.Checksum; } byte[] sumBytes = BitConverter.GetBytes(sum); if (BitConverter.IsLittleEndian) { Array.Reverse(sumBytes); } ms.Write(sumBytes, 0, sumBytes.Length); return(ms.ToArray()); } } }
protected override void Init(CompoundTag nbt) { this.Inventory = new ChestInventory(this); this.Inventory.LoadNBT(nbt); base.Init(nbt); }
private void gZipGToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.AddExtension = true; dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Multiselect = false; dialog.RestoreDirectory = true; dialog.Title = LangManager.GetString("nbtViewer_selectNBTFormatFile"); DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { this.cacheData.NBTViewerCache.Rows.Clear(); try { CompoundTag tag = NBTIO.ReadGZIPFile(dialog.FileName, this.Endian); this._Load(tag); } catch (Exception exc) { MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public static Item ReadItem(CompoundTag nbt) { Item item = Item.Get(nbt.GetShort("id"), nbt.GetShort("damage"), nbt.GetByte("count")); if (nbt.Exist("tag")) { CompoundTag tag = (CompoundTag)nbt.GetCompound("tag").Clone(); tag.Name = ""; item.SetNamedTag(tag); } if (nbt.Exist("CanPlaceOn")) { ListTag list = nbt.GetList("CanPlaceOn"); for (int i = 0; i < list.Count; ++i) { item.AddCanPlaceOn(((StringTag)list[i]).Data); } } if (nbt.Exist("CanDestroy")) { ListTag list = nbt.GetList("CanDestroy"); for (int i = 0; i < list.Count; ++i) { item.AddCanDestroy(((StringTag)list[i]).Data); } } return(item); }
public Item AddEnchantment(Enchantment enchantment) { CompoundTag tag = this.NamedTag; ListTag list; if (tag.Exist("ench")) { list = tag.GetList("ench"); } else { list = new ListTag("ench", NBTTagType.COMPOUND); tag.PutList(list); } for (int i = 0; i < list.Count; ++i) { if (((CompoundTag)list[i]).GetShort("id") == enchantment.ID) { list[i] = new CompoundTag() .PutShort("id", (short)enchantment.ID) .PutShort("lvl", (short)enchantment.Level); this.NamedTag = tag; return(this); } } CompoundTag ench = new CompoundTag() .PutShort("id", (short)enchantment.ID) .PutShort("lvl", (short)enchantment.Level); list.Add(ench); this.NamedTag = tag; return(this); }
public Entity(World world, CompoundTag tag) { this.EntityID = ++Entity.nextEntityId; this.SetDataProperty(new EntityDataLong(Entity.DATA_FLAGS, 0)); this.SetDataProperty(new EntityDataShort(Entity.DATA_AIR, 400)); this.SetDataProperty(new EntityDataShort(Entity.DATA_MAX_AIR, 400)); this.SetDataProperty(new EntityDataString(Entity.DATA_NAMETAG, "")); this.SetDataProperty(new EntityDataLong(Entity.DATA_LEAD_HOLDER_EID, -1)); this.SetDataProperty(new EntityDataFloat(Entity.DATA_SCALE, 1.0f)); this.SetDataProperty(new EntityDataFloat(Entity.DATA_BOUNDING_BOX_WIDTH, this.WIDTH)); this.SetDataProperty(new EntityDataFloat(Entity.DATA_BOUNDING_BOX_HEIGHT, this.HEIGHT)); this.SetFlag(Entity.DATA_FLAGS, Entity.DATA_FLAG_HAS_COLLISION); this.SetFlag(Entity.DATA_FLAGS, Entity.DATA_FLAG_AFFECTED_BY_GRAVITY); if (!this.IsPlayer) { ListTag pos = tag.GetList("Pos"); this.Vector3 = new Vector3( pos.GetTag <FloatTag>(0).Data, pos.GetTag <FloatTag>(1).Data, pos.GetTag <FloatTag>(2).Data); ListTag rotation = tag.GetList("Rotation"); this.Yaw = pos.GetTag <FloatTag>(0).Data; this.Pitch = pos.GetTag <FloatTag>(1).Data; this.World = world; this.World.AddEntity(this); } this.EntityInit(); }
public ChunkData Read(int x, int z) { const int width = 32; const int depth = 32; using (FileStream regionFile = File.OpenRead(RegionPath)) { byte[] buffer = new byte[8192]; regionFile.Read(buffer, 0, 8192); int xi = (x % width); if (xi < 0) { xi += 32; } int zi = (z % depth); if (zi < 0) { zi += 32; } int tableOffset = (xi + zi * width) * 4; regionFile.Seek(tableOffset, SeekOrigin.Begin); byte[] offsetBuffer = new byte[4]; regionFile.Read(offsetBuffer, 0, 3); Array.Reverse(offsetBuffer); int offset = BitConverter.ToInt32(offsetBuffer, 0) << 4; byte[] bytes = BitConverter.GetBytes(offset >> 4); Array.Reverse(bytes); if (offset != 0 && offsetBuffer[0] != bytes[0] && offsetBuffer[1] != bytes[1] && offsetBuffer[2] != bytes[2]) { throw new FormatException(); } int length = regionFile.ReadByte(); if (offset == 0 || length == 0) { return(null); } regionFile.Seek(offset, SeekOrigin.Begin); byte[] waste = new byte[4]; regionFile.Read(waste, 0, 4); int compressionMode = regionFile.ReadByte(); if (compressionMode != 0x02) { throw new FormatException(); } CompoundTag tag = NBTIO.ReadZLIBFile(new BinaryReader(regionFile).ReadBytes((int)(regionFile.Length - regionFile.Position)), NBTEndian.BIG_ENDIAN); return(new ChunkData(RegionPosition, new Tuple <int, int>(x, z), tag)); } }
private void LoadTag(ListTag tags) { foreach (Tag tag in tags.Tags) { if (tag is EndTag) { this.AddEndTag(); } else if (tag is ByteTag) { ByteTag t = (ByteTag)tag; this.AddTag(t.Data, t.TagType); } else if (tag is ShortTag) { ShortTag t = (ShortTag)tag; this.AddTag(t.Data, t.TagType); } else if (tag is IntTag) { IntTag t = (IntTag)tag; this.AddTag(t.Data, t.TagType); } else if (tag is LongTag) { LongTag t = (LongTag)tag; this.AddTag(t.Data, t.TagType); } else if (tag is FloatTag) { FloatTag t = (FloatTag)tag; this.AddTag(t.Data, t.TagType); } else if (tag is DoubleTag) { DoubleTag t = (DoubleTag)tag; this.AddTag(t.Data, t.TagType); } //TODO: ByteArrayTag... else if (tag is StringTag) { StringTag t = (StringTag)tag; this.AddTag(t.Data, t.TagType); } else if (tag is ListTag) { ListTag t = (ListTag)tag; this.AddListInListTagHeader(t); this.LoadTag(t); } else if (tag is CompoundTag) { CompoundTag t = (CompoundTag)tag; this.LoadTag(t, false, true); } //TODO: IntArrayTag... //TDDO: LongArrayTag... } }
public Item SetUnbreakable(bool value) { CompoundTag tag = this.NamedTag; tag.PutBool("Unbreakable", value); this.NamedTag = tag; return(this); }
public static void WriteRawFile(string fileName, CompoundTag tag, NBTEndian endian = NBTEndian.LITTLE_ENDIAN) { using (NBTStream stream = new NBTStream(endian)) { tag.Write(stream); File.WriteAllBytes(fileName, stream.ToArray()); } }
public void Create(World world) { CompoundTag tag = new CompoundTag(""); tag.PutCompound("Data", this.CreateData(world)); NBTIO.WriteGZIPFile($"{Server.ExecutePath}\\worlds\\{world.Name}\\level.dat", tag); }