首次提交
This commit is contained in:
16
Assets/Scripts/NBC/Asset/Runtime/Tasks/AssetTaskBase.cs
Normal file
16
Assets/Scripts/NBC/Asset/Runtime/Tasks/AssetTaskBase.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
// public class AssetTaskBase : Task
|
||||
// {
|
||||
// public override void Run(IRunner runner = null)
|
||||
// {
|
||||
// Reset();
|
||||
// if (runner == null)
|
||||
// {
|
||||
// runner = TaskRunner.DefRunner;
|
||||
// }
|
||||
//
|
||||
// runner.Run(this);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a230a7ede67349d0ac9560cbfc937cbf
|
||||
timeCreated: 1678766484
|
||||
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Download.meta
Normal file
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Download.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 26f8ef4bac2749ff9c5eba207ffc611f
|
||||
timeCreated: 1678178605
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载当前版本资源包任务
|
||||
/// </summary>
|
||||
public class DownloadBundlesTask : DownloadTaskBase
|
||||
{
|
||||
private readonly SequenceTaskCollection _taskList = new SequenceTaskCollection();
|
||||
private readonly List<BundleInfo> _downloadBundles;
|
||||
|
||||
public DownloadBundlesTask(List<BundleInfo> downloadBundles)
|
||||
{
|
||||
_downloadBundles = downloadBundles;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
foreach (var bundle in _downloadBundles)
|
||||
{
|
||||
if (bundle.LoadMode != BundleLoadMode.LoadFromRemote) continue;
|
||||
var bundleData = bundle.Bundle;
|
||||
_taskList.AddTask(new DownloadFileTask(bundleData.RemoteDataFilePath, bundleData.CachedDataFilePath,
|
||||
bundleData.Hash));
|
||||
}
|
||||
|
||||
_taskList.Run(TaskRunner.DownloadRunner);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _taskList.IsDone ? NTaskStatus.Success : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbe7d222c526467784688d950118b1b4
|
||||
timeCreated: 1678689987
|
||||
@@ -0,0 +1,12 @@
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class DownloadCertificateHandler : CertificateHandler
|
||||
{
|
||||
protected override bool ValidateCertificate(byte[] certificateData)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f6add9368cc45bcb0720de176a19000
|
||||
timeCreated: 1679280715
|
||||
@@ -0,0 +1,300 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载文件
|
||||
/// </summary>
|
||||
public class DownloadFileTask : DownloadTaskBase
|
||||
{
|
||||
private const string DownloadHeaderKey = "Content-Length";
|
||||
private const int RetryDownloadCount = 3;
|
||||
|
||||
public readonly string DownloadPath;
|
||||
|
||||
public readonly string SavePath;
|
||||
public readonly string FileHash;
|
||||
|
||||
/// <summary>
|
||||
/// 开启断点续传
|
||||
/// </summary>
|
||||
public bool ReDownload = true;
|
||||
|
||||
public DownloadFileTask(string path, string savePath, string hash = "")
|
||||
{
|
||||
DownloadPath = path;
|
||||
SavePath = savePath;
|
||||
FileHash = hash;
|
||||
}
|
||||
|
||||
public enum DownLoadStatus
|
||||
{
|
||||
None,
|
||||
GetHeader,
|
||||
PrepareDownload,
|
||||
Download,
|
||||
VerifyingFile,
|
||||
Success,
|
||||
Failed,
|
||||
}
|
||||
|
||||
private ulong _downloadTotalSize = 1;
|
||||
private UnityWebRequest _content;
|
||||
private UnityWebRequest _header;
|
||||
private bool _isAbort;
|
||||
private ulong _latestDownloadBytes;
|
||||
private float _latestDownloadRealtime;
|
||||
private ulong _fileOriginLength;
|
||||
private int RetryCount;
|
||||
|
||||
private long ResponseCode = 0;
|
||||
|
||||
public DownLoadStatus DownloadStatus { get; protected internal set; } = DownLoadStatus.None;
|
||||
public override float Progress => DownloadedBytes * 1f / DownloadTotalSize;
|
||||
|
||||
public ulong DownloadedBytes { get; protected set; }
|
||||
|
||||
public ulong DownloadTotalSize
|
||||
{
|
||||
get => _downloadTotalSize;
|
||||
set
|
||||
{
|
||||
_downloadTotalSize = value;
|
||||
if (_downloadTotalSize < 1) _downloadTotalSize = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Abort()
|
||||
{
|
||||
Fail("abort");
|
||||
Dispose();
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
DownloadStatus = DownLoadStatus.GetHeader;
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
if (DownloadStatus == DownLoadStatus.GetHeader)
|
||||
{
|
||||
_header = UnityWebRequest.Head(DownloadPath);
|
||||
_header.SendWebRequest();
|
||||
DownloadStatus = DownLoadStatus.PrepareDownload;
|
||||
}
|
||||
|
||||
if (DownloadStatus == DownLoadStatus.PrepareDownload)
|
||||
{
|
||||
if (_header == null)
|
||||
{
|
||||
Fail($"get header info error");
|
||||
Debug.LogError("get header info error");
|
||||
return NTaskStatus.Fail;
|
||||
}
|
||||
|
||||
if (!_header.isDone) return NTaskStatus.Running;
|
||||
|
||||
Reset();
|
||||
//远程文件信息
|
||||
var value = _header.GetResponseHeader(DownloadHeaderKey);
|
||||
if (ulong.TryParse(value, out var totalSize))
|
||||
{
|
||||
DownloadTotalSize = totalSize;
|
||||
}
|
||||
|
||||
if (ReDownload)
|
||||
{
|
||||
//读取未下载完成的文件信息
|
||||
var tempInfo = new FileInfo(SavePath);
|
||||
if (tempInfo.Exists)
|
||||
{
|
||||
_fileOriginLength = (ulong)tempInfo.Length;
|
||||
if (_fileOriginLength == DownloadTotalSize)
|
||||
{
|
||||
DownloadedBytes = _fileOriginLength;
|
||||
DownloadStatus = DownLoadStatus.VerifyingFile;
|
||||
return NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_fileOriginLength = 0;
|
||||
}
|
||||
|
||||
_content = UnityWebRequest.Get(DownloadPath);
|
||||
if (_fileOriginLength > 0)
|
||||
{
|
||||
Debug.Log($"断点续传===={_fileOriginLength} path={DownloadPath}");
|
||||
#if UNITY_2019_1_OR_NEWER
|
||||
_content.SetRequestHeader("Range", $"bytes={_fileOriginLength}-");
|
||||
_content.downloadHandler = new DownloadHandlerFile(SavePath, true);
|
||||
#else
|
||||
_request.DownloadedBytes = 0;
|
||||
_content.downloadHandler = new DownloadHandlerFile(TempPath);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
_content.downloadHandler = new DownloadHandlerFile(SavePath);
|
||||
}
|
||||
|
||||
_content.certificateHandler = new DownloadCertificateHandler();
|
||||
_content.disposeDownloadHandlerOnDispose = true;
|
||||
_content.disposeCertificateHandlerOnDispose = true;
|
||||
_content.disposeUploadHandlerOnDispose = true;
|
||||
_content.SendWebRequest();
|
||||
DownloadStatus = DownLoadStatus.Download;
|
||||
}
|
||||
|
||||
if (DownloadStatus == DownLoadStatus.Download)
|
||||
{
|
||||
DownloadedBytes = _fileOriginLength + _content.downloadedBytes;
|
||||
if (!_content.isDone)
|
||||
{
|
||||
CheckTimeout();
|
||||
return NTaskStatus.Running;
|
||||
}
|
||||
|
||||
bool hasError = false;
|
||||
// 检查网络错误
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
if (_content.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
hasError = true;
|
||||
_errorMsg = _content.error;
|
||||
ResponseCode = _content.responseCode;
|
||||
}
|
||||
#else
|
||||
if (_content.isNetworkError || _content.isHttpError)
|
||||
{
|
||||
hasError = true;
|
||||
_errorMsg = _content.error;
|
||||
ResponseCode = _content.responseCode;
|
||||
}
|
||||
#endif
|
||||
// 如果网络异常
|
||||
if (hasError)
|
||||
{
|
||||
RetryCount++;
|
||||
if (RetryCount <= RetryDownloadCount)
|
||||
{
|
||||
Debug.Log($"网络异常 重新开始下载={DownloadPath} code={ResponseCode} msg={_content.error}");
|
||||
//重新开始下载
|
||||
DownloadStatus = DownLoadStatus.PrepareDownload;
|
||||
}
|
||||
else
|
||||
{
|
||||
//重试后还是网络错误,直接失败
|
||||
Debug.Log("重试后还是网络错误,直接失败");
|
||||
DownloadStatus = DownLoadStatus.Failed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DownloadStatus = DownLoadStatus.VerifyingFile;
|
||||
}
|
||||
}
|
||||
|
||||
if (DownloadStatus == DownLoadStatus.VerifyingFile)
|
||||
{
|
||||
Dispose();
|
||||
var tryPass = false;
|
||||
var tempInfo = new FileInfo(SavePath);
|
||||
|
||||
if (tempInfo.Exists)
|
||||
{
|
||||
if (tempInfo.Length == (long)DownloadTotalSize)
|
||||
{
|
||||
tryPass = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMsg = "file size error";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(FileHash))
|
||||
{
|
||||
var hash = Util.ComputeHash(SavePath);
|
||||
if (FileHash.Equals(hash))
|
||||
{
|
||||
tryPass = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMsg = "file hash error";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorMsg = "file not exists";
|
||||
}
|
||||
|
||||
if (!tryPass)
|
||||
{
|
||||
// 验证失败后删除文件
|
||||
if (File.Exists(SavePath))
|
||||
File.Delete(SavePath);
|
||||
Debug.Log("验证失败后删除文件,尝试重新下载");
|
||||
//重新下载
|
||||
DownloadStatus = DownLoadStatus.PrepareDownload;
|
||||
}
|
||||
else
|
||||
{
|
||||
DownloadStatus = DownLoadStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
if (DownloadStatus == DownLoadStatus.Success)
|
||||
{
|
||||
return NTaskStatus.Success;
|
||||
}
|
||||
|
||||
if (DownloadStatus == DownLoadStatus.Failed)
|
||||
{
|
||||
return NTaskStatus.Fail;
|
||||
}
|
||||
|
||||
return NTaskStatus.Running;
|
||||
}
|
||||
|
||||
|
||||
private void CheckTimeout()
|
||||
{
|
||||
if (_isAbort) return;
|
||||
|
||||
if (_latestDownloadBytes != DownloadedBytes)
|
||||
{
|
||||
_latestDownloadBytes = DownloadedBytes;
|
||||
_latestDownloadRealtime = Time.realtimeSinceStartup;
|
||||
}
|
||||
|
||||
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
|
||||
if (_latestDownloadRealtime > 0 && offset > Const.DownloadTimeOut)
|
||||
{
|
||||
_content.Abort();
|
||||
_isAbort = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void Dispose()
|
||||
{
|
||||
if (_header != null)
|
||||
{
|
||||
_header.Dispose();
|
||||
_header = null;
|
||||
}
|
||||
|
||||
if (_content != null)
|
||||
{
|
||||
_content.Dispose();
|
||||
_content = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc534770270e47bd9b36acb09ba8bad9
|
||||
timeCreated: 1678176990
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class DownloadRunner: Runner
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4f1c82b77b844fca292c3226b3a1620
|
||||
timeCreated: 1678761539
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class DownloadTaskBase : NTask
|
||||
{
|
||||
public virtual void Run()
|
||||
{
|
||||
Run(TaskRunner.DownloadRunner);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 321c6ff70e3b45138a9af2585706447f
|
||||
timeCreated: 1678762460
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 986e39ec068243938bc33d4beb6931f7
|
||||
timeCreated: 1678178617
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 编辑器初始化任务
|
||||
/// </summary>
|
||||
internal sealed class EditorInitializationTask : InitializationTask
|
||||
{
|
||||
public override float Progress => _taskList.Progress;
|
||||
private readonly SequenceTaskCollection _taskList = new SequenceTaskCollection();
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_taskList.AddTask(new RunFunctionTask(Addressable.Load));
|
||||
_taskList.Run(TaskRunner.Def);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _taskList.IsDone ? NTaskStatus.Success : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a024ad33e47f472bac1560a1445a86e0
|
||||
timeCreated: 1678289705
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public abstract class InitializationTask : NTask
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ddf142fca6745ffab9246551b920fb5
|
||||
timeCreated: 1678178623
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 移动端离线模式初始化任务
|
||||
/// </summary>
|
||||
internal sealed class OfflineInitializationTask : InitializationTask
|
||||
{
|
||||
public override float Progress => _taskList.Progress;
|
||||
private readonly SequenceTaskCollection _taskList = new SequenceTaskCollection();
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
if (!Const.NotCache)
|
||||
{
|
||||
_taskList.AddTask(new CheckCoverInstallTask());
|
||||
_taskList.AddTask(new UnpackVersionTask());
|
||||
_taskList.AddTask(new CheckUnpackPackageTask());
|
||||
}
|
||||
|
||||
_taskList.AddTask(new RunFunctionTask(Addressable.Load));
|
||||
_taskList.Run(TaskRunner.Def);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _taskList.IsDone ? _taskList.Status : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae221ab297cd4bad9ee57abccd8bf15a
|
||||
timeCreated: 1678289694
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 线上模式初始化任务
|
||||
/// 优先从persistentData读取,如果没有,则从StreamingAssets拷贝清单文件到解压目录
|
||||
/// </summary>
|
||||
internal sealed class OnlineInitializationTask : InitializationTask
|
||||
{
|
||||
public override float Progress => _taskList.Progress;
|
||||
private readonly SequenceTaskCollection _taskList = new SequenceTaskCollection();
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_taskList.AddTask(new CheckCoverInstallTask());
|
||||
_taskList.AddTask(new UnpackVersionTask(true));
|
||||
_taskList.AddTask(new CheckUnpackPackageTask(true));
|
||||
_taskList.AddTask(new RunFunctionTask(Addressable.Load));
|
||||
_taskList.Run(TaskRunner.Def);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _taskList.IsDone ? _taskList.Status : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f61ebb0aa33b4ad29959494481b9f791
|
||||
timeCreated: 1678289682
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4eb2452a725944268e0beb871bbd6a22
|
||||
timeCreated: 1678290546
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 检查是否覆盖安装相关操作
|
||||
/// </summary>
|
||||
public class CheckCoverInstallTask : NTask
|
||||
{
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return NTaskStatus.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5320f83282e844c398ba79be297eabca
|
||||
timeCreated: 1678291123
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.IO;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 检查是否需要解压package清单文件
|
||||
/// </summary>
|
||||
public class CheckUnpackPackageTask : NTask
|
||||
{
|
||||
private readonly ParallelTaskCollection _taskList = new ParallelTaskCollection();
|
||||
private readonly bool _download;
|
||||
|
||||
public CheckUnpackPackageTask(bool download = false)
|
||||
{
|
||||
_download = download;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
var versionData = Util.ReadJson<VersionData>(Const.GetCachePath(Const.VersionFileName));
|
||||
if (versionData != null)
|
||||
{
|
||||
var cachePath = Const.GetCachePath(versionData.NameHash);
|
||||
if (!File.Exists(cachePath))
|
||||
{
|
||||
_taskList.AddTask(new UnpackFileTask(versionData.NameHash, _download));
|
||||
}
|
||||
}
|
||||
|
||||
_taskList.Run(TaskRunner.Def);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _taskList.IsDone ? _taskList.Status : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8a22acc2aba454e9ebab180f56b2427
|
||||
timeCreated: 1678610857
|
||||
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Provider.meta
Normal file
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Provider.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89ae896710534240ba9eef93e8d5cb29
|
||||
timeCreated: 1679225311
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class ProviderRunner : Runner
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c2dd1bfc154446d958ee446f6df2c30
|
||||
timeCreated: 1679225326
|
||||
20
Assets/Scripts/NBC/Asset/Runtime/Tasks/RunFunctionTask.cs
Normal file
20
Assets/Scripts/NBC/Asset/Runtime/Tasks/RunFunctionTask.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class RunFunctionTask : NTask
|
||||
{
|
||||
private readonly Action _action;
|
||||
|
||||
public RunFunctionTask(Action action)
|
||||
{
|
||||
_action = action;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_action?.Invoke();
|
||||
Finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 08fc4f83348346ec9092cd019a4d0c07
|
||||
timeCreated: 1678610568
|
||||
21
Assets/Scripts/NBC/Asset/Runtime/Tasks/TaskRunner.cs
Normal file
21
Assets/Scripts/NBC/Asset/Runtime/Tasks/TaskRunner.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public static class TaskRunner
|
||||
{
|
||||
#region Static
|
||||
|
||||
public static readonly DownloadRunner DownloadRunner = new DownloadRunner();
|
||||
public static readonly ProviderRunner ProviderRunner = new ProviderRunner();
|
||||
public static readonly Runner Def = new Runner();
|
||||
|
||||
|
||||
public static void Update()
|
||||
{
|
||||
DownloadRunner.Process();
|
||||
ProviderRunner.Process();
|
||||
Def.Process();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a7125bea4a9b4d80a044469ce0e81f4c
|
||||
timeCreated: 1678762319
|
||||
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Unpack.meta
Normal file
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Unpack.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1535c7c444cc41c680aeff9570417415
|
||||
timeCreated: 1678681206
|
||||
106
Assets/Scripts/NBC/Asset/Runtime/Tasks/Unpack/UnpackFileTask.cs
Normal file
106
Assets/Scripts/NBC/Asset/Runtime/Tasks/Unpack/UnpackFileTask.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System.IO;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 解压文件到指定目录
|
||||
/// </summary>
|
||||
public class UnpackFileTask : NTask
|
||||
{
|
||||
private enum Steps
|
||||
{
|
||||
LoadStreaming,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly string _fileName;
|
||||
private readonly string _savePath;
|
||||
private Steps _steps;
|
||||
private NTaskStatus _taskStatus = NTaskStatus.Success;
|
||||
private UnityWebRequest _request;
|
||||
private DownloadFileTask _downloadFileTask;
|
||||
private bool _download;
|
||||
|
||||
public UnpackFileTask(string fileName, bool download = false)
|
||||
{
|
||||
_fileName = fileName;
|
||||
_savePath = Const.GetCachePath(fileName);
|
||||
_download = download;
|
||||
}
|
||||
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
if (File.Exists(_savePath))
|
||||
{
|
||||
File.Delete(_savePath);
|
||||
}
|
||||
|
||||
_steps = Steps.LoadStreaming;
|
||||
}
|
||||
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
if (_steps == Steps.LoadStreaming)
|
||||
{
|
||||
_progress = 0;
|
||||
if (_request == null)
|
||||
{
|
||||
var filePath = Const.GetStreamingPath(_fileName);
|
||||
_request = UnityWebRequest.Get(filePath);
|
||||
_request.downloadHandler = new DownloadHandlerFile(_savePath);
|
||||
_request.SendWebRequest();
|
||||
}
|
||||
|
||||
_progress = _request.downloadProgress * 0.5f;
|
||||
|
||||
if (!_request.isDone) return NTaskStatus.Running;
|
||||
if (_request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
//结束,判断是否成功
|
||||
if (File.Exists(_savePath))
|
||||
{
|
||||
_progress = 1;
|
||||
_steps = Steps.Done;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_download)
|
||||
{
|
||||
_taskStatus = NTaskStatus.Fail;
|
||||
_steps = Steps.Download;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_download)
|
||||
{
|
||||
_taskStatus = NTaskStatus.Fail;
|
||||
_steps = Steps.Download;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (_steps == Steps.Download)
|
||||
{
|
||||
if (_downloadFileTask == null)
|
||||
{
|
||||
_downloadFileTask = new DownloadFileTask(Const.GetRemotePath(_fileName), _savePath);
|
||||
_downloadFileTask.Run();
|
||||
}
|
||||
|
||||
_progress = 0.5f + _downloadFileTask.Progress * 0.5f;
|
||||
if (!_downloadFileTask.IsDone) return NTaskStatus.Running;
|
||||
_taskStatus = _downloadFileTask.Status;
|
||||
_steps = Steps.Done;
|
||||
_progress = 1;
|
||||
}
|
||||
|
||||
|
||||
return _steps == Steps.Done ? _taskStatus : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8ab8a4dc8dd4137b6f28a7a4180199e
|
||||
timeCreated: 1678608659
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 解压资源包任务
|
||||
/// </summary>
|
||||
public class UnpackPackagesTask : NTask
|
||||
{
|
||||
private readonly ParallelTaskCollection _taskList = new ParallelTaskCollection();
|
||||
|
||||
public override float Progress => _taskList.Progress;
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_taskList.ParallelNum = 5;
|
||||
var bundles = Addressable.GetCanUnpackBundles();
|
||||
foreach (var bundle in bundles)
|
||||
{
|
||||
_taskList.AddTask(new UnpackFileTask(bundle.Bundle.NameHash));
|
||||
}
|
||||
|
||||
_taskList.Run(TaskRunner.Def);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _taskList.IsDone ? NTaskStatus.Success : NTaskStatus.Running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2766582c3bd64d04a342ffd7c335c78c
|
||||
timeCreated: 1678681264
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.IO;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class UnpackVersionTask : NTask
|
||||
{
|
||||
private readonly bool _download;
|
||||
private readonly string _savePath;
|
||||
private UnpackFileTask _unpackFileTask;
|
||||
|
||||
public UnpackVersionTask(bool download = false)
|
||||
{
|
||||
_savePath = Const.GetCachePath(Const.VersionFileName);
|
||||
_download = download;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
if (File.Exists(_savePath))
|
||||
{
|
||||
Finish();
|
||||
}
|
||||
else
|
||||
{
|
||||
_unpackFileTask = new UnpackFileTask(Const.VersionFileName, _download);
|
||||
_unpackFileTask.Run(TaskRunner.Def);
|
||||
}
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
if (_unpackFileTask != null)
|
||||
{
|
||||
return _unpackFileTask.Status;
|
||||
}
|
||||
|
||||
return NTaskStatus.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1f227f69183cd14284e786c29c74120
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Update.meta
Normal file
3
Assets/Scripts/NBC/Asset/Runtime/Tasks/Update.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dee2f81c0c774940a5852483047cb886
|
||||
timeCreated: 1678695432
|
||||
159
Assets/Scripts/NBC/Asset/Runtime/Tasks/Update/CheckUpdateTask.cs
Normal file
159
Assets/Scripts/NBC/Asset/Runtime/Tasks/Update/CheckUpdateTask.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 检查更新内容任务
|
||||
/// </summary>
|
||||
public class CheckUpdateTask : NTask
|
||||
{
|
||||
public readonly UpdateContext Context = new UpdateContext();
|
||||
|
||||
enum Step
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取远程版本清单
|
||||
/// </summary>
|
||||
GetVersionData,
|
||||
|
||||
/// <summary>
|
||||
/// 检测版本清单
|
||||
/// </summary>
|
||||
CheckVersionData,
|
||||
|
||||
/// <summary>
|
||||
/// 获取包清单
|
||||
/// </summary>
|
||||
GetPackageData,
|
||||
|
||||
/// <summary>
|
||||
/// 检查包清单
|
||||
/// </summary>
|
||||
CheckPackageData,
|
||||
Success,
|
||||
}
|
||||
|
||||
private Step _step = Step.GetVersionData;
|
||||
|
||||
private DownloadFileTask _getVersionFileTask;
|
||||
|
||||
private DownloadFileTask _getPackageTask;
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
if (Const.Simulate) return NTaskStatus.Success;
|
||||
if (_step == Step.GetVersionData)
|
||||
{
|
||||
if (_getVersionFileTask == null)
|
||||
{
|
||||
_getVersionFileTask = new DownloadFileTask(
|
||||
Const.GetRemotePath($"{Const.VersionFileName}?t={Util.GetTimestamp()}"),
|
||||
Const.GetCacheTempPath(Const.VersionFileName));
|
||||
_getVersionFileTask.ReDownload = false;
|
||||
_getVersionFileTask.Run();
|
||||
}
|
||||
|
||||
if (!_getVersionFileTask.IsDone) return NTaskStatus.Running;
|
||||
_step = Step.CheckVersionData;
|
||||
}
|
||||
|
||||
if (_step == Step.CheckVersionData)
|
||||
{
|
||||
CheckVersionData();
|
||||
}
|
||||
|
||||
if (_step == Step.GetPackageData)
|
||||
{
|
||||
if (Context.NewVersionData == null)
|
||||
{
|
||||
_step = Step.Success;
|
||||
return NTaskStatus.Running;
|
||||
}
|
||||
|
||||
if (_getPackageTask == null)
|
||||
{
|
||||
var newVersionData = Context.NewVersionData;
|
||||
var fileName = newVersionData.NameHash;
|
||||
_getPackageTask = new DownloadFileTask(Const.GetRemotePath(fileName),
|
||||
Const.GetCacheTempPath(fileName), newVersionData.Hash);
|
||||
_getPackageTask.Run(TaskRunner.Def);
|
||||
}
|
||||
|
||||
if (!_getPackageTask.IsDone) return NTaskStatus.Running;
|
||||
_step = Step.CheckPackageData;
|
||||
}
|
||||
|
||||
if (_step == Step.CheckPackageData)
|
||||
{
|
||||
//检查需要下载的bundle信息
|
||||
CheckPackageData();
|
||||
}
|
||||
|
||||
|
||||
return NTaskStatus.Success;
|
||||
}
|
||||
|
||||
private void CheckVersionData()
|
||||
{
|
||||
var newVersionData = Util.ReadJson<VersionData>(Const.GetCacheTempPath(Const.VersionFileName));
|
||||
var nowVersionData = Addressable.GetVersionData();
|
||||
if (newVersionData != null)
|
||||
{
|
||||
if (newVersionData.Hash == nowVersionData.Hash && newVersionData.Size == nowVersionData.Size)
|
||||
{
|
||||
//没有变化,不需要检查
|
||||
_step = Step.Success;
|
||||
return;
|
||||
}
|
||||
|
||||
Context.NewVersionData = newVersionData;
|
||||
}
|
||||
|
||||
_step = Step.GetPackageData;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检查资源包需要更新的bundle
|
||||
/// </summary>
|
||||
private void CheckPackageData()
|
||||
{
|
||||
var fileName = Context.NewVersionData.NameHash;
|
||||
var versionPackageData = Util.ReadJson<VersionPackageData>(Const.GetCacheTempPath(fileName));
|
||||
if (versionPackageData != null)
|
||||
{
|
||||
foreach (var package in versionPackageData.Packages)
|
||||
{
|
||||
var can = package.Def == 1 || Assets.IsNeedfulPackage(package.Name);
|
||||
//不需要检测的包直接跳过
|
||||
if (!can) return;
|
||||
var oldPackageData = Addressable.GetPackageData(package.Name);
|
||||
var different = CompareBundles(package.Bundles, oldPackageData.Bundles);
|
||||
if (different.Count > 0)
|
||||
{
|
||||
foreach (var data in different)
|
||||
{
|
||||
Context.NeedUpdateBundleList.Add(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<BundleData> CompareBundles(List<BundleData> newBundles, List<BundleData> oldBundles)
|
||||
{
|
||||
List<BundleData> list = new List<BundleData>();
|
||||
foreach (var bundle in newBundles)
|
||||
{
|
||||
var o = oldBundles.Find(b => b.Name == bundle.Name);
|
||||
if (o == null || o.Hash != bundle.Hash || o.Size != bundle.Size)
|
||||
{
|
||||
list.Add(bundle);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c86b2e1c83c44b5392fc5b315955ea62
|
||||
timeCreated: 1678695473
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class UpdateContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 需要更新的文件总大小
|
||||
/// </summary>
|
||||
public long DownloadTotalSize => NeedUpdateBundleList.Sum(data => data.Size);
|
||||
|
||||
/// <summary>
|
||||
/// 需要更新的bundles
|
||||
/// </summary>
|
||||
public readonly HashSet<BundleData> NeedUpdateBundleList = new HashSet<BundleData>();
|
||||
|
||||
// /// <summary>
|
||||
// /// 需要更新的package
|
||||
// /// </summary>
|
||||
// public readonly HashSet<string> NeedVersionPackages = new HashSet<string>();
|
||||
|
||||
public VersionData NewVersionData;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c885b61fa9f498996c98a6c15c988ca
|
||||
timeCreated: 1678802682
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
/// <summary>
|
||||
/// 更新版本内容任务
|
||||
/// </summary>
|
||||
public class UpdateVersionTask : DownloadTaskBase
|
||||
{
|
||||
private UpdateContext _context;
|
||||
private SequenceTaskCollection _sequence = new SequenceTaskCollection();
|
||||
private ParallelTaskCollection _downloadParallel = new ParallelTaskCollection();
|
||||
public override float Progress => _downloadParallel.Progress;
|
||||
|
||||
public UpdateVersionTask(UpdateContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_sequence.FailBreak = true;
|
||||
_downloadParallel.ParallelNum = 5;
|
||||
_downloadParallel.FailBreak = true;
|
||||
var bundles = _context.NeedUpdateBundleList;
|
||||
if (bundles != null && bundles.Count > 0)
|
||||
{
|
||||
foreach (var bundle in bundles)
|
||||
{
|
||||
_downloadParallel.AddTask(new DownloadFileTask(bundle.RemoteDataFilePath,
|
||||
bundle.CachedDataFilePath));
|
||||
}
|
||||
|
||||
_sequence.AddTask(_downloadParallel);
|
||||
}
|
||||
|
||||
_sequence.AddTask(new RunFunctionTask(TryCoverNewVersionData));
|
||||
_sequence.AddTask(new RunFunctionTask(Addressable.Load));
|
||||
|
||||
_sequence.Run(TaskRunner.DownloadRunner);
|
||||
}
|
||||
|
||||
protected override NTaskStatus OnProcess()
|
||||
{
|
||||
return _sequence.IsDone ? _sequence.Status : NTaskStatus.Running;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试覆盖旧的版本清单文件
|
||||
/// </summary>
|
||||
private void TryCoverNewVersionData()
|
||||
{
|
||||
if (_context.NewVersionData != null)
|
||||
{
|
||||
var nameHash = _context.NewVersionData.NameHash;
|
||||
//覆盖旧的清单文件
|
||||
File.Copy(Const.GetCacheTempPath(nameHash), Const.GetCachePath(nameHash), true);
|
||||
}
|
||||
|
||||
//覆盖旧的version.json
|
||||
File.Copy(Const.GetCacheTempPath(Const.VersionFileName), Const.GetCachePath(Const.VersionFileName), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 613c439d2f0342cdb2ec473d38a3c0a3
|
||||
timeCreated: 1678769071
|
||||
Reference in New Issue
Block a user