SaveToStream() public method

Saves this NBT file to a stream. Nothing is written to stream if RootTag is null.
is null. If AutoDetect was given as the mode. If an unrecognized/unsupported value was given for . If given stream does not support writing. If RootTag is null; /// or if RootTag is unnamed; /// or if one of the NbtCompound tags contained unnamed tags; /// or if an NbtList tag had Unknown list type and no elements.
public SaveToStream ( [ stream, NbtCompression compression ) : long
stream [ Stream to write data to. May not be null.
compression NbtCompression Compression mode to use for saving. May not be AutoDetect.
return long
Example #1
0
 public void Export(string path, NbtTag root)
 {
     if (Snbt)
     {
         File.WriteAllText(path, root.ToSnbt(CreateOptions()));
     }
     else
     {
         var file = new fNbt.NbtFile();
         file.BigEndian = BigEndian;
         file.RootTag   = root;
         using (var writer = File.Create(path))
         {
             if (BedrockHeader)
             {
                 writer.Seek(8, SeekOrigin.Begin);
             }
             long size = file.SaveToStream(writer, Compression);
             if (BedrockHeader)
             {
                 // bedrock level.dat files start with a header containing a magic number and then the little-endian size of the data
                 writer.Seek(0, SeekOrigin.Begin);
                 writer.Write(new byte[] { 8, 0, 0, 0 }, 0, 4);
                 writer.Write(DataUtils.GetBytes((int)size, little_endian: !BigEndian), 0, 4);
             }
         }
     }
 }
Example #2
0
        public void SavePlayer(PlayerEntity entity)
        {
            // TODO: Generalize to all mobs
            NbtFile file = new NbtFile();
            var data = new NbtCompound("");
            data.Add(new NbtByte("OnGround", (byte)(entity.OnGround ? 1 : 0)));
            data.Add(new NbtShort("Air", entity.Air));
            data.Add(new NbtShort("Health", entity.Health));
            data.Add(new NbtInt("Dimension", 0)); // TODO
            data.Add(new NbtInt("foodLevel", entity.Food));
            data.Add(new NbtInt("XpLevel", entity.XpLevel));
            data.Add(new NbtInt("XpTotal", entity.XpTotal));
            data.Add(new NbtFloat("foodExhaustionLevel", entity.FoodExhaustion));
            data.Add(new NbtFloat("foodSaturationLevel", entity.FoodSaturation));
            data.Add(new NbtFloat("XpP", entity.XpProgress));
            data.Add(new NbtList("Equipment", NbtTagType.Compound));
            var inventory = new NbtList("Inventory", NbtTagType.Compound);
            for (int index = 0; index < entity.Inventory.Length; index++)
            {
                var slot = entity.Inventory[index];
                if (slot.Empty)
                    continue;
                slot.Index = NetworkSlotToDataSlot(index);
                inventory.Add(slot.ToNbt());
            }
            data.Add(inventory);
            var motion = new NbtList("Motion", NbtTagType.Double);
            motion.Add(new NbtDouble(entity.Velocity.X));
            motion.Add(new NbtDouble(entity.Velocity.Y));
            motion.Add(new NbtDouble(entity.Velocity.Z));
            data.Add(motion);

            var pos = new NbtList("Pos", NbtTagType.Double);
            pos.Add(new NbtDouble(entity.Position.X));
            pos.Add(new NbtDouble(entity.Position.Y + entity.Size.Height)); // TODO: Why do we need to add this
            pos.Add(new NbtDouble(entity.Position.Z));
            data.Add(pos);

            var rotation = new NbtList("Rotation", NbtTagType.Float);
            rotation.Add(new NbtFloat(entity.Yaw));
            rotation.Add(new NbtFloat(entity.Pitch));
            data.Add(rotation);

            data.Add(new NbtCompound("abilities"));

            file.RootTag = data;
            if (!Directory.Exists(Path.Combine(LevelDirectory, "players")))
                Directory.CreateDirectory(Path.Combine(LevelDirectory, "players"));
            using (Stream stream = File.Open(Path.Combine(LevelDirectory, "players", entity.Username + ".dat"), FileMode.OpenOrCreate))
                file.SaveToStream(stream, NbtCompression.GZip);
        }
Example #3
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 #4
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();
        }