Ejemplo n.º 1
0
        private static GrtPackge _create(Stream _stream)
        {
            lock ( s_buffer )
            {
                GrtPackge package = new GrtPackge();
                _stream.Seek(-8, SeekOrigin.End);
                _stream.Read(s_buffer, 0, 8);
                Int64 start_idx = BitConverter.ToInt64(s_buffer, 0);

                _stream.Seek(start_idx, SeekOrigin.Begin);

                // 读取描述段
                while (_stream.Position < _stream.Length - 8)
                {
                    GrtFile grt_file = new GrtFile();
                    _stream.Read(s_buffer, 0, 8);
                    grt_file.Size = BitConverter.ToInt64(s_buffer, 0);

                    _stream.Read(s_buffer, 0, 8);
                    grt_file.CompressedSize = BitConverter.ToInt64(s_buffer, 0);

                    _stream.Read(s_buffer, 0, 8);
                    grt_file.Offset = BitConverter.ToInt64(s_buffer, 0);

                    Byte file_name_length = (Byte)_stream.ReadByte();
                    _stream.Read(s_buffer, 0, file_name_length);
                    grt_file.Name = System.Text.Encoding.UTF8.GetString(s_buffer, 0, file_name_length);

                    package.m_files.Add(grt_file.Name, grt_file);
                    Debug.LogFormat("name: {0}, size: {1} compressed_size: {2}, offset: {3}", grt_file.Name, grt_file.Size, grt_file.CompressedSize, grt_file.Offset);
                }
                return(package);
            }
        }
Ejemplo n.º 2
0
        public void UnloadFile(String _name)
        {
            GrtFile grt_file = null;

            if (!m_files.TryGetValue(_name, out grt_file))
            {
                return;
            }
            grt_file.Bytes = null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 加载一个文件,并且缓存
        /// </summary>
        /// <param name="_name"></param>
        /// <returns></returns>
        public Byte[] LoadFile(String _name)
        {
            try
            {
                Int32   count    = m_files.Count;
                GrtFile grt_file = null;
                if (!m_files.TryGetValue(_name, out grt_file))
                {
                    Debug.LogErrorFormat("[GrtPackage.LoadFile]: {0} not in this package!", _name);
                    return(null);
                }

                if (grt_file.Bytes != null)
                {
                    return(grt_file.Bytes);
                }

                // 如果路径为空,表示需要从内存读取
                if (m_package_path == null)
                {
                    using (MemoryStream ms = new MemoryStream(m_bytes))
                    {
                        ms.Seek(grt_file.Offset, SeekOrigin.Begin);
                        _decompress(ms, grt_file.CompressedSize, grt_file.Size, out grt_file.Bytes);
                    }
                }
                else
                {
                    using (FileStream fs = new FileStream(m_package_path, FileMode.Open))
                    {
                        fs.Seek(grt_file.Offset, SeekOrigin.Begin);
                        _decompress(fs, grt_file.CompressedSize, grt_file.Size, out grt_file.Bytes);
                    }
                }
                return(grt_file.Bytes);
            }
            catch (FileNotFoundException)
            {
                Debug.LogErrorFormat("[GrtPackage.LoadFile]: Can't Load file from GrtPackage, package: {0} no longer exist!", m_package_path);
            }
            catch (Exception e)
            {
                Debug.LogErrorFormat("[GrtPackage.LoadFile]: " + e);
            }
            return(null);
        }
Ejemplo n.º 4
0
        public static void Pack(String _src_path, String _output_root, String _name)
        {
            String output = String.Concat(_output_root, "/", _name);

            if (!Directory.Exists(_src_path))
            {
                Log.Error("[GrtFielEditor]: 打包目录: {0} 不存在!", _src_path);
                return;
            }
            try
            {
                DirectoryInfo  di         = new DirectoryInfo(_src_path);
                var            files      = FileHelper.GetAllFileInfo(_src_path);
                List <GrtFile> pack_files = new List <GrtFile>();
                foreach (FileInfo file_info in files)
                {
                    GrtFile grt_file = new GrtFile();
                    grt_file.Bytes = FileHelper.ReadAllBytesFromFile(file_info.FullName);
                    grt_file.Size  = grt_file.Bytes.Length;
                    grt_file.Name  = FileHelper.GetRelativePath(file_info.FullName, di.FullName);
                    pack_files.Add(grt_file);
                }

                FileHelper.CreateDirectoryByFilePath(output);
                using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write))
                {
                    Int64 offset = 0;
                    // 压缩,写入文件
                    foreach (GrtFile pack_file_info in pack_files)
                    {
                        Byte[] compressed_bytes;
                        pack_file_info.Offset = offset;
                        _compress(pack_file_info.Bytes, out compressed_bytes, out pack_file_info.CompressedSize);
                        fs.Write(compressed_bytes, 0, (Int32)pack_file_info.CompressedSize);
                        offset += pack_file_info.CompressedSize;
                    }

                    //offset = fs.Length; // 描述段开始的位置

                    Byte[] bytes;
                    // 写入文件列表
                    foreach (GrtFile pack_file_info in pack_files)
                    {
                        // 压缩前的size
                        bytes = BitConverter.GetBytes(pack_file_info.Size);
                        if (!BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(bytes);
                        }
                        fs.Write(bytes, 0, bytes.Length);

                        // 压缩后的size
                        bytes = BitConverter.GetBytes(pack_file_info.CompressedSize);
                        if (!BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(bytes);
                        }
                        fs.Write(bytes, 0, bytes.Length);

                        // 写入偏移值
                        bytes = BitConverter.GetBytes(pack_file_info.Offset);
                        if (!BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(bytes);
                        }
                        fs.Write(bytes, 0, bytes.Length);

                        // 名字
                        bytes = System.Text.Encoding.UTF8.GetBytes(pack_file_info.Name);

                        if (bytes.Length > 255)
                        {
                            throw new Exception("文件名称超过255位,不能打包!");
                        }
                        fs.WriteByte((Byte)bytes.Length);                           // 名字长度最大不超过256个字节
                        fs.Write(bytes, 0, bytes.Length);
                    }

                    bytes = BitConverter.GetBytes(offset);
                    if (!BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(bytes);
                    }

                    fs.Write(bytes, 0, bytes.Length);
                    fs.Flush();

                    StringBuilder sb = new StringBuilder(1024);
                    sb.AppendFormat("GrtPackage打包成功: {0}\n", _name);
                    foreach (GrtFile pack_file_info in pack_files)
                    {
                        sb.AppendLine(pack_file_info.Name);
                    }
                    //Debug.Log( sb );
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }