Пример #1
0
        public static string CompressData(Chunk chunk)
        {
            // returns the data of chunk in compressed string format
            StringWriter writer = new StringWriter();

            int i      = 0;
            int length = chunk.GetDataLength(); // length of VoxelData array

            ushort currentCount = 0;            // count of consecutive voxels of the same type
            ushort currentData  = 0;            // data of the current voxel

            for (i = 0; i < length; i++)
            {
                // for each voxel
                ushort thisData = chunk.GetVoxelSimple(i); // read raw data at i

                if (thisData != currentData)
                {
                    // if the data is different from the previous data, write the last block and start a new one
                    // write previous block
                    if (i != 0)
                    { // (don't write in the first loop iteration, because count would be 0 (no previous blocks))
                        writer.Write((char)currentCount);
                        writer.Write((char)currentData);
                    }

                    // start new block
                    currentCount = 1;
                    currentData  = thisData;
                }
                else
                {
                    // if the data is the same as the last data, simply add to the count
                    currentCount++;
                }

                if (i == length - 1)
                {
                    // if this is the last iteration of the loop, close and write the current block
                    writer.Write((char)currentCount);
                    writer.Write((char)currentData);
                }
            }

            string compressedData = writer.ToString();

            writer.Flush();
            writer.Close();

            return(compressedData);
        }
Пример #2
0
        public static Dictionary<string, string> TempChunkData; // stores chunk's data to write into a region file later

        #endregion Fields

        #region Methods

        public static string CompressData( Chunk chunk )
        {
            // returns the data of chunk in compressed string format

            StringWriter writer = new StringWriter();

            int i = 0;
            int length = chunk.GetDataLength(); // length of VoxelData array

            ushort currentCount = 0; // count of consecutive voxels of the same type
            ushort currentData = 0; // data of the current voxel

            for (i=0; i<length; i++) { // for each voxel

            ushort thisData = chunk.GetVoxelSimple(i); // read raw data at i

            if (thisData != currentData) { // if the data is different from the previous data, write the last block and start a new one

                // write previous block
                if (i != 0) { // (don't write in the first loop iteration, because count would be 0 (no previous blocks))
                    writer.Write ((char)currentCount);
                    writer.Write ((char)currentData);
                }
                // start new block
                currentCount = 1;
                currentData = thisData;
            }

            else { // if the data is the same as the last data, simply add to the count
                currentCount ++;
            }

            if (i == length-1) { // if this is the last iteration of the loop, close and write the current block
                writer.Write ((char)currentCount);
                writer.Write ((char)currentData);
            }

            }

            string compressedData = writer.ToString();
            writer.Flush ();
            writer.Close ();
            return compressedData;
        }
Пример #3
0
        public static void DecompressData(Chunk chunk, string data)
        {
            // decompresses voxel data and loads it into the VoxelData array
            // check if chunk is empty
            if (data.Length == 2 && data[1] == (char)0)
            {
                chunk.empty = true;
            }

            StringReader reader = new StringReader(data);

            int i      = 0;
            int length = chunk.GetDataLength(); // length of VoxelData array

            try
            {
                while (i < length)
                {
                    // this loop will stop once the VoxelData array has been populated completely. Iterates once per count-data block.
                    ushort currentCount = (ushort)reader.Read(); // read the count
                    ushort currentData  = (ushort)reader.Read(); // read the data

                    int ii = 0;

                    while (ii < currentCount)
                    {
                        chunk.SetVoxelSimple(i, (ushort)currentData);// write a single voxel for every currentCount
                        ii++;
                        i++;
                    }
                }
            }
            catch (System.Exception)
            {
                Debug.LogError("Uniblocks: Corrupt chunk data for chunk: " + chunk.chunkIndex.ToString() + ". Has the data been saved using a different chunk size?");
                reader.Close();
                return;
            }

            reader.Close();
        }
Пример #4
0
        public static void DecompressData( Chunk chunk, string data )
        {
            // decompresses voxel data and loads it into the VoxelData array

            // check if chunk is empty
            if (data.Length == 2 && data[1] == (char)0) {
            chunk.Empty = true;
            }

            StringReader reader = new StringReader (data);

            int i = 0;
            int length = chunk.GetDataLength(); // length of VoxelData array

            try {
            while ( i < length ) { // this loop will stop once the VoxelData array has been populated completely. Iterates once per count-data block.

            ushort currentCount =  (ushort) reader.Read(); // read the count
            ushort currentData = (ushort) reader.Read(); // read the data

            int ii = 0;

            while ( ii < currentCount ) {
                chunk.SetVoxelSimple (i, (ushort)currentData);// write a single voxel for every currentCount
                ii ++;
                i ++;
            }
            }
            }
            catch (System.Exception) {
            Debug.LogError ("Uniblocks: Corrupt chunk data for chunk: " + chunk.ChunkIndex.ToString() + ". Has the data been saved using a different chunk size?");
            reader.Close();
            return;
            }

            reader.Close();
        }