Skip to content

Commit

Permalink
添加取消下载以及恢复下载功能
Browse files Browse the repository at this point in the history
  • Loading branch information
mister91jiao committed Jul 14, 2022
1 parent 867ced5 commit 5f41e0b
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 184 deletions.
134 changes: 13 additions & 121 deletions BundleMasterRuntime/AssetComponentInit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using ET;
Expand All @@ -17,17 +16,17 @@ public static partial class AssetComponent
/// <summary>
/// 初始化
/// </summary>
public static async ETTask Initialize(string bundlePackageName, string secretKey = null)
public static async ETTask<bool> Initialize(string bundlePackageName, string secretKey = null)
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Develop)
{
AssetLogHelper.Log("AssetLoadMode = Develop 不需要初始化Bundle配置文件");
return;
return false;
}
if (BundleNameToRuntimeInfo.ContainsKey(bundlePackageName))
{
AssetLogHelper.LogError(bundlePackageName + " 重复初始化");
return;
return false;
}
BundleRuntimeInfo bundleRuntimeInfo = new BundleRuntimeInfo(bundlePackageName, secretKey);
BundleNameToRuntimeInfo.Add(bundlePackageName, bundleRuntimeInfo);
Expand All @@ -42,52 +41,16 @@ public static async ETTask Initialize(string bundlePackageName, string secretKey
fileTcs.SetResult();
};
await fileTcs;
string fileLogs;
#if UNITY_2020_1_OR_NEWER
if (webRequest.result != UnityWebRequest.Result.Success)
#else
if (!string.IsNullOrEmpty(webRequest.error))
#endif
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Build)
{
byte[] fileLogsData = await DownloadBundleHelper.DownloadDataByUrl(Path.Combine(AssetComponentConfig.BundleServerUrl, bundlePackageName, "FileLogs.txt"));
if (fileLogsData == null)
{
AssetLogHelper.LogError("获取Log表失败, PackageName: " + bundlePackageName + "\n " +
"没有找到 " + bundlePackageName + " Bundle的FileLogs\n" + filePath);
return;
}
fileLogs = System.Text.Encoding.UTF8.GetString(fileLogsData);
CreateUpdateLogFile(Path.Combine(AssetComponentConfig.HotfixPath, bundlePackageName, "FileLogs.txt"), fileLogs);
}
else
{
AssetLogHelper.LogError("没有找到 " + bundlePackageName + " Bundle的FileLogs\n" + filePath);
return;
}
}
else
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Build)
{
byte[] fileLogsData = await DownloadBundleHelper.DownloadDataByUrl(Path.Combine(AssetComponentConfig.BundleServerUrl, bundlePackageName, "FileLogs.txt"));
if (fileLogsData == null)
{
AssetLogHelper.LogError("获取Log表失败, PackageName: " + bundlePackageName);
return;
}
fileLogs = System.Text.Encoding.UTF8.GetString(fileLogsData);
if (fileLogs != webRequest.downloadHandler.text)
{
CreateUpdateLogFile(Path.Combine(AssetComponentConfig.HotfixPath, bundlePackageName, "FileLogs.txt"), fileLogs);
}
}
else
{
fileLogs = webRequest.downloadHandler.text;
}
AssetLogHelper.LogError("初始化分包未找到FileLogs 分包名: " + bundlePackageName);
return false;
}
string fileLogs = webRequest.downloadHandler.text;
Regex reg = new Regex(@"\<(.+?)>");
MatchCollection matchCollection = reg.Matches(fileLogs);
List<string> dependFileName = new List<string>();
Expand Down Expand Up @@ -120,52 +83,16 @@ public static async ETTask Initialize(string bundlePackageName, string secretKey
dependTcs.SetResult();
};
await dependTcs;
string dependLogs;
#if UNITY_2020_1_OR_NEWER
if (webRequest.result != UnityWebRequest.Result.Success)
#else
if (!string.IsNullOrEmpty(webRequest.error))
#endif
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Build)
{
byte[] dependLogsData = await DownloadBundleHelper.DownloadDataByUrl(Path.Combine(AssetComponentConfig.BundleServerUrl, bundlePackageName, "DependLogs.txt"));
if (dependLogsData == null)
{
AssetLogHelper.LogError("获取Log表失败, PackageName: " + bundlePackageName + "\n " +
"没有找到 " + bundlePackageName + " Bundle的DependLogs\n" + dependPath);
return;
}
dependLogs = System.Text.Encoding.UTF8.GetString(dependLogsData);
CreateUpdateLogFile(Path.Combine(AssetComponentConfig.HotfixPath, bundlePackageName, "DependLogs.txt"), dependLogs);
}
else
{
AssetLogHelper.LogError("没有找到 " + bundlePackageName + " Bundle的DependLogs\n" + dependPath);
return;
}
}
else
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Build)
{
byte[] dependLogsData = await DownloadBundleHelper.DownloadDataByUrl(Path.Combine(AssetComponentConfig.BundleServerUrl, bundlePackageName, "DependLogs.txt"));
if (dependLogsData == null)
{
AssetLogHelper.LogError("获取Log表失败, PackageName: " + bundlePackageName);
return;
}
dependLogs = System.Text.Encoding.UTF8.GetString(dependLogsData);
if (dependLogs != webRequest.downloadHandler.text)
{
CreateUpdateLogFile(Path.Combine(AssetComponentConfig.HotfixPath, bundlePackageName, "DependLogs.txt"), dependLogs);
}
}
else
{
dependLogs = webRequest.downloadHandler.text;
}
AssetLogHelper.LogError("初始化分包未找到DependLogs 分包名: " + bundlePackageName);
return false;
}
string dependLogs = webRequest.downloadHandler.text;
Regex reg = new Regex(@"\<(.+?)>");
MatchCollection matchCollection = reg.Matches(dependLogs);
foreach (Match m in matchCollection)
Expand All @@ -187,52 +114,16 @@ public static async ETTask Initialize(string bundlePackageName, string secretKey
groupTcs.SetResult();
};
await groupTcs;
string groupLogs;
#if UNITY_2020_1_OR_NEWER
if (webRequest.result != UnityWebRequest.Result.Success)
#else
if (!string.IsNullOrEmpty(webRequest.error))
#endif
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Build)
{
byte[] groupLogsData = await DownloadBundleHelper.DownloadDataByUrl(Path.Combine(AssetComponentConfig.BundleServerUrl, bundlePackageName, "GroupLogs.txt"));
if (groupLogsData == null)
{
AssetLogHelper.LogError("获取Log表失败, PackageName: " + bundlePackageName + "\n " +
"没有找到 " + bundlePackageName + " Bundle的GroupLogs\n" + groupPath);
return;
}
groupLogs = System.Text.Encoding.UTF8.GetString(groupLogsData);
CreateUpdateLogFile(Path.Combine(AssetComponentConfig.HotfixPath, bundlePackageName, "GroupLogs.txt"), groupLogs);
}
else
{
AssetLogHelper.LogError("没有找到 " + bundlePackageName + " Bundle的GroupLogs\n" + groupPath);
return;
}
}
else
{
if (AssetComponentConfig.AssetLoadMode == AssetLoadMode.Build)
{
byte[] groupLogsData = await DownloadBundleHelper.DownloadDataByUrl(Path.Combine(AssetComponentConfig.BundleServerUrl, bundlePackageName, "GroupLogs.txt"));
if (groupLogsData == null)
{
AssetLogHelper.LogError("获取Log表失败, PackageName: " + bundlePackageName);
return;
}
groupLogs = System.Text.Encoding.UTF8.GetString(groupLogsData);
if (groupLogs != webRequest.downloadHandler.text)
{
CreateUpdateLogFile(Path.Combine(AssetComponentConfig.HotfixPath, bundlePackageName, "GroupLogs.txt"), groupLogs);
}
}
else
{
groupLogs = webRequest.downloadHandler.text;
}
AssetLogHelper.LogError("初始化分包未找到GroupLogs 分包名: " + bundlePackageName);
return false;
}
string groupLogs = webRequest.downloadHandler.text;
Regex reg = new Regex(@"\<(.+?)>");
MatchCollection matchCollection = reg.Matches(groupLogs);
foreach (Match m in matchCollection)
Expand All @@ -254,6 +145,7 @@ public static async ETTask Initialize(string bundlePackageName, string secretKey
}
//加载当前分包的shader
await LoadShader(bundlePackageName);
return true;
}

