/// <summary> /// 解压释放单个文件 /// </summary> /// <param name="finishedObject"></param> private void onExtractDone(IThreadWorkerObject finishedObject) { LocalExtractExecutor lee = finishedObject as LocalExtractExecutor; // 文件本身 //PlayerPrefs.SetInt(lee.FileName , 1); //PlayerPrefs.SetString(lee.FileName + "_md5", lee.FileMD5); //解压出来的文件 foreach (string key in lee.SubFileMD5Dic.Keys) { PlayerPrefs.SetInt(key, 1); PlayerPrefs.SetString(key + "_md5", lee.SubFileMD5Dic[key]); } PlayerPrefs.Save(); extractFiles.Remove(lee.FileName); //Debugger.Log("正在解包文件:>" + lee.FileName); //监听解压进度 #if !UNITY_IOS var percent = (double)(totalExtractCount - extractFiles.Count) / (double)totalExtractCount; bar.SetMessage(string.Format("{0}:{1}%", LanguageTips.EXTRACT_PACKAGE, (percent * 100).ToString("f0"))); bar.UpdateProgress((float)percent); #endif Util.ClearMemory(); }
public IEnumerator ExtractOneByOne() { if (extractLeftFiles.Count == 0) { LuaHelper.GetGameManager().StartCoroutine(WaitForThreadExtractDone()); yield break; } var file = extractLeftFiles[0].Trim(); curSingleStreamingIndex++; string dataPath = Util.DataPath; //数据目录 string resPath = Path.Combine(Util.StreamingAssetsPath(), LuaConst.osDir); //游戏包资源目录 var percent1 = (float)curSingleStreamingIndex / (float)totalSingleStreamingCount; bar.SetMessage(string.Format("{0}: {1}%", LanguageTips.LOADING_LUA_ING, (percent1 * 100).ToString("f0"))); bar.UpdateProgress(percent1); if (string.IsNullOrEmpty(file)) { extractLeftFiles.RemoveAt(0); LuaHelper.GetGameManager().StartCoroutine(ExtractOneByOne()); yield break; } string[] fs = file.Split('|'); string realFileName = fs[0]; if (realFileName.CustomEndsWith(".unity3d")) { string newStr = Convert.ToBase64String(Crypto.Encode(Encoding.GetBytes(Path.GetFileName(realFileName)))); if (newStr.Contains("/")) { newStr = newStr.Replace("/", ""); } realFileName = string.Format("lua/{0}.unity3d", newStr); } var infile = Path.Combine(resPath, realFileName); // var outfile = dataPath + fs[0]; string srcMd5 = fs[1].Trim(); Debug.Assert(fs.Length == 2, "read packlist.txt error:" + file); bool isBinFile = outfile.CustomEndsWith(".bin"); //文件被非法删除,Bin文件无法确认存在,只能直接再释放一次 var tempMd5 = string.Format("{0}_md5", fs[0]); if (isBinFile || !File.Exists(outfile)) { PlayerPrefs.DeleteKey(fs[0]); PlayerPrefs.DeleteKey(tempMd5); } if (srcMd5 == PlayerPrefs.GetString(tempMd5)) { if (isBinFile) { totalExtractCount++; } extractLeftFiles.RemoveAt(0); LuaHelper.GetGameManager().StartCoroutine(ExtractOneByOne()); yield break; } WWW streamingWWW = new WWW(infile); yield return(streamingWWW); if (streamingWWW.error != null) { Debug.LogError(infile); extractLeftFiles.RemoveAt(0); LuaHelper.GetGameManager().StartCoroutine(ExtractOneByOne()); yield break; } if (streamingWWW.isDone) { List <LocalExtractExecutor> les = new List <LocalExtractExecutor>(); string dir = Path.GetDirectoryName(outfile); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (isBinFile) { while (extractFiles.Count >= 2) { yield return(null); } LocalExtractExecutor lee = new LocalExtractExecutor(); lee.LocalPath = dataPath; lee.FileName = fs[0]; lee.SrcMD5 = srcMd5; lee.FileBytes = streamingWWW.bytes; lee.OnComplete = OnUpdateChangeStatus; totalExtractCount++; les.Add(lee); extractFiles.Add(fs[0]); Amib.Threading.SmartThreadPool.Instance.QueueWorkItem(() => { lee.ExecuteThreadedWork(); Loom.QueueOnMainThread(() => { onExtractDone(lee); }); }); } else { File.WriteAllBytes(outfile, streamingWWW.bytes); PlayerPrefs.SetInt(fs[0], 1); PlayerPrefs.SetString(tempMd5, srcMd5); if (curSingleStreamingIndex % 3 == 0) //每写入3个文件后保存记录,减少IO { PlayerPrefs.Save(); } // Debugger.Log("正在解包文件:>" + infile) ; } } streamingWWW.Dispose(); extractLeftFiles.RemoveAt(0); LuaHelper.GetGameManager().StartCoroutine(ExtractOneByOne()); }
/// <summary> /// 释放本地的整包资源 /// </summary> /// <returns></returns> public IEnumerator OnExtractResources() { ThreadManager threadMgr = AppFacade.Instance.GetManager <ThreadManager>(); if (extractStartTime == 0) { extractStartTime = Time.realtimeSinceStartup; } if (extractErrorCount >= 3) { MessageBox.Show(LanguageTips.READ_FILES_ERROR_QUIT, (result) => { Application.Quit(); }); yield break; } extractFiles.Clear(); #if UNITY_EDITOR PlayerPrefs.DeleteAll(); #else if (Application.platform == RuntimePlatform.WindowsPlayer) { PlayerPrefs.DeleteAll(); } #endif string dataPath = Util.DataPath; //数据目录 string resPath = Path.Combine(Util.StreamingAssetsPath(), LuaConst.osDir); //游戏包资源目录 if (Application.platform != RuntimePlatform.WindowsPlayer) { if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, true); } Directory.CreateDirectory(dataPath); } // 解压前先重新加载一次config,防止覆盖安装config文件不能更改的问题 TextAsset obj = Resources.Load <TextAsset>("config"); if (obj == null) { MessageBox.Show(LanguageTips.NO_CONFIG_VIA_MOBLIEPLATFORM, (result) => { Application.Quit(); }); } string config_path = Util.DataPath + "/config.txt"; if (File.Exists(config_path)) { File.Delete(config_path); } FileManager.WriteFile(config_path, obj.text); User_Config.ResetConfig(obj.text); User_Config.LoadGlobalSetting(); string splitFile = Path.Combine(resPath, "split.txt"); WWW www = new WWW(splitFile); yield return(www); if (www.isDone && www.text == "1") { LuaHelper.GetGameManager().downloadAll = true; } if (www != null) { www.Dispose(); } if (LuaHelper.GetGameManager().downloadAll) { onExtractComplete(null); yield break; } yield return(Yielders.EndOfFrame); string infile = Path.Combine(resPath, "packlist.txt"); string content = string.Empty; // Debug.Log(infile); www = new WWW(infile); yield return(www); if (www.isDone) { content = www.text; } www.Dispose(); yield return(Yielders.EndOfFrame); //释放所有文件到数据目录 string[] files = content.Split('\n'); #if UNITY_IOS extractLeftFiles.AddRange(files); curSingleStreamingIndex = 0; totalSingleStreamingCount = files.Length; bar.UpdateProgress(0); if (extractLeftFiles.Count > 0) { bar.SetMessage(LanguageTips.READ_FILES); } LuaHelper.GetGameManager().StartCoroutine(ExtractOneByOne()); #else int curStreamingIndex = 0; int totalStreamingCount = files.Length; string outfile = string.Empty; List <LocalExtractExecutor> les = new List <LocalExtractExecutor>(); bar.SetMessage(LanguageTips.READ_FILES); for (int i = 0; i < files.Length; ++i) { var file = files[i].Trim(); curStreamingIndex++; var percent1 = (float)curStreamingIndex / (float)totalStreamingCount; bar.SetMessage(string.Format("{0}: {1}%", LanguageTips.READ_FILES, (percent1 * 100).ToString("f0"))); bar.UpdateProgress(percent1); if (string.IsNullOrEmpty(file)) { continue; } string[] fs = file.Split('|'); infile = Path.Combine(resPath, fs[0]); // outfile = dataPath + fs[0]; string srcMd5 = fs[1].Trim(); Debug.Assert(fs.Length == 2, "read packlist.txt error:" + file); bool isBinFile = outfile.CustomEndsWith(".bin"); //文件被非法删除,Bin文件无法确认存在,只能直接再释放一次 var tempMd5 = string.Format("{0}_md5", fs[0]); if (isBinFile || !File.Exists(outfile)) { PlayerPrefs.DeleteKey(fs[0]); PlayerPrefs.DeleteKey(tempMd5); } if (srcMd5 == PlayerPrefs.GetString(tempMd5)) { if (isBinFile) { totalExtractCount++; } continue; } WWW streamingWWW = new WWW(infile); yield return(streamingWWW); if (streamingWWW.error != null) { Debug.LogError(infile); continue; } if (streamingWWW.isDone) { string dir = Path.GetDirectoryName(outfile); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (isBinFile) { LocalExtractExecutor lee = new LocalExtractExecutor(); lee.LocalPath = dataPath; lee.FileName = fs[0]; lee.SrcMD5 = srcMd5; lee.FileBytes = streamingWWW.bytes; lee.OnComplete = OnUpdateChangeStatus; totalExtractCount++; les.Add(lee); extractFiles.Add(fs[0]); } else { File.WriteAllBytes(outfile, streamingWWW.bytes); PlayerPrefs.SetInt(fs[0], 1); PlayerPrefs.SetString(tempMd5, srcMd5); if (curStreamingIndex % 3 == 0) //每写入3个文件后保存记录,减少IO { PlayerPrefs.Save(); } // Debugger.Log("正在解包文件:>" + infile) ; } } streamingWWW.Dispose(); } yield return(Yielders.EndOfFrame); //启动多线程解压 // Debugger.Log("MaxThreadCount:" + maxThreadCount); bar.UpdateProgress(0); if (les.Count > 0) { bar.SetMessage(LanguageTips.EXTRACT_PACKAGE); threadMgr.PoolScheduler.StartASyncThreads(les.ToArray(), onExtractComplete, onExtractDone, maxThreadCount); } else { onExtractComplete(null); } #endif }