update file system

新增ICopyLocalFileServices服务类
This commit is contained in:
何冠峰
2025-06-27 17:42:57 +08:00
parent ebde5a52d3
commit 150b46a0a7
12 changed files with 308 additions and 178 deletions

View File

@@ -97,7 +97,7 @@ namespace YooAsset
/// <summary>
/// 自定义参数:拷贝内置文件服务类
/// </summary>
public ICopyBuildinBundleServices CopyBuildinBundleServices { private set; get; }
public ICopyLocalFileServices CopyLocalFileServices { private set; get; }
#endregion
@@ -188,9 +188,9 @@ namespace YooAsset
{
ManifestServices = (IManifestServices)value;
}
else if (name == FileSystemParametersDefine.COPY_BUILDIN_BUNDLE_SERVICES)
else if (name == FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES)
{
CopyBuildinBundleServices = (ICopyBuildinBundleServices)value;
CopyLocalFileServices = (ICopyLocalFileServices)value;
}
else
{
@@ -214,7 +214,7 @@ namespace YooAsset
_unpackFileSystem.SetParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, InstallClearMode);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, AppendFileExtension);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, DecryptionServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.COPY_BUILDIN_BUNDLE_SERVICES, CopyBuildinBundleServices);
_unpackFileSystem.SetParameter(FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES, CopyLocalFileServices);
_unpackFileSystem.OnCreate(packageName, null);
}
public virtual void OnDestroy()

View File

@@ -108,7 +108,7 @@ namespace YooAsset
/// <summary>
/// 自定义参数:拷贝内置文件服务类
/// </summary>
public ICopyBuildinBundleServices CopyBuildinBundleServices { private set; get; }
public ICopyLocalFileServices CopyLocalFileServices { private set; get; }
#endregion
@@ -235,9 +235,9 @@ namespace YooAsset
{
ManifestServices = (IManifestServices)value;
}
else if (name == FileSystemParametersDefine.COPY_BUILDIN_BUNDLE_SERVICES)
else if (name == FileSystemParametersDefine.COPY_LOCAL_FILE_SERVICES)
{
CopyBuildinBundleServices = (ICopyBuildinBundleServices)value;
CopyLocalFileServices = (ICopyLocalFileServices)value;
}
else
{

View File

@@ -228,10 +228,6 @@ namespace YooAsset
{
if (ExecuteWhileDone())
{
//TODO 拷贝本地文件失败也会触发该错误!
if (_downloadFileOp != null && _downloadFileOp.Status == EOperationStatus.Failed)
YooLogger.Error($"Try load bundle {_bundle.BundleName} from remote !");
_steps = ESteps.Done;
break;
}
@@ -361,10 +357,6 @@ namespace YooAsset
{
if (ExecuteWhileDone())
{
//TODO 拷贝本地文件失败也会触发该错误!
if (_downloadFileOp != null && _downloadFileOp.Status == EOperationStatus.Failed)
YooLogger.Error($"Try load bundle {_bundle.BundleName} from remote !");
_steps = ESteps.Done;
break;
}

View File

@@ -82,33 +82,40 @@ namespace YooAsset
return oldDownloader;
}
// 设置请求URL
if (string.IsNullOrEmpty(options.ImportFilePath))
{
options.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(bundle.FileName);
options.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(bundle.FileName);
}
else
{
// 注意:把本地文件路径指定为远端下载地址
options.MainURL = DownloadSystemHelper.ConvertToWWWPath(options.ImportFilePath);
options.FallbackURL = options.MainURL;
}
// 创建新的下载器
DefaultDownloadFileOperation newDownloader;
if (bundle.FileSize >= _fileSystem.ResumeDownloadMinimumSize)
if (string.IsNullOrEmpty(options.ImportFilePath))
{
newDownloader = new DownloadResumeFileOperation(_fileSystem, bundle, options);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
// 远端下载地址
options.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(bundle.FileName);
options.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(bundle.FileName);
// 创建新的下载器
if (bundle.FileSize >= _fileSystem.ResumeDownloadMinimumSize)
{
newDownloader = new DownloadResumeFileOperation(_fileSystem, bundle, options);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
}
else
{
newDownloader = new DownloadNormalFileOperation(_fileSystem, bundle, options);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
}
}
else
{
newDownloader = new DownloadNormalFileOperation(_fileSystem, bundle, options);
// 注意:把本地文件路径指定为可下载地址
options.MainURL = DownloadSystemHelper.ConvertToWWWPath(options.ImportFilePath);
options.FallbackURL = options.MainURL;
// 创建新的下载器
newDownloader = new DownloadLocalFileOperation(_fileSystem, bundle, options);
AddChildOperation(newDownloader);
_downloaders.Add(bundle.BundleGUID, newDownloader);
}
return newDownloader;
}

View File

@@ -0,0 +1,221 @@
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal class DownloadLocalFileOperation : DefaultDownloadFileOperation
{
private readonly DefaultCacheFileSystem _fileSystem;
private VerifyTempFileOperation _verifyOperation;
private string _tempFilePath;
private ESteps _steps = ESteps.None;
internal DownloadLocalFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
{
_fileSystem = fileSystem;
}
internal override void InternalStart()
{
_tempFilePath = _fileSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 检测文件是否存在
if (_steps == ESteps.CheckExists)
{
if (_fileSystem.Exists(Bundle))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
if (_fileSystem.CopyLocalFileServices != null)
_steps = ESteps.CopyBuildinBundle;
else
_steps = ESteps.CreateRequest;
}
}
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
FileUtility.CreateFileDirectory(_tempFilePath);
// 删除临时文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
// 重置请求
ResetRequestFiled();
// 创建下载器
CreateWebRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
Progress = DownloadProgress;
if (_webRequest.isDone == false)
{
CheckRequestTimeout();
return;
}
// 检查网络错误
if (CheckRequestResult())
_steps = ESteps.VerifyTempFile;
else
_steps = ESteps.TryAgain;
// 注意:最终释放请求器
DisposeWebRequest();
}
// 拷贝内置文件
if (_steps == ESteps.CopyBuildinBundle)
{
FileUtility.CreateFileDirectory(_tempFilePath);
// 删除临时文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
try
{
//TODO 团结引擎,在某些机型(红米),拷贝包内文件会小概率失败!需要借助其它方式来拷贝包内文件。
var localFileInfo = new LocalFileInfo();
localFileInfo.PackageName = _fileSystem.PackageName;
localFileInfo.BundleName = Bundle.BundleName;
localFileInfo.SourceFileURL = _requestURL;
_fileSystem.CopyLocalFileServices.CopyFile(localFileInfo, _tempFilePath);
if (File.Exists(_tempFilePath))
{
DownloadProgress = 1f;
DownloadedBytes = Bundle.FileSize;
Progress = DownloadProgress;
_steps = ESteps.VerifyTempFile;
}
else
{
Error = $"Failed copy buildin bundle : {_requestURL}";
_steps = ESteps.TryAgain;
}
}
catch (System.Exception ex)
{
Error = $"Failed copy buildin bundle : {ex.Message}";
_steps = ESteps.TryAgain;
}
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
var element = new TempFileElement(_tempFilePath, Bundle.FileCRC, Bundle.FileSize);
_verifyOperation = new VerifyTempFileOperation(element);
_verifyOperation.StartOperation();
AddChildOperation(_verifyOperation);
_steps = ESteps.CheckVerifyTempFile;
}
// 等待验证完成
if (_steps == ESteps.CheckVerifyTempFile)
{
if (IsWaitForAsyncComplete)
_verifyOperation.WaitForAsyncComplete();
_verifyOperation.UpdateOperation();
if (_verifyOperation.IsDone == false)
return;
if (_verifyOperation.Status == EOperationStatus.Succeed)
{
if (_fileSystem.WriteCacheBundleFile(Bundle, _tempFilePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
YooLogger.Error(Error);
}
}
else
{
_steps = ESteps.TryAgain;
Error = _verifyOperation.Error;
}
// 注意:验证完成后直接删除文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
//TODO 拷贝本地文件失败后不再尝试!
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
}
}
internal override void InternalAbort()
{
_steps = ESteps.Done;
DisposeWebRequest();
}
internal override void InternalWaitForAsyncComplete()
{
while (true)
{
//TODO 等待导入或解压本地文件完毕,该操作会挂起主线程!
InternalUpdate();
if (IsDone)
break;
// 短暂休眠避免完全卡死
System.Threading.Thread.Sleep(1);
}
}
private void CreateWebRequest()
{
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
DownloadHandlerFile handler = new DownloadHandlerFile(_tempFilePath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest();
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
//注意引擎底层会自动调用Abort方法
_webRequest.Dispose();
_webRequest = null;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c68640bb1d36552469024324e3357bc2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -8,7 +8,6 @@ namespace YooAsset
{
private readonly DefaultCacheFileSystem _fileSystem;
private VerifyTempFileOperation _verifyOperation;
private bool _isReuqestLocalFile;
private string _tempFilePath;
private ESteps _steps = ESteps.None;
@@ -18,7 +17,6 @@ namespace YooAsset
}
internal override void InternalStart()
{
_isReuqestLocalFile = DownloadSystemHelper.IsRequestLocalFile(Options.MainURL);
_tempFilePath = _fileSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists;
}
@@ -37,10 +35,7 @@ namespace YooAsset
}
else
{
if (_isReuqestLocalFile && _fileSystem.CopyBuildinBundleServices != null)
_steps = ESteps.CopyBuildinBundle;
else
_steps = ESteps.CreateRequest;
_steps = ESteps.CreateRequest;
}
}
@@ -87,42 +82,6 @@ namespace YooAsset
DisposeWebRequest();
}
// 拷贝内置文件
if (_steps == ESteps.CopyBuildinBundle)
{
FileUtility.CreateFileDirectory(_tempFilePath);
// 删除临时文件
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
try
{
//TODO 团结引擎,在某些机型(红米),拷贝包内文件会小概率失败!需要借助其它方式来拷贝包内文件。
_fileSystem.CopyBuildinBundleServices.CopyBuildinFile(_requestURL, _tempFilePath);
if (File.Exists(_tempFilePath))
{
DownloadProgress = 1f;
DownloadedBytes = Bundle.FileSize;
Progress = DownloadProgress;
_steps = ESteps.VerifyTempFile;
}
else
{
Error = $"Failed copy buildin bundle : {_requestURL}";
_steps = ESteps.TryAgain;
}
}
catch (System.Exception ex)
{
Error = $"Failed copy buildin bundle : {ex.Message}";
_steps = ESteps.TryAgain;
}
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
@@ -172,15 +131,6 @@ namespace YooAsset
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
//TODO 拷贝本地文件失败后不再尝试!
if (_isReuqestLocalFile)
{
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
return;
}
if (FailedTryAgain <= 0)
{
Status = EOperationStatus.Failed;
@@ -207,23 +157,14 @@ namespace YooAsset
{
while (true)
{
//TODO 如果是导入或解压本地文件,执行等待完毕,该操作会挂起主线程!
if (_isReuqestLocalFile)
if (ExecuteWhileDone())
{
InternalUpdate();
if (IsDone)
break;
//TODO 同步加载失败
if (Status == EOperationStatus.Failed)
YooLogger.Error($"Try load bundle {Bundle.BundleName} from remote !");
// 短暂休眠避免完全卡死
System.Threading.Thread.Sleep(1);
}
else
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
_steps = ESteps.Done;
break;
}
}
}

View File

@@ -9,19 +9,16 @@ namespace YooAsset
private readonly DefaultCacheFileSystem _fileSystem;
private DownloadHandlerFileRange _downloadHandle;
private VerifyTempFileOperation _verifyOperation;
private bool _isReuqestLocalFile;
private long _fileOriginLength = 0;
private string _tempFilePath;
private ESteps _steps = ESteps.None;
internal DownloadResumeFileOperation(DefaultCacheFileSystem fileSystem, PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
{
_fileSystem = fileSystem;
}
internal override void InternalStart()
{
_isReuqestLocalFile = DownloadSystemHelper.IsRequestLocalFile(Options.MainURL);
_tempFilePath = _fileSystem.GetTempFilePath(Bundle);
_steps = ESteps.CheckExists;
}
@@ -40,10 +37,7 @@ namespace YooAsset
}
else
{
if (_isReuqestLocalFile && _fileSystem.CopyBuildinBundleServices != null)
_steps = ESteps.CopyBuildinBundle;
else
_steps = ESteps.CreateRequest;
_steps = ESteps.CreateRequest;
}
}
@@ -107,42 +101,6 @@ namespace YooAsset
DisposeWebRequest();
}
// 拷贝内置文件
if (_steps == ESteps.CopyBuildinBundle)
{
FileUtility.CreateFileDirectory(_tempFilePath);
// 删除临时文件(不支持断点续传)
if (File.Exists(_tempFilePath))
File.Delete(_tempFilePath);
// 获取请求地址
_requestURL = GetRequestURL();
try
{
//TODO 团结引擎,在某些机型(红米),拷贝包内文件会小概率失败!需要借助其它方式来拷贝包内文件。
_fileSystem.CopyBuildinBundleServices.CopyBuildinFile(_requestURL, _tempFilePath);
if (File.Exists(_tempFilePath))
{
DownloadProgress = 1f;
DownloadedBytes = Bundle.FileSize;
Progress = DownloadProgress;
_steps = ESteps.VerifyTempFile;
}
else
{
Error = $"Failed copy buildin bundle : {_requestURL}";
_steps = ESteps.TryAgain;
}
}
catch (System.Exception ex)
{
Error = $"Failed copy buildin bundle : {ex.Message}";
_steps = ESteps.TryAgain;
}
}
// 验证下载文件
if (_steps == ESteps.VerifyTempFile)
{
@@ -191,15 +149,6 @@ namespace YooAsset
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
//TODO 拷贝本地文件失败后不再尝试!
if (_isReuqestLocalFile)
{
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
YooLogger.Error(Error);
return;
}
if (FailedTryAgain <= 0)
{
Status = EOperationStatus.Failed;
@@ -226,23 +175,14 @@ namespace YooAsset
{
while (true)
{
//TODO 如果是导入或解压本地文件,执行等待完毕,该操作会挂起主线程!
if (_isReuqestLocalFile)
if (ExecuteWhileDone())
{
InternalUpdate();
if (IsDone)
break;
//TODO 同步加载失败
if (Status == EOperationStatus.Failed)
YooLogger.Error($"Try load bundle {Bundle.BundleName} from remote !");
// 短暂休眠避免完全卡死
System.Threading.Thread.Sleep(1);
}
else
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
_steps = ESteps.Done;
break;
}
}
}

View File

@@ -19,6 +19,6 @@ namespace YooAsset
public const string ASYNC_SIMULATE_MAX_FRAME = "ASYNC_SIMULATE_MAX_FRAME";
public const string COPY_BUILDIN_PACKAGE_MANIFEST = "COPY_BUILDIN_PACKAGE_MANIFEST";
public const string COPY_BUILDIN_PACKAGE_MANIFEST_DEST_ROOT = "COPY_BUILDIN_PACKAGE_MANIFEST_DEST_ROOT";
public const string COPY_BUILDIN_BUNDLE_SERVICES = "COPY_BUILDIN_BUNDLE_SERVICES";
public const string COPY_LOCAL_FILE_SERVICES = "COPY_LOCAL_FILE_SERVICES";
}
}

View File

@@ -1,11 +0,0 @@

namespace YooAsset
{
/// <summary>
/// 拷贝内置文件服务类
/// </summary>
public interface ICopyBuildinBundleServices
{
void CopyBuildinFile(string buildinFileURL, string destFilePath);
}
}

View File

@@ -0,0 +1,29 @@

namespace YooAsset
{
public struct LocalFileInfo
{
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName;
/// <summary>
/// 资源包名称
/// </summary>
public string BundleName;
/// <summary>
/// 源文件请求地址
/// </summary>
public string SourceFileURL;
}
/// <summary>
/// 本地文件拷贝服务类
/// </summary>
public interface ICopyLocalFileServices
{
void CopyFile(LocalFileInfo sourceFileInfo, string destFilePath);
}
}