Exemple #1
0
        //压缩一个chunk的数据成一个字符串,并返回
        public static string CompressData(Chunk chunk)
        {
            StringWriter writer = new StringWriter();

            int i      = 0;
            int length = chunk.GetDataLength(); // 长度

            ushort currentCount = 0;            //连续的同类voxel数目
            ushort currentData  = 0;            // 当前voxel的数据

            //对每一个voxel
            for (i = 0; i < length; i++)
            {
                //读取行数据
                ushort thisData = chunk.GetVoxelSimple(i);
                //如果这个数据和前一个数据不一样,写入上一个数据,然后搞一个新的去写入this数据
                if (thisData != currentData)
                {
                    // 写如current,也就是上一个
                    if (i != 0)
                    {
                        writer.Write((char)currentCount);
                        writer.Write((char)currentData);
                    }
                    // 新的block
                    currentCount = 1;
                    currentData  = thisData;
                }
                //如果this数据和上一个current一样,那就自增currentcount。
                else
                {
                    currentCount++;
                }

                //在最后一次循环中关闭并写入current
                if (i == length - 1)
                {
                    writer.Write((char)currentCount);
                    writer.Write((char)currentData);
                }
            }

            string compressedData = writer.ToString();

            writer.Flush();
            writer.Close();
            return(compressedData);
        }
Exemple #2
0
        //解压voxel数据,载入到voxeldata数组中
        public static void DecompressData(Chunk chunk, string data)
        {
            //chunk是否为空
            if (data.Length == 2 && data[1] == (char)0)
            {
                chunk.Empty = true;
            }

            StringReader reader = new StringReader(data);

            int i      = 0;
            int length = chunk.GetDataLength();             //voxeldata的长度

            try
            {
                //完全填充voxeldata数组后,停止循环
                while (i < length)
                {
                    ushort currentCount = (ushort)reader.Read();
                    ushort currentData  = (ushort)reader.Read();                    // read the data

                    int ii = 0;

                    //对每一个currentcount,写入一个voxel
                    while (ii < currentCount)
                    {
                        chunk.SetVoxelSimple(i, (ushort)currentData);
                        ii++;
                        i++;
                    }
                }
            }
            catch (System.Exception)
            {
                Debug.LogError("CubeMaster: 数据损坏 chunk: " + chunk.ChunkIndex.ToString() + ".");
                reader.Close();
                return;
            }

            reader.Close();
        }