コード例 #1
0
    public static bool WriteFile(string filePath, byte[] data, int offset, int length)
    {
        FileStream fileStream = null;
        int        num        = 0;
        bool       result;

        while (true)
        {
            try
            {
                fileStream = new FileStream(filePath, 4, 2, 3);
                fileStream.Write(data, offset, length);
                fileStream.Close();
                result = true;
                break;
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                num++;
                if (num >= 3)
                {
                    Debug.Log("Write File " + filePath + " Error! Exception = " + ex.ToString());
                    CFileManager.DeleteFile(filePath);
                    CFileManager.s_delegateOnOperateFileFail(filePath, enFileOperation.WriteFile, ex);
                    result = false;
                    break;
                }
            }
        }
        return(result);
    }
コード例 #2
0
    public static bool WriteFile(string filePath, byte[] data)
    {
        int  num = 0;
        bool result;

        while (true)
        {
            try
            {
                File.WriteAllBytes(filePath, data);
                result = true;
                break;
            }
            catch (Exception ex)
            {
                num++;
                if (num >= 3)
                {
                    Debug.Log("Write File " + filePath + " Error! Exception = " + ex.ToString());
                    CFileManager.DeleteFile(filePath);
                    CFileManager.s_delegateOnOperateFileFail(filePath, enFileOperation.WriteFile, ex);
                    result = false;
                    break;
                }
            }
        }
        return(result);
    }
コード例 #3
0
    public void RemoveFile(string key)
    {
        string cachedFileFullPath = CFileManager.CombinePath(m_dir, key + m_fileExtension);

        if (CFileManager.IsFileExist(cachedFileFullPath))
        {
            CFileManager.DeleteFile(cachedFileFullPath);
        }

        m_cachedFileInfoSet.RemoveFileInfo(key);
    }
コード例 #4
0
        public void AddCachedTexture(string url, int width, int height, bool isGif, byte[] data)
        {
            string key = CFileManager.GetMd5(url.ToLower());

            if (this.m_cachedTextureInfoSet.m_cachedTextureInfoMap.ContainsKey(key))
            {
                CCachedTextureInfo info = null;
                this.m_cachedTextureInfoSet.m_cachedTextureInfoMap.TryGetValue(key, out info);
                DebugHelper.Assert(this.m_cachedTextureInfoSet.m_cachedTextureInfos.Contains(info), "zen me ke neng?");
                info.m_width          = width;
                info.m_height         = height;
                info.m_lastModifyTime = DateTime.Now;
                info.m_isGif          = isGif;
            }
            else
            {
                if (this.m_cachedTextureInfoSet.m_cachedTextureInfos.Count >= 100)
                {
                    string str2 = this.m_cachedTextureInfoSet.RemoveEarliestTextureInfo();
                    if (!string.IsNullOrEmpty(str2))
                    {
                        string str3 = CFileManager.CombinePath(s_cachedTextureDirectory, str2 + ".bytes");
                        if (CFileManager.IsFileExist(str3))
                        {
                            CFileManager.DeleteFile(str3);
                        }
                    }
                }
                CCachedTextureInfo cachedTextureInfo = new CCachedTextureInfo {
                    m_key            = key,
                    m_width          = width,
                    m_height         = height,
                    m_lastModifyTime = DateTime.Now,
                    m_isGif          = isGif
                };
                this.m_cachedTextureInfoSet.AddTextureInfo(key, cachedTextureInfo);
            }
            this.m_cachedTextureInfoSet.SortTextureInfo();
            int offset = 0;

            this.m_cachedTextureInfoSet.Write(s_buffer, ref offset);
            if (CFileManager.IsFileExist(s_cachedTextureInfoSetFileFullPath))
            {
                CFileManager.DeleteFile(s_cachedTextureInfoSetFileFullPath);
            }
            CFileManager.WriteFile(s_cachedTextureInfoSetFileFullPath, s_buffer, 0, offset);
            string filePath = CFileManager.CombinePath(s_cachedTextureDirectory, key + ".bytes");

            if (CFileManager.IsFileExist(filePath))
            {
                CFileManager.DeleteFile(filePath);
            }
            CFileManager.WriteFile(filePath, data);
        }
