Beispiel #1
0
    public System.Collections.IEnumerator UnzipProcess(object obj)
    {
        float step = 0.5f / ResManager.RES_NUM;

        for (int i = 0; i < ResManager.RES_NUM; ++i)
        {
            ResType rt           = (ResType)i;
            string  path         = RuntimeInfo.GetResStreamingPath(rt);
            string  destPath     = RuntimeInfo.GetResPersistentPath(rt);
            string  destPathTemp = destPath + ".temp";
            WWW     www          = new WWW(path);
            while (www.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }
            if (www.bytes == null || www.bytes.Length == 0)
            {
                m_Error = "资源解压失败:" + path;
                LogMgr.Log("解压失败,未找到文件:" + www.error + ", Path:" + path);
                yield break;
            }
            try
            {
                FileInfo fi = new FileInfo(destPathTemp);
                if (fi.Exists)
                {
                    fi.Delete();
                }
                FileStream fss = fi.Create();
                fss.Write(www.bytes, 0, www.bytes.Length);
                fss.Flush();
                fss.Close();
                LZMA.DecompressFile(destPathTemp, destPath);
                if (fi.Exists)
                {
                    fi.Delete();
                }
                m_Progress += step;
            }
            catch (System.Exception e)
            {
                m_Error = "资源解压失败:" + path;
                ReportException.Instance.AddException("Exception:解压文件失败: " + e.ToString());
                yield break;
            }
            yield return(new WaitForEndOfFrame());

            try
            {
                m_ResVer[i].ResCrc = Crc.Crc32FromFile(destPathTemp);
                FileInfo ff = new FileInfo(destPath);
                m_ResVer[i].ResSize = (uint)ff.Length;
                m_Progress         += step;
            }
            catch (System.Exception e)
            {
                m_Error = "计算CRC失败:" + path;
                ReportException.Instance.AddException("Exception:计算文件CRC失败: " + e.ToString());
                yield break;
            }
            File.Delete(destPathTemp);
            yield return(new WaitForEndOfFrame());
        }
        m_bHasVerFile = true;
        m_Progress    = 1.0f;
        if (!SaveVersion())
        {
            ReportException.Instance.AddException("Exception:保存版本文件失败:");
        }
        yield break;
    }
    bool SaveRes(MultiFileOK dd)
    {
        //验证crc
        DownResData drd = dd.Drd;

        if (drd.ResSize != dd.Data.Length)
        {
            LogMgr.Log("Res:" + drd.ResType + " 大小不匹配,localSize:" + dd.Data.Length + ", serverSize:" + drd.ResSize);
            return(false);
        }
        uint crc = Crc.Crc32(dd.Data, 0, dd.Data.Length);

        if (crc != drd.ResCrc)
        {
            LogMgr.Log("Res:" + drd.ResType + "资源检验失败, size:" + dd.Data.Length + ", localCrc:" + crc + ", serverCrc:" + drd.ResCrc);
            return(false);
        }

        System.Random rr       = new System.Random();
        int           name     = rr.Next(100, 10000);
        string        tempFile = RuntimeInfo.GetLocalPath(drd.ResType + name.ToString() + "_temp.dat");
        string        path     = RuntimeInfo.GetResPersistentPath(drd.ResType);

        try
        {
            File.Delete(tempFile);
        }
        catch
        {
        }
        try
        {
            File.Delete(path);
        }
        catch
        {
        }
        try
        {
            FileStream fsTemp = File.Create(tempFile);
            if (fsTemp == null)
            {
                LogMgr.Log("RES文件创建失败:" + drd.ResType);
                return(false);
            }
            //解压
            fsTemp.Write(dd.Data, 0, dd.Data.Length);
            fsTemp.Flush();
            fsTemp.Close();
            fsTemp = null;
            LZMA.DecompressFile(tempFile, path);
        }
        catch (System.Exception e)
        {
            ReportException.Instance.AddException("文件解压失败:" + e.ToString());
        }
        try
        {
            File.Delete(tempFile);
        }
        catch
        {
        }
        if (File.Exists(path) == false)
        {
            LogMgr.Log("RES文件解压失败:" + drd.ResType);
            return(false);
        }
        ResManager.Instance.VersionMgr.SetResVersion(drd.ResType, drd.ResCrc, drd.ResUnzipSize);
        return(ResManager.Instance.VersionMgr.SaveVersion());
    }