//下载完成 private async void DownloadComplete() { DownloadTxtFile.Save(); if (_downloads.Count > 0) { for (int i = 0; i < _downloads.Count; i++) { var item = _downloads[i]; if (!item.isDone) { break; } else { if (_serverVersions.ContainsKey(item.path)) { _versions[item.path] = _serverVersions[item.path]; } } } StringBuilder sb = new StringBuilder(); foreach (var item in _versions) { sb.AppendLine(string.Format("{0}:{1}", item.Key, item.Value)); } var path = LWUtility.GetRelativePath4Update(versionsTxt); if (File.Exists(path)) { File.Delete(path); } File.WriteAllText(path, sb.ToString()); AssetsManagerRequest assetsManagerRequest = await MainManager.Instance.GetManager <AssetsManager>().InitializeAsync(); if (assetsManagerRequest.isSuccess) { onCompleted(); } else { onError(assetsManagerRequest.error); } state = State.Completed; return; } if (onCompleted != null) { onCompleted(); } state = State.Completed; }
private void LoadVersions(string text) { //解析本地版本文件 LoadText2Map(text, ref _versions); //加载服务器版本文件 var asset = MainManager.Instance.GetManager <AssetsManager>().LoadAsync(LWUtility.GetDownloadURL(versionsTxt), typeof(TextAsset)); asset.completed += delegate { if (asset.error != null) { OnError(asset.error); return; } //解析服务器版本文件 LoadText2Map(asset.text, ref _serverVersions); foreach (var item in _serverVersions) { string ver; //对比本地版本与服务器版本 if (!_versions.TryGetValue(item.Key, out ver) || !ver.Equals(item.Value)) { var downloader = new Download(); downloader.url = LWUtility.GetDownloadURL(item.Key); downloader.path = item.Key; downloader.versionHash = item.Value; downloader.savePath = LWUtility.GetRelativePath4Update(item.Key); _downloads.Add(downloader); } } //不需要下载 if (_downloads.Count == 0) { DownloadComplete(); } else { //下载对应的依赖关系文件 var downloader = new Download(); downloader.url = LWUtility.GetDownloadURL(LWUtility.GetPlatform()); downloader.path = LWUtility.GetPlatform(); downloader.savePath = LWUtility.GetRelativePath4Update(LWUtility.GetPlatform()); _downloads.Add(downloader); state = State.WaitDownload; Debug.Log("开始下载资源"); Download(); } }; }
/// <summary> /// 加载Dll热更脚本 /// </summary> /// <param name="root">路径</param> /// <param name="mode"></param> /// <returns></returns> public IEnumerator IE_LoadScript(HotfixCodeRunMode mode) { string dllPath = ""; if (Application.isEditor) { //这里情况比较复杂,Mobile上基本认为Persistent才支持File操作, dllPath = Application.streamingAssetsPath + "/Hotfix/" + LWUtility.HotfixFileName; } else { //这里情况比较复杂,Mobile上基本认为Persistent才支持File操作, //可寻址目录也只有 StreamingAsset var firstPath = FileTool.CombinePaths(LWUtility.updatePath, LWUtility.HotfixFileName); //LWUtility.updatePath + "/" + LWUtility.HotfixFileName; var secondPath = FileTool.CombinePaths(Application.streamingAssetsPath, LWUtility.AssetBundles, LWUtility.GetPlatform(), LWUtility.HotfixFileName); // Application.streamingAssetsPath + "/" + LWUtility.AssetBundles + "/" + LWUtility.GetPlatform() + "/" + LWUtility.HotfixFileName; if (!File.Exists(firstPath)) { var request = UnityWebRequest.Get(secondPath); LWDebug.Log("firstPath:" + firstPath); LWDebug.Log("secondPath:" + secondPath); yield return(request.SendWebRequest()); if (request.isDone && request.error == null) { LWDebug.Log("request.downloadHandler.data:" + request.downloadHandler.data.Length); LWDebug.Log("拷贝dll成功:" + firstPath); byte[] results = request.downloadHandler.data; FileTool.WriteByteToFile(firstPath, results, ""); } } dllPath = firstPath; } LWDebug.Log("Dll路径:" + dllPath); //反射执行 if (mode == HotfixCodeRunMode.ByReflection) { var bytes = File.ReadAllBytes(dllPath); var mdb = dllPath + ".mdb"; if (File.Exists(mdb)) { var bytes2 = File.ReadAllBytes(mdb); Assembly = Assembly.Load(bytes, bytes2); } else { Assembly = Assembly.Load(bytes); } //Debug.Log("反射模式,开始执行Start"); //var type = Assembly.GetType("StartupBridge_Hotfix"); //var method = type.GetMethod("Start", BindingFlags.Public | BindingFlags.Static); //method.Invoke(null, new object[] { false }); StartupBridge_Hotfix.StartReflection(Assembly); } #if ILRUNTIME //解释执行 else if (mode == HotfixCodeRunMode.ByILRuntime) { //解释执行模式 // ILRuntimeHelper.LoadHotfix(dllPath); // ILRuntimeHelper.AppDomain.Invoke("StartupBridge_Hotfix", "Start", null, new object[] { true }); } #endif else if (mode == HotfixCodeRunMode.ByCode) { LWDebug.Log("内置code模式!", LogColor.green); StartupBridge_Hotfix.StartCode(); //反射调用,防止编译报错 //Assembly assembly = Assembly.GetExecutingAssembly(); //var type = assembly.GetType("StartupBridge_Hotfix"); //var method = type.GetMethod("Start", BindingFlags.Public | BindingFlags.Static); //method.Invoke(null, new object[] { false }); } }
public async void Check() { if (state != State.Wait) { return; } //不连服务器 if (!LWUtility.GlobalConfig.connServer) { AssetsManagerRequest _Request = await MainManager.Instance.GetManager <AssetsManager>().InitializeAsync(); if (_Request.isSuccess) { onCompleted(); } else { onError(_Request.error); } return; } state = State.Checking; AssetsManagerRequest request = await MainManager.Instance.GetManager <AssetsManager>().InitializeAsync(); if (request.isSuccess) { //获取本地的版本文件路径 var path = LWUtility.GetRelativePath4Update(versionsTxt); //当前不存在该文件,即第一次打开应用 if (!File.Exists(path)) { //加载StreamingAssets下的的版本文件 var asset = MainManager.Instance.GetManager <AssetsManager>().LoadAsync(LWUtility.GetWebUrlFromDataPath(versionsTxt), typeof(TextAsset)); asset.completed += delegate { if (asset.error != null) { LoadVersions(string.Empty); return; } var dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } //将StreamingAssets下的版本文件写入到persistentDataPath File.WriteAllText(path, asset.text); LoadVersions(asset.text); asset.Release(); }; } else { LoadVersions(File.ReadAllText(path)); } } else { // 本地没有文件,直接更新 LoadVersions(string.Empty); } }