コード例 #5
0
        public void AddCachedTexture(string url, int width, int height, bool isGif, byte[] data)
        {
            string md = CFileManager.GetMd5(url.ToLower());

            if (this.m_cachedTextureInfoSet.m_cachedTextureInfoMap.ContainsKey(md))
            {
                CCachedTextureInfo cCachedTextureInfo = null;
                this.m_cachedTextureInfoSet.m_cachedTextureInfoMap.TryGetValue(md, ref cCachedTextureInfo);
                DebugHelper.Assert(this.m_cachedTextureInfoSet.m_cachedTextureInfos.Contains(cCachedTextureInfo), "zen me ke neng?");
                cCachedTextureInfo.m_width          = width;
                cCachedTextureInfo.m_height         = height;
                cCachedTextureInfo.m_lastModifyTime = DateTime.get_Now();
                cCachedTextureInfo.m_isGif          = isGif;
            }
            else
            {
                if (this.m_cachedTextureInfoSet.m_cachedTextureInfos.get_Count() >= 100)
                {
                    string text = this.m_cachedTextureInfoSet.RemoveEarliestTextureInfo();
                    if (!string.IsNullOrEmpty(text))
                    {
                        string text2 = CFileManager.CombinePath(CCachedTextureManager.s_cachedTextureDirectory, text + ".bytes");
                        if (CFileManager.IsFileExist(text2))
                        {
                            CFileManager.DeleteFile(text2);
                        }
                    }
                }
                CCachedTextureInfo cCachedTextureInfo2 = new CCachedTextureInfo();
                cCachedTextureInfo2.m_key            = md;
                cCachedTextureInfo2.m_width          = width;
                cCachedTextureInfo2.m_height         = height;
                cCachedTextureInfo2.m_lastModifyTime = DateTime.get_Now();
                cCachedTextureInfo2.m_isGif          = isGif;
                this.m_cachedTextureInfoSet.AddTextureInfo(md, cCachedTextureInfo2);
            }
            this.m_cachedTextureInfoSet.SortTextureInfo();
            int num = 0;

            this.m_cachedTextureInfoSet.Write(CCachedTextureManager.s_buffer, ref num);
            if (CFileManager.IsFileExist(CCachedTextureManager.s_cachedTextureInfoSetFileFullPath))
            {
                CFileManager.DeleteFile(CCachedTextureManager.s_cachedTextureInfoSetFileFullPath);
            }
            CFileManager.WriteFile(CCachedTextureManager.s_cachedTextureInfoSetFileFullPath, CCachedTextureManager.s_buffer, 0, num);
            string text3 = CFileManager.CombinePath(CCachedTextureManager.s_cachedTextureDirectory, md + ".bytes");

            if (CFileManager.IsFileExist(text3))
            {
                CFileManager.DeleteFile(text3);
            }
            CFileManager.WriteFile(text3, data);
        }
コード例 #6
0
    public static bool ClearDirectory(string fullPath, string[] fileExtensionFilter, string[] folderFilter)
    {
        bool result;

        try
        {
            if (fileExtensionFilter != null)
            {
                string[] files = Directory.GetFiles(fullPath);
                for (int i = 0; i < files.Length; i++)
                {
                    if (fileExtensionFilter != null && fileExtensionFilter.Length > 0)
                    {
                        for (int j = 0; j < fileExtensionFilter.Length; j++)
                        {
                            if (files[i].Contains(fileExtensionFilter[j]))
                            {
                                CFileManager.DeleteFile(files[i]);
                                break;
                            }
                        }
                    }
                }
            }
            if (folderFilter != null)
            {
                string[] directories = Directory.GetDirectories(fullPath);
                for (int k = 0; k < directories.Length; k++)
                {
                    if (folderFilter != null && folderFilter.Length > 0)
                    {
                        for (int l = 0; l < folderFilter.Length; l++)
                        {
                            if (directories[k].Contains(folderFilter[l]))
                            {
                                CFileManager.DeleteDirectory(directories[k]);
                                break;
                            }
                        }
                    }
                }
            }
            result = true;
        }
        catch (Exception)
        {
            result = false;
        }
        return(result);
    }
