public void AddingAndRemoving() { var test = new NbtCompound(); var foo = new NbtInt("Foo"); test.Add(foo); // adding duplicate object Assert.Throws <ArgumentException>(() => test.Add(foo)); // adding duplicate name Assert.Throws <ArgumentException>(() => test.Add(new NbtByte("Foo"))); // adding unnamed tag Assert.Throws <ArgumentException>(() => test.Add(new NbtInt())); // adding null Assert.Throws <ArgumentNullException>(() => test.Add(null)); // contains existing name Assert.IsTrue(test.Contains("Foo")); // contains existing object Assert.IsTrue(test.Contains(foo)); // contains non-existent name Assert.IsFalse(test.Contains("Bar")); // contains existing name / different object Assert.IsFalse(test.Contains(new NbtInt("Foo"))); // removing non-existent name Assert.IsFalse(test.Remove("Bar")); // removing existing name Assert.IsTrue(test.Remove("Foo")); // removing non-existent name Assert.IsFalse(test.Remove("Foo")); // re-adding object test.Add(foo); // removing existing object Assert.IsTrue(test.Remove(foo)); // clearing an empty NbtCompound Assert.AreEqual(test.Count, 0); test.Clear(); // re-adding after clearing test.Add(foo); Assert.AreEqual(test.Count, 1); // clearing a non-empty NbtCompound test.Clear(); Assert.AreEqual(test.Count, 0); }
public void AddingAndRemoving() { NbtCompound test = new NbtCompound(); NbtInt foo = new NbtInt( "Foo" ); test.Add( foo ); // adding duplicate object Assert.Throws<ArgumentException>( () => test.Add( foo ) ); // adding duplicate name Assert.Throws<ArgumentException>( () => test.Add( new NbtByte( "Foo" ) ) ); // adding unnamed tag Assert.Throws<ArgumentException>( () => test.Add( new NbtInt() ) ); // adding null Assert.Throws<ArgumentNullException>( () => test.Add( null ) ); // contains existing name Assert.IsTrue( test.Contains( "Foo" ) ); // contains existing object Assert.IsTrue( test.Contains( foo ) ); // contains non-existent name Assert.IsFalse( test.Contains( "Bar" ) ); // contains existing name / different object Assert.IsFalse( test.Contains( new NbtInt( "Foo" ) ) ); // removing non-existent name Assert.IsFalse( test.Remove( "Bar" ) ); // removing existing name Assert.IsTrue( test.Remove( "Foo" ) ); // removing non-existent name Assert.IsFalse( test.Remove( "Foo" ) ); // re-adding object test.Add( foo ); // removing existing object Assert.IsTrue( test.Remove( foo ) ); // clearing an empty NbtCompound Assert.AreEqual( test.Count, 0 ); test.Clear(); // re-adding after clearing test.Add( foo ); Assert.AreEqual( test.Count, 1 ); // clearing a non-empty NbtCompound test.Clear(); Assert.AreEqual( test.Count, 0 ); }
public NbtCompound Read(NbtCompound Metadata) { var HCData = Metadata.Get <NbtCompound>("Hypercube"); if (HCData != null) { try { BuildRanks = HCData["BuildRanks"].StringValue; ShowRanks = HCData["ShowRanks"].StringValue; JoinRanks = HCData["JoinRanks"].StringValue; Physics = Convert.ToBoolean(HCData["Physics"].ByteValue); Building = Convert.ToBoolean(HCData["Building"].ByteValue); History = HCData["History"].IntArrayValue; if (HCData["MOTD"] != null) { MOTD = HCData["MOTD"].StringValue; } } catch { } Metadata.Remove(HCData); } return(Metadata); }
public void CheckSynchronized() { var root = new NbtCompound("test"); var view = new NbtTreeView(); var model = new NbtTreeModel((object)root); view.Model = model; Assert.AreEqual(model.Root.Children.Count(), 1); Assert.AreEqual(view.Root.Children.Count, 1); AssertSynchronized(view, model); root.Add(new NbtByte("test1")); AssertSynchronized(view, model); root.Add(new NbtByte("test2")); AssertSynchronized(view, model); root.Add(new NbtCompound("test3")); AssertSynchronized(view, model); root.Get <NbtCompound>("test3").Add(new NbtShort("test4")); Assert.AreEqual(view.Root.DescendantsCount, 5); Assert.AreEqual(model.Root.DescendantsCount, 5); AssertSynchronized(view, model); root.Remove("test2"); AssertSynchronized(view, model); root.Get <NbtCompound>("test3").Clear(); Assert.AreEqual(view.Root.DescendantsCount, 3); Assert.AreEqual(model.Root.DescendantsCount, 3); AssertSynchronized(view, model); root.Clear(); AssertSynchronized(view, model); }
/// <summary> /// 序列化Schmeatic序列 /// </summary> /// <param name="commandLine"></param> /// <param name="SettingParam"></param> /// <returns></returns> public NbtCompound Serialize(CommandLine commandLine, ExportSetting SettingParam) { try { var Schematic = new NbtCompound("Schematic"); //CommandLine -> BlockInfo var blockInfo = CommandLine2SchematicInfo(commandLine, SettingParam, (SettingParam.Type == ExportSetting.ExportType.WorldEdit_113) ? "1.13" : ""); //BlockInfo -> Schematic Schematic.Add(blockInfo.Height); Schematic.Add(blockInfo.Length); Schematic.Add(blockInfo.Width); Schematic.Add(new NbtList("Entities", NbtTagType.Compound)); Schematic.Add(blockInfo.Data); Schematic.Add(blockInfo.Blocks); Schematic.Add(blockInfo.TileEntities); if (SettingParam.Type == ExportSetting.ExportType.WorldEdit) { var weInfo = BlockInfo2WorldEditBlockInfo(blockInfo, SettingParam.Direction); Schematic.Remove("Entities"); Schematic.Add(weInfo.Materials); Schematic.Add(weInfo.WEOriginX); Schematic.Add(weInfo.WEOriginY); Schematic.Add(weInfo.WEOriginZ); Schematic.Add(weInfo.WEOffsetX); Schematic.Add(weInfo.WEOffsetY); Schematic.Add(weInfo.WEOffsetZ); } else if (SettingParam.Type == ExportSetting.ExportType.WorldEdit_113) //1.13 { var schem = BlockInfo2Schema113Info(blockInfo, SettingParam.Direction); Schematic.Remove(blockInfo.Data); Schematic.Remove(blockInfo.Blocks); Schematic.Remove("Entities"); Schematic.Add(schem.Metadata); Schematic.Add(schem.Palette); Schematic.Add(schem.PaletteMax); Schematic.Add(schem.Version); Schematic.Add(schem.BlockData); Schematic.Add(schem.Offset); } return(Schematic); } catch { return(null); } }
public NbtCompound Read(NbtCompound metadata) { Tags = new NbtTag[metadata.Tags.Count()]; metadata.CopyTo(Tags, 0); foreach (NbtTag b in Tags) metadata.Remove(b); return metadata; }
public NbtCompound Read(NbtCompound Metadata) { Tags = new NbtTag[Metadata.Tags.Count()]; Metadata.CopyTo(Tags, 0); foreach (NbtTag b in Tags) { Metadata.Remove(b); } return(Metadata); }
public NbtCompound Read(NbtCompound metadata) { var Data = metadata.Get<NbtCompound>("MCForge"); Logger.Log(Data["perbuild"].ToString()); if (Data != null) { perbuild = Data["perbuild"].ByteValue; pervisit = Data["pervisit"].ByteValue; metadata.Remove(Data); } return metadata; }
public NbtCompound Read(NbtCompound metadata) { var Data = metadata.Get <NbtCompound>("MCForge"); Logger.Log(Data["perbuild"].ToString()); if (Data != null) { perbuild = Data["perbuild"].ByteValue; pervisit = Data["pervisit"].ByteValue; metadata.Remove(Data); } return(metadata); }
public NbtCompound Read(NbtCompound metadata) { var hcData = metadata.Get <NbtCompound>("ZBase"); if (hcData == null) { return(metadata); } int currentVersion = hcData["Version"].IntValue; if (currentVersion == 1) { ReadVersionOne(hcData); } metadata.Remove(hcData); return(metadata); }
public static void SetNbt(NbtCompound compound, NbtTag value, params string[] path) { NbtCompound tag = compound; foreach (var item in path) { if (!tag.Contains(item)) { tag.Add(new NbtCompound(item)); } tag = (NbtCompound)tag[item]; // exception if existing non-compound tag is present } if (tag.TryGet(value.Name, out NbtTag existing) && existing.TagType == value.TagType) { tag.Remove(value.Name); } tag.Add(value); }
public NbtCompound Read(NbtCompound Metadata) { NbtCompound CPEData = Metadata.Get <NbtCompound>("CPE"); if (CPEData != null) { if (CPEData["ClickDistance"] != null) { ClickDistanceVersion = CPEData["ClickDistance"]["ExtensionVersion"].IntValue; ClickDistance = CPEData["ClickDistance"]["Distance"].ShortValue; } if (CPEData["CustomBlocks"] != null) { CustomBlocksVersion = CPEData["CustomBlocks"]["ExtensionVersion"].IntValue; CustomBlocksLevel = CPEData["CustomBlocks"]["SupportLevel"].ShortValue; CustomBlocksFallback = CPEData["CustomBlocks"]["Fallback"].ByteArrayValue; } if (CPEData["EnvColors"] != null) { EnvColorsVersion = CPEData["EnvColors"]["ExtensionVersion"].IntValue; SkyColor = new short[] { CPEData["EnvColors"]["Sky"]["R"].ShortValue, CPEData["EnvColors"]["Sky"]["G"].ShortValue, CPEData["EnvColors"]["Sky"]["B"].ShortValue }; CloudColor = new short[] { CPEData["EnvColors"]["Cloud"]["R"].ShortValue, CPEData["EnvColors"]["Cloud"]["G"].ShortValue, CPEData["EnvColors"]["Cloud"]["B"].ShortValue }; FogColor = new short[] { CPEData["EnvColors"]["Fog"]["R"].ShortValue, CPEData["EnvColors"]["Fog"]["G"].ShortValue, CPEData["EnvColors"]["Fog"]["B"].ShortValue }; AmbientColor = new short[] { CPEData["EnvColors"]["Ambient"]["R"].ShortValue, CPEData["EnvColors"]["Ambient"]["G"].ShortValue, CPEData["EnvColors"]["Ambient"]["B"].ShortValue }; SunlightColor = new short[] { CPEData["EnvColors"]["Sunlight"]["R"].ShortValue, CPEData["EnvColors"]["Sunlight"]["R"].ShortValue, CPEData["EnvColors"]["Sunlight"]["R"].ShortValue }; } if (CPEData["EnvMapAppearance"] != null) { EnvMapAppearanceVersion = CPEData["EnvMapAppearance"]["ExtensionVersion"].IntValue; TextureURL = CPEData["EnvMapAppearance"]["TextureURL"].StringValue; SideBlock = CPEData["EnvMapAppearance"]["SideBlock"].ByteValue; EdgeBlock = CPEData["EnvMapAppearance"]["EdgeBlock"].ByteValue; SideLevel = CPEData["EnvMapAppearance"]["SideLevel"].ShortValue; } Metadata.Remove(CPEData); } return(Metadata); }
public void ChildrenRefresh() { var compound = new NbtCompound("root") { new NbtByte("sub1"), new NbtShort("sub2"), new NbtCompound("sub3") { new NbtString("grandchild", "") } }; var root = new NbtTagNode(new NbtTreeModel(), null, compound); Assert.AreEqual(root.Children.Count(), 3); compound.Add(new NbtInt("more1")); compound.Add(new NbtInt("more2")); compound.Add(new NbtInt("more3")); Assert.AreEqual(root.Children.Count(), 6); compound.Remove("more1"); Assert.AreEqual(root.Children.Count(), 5); }
public void Renaming() { var tagToRename = new NbtInt("DifferentName", 1); var compound = new NbtCompound { new NbtInt("SameName", 1), tagToRename }; // proper renaming, should not throw tagToRename.Name = "SomeOtherName"; // attempting to use a duplicate name Assert.Throws <ArgumentException>(() => tagToRename.Name = "SameName"); // assigning a null name to a tag inside a compound; should throw Assert.Throws <ArgumentNullException>(() => tagToRename.Name = null); // assigning a null name to a tag that's been removed; should not throw compound.Remove(tagToRename); tagToRename.Name = null; }
public void SetItem(Item item, int rotation) { ItemInFrame = item; Rotation = rotation; var comp = new NbtCompound(string.Empty) { new NbtString("id", Id), new NbtInt("x", Coordinates.X), new NbtInt("y", Coordinates.Y), new NbtInt("z", Coordinates.Z), new NbtFloat("ItemDropChance", DropChance), new NbtByte("ItemRotation", (byte)Rotation), }; if (item != null) { var newItem = new NbtCompound("Item") { new NbtShort("id", item.Id), new NbtShort("Damage", item.Metadata), new NbtByte("Count", 1) }; if (item.ExtraData != null) { var newTag = (NbtTag)item.ExtraData.Clone(); newTag.Name = "tag"; newItem.Add(newTag); } comp["Item"] = newItem; } else { comp.Remove("Item"); } Compound = comp; }
public NbtCompound Read(NbtCompound metadata) { var hcData = metadata.Get<NbtCompound>("Hypercube"); if (hcData == null) return metadata; BuildPerms = hcData["BuildPerms"].StringValue; ShowPerms = hcData["ShowPerms"].StringValue; JoinPerms = hcData["JoinPerms"].StringValue; Physics = Convert.ToBoolean(hcData["Physics"].ByteValue); Building = Convert.ToBoolean(hcData["Building"].ByteValue); History = Convert.ToBoolean(hcData["History"].ByteValue); SaveInterval = hcData["SaveInterval"].IntValue; if (hcData["MOTD"] != null) Motd = hcData["MOTD"].StringValue; metadata.Remove(hcData); return metadata; }
public NbtCompound Read(NbtCompound Metadata) { NbtCompound CPEData = Metadata.Get<NbtCompound>("CPE"); if (CPEData != null) { if (CPEData["ClickDistance"] != null) { ClickDistanceVersion = CPEData["ClickDistance"]["ExtensionVersion"].IntValue; ClickDistance = CPEData["ClickDistance"]["Distance"].ShortValue; } if (CPEData["CustomBlocks"] != null) { CustomBlocksVersion = CPEData["CustomBlocks"]["ExtensionVersion"].IntValue; CustomBlocksLevel = CPEData["CustomBlocks"]["SupportLevel"].ShortValue; CustomBlocksFallback = CPEData["CustomBlocks"]["Fallback"].ByteArrayValue; } if (CPEData["EnvColors"] != null) { EnvColorsVersion = CPEData["EnvColors"]["ExtensionVersion"].IntValue; SkyColor = new short[] { CPEData["EnvColors"]["Sky"]["R"].ShortValue, CPEData["EnvColors"]["Sky"]["G"].ShortValue, CPEData["EnvColors"]["Sky"]["B"].ShortValue }; CloudColor = new short[] { CPEData["EnvColors"]["Cloud"]["R"].ShortValue, CPEData["EnvColors"]["Cloud"]["G"].ShortValue, CPEData["EnvColors"]["Cloud"]["B"].ShortValue }; FogColor = new short[] { CPEData["EnvColors"]["Fog"]["R"].ShortValue, CPEData["EnvColors"]["Fog"]["G"].ShortValue, CPEData["EnvColors"]["Fog"]["B"].ShortValue }; AmbientColor = new short[] { CPEData["EnvColors"]["Ambient"]["R"].ShortValue, CPEData["EnvColors"]["Ambient"]["G"].ShortValue, CPEData["EnvColors"]["Ambient"]["B"].ShortValue }; SunlightColor = new short[] { CPEData["EnvColors"]["Sunlight"]["R"].ShortValue, CPEData["EnvColors"]["Sunlight"]["R"].ShortValue, CPEData["EnvColors"]["Sunlight"]["R"].ShortValue }; } if (CPEData["EnvMapAppearance"] != null) { EnvMapAppearanceVersion = CPEData["EnvMapAppearance"]["ExtensionVersion"].IntValue; TextureURL = CPEData["EnvMapAppearance"]["TextureURL"].StringValue; SideBlock = CPEData["EnvMapAppearance"]["SideBlock"].ByteValue; EdgeBlock = CPEData["EnvMapAppearance"]["EdgeBlock"].ByteValue; SideLevel = CPEData["EnvMapAppearance"]["SideLevel"].ShortValue; } Metadata.Remove(CPEData); } return Metadata; }
public static ChunkColumn GetChunk(ChunkCoordinates coordinates, string basePath, IWorldProvider generator, int yoffset) { int width = 32; int depth = 32; int rx = coordinates.X >> 5; int rz = coordinates.Z >> 5; string filePath = Path.Combine(basePath, string.Format(@"region{2}r.{0}.{1}.mca", rx, rz, Path.DirectorySeparatorChar)); if (!File.Exists(filePath)) { return(generator?.GenerateChunkColumn(coordinates)); //return new ChunkColumn //{ // x = coordinates.X, // z = coordinates.Z, //}; } using (var regionFile = File.OpenRead(filePath)) { byte[] buffer = new byte[8192]; regionFile.Read(buffer, 0, 8192); int xi = (coordinates.X % width); if (xi < 0) { xi += 32; } int zi = (coordinates.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; int length = regionFile.ReadByte(); if (offset == 0 || length == 0) { return(generator?.GenerateChunkColumn(coordinates)); //return new ChunkColumn //{ // x = coordinates.X, // z = coordinates.Z, //}; } regionFile.Seek(offset, SeekOrigin.Begin); byte[] waste = new byte[4]; regionFile.Read(waste, 0, 4); int compressionMode = regionFile.ReadByte(); var nbt = new NbtFile(); nbt.LoadFromStream(regionFile, NbtCompression.ZLib); NbtTag dataTag = nbt.RootTag["Level"]; NbtList sections = dataTag["Sections"] as NbtList; ChunkColumn chunk = new ChunkColumn { x = coordinates.X, z = coordinates.Z, biomeId = dataTag["Biomes"].ByteArrayValue }; if (chunk.biomeId.Length > 256) { throw new Exception(); } // This will turn into a full chunk column foreach (NbtTag sectionTag in sections) { int sy = sectionTag["Y"].ByteValue * 16; byte[] blocks = sectionTag["Blocks"].ByteArrayValue; byte[] data = sectionTag["Data"].ByteArrayValue; NbtTag addTag = sectionTag["Add"]; byte[] adddata = new byte[2048]; if (addTag != null) { adddata = addTag.ByteArrayValue; } byte[] blockLight = sectionTag["BlockLight"].ByteArrayValue; byte[] skyLight = sectionTag["SkyLight"].ByteArrayValue; for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { for (int y = 0; y < 16; y++) { int yi = sy + y - yoffset; if (yi < 0 || yi >= 128) { continue; } int anvilIndex = y * 16 * 16 + z * 16 + x; int blockId = blocks[anvilIndex] + (Nibble4(adddata, anvilIndex) << 8); // Anvil to PE friendly converstion Func <int, byte, byte> dataConverter = (i, b) => b; // Default no-op converter if (Convert.ContainsKey(blockId)) { dataConverter = Convert[blockId].Item2; blockId = Convert[blockId].Item1; } if (blockId > 255) { blockId = 41; } //if (yi == 127 && blockId != 0) blockId = 30; if (yi == 0 && (blockId == 8 || blockId == 9)) { blockId = 7; } chunk.SetBlock(x, yi, z, (byte)blockId); byte metadata = Nibble4(data, anvilIndex); metadata = dataConverter(blockId, metadata); chunk.SetMetadata(x, yi, z, metadata); chunk.SetBlocklight(x, yi, z, Nibble4(blockLight, anvilIndex)); chunk.SetSkylight(x, yi, z, Nibble4(skyLight, anvilIndex)); var block = BlockFactory.GetBlockById(chunk.GetBlock(x, yi, z)); if (block is BlockStairs || block is StoneSlab || block is WoodSlab) { chunk.SetSkylight(x, yi, z, 0xff); } if (blockId == 43 && chunk.GetMetadata(x, yi, z) == 7) { chunk.SetMetadata(x, yi, z, 6); } else if (blockId == 44 && chunk.GetMetadata(x, yi, z) == 7) { chunk.SetMetadata(x, yi, z, 6); } else if (blockId == 44 && chunk.GetMetadata(x, yi, z) == 15) { chunk.SetMetadata(x, yi, z, 14); } else if (blockId == 3 && chunk.GetMetadata(x, yi, z) == 1) { // Dirt Course => (Grass Path) chunk.SetBlock(x, yi, z, 198); chunk.SetMetadata(x, yi, z, 0); } else if (blockId == 3 && chunk.GetMetadata(x, yi, z) == 2) { // Dirt Podzol => (Podzol) chunk.SetBlock(x, yi, z, 243); chunk.SetMetadata(x, yi, z, 0); } } } } } NbtList entities = dataTag["Entities"] as NbtList; NbtList blockEntities = dataTag["TileEntities"] as NbtList; if (blockEntities != null) { foreach (var nbtTag in blockEntities) { var blockEntityTag = (NbtCompound)nbtTag.Clone(); string entityId = blockEntityTag["id"].StringValue; int x = blockEntityTag["x"].IntValue; int y = blockEntityTag["y"].IntValue - yoffset; int z = blockEntityTag["z"].IntValue; blockEntityTag["y"] = new NbtInt("y", y); BlockEntity blockEntity = BlockEntityFactory.GetBlockEntityById(entityId); if (blockEntity != null) { blockEntityTag.Name = string.Empty; if (blockEntity is Sign) { // Remove the JSON stuff and get the text out of extra data. // TAG_String("Text2"): "{"extra":["10c a loaf!"],"text":""}" CleanSignText(blockEntityTag, "Text1"); CleanSignText(blockEntityTag, "Text2"); CleanSignText(blockEntityTag, "Text3"); CleanSignText(blockEntityTag, "Text4"); } else if (blockEntity is ChestBlockEntity) { NbtList items = (NbtList)blockEntityTag["Items"]; for (byte i = 0; i < items.Count; i++) { NbtCompound item = (NbtCompound)items[i]; item.Add(new NbtShort("OriginalDamage", item["Damage"].ShortValue)); byte metadata = (byte)(item["Damage"].ShortValue & 0xff); item.Remove("Damage"); item.Add(new NbtByte("Damage", metadata)); } } chunk.SetBlockEntity(new BlockCoordinates(x, y, z), blockEntityTag); } } } //NbtList tileTicks = dataTag["TileTicks"] as NbtList; chunk.isDirty = false; return(chunk); } }
public void AddingAndRemoving() { var foo = new NbtInt("Foo"); var test = new NbtCompound { foo }; // adding duplicate object Assert.Throws <ArgumentException>(() => test.Add(foo)); // adding duplicate name Assert.Throws <ArgumentException>(() => test.Add(new NbtByte("Foo"))); // adding unnamed tag Assert.Throws <ArgumentException>(() => test.Add(new NbtInt())); // adding null Assert.Throws <ArgumentNullException>(() => test.Add(null)); // adding tag to self Assert.Throws <ArgumentException>(() => test.Add(test)); // contains existing name/object Assert.True(test.Contains("Foo")); Assert.Contains(foo, test); Assert.Throws <ArgumentNullException>(() => test.Contains((string)null)); Assert.Throws <ArgumentNullException>(() => test.Contains((NbtTag)null)); // contains non-existent name Assert.False(test.Contains("Bar")); // contains existing name / different object Assert.DoesNotContain(new NbtInt("Foo"), test); // removing non-existent name Assert.Throws <ArgumentNullException>(() => test.Remove((string)null)); Assert.False(test.Remove("Bar")); // removing existing name Assert.True(test.Remove("Foo")); // removing non-existent name Assert.False(test.Remove("Foo")); // re-adding object test.Add(foo); // removing existing object Assert.Throws <ArgumentNullException>(() => test.Remove((NbtTag)null)); Assert.True(test.Remove(foo)); Assert.False(test.Remove(foo)); // clearing an empty NbtCompound Assert.Empty(test); test.Clear(); // re-adding after clearing test.Add(foo); Assert.Single(test); // clearing a non-empty NbtCompound test.Clear(); Assert.Empty(test); }
public NbtCompound Read(NbtCompound metadata) { var cpeData = metadata.Get<NbtCompound>("CPE"); if (cpeData == null) return metadata; if (cpeData["ClickDistance"] != null) { ClickDistanceVersion = cpeData["ClickDistance"]["ExtensionVersion"].IntValue; ClickDistance = cpeData["ClickDistance"]["Distance"].ShortValue; } if (cpeData["CustomBlocks"] != null) { CustomBlocksVersion = cpeData["CustomBlocks"]["ExtensionVersion"].IntValue; CustomBlocksLevel = cpeData["CustomBlocks"]["SupportLevel"].ShortValue; CustomBlocksFallback = cpeData["CustomBlocks"]["Fallback"].ByteArrayValue; } if (cpeData["EnvColors"] != null) { EnvColorsVersion = cpeData["EnvColors"]["ExtensionVersion"].IntValue; SkyColor = new[] { cpeData["EnvColors"]["Sky"]["R"].ShortValue, cpeData["EnvColors"]["Sky"]["G"].ShortValue, cpeData["EnvColors"]["Sky"]["B"].ShortValue }; CloudColor = new[] { cpeData["EnvColors"]["Cloud"]["R"].ShortValue, cpeData["EnvColors"]["Cloud"]["G"].ShortValue, cpeData["EnvColors"]["Cloud"]["B"].ShortValue }; FogColor = new[] { cpeData["EnvColors"]["Fog"]["R"].ShortValue, cpeData["EnvColors"]["Fog"]["G"].ShortValue, cpeData["EnvColors"]["Fog"]["B"].ShortValue }; AmbientColor = new[] { cpeData["EnvColors"]["Ambient"]["R"].ShortValue, cpeData["EnvColors"]["Ambient"]["G"].ShortValue, cpeData["EnvColors"]["Ambient"]["B"].ShortValue }; SunlightColor = new[] { cpeData["EnvColors"]["Sunlight"]["R"].ShortValue, cpeData["EnvColors"]["Sunlight"]["R"].ShortValue, cpeData["EnvColors"]["Sunlight"]["R"].ShortValue }; } if (cpeData["EnvMapAppearance"] != null) { EnvMapAppearanceVersion = cpeData["EnvMapAppearance"]["ExtensionVersion"].IntValue; TextureUrl = cpeData["EnvMapAppearance"]["TextureURL"].StringValue; SideBlock = cpeData["EnvMapAppearance"]["SideBlock"].ByteValue; EdgeBlock = cpeData["EnvMapAppearance"]["EdgeBlock"].ByteValue; SideLevel = cpeData["EnvMapAppearance"]["SideLevel"].ShortValue; } if (cpeData["EnvWeatherType"] != null) Weather = cpeData["EnvWeatherType"]["WeatherType"].ByteValue; metadata.Remove(cpeData); return metadata; }
public void AddingAndRemoving() { var foo = new NbtInt("Foo"); var test = new NbtCompound { foo }; // adding duplicate object Assert.Throws<ArgumentException>(() => test.Add(foo)); // adding duplicate name Assert.Throws<ArgumentException>(() => test.Add(new NbtByte("Foo"))); // adding unnamed tag Assert.Throws<ArgumentException>(() => test.Add(new NbtInt())); // adding null Assert.Throws<ArgumentNullException>(() => test.Add(null)); // adding tag to self Assert.Throws<ArgumentException>(() => test.Add(test)); // contains existing name/object Assert.True(test.Contains("Foo")); Assert.True(test.Contains(foo)); Assert.Throws<ArgumentNullException>(() => test.Contains((string) null)); Assert.Throws<ArgumentNullException>(() => test.Contains((NbtTag) null)); // contains non-existent name Assert.False(test.Contains("Bar")); // contains existing name / different object Assert.False(test.Contains(new NbtInt("Foo"))); // removing non-existent name Assert.Throws<ArgumentNullException>(() => test.Remove((string) null)); Assert.False(test.Remove("Bar")); // removing existing name Assert.True(test.Remove("Foo")); // removing non-existent name Assert.False(test.Remove("Foo")); // re-adding object test.Add(foo); // removing existing object Assert.Throws<ArgumentNullException>(() => test.Remove((NbtTag) null)); Assert.True(test.Remove(foo)); Assert.False(test.Remove(foo)); // clearing an empty NbtCompound Assert.Equal(0, test.Count); test.Clear(); // re-adding after clearing test.Add(foo); Assert.Equal(1, test.Count); // clearing a non-empty NbtCompound test.Clear(); Assert.Equal(0, test.Count); }
public Map Load(string path) { NbtFile file = new NbtFile(path); NbtCompound root = file.RootTag; int formatVersion = root["FormatVersion"].ByteValue; if (formatVersion != 1) { throw new MapFormatException("Unsupported format version: " + formatVersion); } // Read dimensions and create the map Map map = new Map(null, root["X"].ShortValue, root["Z"].ShortValue, root["Y"].ShortValue, false); // read spawn coordinates NbtCompound spawn = (NbtCompound)root["Spawn"]; map.Spawn = new Position(spawn["X"].ShortValue, spawn["Z"].ShortValue, spawn["Y"].ShortValue, spawn["H"].ByteValue, spawn["P"].ByteValue); // read UUID map.Guid = new Guid(root["UUID"].ByteArrayValue); // read creation/modification dates of the file (for fallback) DateTime fileCreationDate = File.GetCreationTime(path); DateTime fileModTime = File.GetCreationTime(path); // try to read embedded creation date NbtLong creationDate = root.Get <NbtLong>("TimeCreated"); if (creationDate != null) { map.DateCreated = DateTimeUtil.ToDateTime(creationDate.Value); } else { // for fallback, pick the older of two filesystem dates map.DateCreated = (fileModTime > fileCreationDate) ? fileCreationDate : fileModTime; } // try to read embedded modification date NbtLong modTime = root.Get <NbtLong>("LastModified"); if (modTime != null) { map.DateModified = DateTimeUtil.ToDateTime(modTime.Value); } else { // for fallback, use file modification date map.DateModified = fileModTime; } // TODO: LastAccessed // TODO: read CreatedBy and MapGenerator // read blocks map.Blocks = root["BlockArray"].ByteArrayValue; // TODO: CPE CustomBlock conversion // read metadata, if present NbtCompound metadata = root.Get <NbtCompound>("Metadata"); if (metadata == null) { return(map); } NbtCompound fCraftMetadata = metadata.Get <NbtCompound>("fCraft"); if (fCraftMetadata != null) { foreach (NbtCompound groupTag in fCraftMetadata) { string groupName = groupTag.Name; foreach (NbtString keyValueTag in groupTag) { // ReSharper disable AssignNullToNotNullAttribute // names are never null within compound map.Metadata.Add(groupName, keyValueTag.Name, keyValueTag.Value); // ReSharper restore AssignNullToNotNullAttribute } } } // read CPE settings NbtCompound cpeMetadata = metadata.Get <NbtCompound>("CPE"); if (cpeMetadata != null) { // TODO: CPE metadata } // preserve foreign metadata, if needed if (MapUtility.PreserveForeignMetadata) { metadata.Remove("fCraft"); metadata.Remove("CPE"); map.ForeignMetadata = metadata; } return(map); }
public void Renaming() { var tagToRename = new NbtInt("DifferentName", 1); var compound = new NbtCompound { new NbtInt("SameName", 1), tagToRename }; // proper renaming, should not throw tagToRename.Name = "SomeOtherName"; // attempting to use a duplicate name Assert.Throws<ArgumentException>(() => tagToRename.Name = "SameName"); // assigning a null name to a tag inside a compound; should throw Assert.Throws<ArgumentNullException>(() => tagToRename.Name = null); // assigning a null name to a tag inside a compound; should not throw compound.Remove(tagToRename); tagToRename.Name = null; }