首次提交
This commit is contained in:
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:
|
||||
Reference in New Issue
Block a user