public void Save(ChunkData chunk) { var nbt = new NbtFile { RootTag = new NbtCompound("", new NbtTag[] { new NbtCompound("Level", new NbtTag[] { new NbtByteArray("Blocks", chunk.Blocks), new NbtByteArray("BlockLight", chunk.BlockLight.Data), new NbtByteArray("SkyLight", chunk.SkyLight.Data), new NbtByteArray("Data", chunk.MetaData.Data), new NbtByteArray("HeightMap", chunk.Heightmap), new NbtInt("xPos", chunk.X), new NbtInt("yPos", chunk.Y), }) }) }; var path = GetChunkPath(chunk.X, chunk.Y); Directory.CreateDirectory(Path.GetDirectoryName(path)); nbt.SaveFile(path, true); }
protected void SaveChunk(int x, int y, NbtFile file) { var realPath = GetChunkPath(x, y); file.SaveFile(realPath, true); }
public byte[] GetChunk(int x, int y) { NbtFile file = new NbtFile(); NbtCompound c = new NbtCompound("__ROOT__"); c.Tags.Add(new NbtInt("x", x)); c.Tags.Add(new NbtInt("y", y)); c.Tags.Add(new NbtInt("z", 0)); c.Tags.Add(new NbtByteArray("c", GetChunkData(x,y))); file.RootTag = c; byte[] endresult; using (MemoryStream ms = new MemoryStream()) { file.SaveFile(ms,true); endresult= ms.ToArray(); } return endresult; }
public void SaveToFile(string file) { using(NbtFile rdr = new NbtFile()) { rdr.RootTag=new NbtCompound("Region"); NbtCompound cMats = new NbtCompound("VoxMaterials"); rdr.RootTag.Tags.Add(mMaterials.ToNBT()); rdr.RootTag.Tags.Add(new NbtByteArray("Voxels",ToBytes())); rdr.SaveFile(file); } }
public void SaveChunk(Chunk cnk) { NbtFile c = new NbtFile(cnk.Filename); // TODO: PosY -> PosZ c.RootTag = NewNBTChunk(cnk.Position.X, cnk.Position.Y); NbtCompound Level = (NbtCompound)c.RootTag["Level"]; // BLOCKS ///////////////////////////////////////////////////// byte[] blocks = new byte[ChunkX * ChunkY * ChunkZ]; for (int X = 0; X < ChunkX; X++) { for (int Y = 0; Y < ChunkY; Y++) { for (int Z = 0; Z < ChunkZ; Z++) { blocks[GetBlockIndex(X, Y, Z)] = cnk.Blocks[X, Y, Z]; } } } Level.Tags.Add(new NbtByteArray("Blocks", blocks)); blocks = null; // LIGHTING /////////////////////////////////////////////////// // TODO: Whatever is going on in here is crashing Minecraft now. byte[] lighting = CompressLightmap(cnk.SkyLight); Level.Tags.Add(new NbtByteArray("SkyLight", lighting)); lighting = CompressLightmap(cnk.BlockLight); Level.Tags.Add(new NbtByteArray("BlockLight", lighting)); lighting = CompressDatamap(cnk.Data); Level.Tags.Add(new NbtByteArray("Data", lighting)); // HEIGHTMAP (Needed for lighting). byte[] hm = new byte[256]; for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { hm[y + (x * 16)] = (byte)cnk.HeightMap[x, y]; } } NbtList ents = new NbtList("Entities"); // ENTITIES /////////////////////////////////////////////////// foreach (KeyValuePair<Guid, Entity> ent in cnk.Entities) { ents.Tags.Add(ent.Value.ToNBT()); } Level.Tags.Add(ents); NbtList tents = new NbtList("TileEntities"); // TILE ENTITIES ////////////////////////////////////////////// foreach (KeyValuePair<Guid, TileEntity> tent in cnk.TileEntities) { ents.Tags.Add(tent.Value.ToNBT()); } Level.Tags.Add(tents); c.RootTag.Set("Level",Level); c.SaveFile(cnk.Filename); // For debuggan File.WriteAllText(cnk.Filename + ".txt", c.RootTag.ToString()); }
public void RemoveTileEntity(TileEntity e) { long CX = e.Pos.X / 16; long CY = e.Pos.Y / 16; string f = GetChunkFilename((int)CX, (int)CY); try { mChunk = new NbtFile(f); mChunk.LoadFile(); NbtCompound level = (NbtCompound)mChunk.RootTag["Level"]; NbtList TileEntities = (NbtList)level["TileEntities"]; int found = -1; for (int i = 0; i < TileEntities.Tags.Count; i++) { TileEntity te = new TileEntity((NbtCompound)TileEntities[i]); if (te.Pos == e.Pos) { found = i; } } if (found > -1) TileEntities.Tags.RemoveAt(found); level["TileEntities"] = TileEntities; mChunk.RootTag["Level"] = level; mChunk.SaveFile(f); } catch (Exception) { } }
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.Tags.Add(data); using (var stream = File.Open(Path.Combine(LevelDirectory, "level.dat"), FileMode.Create)) file.SaveFile(stream, true); World.Save(); }
public static void Save() { File.WriteAllLines(".luf", LastUsedFiles.ToArray()); NbtFile f = new NbtFile(); f.RootTag.Add("GridLines",new NbtByte("GridLines", (byte) (ShowGridLines ? 0x01 : 0x00))); f.RootTag.Add("ShowChunks",new NbtByte("ShowChunks", (byte) (ShowChunks ? 0x01 : 0x00))); f.RootTag.Add("ShowMapIcons",new NbtByte("ShowMapIcons", (byte) (ShowMapIcons ? 0x01 : 0x00))); f.RootTag.Add("ShowWaterDepth",new NbtByte("ShowWaterDepth", (byte) (ShowWaterDepth ? 0x01 : 0x00))); f.SaveFile(".settings"); f.Dispose(); }
public void SavePlayer(PlayerEntity entity) { // TODO: Generalize to all mobs NbtFile file = new NbtFile(); var data = new NbtCompound(); data.Tags.Add(new NbtByte("OnGround", (byte)(entity.OnGround ? 1 : 0))); data.Tags.Add(new NbtShort("Air", entity.Air)); data.Tags.Add(new NbtShort("Health", entity.Health)); data.Tags.Add(new NbtInt("Dimension", 0)); // TODO data.Tags.Add(new NbtInt("foodLevel", entity.Food)); data.Tags.Add(new NbtInt("XpLevel", entity.XpLevel)); data.Tags.Add(new NbtInt("XpTotal", entity.XpTotal)); data.Tags.Add(new NbtFloat("foodExhaustionLevel", entity.FoodExhaustion)); data.Tags.Add(new NbtFloat("foodSaturationLevel", entity.FoodSaturation)); data.Tags.Add(new NbtFloat("XpP", entity.XpProgress)); data.Tags.Add(new NbtList("Equipment")); var inventory = new NbtList("Inventory"); for (int index = 0; index < entity.Inventory.Length; index++) { var slot = entity.Inventory[index]; if (slot.Empty) continue; slot.Index = NetworkSlotToDataSlot(index); inventory.Tags.Add(slot.ToNbt()); } data.Tags.Add(inventory); var motion = new NbtList("Motion"); motion.Tags.Add(new NbtDouble(entity.Velocity.X)); motion.Tags.Add(new NbtDouble(entity.Velocity.Y)); motion.Tags.Add(new NbtDouble(entity.Velocity.Z)); data.Tags.Add(motion); var pos = new NbtList("Pos"); pos.Tags.Add(new NbtDouble(entity.Position.X)); pos.Tags.Add(new NbtDouble(entity.Position.Y)); pos.Tags.Add(new NbtDouble(entity.Position.Z)); data.Tags.Add(pos); var rotation = new NbtList("Rotation"); rotation.Tags.Add(new NbtFloat(entity.Yaw)); rotation.Tags.Add(new NbtFloat(entity.Pitch)); data.Tags.Add(rotation); data.Tags.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.SaveFile(stream, true); }
public void Save() { NbtFile file = new NbtFile(); NbtCompound data = new NbtCompound("Data"); data.Tags.Add(new NbtByte("raining", (byte)(Raining ? 1 : 0))); data.Tags.Add(new NbtInt("generatorVersion", 0)); // TODO data.Tags.Add(new NbtLong("Time", Time)); data.Tags.Add(new NbtInt("GameType", (int)GameMode)); data.Tags.Add(new NbtByte("MapFeatures", (byte)(MapFeatures ? 1 : 0))); // TODO: Move to world generator data.Tags.Add(new NbtString("generatorName", WorldGenerator.GeneratorName)); data.Tags.Add(new NbtByte("initialized", 1)); data.Tags.Add(new NbtByte("hardcore", 0)); // TODO data.Tags.Add(new NbtLong("RandomSeed", Seed)); data.Tags.Add(new NbtInt("SpawnX", (int)SpawnPoint.X)); data.Tags.Add(new NbtInt("SpawnY", (int)SpawnPoint.Y)); data.Tags.Add(new NbtInt("SpawnZ", (int)SpawnPoint.Z)); data.Tags.Add(new NbtLong("SizeOnDisk", 0)); data.Tags.Add(new NbtInt("thunderTime", ThunderTime)); data.Tags.Add(new NbtInt("rainTime", RainTime)); data.Tags.Add(new NbtInt("version", 19133)); data.Tags.Add(new NbtByte("thundering", (byte)(Thundering ? 1 : 0))); data.Tags.Add(new NbtString("LevelName", Name)); data.Tags.Add(new NbtLong("LastPlayed", DateTime.UtcNow.Ticks)); file.RootTag = new NbtCompound(); file.RootTag.Tags.Add(data); using (var stream = File.Open(Path.Combine(LevelDirectory, "level.dat"), FileMode.Create)) file.SaveFile(stream, true); World.Save(); }
public override void SaveChunk(Chunk cnk) { NbtFile c = new NbtFile(cnk.Filename); c.RootTag = NewNBTChunk(cnk.Position.X, cnk.Position.Z); NbtCompound Level = (NbtCompound)c.RootTag["Level"]; // BLOCKS ///////////////////////////////////////////////////// byte[] blocks = new byte[ChunkX * ChunkZ * ChunkY]; for (int x = 0; x < ChunkX; x++) { for (int y = 0; y < ChunkY; y++) { for (int z = 0; z < ChunkZ; z++) { blocks[GetBlockIndex(x, y, z)] = cnk.Blocks[x, y, z]; } } } Level.Set("xPos", new NbtInt("xPos", (int)cnk.Position.X)); Level.Set("zPos", new NbtInt("zPos", (int)cnk.Position.Z)); Level.Set("Blocks", new NbtByteArray("Blocks", blocks)); blocks = null; // LIGHTING /////////////////////////////////////////////////// byte[] lighting = CompressLightmap(cnk.SkyLight,true); Level.Set("SkyLight",new NbtByteArray("SkyLight", lighting)); lighting = CompressLightmap(cnk.BlockLight,false); Level.Set("BlockLight",new NbtByteArray("BlockLight", lighting)); lighting = CompressDatamap(cnk.Data); Level.Set("Data", new NbtByteArray("Data", lighting)); // HEIGHTMAP (Needed for lighting). byte[] hm = new byte[256]; for (int x = 0; x < ChunkX; x++) { for (int z = 0; z < ChunkZ; z++) { hm[z << 4 | x] = (byte)(cnk.HeightMap[x, z] & 0xff); // idk } } Level.Set("HeightMap", new NbtByteArray("HeightMap", hm)); NbtList ents = new NbtList("Entities"); // ENTITIES /////////////////////////////////////////////////// foreach (KeyValuePair<Guid, Entity> ent in cnk.Entities) { ents.Add(ent.Value.ToNBT()); } Level.Set("Entities",ents); NbtList tents = new NbtList("TileEntities"); // TILE ENTITIES ////////////////////////////////////////////// foreach (KeyValuePair<Guid, TileEntity> tent in cnk.TileEntities) { tents.Add(tent.Value.ToNBT()); } Level.Set("TileEntities",tents); // Tick when last saved. But ticks are process associated... Level.Set("LastUpdate", new NbtLong("LastUpdate", (long)Utils.UnixTimestamp())); c.RootTag.Set("Level",Level); c.SaveFile(GetChunkFilename((int)cnk.Position.X,(int)cnk.Position.Z)); // For debuggan File.WriteAllText(cnk.Filename + ".txt", c.RootTag.ToString()); Cache.SaveChunkMetadata(cnk); // This is probably what lags. }
public void RemoveEntity(Entity e) { Guid ID = e.UUID; if (_Entities.ContainsKey(ID)) _Entities.Remove(ID); int CX = (int)e.Pos.X / 16; int CY = (int)e.Pos.Y / 16; e.Pos.X = (int)e.Pos.X % 16; e.Pos.Y = (int)e.Pos.Y % 16; string f = GetChunkFilename(CX, CY); if (!File.Exists(f)) { Console.WriteLine("! {0}", f); return; } try { chunk = new NbtFile(f); chunk.LoadFile(); NbtCompound level = (NbtCompound)chunk.RootTag["Level"]; NbtList ents = (NbtList)level["Entities"]; if (e.OrigPos != null) { NbtCompound dc = null; foreach (NbtCompound c in ents.Tags) { Entity ent = new Entity(c); if (e.Pos == ent.Pos) { dc = c; break; } } if (dc != null) ents.Tags.Remove(dc); } level["Entities"] = ents; chunk.RootTag["Level"] = level; chunk.SaveFile(f); } catch (Exception) { } }
public Chunk NewChunk(long X, long Y) { NbtCompound c = NewNBTChunk(X, Y); NbtFile cf = new NbtFile(GetChunkFilename((int)X, (int)Y)); cf.RootTag = c; cf.SaveFile(GetChunkFilename((int)X, (int)Y)); return GetChunk(X, Y); }
public void SaveChunk(Chunk cnk) { NbtFile c = new NbtFile(cnk.Filename); c.LoadFile(); //Console.WriteLine("Saving "+f); NbtCompound Level = (NbtCompound)c.RootTag["Level"]; //string ci = string.Format("{0},{1}", x, z); Level.Tags.Remove(Level["Blocks"]); // BLOCKS ///////////////////////////////////////////////////// byte[] blocks = new byte[ChunkX * ChunkY * ChunkZ]; for (int X = 0; X < ChunkX; X++) { for (int Y = 0; Y < ChunkY; Y++) { for (int Z = 0; Z < ChunkZ; Z++) { blocks[GetBlockIndex(X, Y, Z)] = cnk.Blocks[X, Y, Z]; } } } Level.Tags.Add(new NbtByteArray("Blocks", blocks)); blocks = null; // LIGHTING /////////////////////////////////////////////////// // TODO: Whatever is going on in here is crashing Minecraft now. byte[] lighting = CompressLightmap(cnk.SkyLight); Level.Tags.Add(new NbtByteArray("SkyLight", lighting)); lighting = CompressLightmap(cnk.BlockLight); Level.Tags.Add(new NbtByteArray("BlockLight", lighting)); lighting = CompressDatamap(cnk.Data); Level.Tags.Add(new NbtByteArray("Data", lighting)); // HEIGHTMAP (Needed for lighting). byte[] hm = new byte[256]; for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { hm[y + (x * 16)] = (byte)cnk.HeightMap[x, y]; } } NbtList ents = new NbtList("Entities"); // ENTITIES /////////////////////////////////////////////////// foreach (KeyValuePair<Guid, Entity> ent in cnk.Entities) { ents.Tags.Add(ent.Value.ToNBT()); } Level.Tags.Add(ents); NbtList tents = new NbtList("TileEntities"); // TILE ENTITIES ////////////////////////////////////////////// foreach (KeyValuePair<Guid, TileEntity> tent in cnk.TileEntities) { ents.Tags.Add(tent.Value.ToNBT()); } Level.Tags.Add(tents); c.RootTag["Level"] = Level; //try //{ c.SaveFile(cnk.Filename); //} //catch (Exception e) //{ // Console.WriteLine(e); //} }
public Chunk NewChunk(long X, long Y) { NbtCompound c = NewNBTChunk(X, Y); string f = GetChunkFilename((int)X, (int)Y); // Create folder first. string d = Path.GetDirectoryName(f); if(!Directory.Exists(d)) Directory.CreateDirectory(d); // Get around crash where Windows doesn't like creating new files in Write mode for some f*****g stupid reason { FileInfo fi = new FileInfo(f); FileStream fs = fi.Create(); fs.Close(); } // Create the damn file. NbtFile cf = new NbtFile(f); cf.RootTag = c; cf.SaveFile(f); cf.Dispose(); // Pray. return GetChunk(X, Y); }
public static bool Set(string input, string file, bool starterKit, bool sunRize) { try { byte[] XOR_KEY = { 108, 111, 108, 32, 101, 97, 115, 116, 101, 114, 32, 101, 103, 103 }; input = input.Trim(); if (!input.StartsWith("[") || !input.EndsWith("]")) return false; input = input.Replace("[", "").Replace("]", ""); byte[] data = Convert.FromBase64String(input); for (int i = 0; i < data.Length; i++) { int mod = i % XOR_KEY.Length; data[i] = (byte)((int)data[i] ^ (int)XOR_KEY[mod]); } int end = 0; long randomSeed = BitConverter.ToInt64(data, 0); end += sizeof(long); long time = BitConverter.ToInt64(data, end); end += sizeof(long); int playerX = BitConverter.ToInt32(data, end); end += sizeof(int); int playerY = BitConverter.ToInt32(data, end); // height end += sizeof(int); int playerZ = BitConverter.ToInt32(data, end); end += sizeof(int); float rotationX = BitConverter.ToSingle(data, end); end += sizeof(float); float rotationY = BitConverter.ToSingle(data, end); NbtFile nbt = new NbtFile(); NbtCompound compound = new NbtCompound(); NbtCompound data_compound = new NbtCompound("Data"); data_compound.Tags.Add(new NbtLong("RandomSeed", randomSeed)); data_compound.Tags.Add(new NbtLong("Time", sunRize ? 1 : time)); data_compound.Tags.Add(new NbtInt("SpawnX", playerX)); data_compound.Tags.Add(new NbtInt("SpawnY", playerY)); data_compound.Tags.Add(new NbtInt("SpawnZ", playerZ)); data_compound.Tags.Add(new NbtLong("LastPlayed", 1289561130810)); data_compound.Tags.Add(new NbtLong("SizeOnDisk", 1000)); NbtCompound player_compound = new NbtCompound("Player"); player_compound.Tags.Add(new NbtByte("OnGround", 1)); player_compound.Tags.Add(new NbtShort("Air", 300)); player_compound.Tags.Add(new NbtShort("AttackTime", 0)); player_compound.Tags.Add(new NbtShort("DeathTime", 0)); player_compound.Tags.Add(new NbtShort("Fire", -20)); player_compound.Tags.Add(new NbtShort("Health", 20)); player_compound.Tags.Add(new NbtShort("HurtTime", 0)); player_compound.Tags.Add(new NbtInt("Dimension", 0)); player_compound.Tags.Add(new NbtInt("Score", 0)); player_compound.Tags.Add(new NbtFloat("FallDistance", 0)); NbtList player_pos = new NbtList("Pos"); player_pos.Tags.Add(new NbtDouble("", (double)playerX + 0.5)); player_pos.Tags.Add(new NbtDouble("", (double)playerY + 0.65)); player_pos.Tags.Add(new NbtDouble("", (double)playerZ + 0.5)); player_compound.Tags.Add(player_pos); NbtList player_rot = new NbtList("Rotation"); player_rot.Tags.Add(new NbtFloat("", rotationX)); player_rot.Tags.Add(new NbtFloat("", rotationY)); player_compound.Tags.Add(player_rot); NbtList inv_compound = new NbtList("Inventory"); if (starterKit) { inv_compound.Tags.Add(GetItemCompound(263, 3, 34, 0)); inv_compound.Tags.Add(GetItemCompound(17, 10, 35, 0)); inv_compound.Tags.Add(GetItemCompound(274, 1, 0, 0)); inv_compound.Tags.Add(GetItemCompound(273, 1, 1, 0)); inv_compound.Tags.Add(GetItemCompound(319, 1, 6, 0)); inv_compound.Tags.Add(GetItemCompound(319, 1, 7, 0)); inv_compound.Tags.Add(GetItemCompound(50, 10, 8, 0)); } player_compound.Tags.Add(inv_compound); NbtList player_mot = new NbtList("Motion"); player_mot.Tags.Add(new NbtDouble("", 0.0)); player_mot.Tags.Add(new NbtDouble("", 0.0)); player_mot.Tags.Add(new NbtDouble("", 0.0)); player_compound.Tags.Add(player_mot); data_compound.Tags.Add(player_compound); nbt.RootTag = compound; nbt.RootTag.Tags.Add(data_compound); Stream stream = File.OpenWrite(file); nbt.SaveFile(stream, true); stream.Close(); stream.Dispose(); return true; } catch(Exception ex) { return false; } }
public void SetTileEntity(TileEntity e) { long CX=e.Pos.X/16; long CY=e.Pos.Y/16; string f = GetChunkFilename((int)CX, (int)CY); try { mChunk = new NbtFile(f); mChunk.LoadFile(); NbtCompound level = (NbtCompound)mChunk.RootTag["Level"]; NbtList tents = (NbtList)level["TileEntities"]; int found = -1; for(int i = 0;i<tents.Tags.Count;i++) { TileEntity te = new TileEntity((NbtCompound)tents[i]); if (te.Pos == e.Pos) { found = i; } } if (found > -1) tents[found] = e.ToNBT(); else tents.Tags.Add(e.ToNBT()); level["TileEntities"] = tents; mChunk.RootTag["Level"] = level; mChunk.SaveFile(f); } catch (Exception) { } }
public override void Save(string Folder) { string f = Path.Combine(Folder, "DefaultMapGenerator.dat"); NbtFile nf = new NbtFile(f); nf.RootTag = new NbtCompound("__ROOT__"); NbtCompound c = new NbtCompound("DefaultMapGenerator"); c.Tags.Add(new NbtByte("GenerateCaves", (byte) (GenerateCaves ? 1 : 0))); c.Tags.Add(new NbtByte("GenerateDungeons", (byte) (GenerateDungeons ? 1 : 0))); c.Tags.Add(new NbtByte("GenerateOres", (byte) (GenerateOres ? 1 : 0))); c.Tags.Add(new NbtByte("GenerateWater", (byte) (GenerateWater ? 1 : 0))); c.Tags.Add(new NbtByte("HellMode", (byte) (HellMode ? 1 : 0))); c.Tags.Add(new NbtByte("GenerateTrees", (byte) (GenerateTrees ? 1 : 0))); c.Tags.Add(new NbtDouble("Frequency", Frequency)); c.Tags.Add(new NbtByte("NoiseQuality", (byte) NoiseQuality)); c.Tags.Add(new NbtInt("OctaveCount", OctaveCount)); c.Tags.Add(new NbtDouble("Lacunarity", Lacunarity)); c.Tags.Add(new NbtDouble("Persistance", Persistance)); c.Tags.Add(new NbtDouble("ContinentNoiseFrequency", ContinentNoiseFrequency)); c.Tags.Add(new NbtDouble("CaveThreshold", CaveThreshold)); nf.RootTag.Tags.Add(c); nf.SaveFile(f); }
private object NBT2Bytes(NbtCompound nbtCompound) { using (MemoryStream ms = new MemoryStream()) { NbtFile f = new NbtFile(); f.RootTag = nbtCompound; f.SaveFile(ms,false); //ms.Position = 0; //byte[] buffer = new byte[ms.Length]; //ms.Read(buffer, 0, buffer.Length); return ms.ToArray(); } }