/// <summary> /// Loads a level from the given level.dat file. /// </summary> public static Level Load(string file) { if (!Path.IsPathRooted(file)) { file = Path.Combine(Directory.GetCurrentDirectory(), file); } var serializer = new NbtSerializer(typeof(Level)); var nbtFile = new NbtFile(file); var level = (Level)serializer.Deserialize(nbtFile.RootTag["Data"]); level.DatFile = file; level.BaseDirectory = Path.GetDirectoryName(file); level.WorldGenerator = GetGenerator(level.GeneratorName); level.WorldGenerator.Initialize(level); var worlds = Directory.GetDirectories(level.BaseDirectory).Where( d => Directory.GetFiles(d).Any(f => f.EndsWith(".mca") || f.EndsWith(".mcr"))); foreach (var world in worlds) { var w = World.LoadWorld(world); w.WorldGenerator = level.WorldGenerator; level.AddWorld(w); } return(level); }
private void WriteDimension(Dimension dimension, IMinecraftPacket packet) { var nbtDimension = NbtSerializer.SerializeCompound(dimension.Element, ""); var nbtFile = new NbtFile(nbtDimension); packet.WriteBytes(nbtFile.GetBuffer()); }
/// <summary> /// Saves this level. This will not work with an in-memory world. /// </summary> public void Save() { if (DatFile == null) { throw new InvalidOperationException("This level exists only in memory. Use Save(string)."); } LastPlayed = DateTime.UtcNow.Ticks; var serializer = new NbtSerializer(typeof(Level)); var tag = serializer.Serialize(this, "Data") as NbtCompound; var file = new NbtFile(); file.RootTag.Add(tag); file.SaveToFile(DatFile, NbtCompression.GZip); // Save worlds foreach (var world in Worlds) { if (world.BaseDirectory == null) { world.Save(Path.Combine(BaseDirectory, world.Name)); } else { world.Save(); } } }
public void SerializeComplexCompoundTest() { var faker = new Faker(); var element = new CustomComplexNbtElement { IntegerValue = faker.Random.Int(), FlatElement = new CustomFlatNbtElement { ByteValue = faker.Random.Byte(), ShortValue = faker.Random.Short(), IntegerValue = faker.Random.Int(), FloatValue = faker.Random.Float(), LongValue = faker.Random.Long(), DoubleValue = faker.Random.Double(), StringValue = faker.Lorem.Sentence(), } }; NbtCompound compound = NbtSerializer.SerializeCompound(element); Assert.NotNull(compound); AssertTag <NbtInt, int>(compound, "integer_value", element.IntegerValue, x => x.IntValue); NbtCompound innerCompound = compound.Get("flat_element_compound") as NbtCompound; Assert.NotNull(innerCompound); AssertTag <NbtByte, byte>(innerCompound, "byte_value", element.FlatElement.ByteValue, x => x.ByteValue); AssertTag <NbtShort, short>(innerCompound, "short_value", element.FlatElement.ShortValue, x => x.ShortValue); AssertTag <NbtInt, int>(innerCompound, "integer_value", element.FlatElement.IntegerValue, x => x.IntValue); AssertTag <NbtFloat, float>(innerCompound, "float_value", element.FlatElement.FloatValue, x => x.FloatValue); AssertTag <NbtLong, long>(innerCompound, "long_value", element.FlatElement.LongValue, x => x.LongValue); AssertTag <NbtDouble, double>(innerCompound, "double_value", element.FlatElement.DoubleValue, x => x.DoubleValue); AssertTag <NbtString, string>(innerCompound, "string_value", element.FlatElement.StringValue, x => x.StringValue); }
public static Chunk FromNbt(NbtFile nbt) { var serializer = new NbtSerializer(typeof(Chunk)); var chunk = (Chunk)serializer.Deserialize(nbt.RootTag["Level"]); return(chunk); }
private void LoadFromFile(string directory) { NbtFile file = new NbtFile(); using (var stream = File.Open(Path.Combine(LevelDirectory, "level.dat"), FileMode.Open)) file.LoadFromStream(stream, NbtCompression.None, null); var data = file.RootTag.Get <NbtCompound>("Data"); var serializer = new NbtSerializer(typeof(SavedLevel)); SavedLevel level = (SavedLevel)serializer.Deserialize(data); Name = level.LevelName; Time = level.Time; GameMode = (GameMode)level.GameMode; MapFeatures = level.MapFeatures; Seed = level.Seed; // Find world generator string generatorName = level.GeneratorName; WorldGenerator = GetGenerator(generatorName); WorldGenerator.Seed = Seed; GeneratorOptions = level.GeneratorOptions; WorldGenerator.Initialize(this); SpawnPoint = level.SpawnPoint; World = new World(this, WorldGenerator, Path.Combine(directory, "region")); }
private void CheckFromTag(bool fill) { var stringValue = "test_string_value_rv"; var intValue = 882882; var shadow = new EasyTestClass(); var tag = new NbtCompound(); tag.Add(new NbtString(nameof(shadow.EasyStringProperty), stringValue)); tag.Add(new NbtInt(nameof(shadow.EasyIntProperty), intValue)); EasyTestClass result = null; if (fill) { result = shadow; NbtSerializer.FillObject(result, tag); } else { result = NbtSerializer.DeserializeObject <EasyTestClass>(tag); } Assert.IsNotNull(result); Assert.AreEqual(stringValue, result.EasyStringProperty); Assert.AreEqual(intValue, result.EasyIntProperty); }
public void TestSerializer() { var serializer = new NbtSerializer(typeof(TestType)); var test = new TestType(); var serialized = serializer.Serialize(test); var deserialized = (TestType)serializer.Deserialize(serialized); Assert.AreEqual(test, deserialized); }
public NbtFile ToNbt() { var serializer = new NbtSerializer(typeof(Chunk)); var compound = serializer.Serialize(this, "Level") as NbtCompound; var file = new NbtFile(); file.RootTag.Add(compound); return(file); }
public NbtCompound ToNbt(Vector3 position) { var serializer = new NbtSerializer(GetType()); var entity = (NbtCompound)serializer.Serialize(this); entity.Add(new NbtInt("x", (int)position.X)); entity.Add(new NbtInt("y", (int)position.Y)); entity.Add(new NbtInt("z", (int)position.Z)); return(entity); }
public static WorldProperties FromFile_Pre(string fileName) { if (!fileName.StartsWith(Utils.Remote)) { return(null); } var stream = new MemoryStream(Utils.GetWebLevelData()); var nbt = new NbtSerializer().Deserialize(new GZipStream(stream, CompressionMode.Decompress)); return(new CustomProperties(nbt, fileName.Remove(fileName.LastIndexOfAny(new[] { '\\', '/' })).Substring(Utils.Remote.Length + 1))); }
public void Save() { NbtFile file = new NbtFile(); var serializer = new NbtSerializer(typeof(SavedLevel)); var level = new SavedLevel { IsRaining = Raining, GeneratorVersion = 0, Time = Time, GameMode = (int)GameMode, MapFeatures = MapFeatures, GeneratorName = WorldGenerator.GeneratorName, Initialized = true, Seed = Seed, SpawnPoint = SpawnPoint, SizeOnDisk = 0, ThunderTime = ThunderTime, RainTime = RainTime, Version = 19133, Thundering = Thundering, LevelName = Name, LastPlayed = DateTime.UtcNow.Ticks }; if (!string.IsNullOrEmpty(PlayerName)) { if (File.Exists(Path.Combine(LevelDirectory, "players", PlayerName + ".dat"))) { var player = new NbtFile(); using (Stream stream = File.Open(Path.Combine(LevelDirectory, "players", PlayerName + ".dat"), FileMode.Open)) player.LoadFromStream(stream, NbtCompression.GZip, null); level.Player = player.RootTag; level.Player.Name = "Player"; } } var data = serializer.Serialize(level); file.RootTag = new NbtCompound(""); file.RootTag.Add(data); using (var stream = File.Create(Path.Combine(LevelDirectory, "level.dat"))) file.SaveToStream(stream, NbtCompression.GZip); if (World.Directory == null) { World.Save(Path.Combine(LevelDirectory, "region")); } else { World.Save(); } }
static void Main(string[] args) { NbtSerializer s = new NbtSerializer(typeof(Player)); using (System.IO.FileStream file = new System.IO.FileStream(@"e:\Users\Stephen\Desktop\mcedit\world\players\skonves.dat", System.IO.FileMode.Open)) { using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(file, System.IO.Compression.CompressionMode.Decompress)) { using (NbtReader reader = new NbtReader(zip)) { object obj = s.Deserialize(reader); } } } //System.IO.Compression.GZipStream }
public void Deserialize(NbtTag value) { IsModified = true; var compound = value as NbtCompound; var chunk = (Chunk)Serializer.Deserialize(value, true); this._TileEntities = chunk._TileEntities; this.Biomes = chunk.Biomes; this.HeightMap = chunk.HeightMap; this.LastUpdate = chunk.LastUpdate; this.Sections = chunk.Sections; this.TerrainPopulated = chunk.TerrainPopulated; this.X = chunk.X; this.Z = chunk.Z; // Entities var entities = compound["Entities"] as NbtList; Entities = new List <IDiskEntity>(); for (int i = 0; i < entities.Count; i++) { var id = entities[i]["id"].StringValue; IDiskEntity entity; if (EntityTypes.ContainsKey(id.ToUpper())) { entity = (IDiskEntity)Activator.CreateInstance(EntityTypes[id]); } else { entity = new UnrecognizedEntity(id); } entity.Deserialize(entities[i]); Entities.Add(entity); } var serializer = new NbtSerializer(typeof(Section)); foreach (var section in compound["Sections"] as NbtList) { int index = section["Y"].IntValue; Sections[index] = (Section)serializer.Deserialize(section); Sections[index].ProcessSection(); } }
public static TileEntity FromNbt(NbtCompound entityTag, out Vector3 position) { position = Vector3.Zero; var id = entityTag.Get <NbtString>("id").Value; Type type = (from e in dummyInstances where e.Id == id select e.GetType()).FirstOrDefault(); if (type == null) { return(null); } var serializer = new NbtSerializer(type); var entity = (TileEntity)serializer.Deserialize(entityTag); position = new Vector3( entityTag.Get <NbtInt>("x").Value, entityTag.Get <NbtInt>("y").Value, entityTag.Get <NbtInt>("z").Value); return(entity); }
public void TestSignTile() { SignTileEntity entity = new SignTileEntity(); entity.Text1 = "Test1"; entity.Text2 = "Test2"; entity.Text3 = "Test3"; entity.Text4 = "Test4"; var serializer = new NbtSerializer(typeof(SignTileEntity)); var obj = serializer.Serialize(entity); var result = (SignTileEntity)serializer.Deserialize(obj); Assert.AreEqual(entity.Id, result.Id); Assert.AreEqual(entity.Text1, result.Text1); Assert.AreEqual(entity.Text2, result.Text2); Assert.AreEqual(entity.Text3, result.Text3); Assert.AreEqual(entity.Text4, result.Text4); }
private void WriteDimensionsAndBiomes(IEnumerable <Dimension> dimensions, IEnumerable <Biome> biomes, IMinecraftPacket packet) { IEnumerable <NbtTag> dimensionsTags = dimensions.Select(x => NbtSerializer.SerializeCompound(x)); IEnumerable <NbtTag> biomesTags = biomes.Select(x => NbtSerializer.SerializeCompound(x)); var nbtCompound = new NbtCompound("") { new NbtCompound("minecraft:dimension_type") { new NbtString("type", "minecraft:dimension_type"), new NbtList("value", dimensionsTags, NbtTagType.Compound) }, new NbtCompound("minecraft:worldgen/biome") { new NbtString("type", "minecraft:worldgen/biome"), new NbtList("value", biomesTags, NbtTagType.Compound) } }; var nbtFile = new NbtFile(nbtCompound); packet.WriteBytes(nbtFile.GetBuffer()); }
public void TypesTest() { var raw = new TypesTestClass(); var tag = NbtSerializer.SerializeObject(raw); var deserialized = NbtSerializer.DeserializeObject <TypesTestClass>(tag); Assert.IsNotNull(tag); Assert.AreEqual(raw.BoolFalseTestProperty, deserialized.BoolFalseTestProperty); Assert.AreEqual(raw.BoolTrueTestProperty, deserialized.BoolTrueTestProperty); Assert.AreEqual(raw.ByteArrayTestProperty, deserialized.ByteArrayTestProperty); Assert.AreEqual(raw.IntArrayTestProperty, deserialized.IntArrayTestProperty); Assert.AreEqual(raw.ByteTestProperty, deserialized.ByteTestProperty); Assert.AreEqual(raw.SByteTestProperty, deserialized.SByteTestProperty); Assert.AreEqual(raw.ShortTestProperty, deserialized.ShortTestProperty); Assert.AreEqual(raw.UShortTestProperty, deserialized.UShortTestProperty); Assert.AreEqual(raw.IntTestProperty, deserialized.IntTestProperty); Assert.AreEqual(raw.UIntTestProperty, deserialized.UIntTestProperty); Assert.AreEqual(raw.LongTestProperty, deserialized.LongTestProperty); Assert.AreEqual(raw.ULongTestProperty, deserialized.ULongTestProperty); Assert.AreEqual(raw.NbtIntProperty.Value, deserialized.NbtIntProperty.Value); AssertTagValue <NbtByte>(tag, nameof(raw.BoolFalseTestProperty), raw.BoolFalseTestProperty, t => Convert.ToBoolean(t.Value)); AssertTagValue <NbtByte>(tag, nameof(raw.BoolTrueTestProperty), raw.BoolTrueTestProperty, t => Convert.ToBoolean(t.Value)); AssertTagValue <NbtByteArray>(tag, nameof(raw.ByteArrayTestProperty), raw.ByteArrayTestProperty, t => t.Value); AssertTagValue <NbtIntArray>(tag, nameof(raw.IntArrayTestProperty), raw.IntArrayTestProperty, t => t.Value); AssertTagValue <NbtByte>(tag, nameof(raw.ByteTestProperty), raw.ByteTestProperty, t => t.Value); AssertTagValue <NbtByte>(tag, nameof(raw.SByteTestProperty), raw.SByteTestProperty, t => (sbyte)t.Value); AssertTagValue <NbtShort>(tag, nameof(raw.ShortTestProperty), raw.ShortTestProperty, t => t.Value); AssertTagValue <NbtShort>(tag, nameof(raw.UShortTestProperty), raw.UShortTestProperty, t => (ushort)t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.IntTestProperty), raw.IntTestProperty, t => t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.UIntTestProperty), raw.UIntTestProperty, t => (uint)t.Value); AssertTagValue <NbtLong>(tag, nameof(raw.LongTestProperty), raw.LongTestProperty, t => t.Value); AssertTagValue <NbtLong>(tag, nameof(raw.ULongTestProperty), raw.ULongTestProperty, t => (ulong)t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.NbtIntProperty), raw.NbtIntProperty.Value, t => t.Value); }
public void FillTest() { var valuesSet = new List <string>() { "testVal1", "test_val_2", "TestVal3", "TEST_VAL_4" }; var original = new FillTestClass(); original.GetOnlyTestStringList.AddRange(valuesSet); original.GetOnlyTestClassProperty.EasyIntProperty = 321; original.GetOnlyTestClassProperty.EasyStringProperty = "ESP_TEST_FILL"; var tag = NbtSerializer.SerializeObject(original); var result = NbtSerializer.DeserializeObject <FillTestClass>(tag); Assert.IsNotNull(result); Assert.AreEqual(original.GetOnlyTestClassProperty.EasyIntProperty, result.GetOnlyTestClassProperty.EasyIntProperty); Assert.AreEqual(original.GetOnlyTestClassProperty.EasyStringProperty, result.GetOnlyTestClassProperty.EasyStringProperty); CollectionAssert.AreEquivalent(original.GetOnlyTestStringList, result.GetOnlyTestStringList); }
public NbtTag Serialize(string tagName) { var chunk = (NbtCompound)Serializer.Serialize(this, tagName, true); var entities = new NbtList("Entities", NbtTagType.Compound); for (int i = 0; i < Entities.Count; i++) { entities.Add(Entities[i].Serialize(string.Empty)); } chunk.Add(entities); var sections = new NbtList("Sections", NbtTagType.Compound); var serializer = new NbtSerializer(typeof(Section)); for (int i = 0; i < Sections.Length; i++) { if (!Sections[i].IsAir) { sections.Add(serializer.Serialize(Sections[i])); } } chunk.Add(sections); return(chunk); }
public void FieldsAndPropertiesTest() { var raw = new FieldsAndPropertiesTestClass(); var tag = NbtSerializer.SerializeObject(raw); Assert.IsNotNull(tag); Assert.IsFalse(tag.Contains(nameof(raw.HidenTestIntFiled))); Assert.IsFalse(tag.Contains(nameof(raw.HidenDefaultTestIntFiled))); Assert.IsFalse(tag.Contains(nameof(raw.HidenTestIntProperty))); Assert.IsFalse(tag.Contains(nameof(raw.HidenDefaultTestIntProperty))); AssertTagValue <NbtInt>(tag, nameof(raw.DefaultTestIntFiled), raw.DefaultTestIntFiled, t => t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.TestIntFiled), raw.TestIntFiled, t => t.Value); AssertTagValue <NbtInt>(tag, "PrivateTestIntFiled", 3, t => t.Value); AssertTagValue <NbtInt>(tag, FieldsAndPropertiesTestClass.NameForNamedTestIndFiled, raw.NamedTestIntFiled, t => t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.DefaultTestIntProperty), raw.DefaultTestIntProperty, t => t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.TestIntProperty), raw.TestIntProperty, t => t.Value); AssertTagValue <NbtInt>(tag, nameof(raw.OnlyGetTestIntProperty), raw.OnlyGetTestIntProperty, t => t.Value); AssertTagValue <NbtInt>(tag, "PrivateTestIntProperty", 8, t => t.Value); AssertTagValue <NbtInt>(tag, FieldsAndPropertiesTestClass.NameForNamedTestIndProperty, raw.NamedTestIntProperty, t => t.Value); }
static Chunk() { EntityTypes = new Dictionary <string, Type>(); Serializer = new NbtSerializer(typeof(Chunk)); }
static void Main(string[] args) { TagCompound tag = NbtSerializer.Serialize(new WhatTheBlah()); WhatTheBlah obj = NbtSerializer.Deserialize <WhatTheBlah>(tag); }