Example #1
0
 public NbtCompound ToNbt(Vector3 position)
 {
     var serializer = new NbtSerializer(GetType());
     
     var entity = (NbtCompound)serializer.Serialize(this);
     
     entity.Tags.Add(new NbtInt("x", (int)position.X));
     entity.Tags.Add(new NbtInt("y", (int)position.Y));
     entity.Tags.Add(new NbtInt("z", (int)position.Z));
    
     return entity;
 }
Example #2
0
 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;
 }
Example #3
0
 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);
 }
Example #4
0
        public object Deserialize(NbtTag value)
        {
            if (value is NbtByte)
                return ((NbtByte)value).Value;
            else if (value is NbtByteArray)
                return ((NbtByteArray)value).Value;
            else if (value is NbtDouble)
                return ((NbtDouble)value).Value;
            else if (value is NbtFloat)
                return ((NbtFloat)value).Value;
            else if (value is NbtInt)
                return ((NbtInt)value).Value;
            else if (value is NbtIntArray)
                return ((NbtIntArray)value).Value;
            else if (value is NbtLong)
                return ((NbtLong)value).Value;
            else if (value is NbtShort)
                return ((NbtShort)value).Value;
            else if (value is NbtString)
                return ((NbtString)value).Value;
            else if(value is NbtCompound)
            {
                var compound = value as NbtCompound;
                var properties = Type.GetProperties().Where(p =>
                    !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                var resultObject = Activator.CreateInstance(Type);
                foreach (var property in properties)
                {
                    if (!property.CanWrite)
                        continue;
                    string name = property.Name;
                    var nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));

                    if (nameAttributes.Length != 0)
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    var node = compound.Tags.SingleOrDefault(a => a.Name == name);
                    if (node == null) continue;
                    var data = new NbtSerializer(property.PropertyType).Deserialize(node);

                    if (property.PropertyType == typeof(bool)
                        && data is byte)
                        data = (byte)data == 1;

                    property.SetValue(resultObject, data, null);
                }
                
                return resultObject;
            }
            
            throw new NotSupportedException("The node type '" + value.GetType() + "' is not supported.");
        }
Example #5
0
        public NbtTag Serialize(object value, string tagName)
        {
            if (value is NbtTag)
                return (NbtTag)value;
            if (value is byte)
                return new NbtByte(tagName, (byte)value);
            if (value is bool)
                return new NbtByte(tagName, (byte)((bool)value ? 1 : 0));
            else if (value is byte[])
                return new NbtByteArray(tagName, (byte[])value);
            else if (value is double)
                return new NbtDouble(tagName, (double)value);
            else if (value is float)
                return new NbtFloat(tagName, (float)value);
            else if (value is int)
                return new NbtInt(tagName, (int)value);
            else if (value is int[])
                return new NbtIntArray(tagName, (int[])value);
            else if (value is long)
                return new NbtLong(tagName, (long)value);
            else if (value is short)
                return new NbtShort(tagName, (short)value);
            else if (value is string)
                return new NbtString(tagName, (string)value);
            else
            {
                var compound = new NbtCompound(tagName);
               
                if(value == null) return compound;
                var nameAttributes = Attribute.GetCustomAttributes(value.GetType(), typeof(TagNameAttribute));

                if (nameAttributes.Length > 0)
                    compound = new NbtCompound(((TagNameAttribute)nameAttributes[0]).Name);

                var properties = Type.GetProperties().Where(p =>
                    !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                
                foreach (var property in properties)
                {
                    if (!property.CanRead)
                        continue;
                    
                    NbtTag tag = null;
                    
                    string name = property.Name;
                    nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));
                    var ignoreOnNullAttribute = Attribute.GetCustomAttribute(property, typeof(IgnoreOnNullAttribute));
                    if (nameAttributes.Length != 0)
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    
                    var innerSerializer = new NbtSerializer(property.PropertyType);
                    var propValue = property.GetValue(value, null);
                    
                    if(propValue == null)
                    {
                        if (ignoreOnNullAttribute != null) continue;
                        if(property.PropertyType.IsValueType)
                        {
                            propValue = Activator.CreateInstance(property.PropertyType);
                        }
                        else if (property.PropertyType == typeof(string))
                            propValue = "";
                    }
                    
                    tag = innerSerializer.Serialize(propValue, name);
                    compound.Add(tag);
                }
                
                return compound;
            }
        }
Example #6
0
        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"));
        }