/// <summary>
Expand Down
48 changes: 48 additions & 0 deletions BundleMasterRuntime/AssetComponentLife.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace BM
{
public static partial class AssetComponent
{
/// <summary>
/// 计时
/// </summary>
private static float _timer = 0;

/// <summary>
/// 下载进度更新器
/// </summary>
internal static Action<float> DownLoadAction = null;

/// <summary>
/// 等待计时队列
/// </summary>
internal static Queue<TimerAwait> TimerAwaitQueue = new Queue<TimerAwait>();

/// <summary>
/// 卸载周期计时循环
/// </summary>
public static void Update()
{
float nowTime = Time.deltaTime;
int awaitQueueCount = TimerAwaitQueue.Count;
for (int i = 0; i < awaitQueueCount; i++)
{
TimerAwait timerAwait = TimerAwaitQueue.Dequeue();
if (!timerAwait.CalcSubTime(nowTime))
{
TimerAwaitQueue.Enqueue(timerAwait);
}
}
_timer += nowTime;
if (_timer >= _unLoadCirculateTime)
{
_timer = 0;
AutoAddToTrueUnLoadPool();
}
DownLoadAction?.Invoke(nowTime);
}
}
}
52 changes: 50 additions & 2 deletions BundleMasterRuntime/AssetComponentTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Text;
using ET;
using UnityEngine;