コード例 #7
0
    //--------------------------------------------------
    /// 添加缓存文件
    /// @url
    /// @cacheType
    /// @width
    /// @height
    /// @isGif
    /// @data
    //--------------------------------------------------
    public void AddFile(string url, byte[] data, int tagInt1 = 0, int tagInt2 = 0, bool tagBool = false)
    {
        string key = CFileManager.GetMd5(url.ToLower());
        List <CCachedFileInfo> cachedFileInfoList = m_cachedFileInfoSet.m_cachedFileInfos;

        if (cachedFileInfoList == null)
        {
            return;
        }

        if (m_cachedFileInfoSet.m_cachedFileInfoMap.ContainsKey(key))
        {
            CCachedFileInfo cachedFileInfo = null;
            m_cachedFileInfoSet.m_cachedFileInfoMap.TryGetValue(key, out cachedFileInfo);

            Debug.Assert(cachedFileInfoList != null && cachedFileInfoList.Contains(cachedFileInfo), "zen me ke neng?");

            //修改信息
            cachedFileInfo.m_fileLength     = data.Length;
            cachedFileInfo.m_lastModifyTime = DateTime.Now;
            cachedFileInfo.m_tagInt1        = tagInt1;
            cachedFileInfo.m_tagInt2        = tagInt2;
            cachedFileInfo.m_tagBool        = tagBool;
        }
        else
        {
            //如果数量达到上限,移除排在最前面的文件
            if (cachedFileInfoList.Count >= m_maxCnt)
            {
                string removeKey = m_cachedFileInfoSet.RemoveEarliestFileInfo();

                //删除缓存文件
                if (!string.IsNullOrEmpty(removeKey))
                {
                    string removeCachedFileFullPath = CFileManager.CombinePath(m_dir, removeKey + m_fileExtension);
                    if (CFileManager.IsFileExist(removeCachedFileFullPath))
                    {
                        CFileManager.DeleteFile(removeCachedFileFullPath);
                    }
                }
            }

            CCachedFileInfo cachedFileInfo = new CCachedFileInfo();
            cachedFileInfo.m_key            = key;
            cachedFileInfo.m_fileLength     = data.Length;
            cachedFileInfo.m_lastModifyTime = DateTime.Now;
            cachedFileInfo.m_tagInt1        = tagInt1;
            cachedFileInfo.m_tagInt2        = tagInt2;
            cachedFileInfo.m_tagBool        = tagBool;

            m_cachedFileInfoSet.AddFileInfo(key, cachedFileInfo);
        }

        //排序
        m_cachedFileInfoSet.m_cachedFileInfos.Sort();

        //写入信息文件
        byte[] buffer = CFileManager.LockFileBuffer();
        try
        {
            MakeDirReady();
            int offset = 0;
            m_cachedFileInfoSet.Write(buffer, ref offset);

            if (CFileManager.IsFileExist(m_metaFilePath))
            {
                CFileManager.DeleteFile(m_metaFilePath);
            }

            CFileManager.WriteFile(m_metaFilePath, buffer, 0, offset);
        }
        finally
        {
            CFileManager.UnLockFileBuffer();
        }

        //写入数据文件
        string cachedFileFullPath = CFileManager.CombinePath(m_dir, key + m_fileExtension);

        if (CFileManager.IsFileExist(cachedFileFullPath))
        {
            CFileManager.DeleteFile(cachedFileFullPath);
        }

        CFileManager.WriteFile(cachedFileFullPath, data);
    }