public override ChunkColumn LoadChunk(int x, int z) { var u = FileCompression.Decompress(File.ReadAllBytes(_folder + "/" + x + "." + z + ".cfile")); var reader = new DataBuffer(u); var blockLength = reader.ReadInt(); var block = reader.ReadUShortLocal(blockLength); var metalength = reader.ReadInt(); var blockmeta = reader.ReadUShortLocal(metalength); var skyLength = reader.ReadInt(); var skylight = reader.Read(skyLength); var lightLength = reader.ReadInt(); var blocklight = reader.Read(lightLength); var biomeIdLength = reader.ReadInt(); var biomeId = reader.Read(biomeIdLength); var cc = new ChunkColumn { Blocks = block, Metadata = blockmeta, Blocklight = { Data = blocklight }, Skylight = { Data = skylight }, BiomeId = biomeId, X = x, Z = z }; Debug.WriteLine("We should have loaded " + x + ", " + z); return(cc); }
public void UnCompressTest() { string tmpPath = PrepareTestFiles("res-files-uncompression-test"); List <FileInfo> files = Directory.GetFiles(tmpPath).Select(f => new FileInfo(f)).ToList(); byte[] data = FileCompression.CompressFile(files, 9, false); Assert.IsNotNull(data); Assert.IsTrue(data.Length > 0); IDictionary <string, byte[]> decompressedFiles = FileCompression.Decompress(data); Assert.IsNotNull(decompressedFiles); Assert.IsTrue(decompressedFiles.Count == 2); Assert.IsTrue(decompressedFiles.ContainsKey("1.txt")); Assert.IsTrue(decompressedFiles.ContainsKey("2.txt")); char[] content1 = new string('*', 1024).ToCharArray(); content1[0] = 'z'; content1[content1.Length - 1] = 'z'; char[] content2 = new string('*', 1024).ToCharArray(); content2[0] = 'y'; content2[content2.Length - 1] = 'y'; Assert.IsTrue(Encoding.UTF8.GetString(decompressedFiles["1.txt"]) == new string(content1)); Assert.IsTrue(Encoding.UTF8.GetString(decompressedFiles["2.txt"]) == new string(content2)); Directory.Delete(tmpPath, true); }
public void LoadPlayer() { string savename = Server.ServerSettings.OnlineMode ? Uuid : Username; if (File.Exists("Players/" + savename + ".pdata")) { byte[] data = File.ReadAllBytes("Players/" + savename + ".pdata"); data = FileCompression.Decompress(data); DataBuffer reader = new DataBuffer(data); double x = reader.ReadDouble(); double y = reader.ReadDouble(); double z = reader.ReadDouble(); float yaw = reader.ReadFloat(); float pitch = reader.ReadFloat(); bool onGround = reader.ReadBool(); ConsoleFunctions.WriteInfoLine("Position Loaded... (X: " + x + " Y: " + y + " Z: " + z + " Yaw: " + yaw + " Pitch: " + pitch + " OnGround: " + onGround + ")"); KnownPosition = new PlayerLocation(x, y, z) { Yaw = yaw, Pitch = pitch, OnGround = onGround }; Gamemode = (Gamemode)reader.ReadVarInt(); int healthLength = reader.ReadVarInt(); byte[] healthData = reader.Read(healthLength); int inventoryLength = reader.ReadVarInt(); byte[] inventoryData = reader.Read(inventoryLength); HealthManager.Import(healthData); Inventory.Import(inventoryData); IsOperator = reader.ReadBool(); } else { KnownPosition = Level.GetSpawnPoint(); } Loaded = true; }
/// <summary> /// 将一个目录下的PP资源包进行解包 /// </summary> /// <param name="kppDestinationFilePath">KPP资源文件路径</param> /// <exception cref="ArgumentNullException">参数不能为空</exception> /// <exception cref="System.IO.FileNotFoundException">目标文件不存在</exception> /// <exception cref="BadImageFormatException">错误的KPP资源包格式</exception> /// <exception cref="UnSupportedSectionTypeException">不支持的数据节类型</exception> public static KPPDataStructure UnPack(string kppDestinationFilePath) { if (string.IsNullOrEmpty(kppDestinationFilePath)) { throw new ArgumentNullException("kppDestinationFilePath"); } if (!File.Exists(kppDestinationFilePath)) { throw new System.IO.FileNotFoundException("#Current file cannot be find, file: " + kppDestinationFilePath); } byte[] data = File.ReadAllBytes(kppDestinationFilePath); using (MemoryStream stream = new MemoryStream(data)) { KPPDataHead head = new KPPDataHead(); head.UnPack(stream); ushort sectionCount = head.GetField <ushort>("SectionCount"); Dictionary <byte, IKPPDataResource> sections = new Dictionary <byte, IKPPDataResource>(); for (int i = 0; i < sectionCount; i++) { byte sectionId = (byte)stream.ReadByte(); //reset position. stream.Position = stream.Position - 1; Type sectionType; if (!_sections.TryGetValue(sectionId, out sectionType)) { throw new UnSupportedSectionTypeException("#Current data section type cannot be supported. #id: " + sectionId); } IKPPDataResource section = (IKPPDataResource)sectionType.GetTypeInfo().Assembly.CreateInstance(sectionType.FullName); section.UnPack(stream); sections.Add(sectionId, section); } byte[] zipData = new byte[data.Length - stream.Position]; Buffer.BlockCopy(data, (int)stream.Position, zipData, 0, zipData.Length); Crc32 crc = new Crc32(); crc.Reset(); crc.Update(zipData); if (head.GetField <long>("CRC") != crc.Value) { throw new BadImageFormatException("#Bad image format of zipped stream CRC."); } IDictionary <string, byte[]> files = FileCompression.Decompress(zipData); return(new KPPDataStructure(head, sections, files)); } }