Example #7
0
        public void Save()
        {
            NbtFile file = new NbtFile();

            var serializer = new NbtSerializer(typeof(SavedLevel));
            var data = serializer.Serialize(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
            });
            file.RootTag = new NbtCompound("");
            file.RootTag.Add(data);
            using (var stream = File.Open(Path.Combine(LevelDirectory, "level.dat"), FileMode.Create))
                file.SaveToStream(stream, NbtCompression.GZip);

            World.Save();
        }
Example #8
0
        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();
        }
Example #9
0
        public NbtTag Serialize(object value, string tagName)
        {
            if (value is NbtTag)
            {
                return((NbtTag)value);
            }
            if (value is byte)
            {
                return(new NbtByte(tagName, (byte)value));
            }
            if (value is bool)
            {
                return(new NbtByte(tagName, (byte)((bool)value ? 1 : 0)));
            }
            else if (value is byte[])
            {
                return(new NbtByteArray(tagName, (byte[])value));
            }
            else if (value is double)
            {
                return(new NbtDouble(tagName, (double)value));
            }
            else if (value is float)
            {
                return(new NbtFloat(tagName, (float)value));
            }
            else if (value is int)
            {
                return(new NbtInt(tagName, (int)value));
            }
            else if (value is int[])
            {
                return(new NbtIntArray(tagName, (int[])value));
            }
            else if (value is long)
            {
                return(new NbtLong(tagName, (long)value));
            }
            else if (value is short)
            {
                return(new NbtShort(tagName, (short)value));
            }
            else if (value is string)
            {
                return(new NbtString(tagName, (string)value));
            }
            else
            {
                var compound = new NbtCompound(tagName);

                if (value == null)
                {
                    return(compound);
                }
                var nameAttributes = Attribute.GetCustomAttributes(value.GetType(), typeof(TagNameAttribute));

                if (nameAttributes.Length > 0)
                {
                    compound = new NbtCompound(((TagNameAttribute)nameAttributes[0]).Name);
                }

                var properties = Type.GetProperties().Where(p =>
                                                            !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());

                foreach (var property in properties)
                {
                    if (!property.CanRead)
                    {
                        continue;
                    }

                    NbtTag tag = null;

                    string name = property.Name;
                    nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));
                    var ignoreOnNullAttribute = Attribute.GetCustomAttribute(property, typeof(IgnoreOnNullAttribute));
                    if (nameAttributes.Length != 0)
                    {
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    }

                    var innerSerializer = new NbtSerializer(property.PropertyType);
                    var propValue       = property.GetValue(value, null);

                    if (propValue == null)
                    {
                        if (ignoreOnNullAttribute != null)
                        {
                            continue;
                        }
                        if (property.PropertyType.IsValueType)
                        {
                            propValue = Activator.CreateInstance(property.PropertyType);
                        }
                        else if (property.PropertyType == typeof(string))
                        {
                            propValue = "";
                        }
                    }

                    tag = innerSerializer.Serialize(propValue, name);
                    compound.Add(tag);
                }

                return(compound);
            }
        }
Example #10
0
        public object Deserialize(NbtTag value)
        {
            if (value is NbtByte)
            {
                return(((NbtByte)value).Value);
            }
            else if (value is NbtByteArray)
            {
                return(((NbtByteArray)value).Value);
            }
            else if (value is NbtDouble)
            {
                return(((NbtDouble)value).Value);
            }
            else if (value is NbtFloat)
            {
                return(((NbtFloat)value).Value);
            }
            else if (value is NbtInt)
            {
                return(((NbtInt)value).Value);
            }
            else if (value is NbtIntArray)
            {
                return(((NbtIntArray)value).Value);
            }
            else if (value is NbtLong)
            {
                return(((NbtLong)value).Value);
            }
            else if (value is NbtShort)
            {
                return(((NbtShort)value).Value);
            }
            else if (value is NbtString)
            {
                return(((NbtString)value).Value);
            }
            else if (value is NbtCompound)
            {
                var compound   = value as NbtCompound;
                var properties = Type.GetProperties().Where(p =>
                                                            !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                var resultObject = Activator.CreateInstance(Type);
                foreach (var property in properties)
                {
                    if (!property.CanWrite)
                    {
                        continue;
                    }
                    string name           = property.Name;
                    var    nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));

                    if (nameAttributes.Length != 0)
                    {
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    }
                    var node = compound.Tags.SingleOrDefault(a => a.Name == name);
                    if (node == null)
                    {
                        continue;
                    }
                    var data = new NbtSerializer(property.PropertyType).Deserialize(node);

                    if (property.PropertyType == typeof(bool) &&
                        data is byte)
                    {
                        data = (byte)data == 1;
                    }

                    property.SetValue(resultObject, data, null);
                }

                return(resultObject);
            }

            throw new NotSupportedException("The node type '" + value.GetType() + "' is not supported.");
        }