// Writes Chunk c's state metadata into given buffer // and returns the amount of bytes written public static int CompressMetadataState(Chunk c, byte[] buffer) { List <ushort> palleteList = Compression.GetPallete(Pallete.METADATA); int bytes; NativeArray <int> writtenBytes = new NativeArray <int>(new int[1] { 0 }, Allocator.TempJob); NativeArray <ushort> chunkData = new NativeArray <ushort>(c.metadata.GetStateData(), Allocator.TempJob); NativeArray <byte> buff = new NativeArray <byte>(buffer, Allocator.TempJob); NativeArray <ushort> palleteArray = new NativeArray <ushort>(palleteList.ToArray(), Allocator.TempJob); CompressionJob cmdJob = new CompressionJob { chunkData = chunkData, buffer = buff, palleteArray = palleteArray, writtenBytes = writtenBytes }; JobHandle handle = cmdJob.Schedule(); handle.Complete(); // NativeArray to Array convertion buff.CopyTo(buffer); bytes = writtenBytes[0]; chunkData.Dispose(); palleteArray.Dispose(); buff.Dispose(); writtenBytes.Dispose(); return(bytes); }
// Writes Chunk c's HP metadata into given buffer // and returns the amount of bytes written public static int CompressMetadataHP(Chunk c, byte[] buffer, int targetPos = 0) { List <ushort> palleteList = Compression.GetPallete(Pallete.METADATA); int bytes; NativeArray <int> writtenBytes = new NativeArray <int>(new int[1] { 0 }, Allocator.TempJob); NativeArray <ushort> chunkData = NativeTools.CopyToNative(c.metadata.GetHPData()); NativeArray <byte> buff = NativeTools.CopyToNative(buffer); NativeArray <ushort> palleteArray = NativeTools.CopyToNative(palleteList.ToArray()); CompressionJob cmdJob = new CompressionJob { chunkData = chunkData, buffer = buff, palleteArray = palleteArray, writtenBytes = writtenBytes }; JobHandle handle = cmdJob.Schedule(); handle.Complete(); NativeArray <byte> .Copy(buff, 0, buffer, targetPos, writtenBytes[0]); bytes = writtenBytes[0]; chunkData.Dispose(); palleteArray.Dispose(); buff.Dispose(); writtenBytes.Dispose(); return(bytes); }
// Writes Chunk c's data using a Pallete's compression into given buffer // and returns the amount of bytes written public static int CompressBlocks(Chunk c, byte[] buffer) { int bytes; Pallete p = Compression.BiomeToPallete(c.biomeName); List <ushort> palleteList = Compression.GetPallete(p); NativeArray <int> writtenBytes = new NativeArray <int>(new int[1] { 0 }, Allocator.TempJob); NativeArray <ushort> chunkData = new NativeArray <ushort>(c.data.GetData(), Allocator.TempJob); NativeArray <byte> buff = new NativeArray <byte>(buffer, Allocator.TempJob); NativeArray <ushort> palleteArray = new NativeArray <ushort>(palleteList.ToArray(), Allocator.TempJob); CompressionJob cbJob = new CompressionJob { chunkData = chunkData, buffer = buff, palleteArray = palleteArray, writtenBytes = writtenBytes }; JobHandle handle = cbJob.Schedule(); handle.Complete(); // NativeArray to Array convertion buff.CopyTo(buffer); bytes = writtenBytes[0]; chunkData.Dispose(); palleteArray.Dispose(); buff.Dispose(); writtenBytes.Dispose(); return(bytes); }