namespace BM
{
Expand Down Expand Up @@ -82,6 +81,11 @@ public class UpdateBundleDataInfo
/// 需要更新的总大小
/// </summary>
public long NeedUpdateSize = 0;

/// <summary>
/// 是否取消
/// </summary>
internal bool Cancel = false;

/// <summary>
/// 需要更新的Bundle的信息
Expand Down Expand Up @@ -169,7 +173,7 @@ internal void UpdateProgress(float deltaTime)
{
SmoothProgress += (Progress - SmoothProgress) * ValueHelper.GetMinValue(deltaTime / 0.1f, 1.0f);
}
progressCallback(SmoothProgress);
progressCallback?.Invoke(SmoothProgress);
}

internal Action FinishCallback;
Expand Down Expand Up @@ -270,6 +274,10 @@ internal void AddCRCFileInfo(string bundlePackageName, string fileName, uint crc
AssetLogHelper.LogError("获取索引号没有找到分包: " + bundlePackageName);
return;
}
if (Cancel)
{
return;
}
if (!crcDictionary.ContainsKey(fileName))
{
crcDictionary.Add(fileName, crc);
Expand All @@ -278,6 +286,46 @@ internal void AddCRCFileInfo(string bundlePackageName, string fileName, uint crc
PackageCRCFile[bundlePackageName].Flush();
}

private Action errorCancelCallback;

/// <summary>
/// 下载失败回调
/// </summary>
public event Action ErrorCancelCallback
{
add => errorCancelCallback += value;
remove => this.errorCancelCallback -= value;
}

/// <summary>
/// 取消更新
/// </summary>
public void CancelUpdate()
{
if (Cancel)
{
return;
}
Cancel = true;
errorCancelCallback?.Invoke();
DestroySelf();
}

private void DestroySelf()
{
foreach (StreamWriter sw in PackageCRCFile.Values)
{
sw.Close();
sw.Dispose();
}
PackageCRCFile.Clear();
AssetComponent.DownLoadAction -= UpdateProgress;
progressCallback = null;
FinishCallback = null;
downLoadSpeedCallback = null;
errorCancelCallback = null;
}

/// <summary>
/// 分包类型
/// </summary>
Expand Down
30 changes: 1 addition & 29 deletions BundleMasterRuntime/AssetComponentUnLoad.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace BM
{
Expand Down Expand Up @@ -247,32 +245,6 @@ private static void AutoAddToTrueUnLoadPool()
}
}


/// <summary>
/// 计时
/// </summary>
private static float _timer = 0;

/// <summary>
/// 下载进度更新器
/// </summary>
private static Action<float> _downLoadAction = null;

/// <summary>
/// 卸载周期计时循环
/// </summary>
public static void Update()
{
float nowTime = Time.deltaTime;
_timer += nowTime;
if (_timer >= _unLoadCirculateTime)
{
_timer = 0;
AutoAddToTrueUnLoadPool();
}
_downLoadAction?.Invoke(nowTime);
}

/// <summary>
/// 取消一个分包的初始化
/// </summary>
Expand Down
Loading

0 comments on commit 5f41e0b

Please sign in to comment.