Update bundleInfo

修复原生文件拷贝目录不存导致的加载失败
完善原生文件异步加载接口
pull/4/head
hevinci 2022-04-18 15:08:39 +08:00
parent abb75fe858
commit b221b8121a
14 changed files with 214 additions and 155 deletions

View File

@ -130,11 +130,11 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步加载原生文件 /// 异步加载原生文件
/// </summary> /// </summary>
public static RawFileOperation LoadRawFileAsync(string assetPath, string savePath) public static RawFileOperation LoadRawFileAsync(string assetPath, string copyPath)
{ {
string bundleName = BundleServices.GetBundleName(assetPath); string bundleName = BundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = BundleServices.GetBundleInfo(bundleName); BundleInfo bundleInfo = BundleServices.GetBundleInfo(bundleName);
RawFileOperation operation = new RawFileOperation(bundleInfo, savePath); RawFileOperation operation = new RawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation); OperationSystem.ProcessOperaiton(operation);
return operation; return operation;
} }

View File

@ -18,6 +18,7 @@ namespace YooAsset
} }
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private string _fileLoadPath;
private bool _isWaitForAsyncComplete = false; private bool _isWaitForAsyncComplete = false;
private bool _isShowWaitForAsyncError = false; private bool _isShowWaitForAsyncError = false;
private DownloaderBase _downloader; private DownloaderBase _downloader;
@ -38,18 +39,32 @@ namespace YooAsset
if (_steps == ESteps.None) if (_steps == ESteps.None)
{ {
// 检测加载地址是否为空 if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.None)
if (string.IsNullOrEmpty(BundleFileInfo.LocalPath))
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
return; return;
} }
if (string.IsNullOrEmpty(BundleFileInfo.RemoteMainURL)) if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
_steps = ESteps.LoadFile; {
else
_steps = ESteps.Download; _steps = ESteps.Download;
_fileLoadPath = BundleFileInfo.GetCacheLoadPath();
}
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
_steps = ESteps.LoadFile;
_fileLoadPath = BundleFileInfo.GetStreamingLoadPath();
}
else if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
_steps = ESteps.LoadFile;
_fileLoadPath = BundleFileInfo.GetCacheLoadPath();
}
else
{
throw new System.NotImplementedException(BundleFileInfo.LoadMode.ToString());
}
} }
// 1. 从服务器下载 // 1. 从服务器下载
@ -83,9 +98,9 @@ namespace YooAsset
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
// 注意Unity2017.4编辑器模式下如果AssetBundle文件不存在会导致编辑器崩溃这里做了预判。 // 注意Unity2017.4编辑器模式下如果AssetBundle文件不存在会导致编辑器崩溃这里做了预判。
if (System.IO.File.Exists(BundleFileInfo.LocalPath) == false) if (System.IO.File.Exists(_fileLoadPath) == false)
{ {
YooLogger.Warning($"Not found assetBundle file : {BundleFileInfo.LocalPath}"); YooLogger.Warning($"Not found assetBundle file : {_fileLoadPath}");
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
return; return;
@ -96,20 +111,20 @@ namespace YooAsset
if (BundleFileInfo.IsEncrypted) if (BundleFileInfo.IsEncrypted)
{ {
if (AssetSystem.DecryptionServices == null) if (AssetSystem.DecryptionServices == null)
throw new Exception($"{nameof(AssetBundleFileLoader)} need IDecryptServices : {BundleFileInfo.BundleName}"); throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {BundleFileInfo.BundleName}");
ulong offset = AssetSystem.DecryptionServices.GetFileOffset(BundleFileInfo); ulong offset = AssetSystem.DecryptionServices.GetFileOffset(BundleFileInfo);
if (_isWaitForAsyncComplete) if (_isWaitForAsyncComplete)
CacheBundle = AssetBundle.LoadFromFile(BundleFileInfo.LocalPath, 0, offset); CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset);
else else
_cacheRequest = AssetBundle.LoadFromFileAsync(BundleFileInfo.LocalPath, 0, offset); _cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
} }
else else
{ {
if (_isWaitForAsyncComplete) if (_isWaitForAsyncComplete)
CacheBundle = AssetBundle.LoadFromFile(BundleFileInfo.LocalPath); CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath);
else else
_cacheRequest = AssetBundle.LoadFromFileAsync(BundleFileInfo.LocalPath); _cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
} }
_steps = ESteps.CheckFile; _steps = ESteps.CheckFile;
} }

View File

@ -37,19 +37,22 @@ namespace YooAsset
if (_steps == ESteps.None) if (_steps == ESteps.None)
{ {
// 检测加载地址是否为空 if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.None)
if (string.IsNullOrEmpty(BundleFileInfo.LocalPath))
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EStatus.Failed; Status = EStatus.Failed;
return; return;
} }
if (string.IsNullOrEmpty(BundleFileInfo.RemoteMainURL)) if (BundleFileInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
_webURL = BundleFileInfo.LocalPath; {
_steps = ESteps.LoadFile;
_webURL = BundleFileInfo.GetStreamingLoadPath();
}
else else
_webURL = BundleFileInfo.RemoteMainURL; {
_steps = ESteps.LoadFile; throw new System.NotImplementedException(BundleFileInfo.LoadMode.ToString());
}
} }
// 1. 从服务器或缓存中获取AssetBundle文件 // 1. 从服务器或缓存中获取AssetBundle文件

View File

@ -10,31 +10,40 @@ namespace YooAsset
Prepare, Prepare,
DownloadFromWeb, DownloadFromWeb,
CheckDownloadFromWeb, CheckDownloadFromWeb,
CheckFile,
DownloadFromApk, DownloadFromApk,
CheckDownloadFromApk, CheckDownloadFromApk,
CheckAndCopyFile,
Done, Done,
} }
private readonly BundleInfo _bundleInfo; private readonly BundleInfo _bundleInfo;
private readonly string _savePath;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private DownloaderBase _downloader; private DownloaderBase _downloader;
private UnityWebFileRequester _fileRequester; private UnityWebFileRequester _fileRequester;
/// <summary> /// <summary>
/// 原生文件的存储路径 /// 原生文件的拷贝路径
/// </summary> /// </summary>
public string SavePath public string CopyPath { private set; get; }
/// <summary>
/// 原生文件的缓存路径
/// </summary>
public string CachePath
{ {
get { return _savePath; } get
{
if (_bundleInfo == null)
return string.Empty;
return _bundleInfo.GetCacheLoadPath();
}
} }
internal RawFileOperation(BundleInfo bundleInfo, string savePath) internal RawFileOperation(BundleInfo bundleInfo, string copyPath)
{ {
_bundleInfo = bundleInfo; _bundleInfo = bundleInfo;
_savePath = savePath; CopyPath = copyPath;
} }
internal override void Start() internal override void Start()
{ {
@ -48,19 +57,30 @@ namespace YooAsset
// 1. 准备工作 // 1. 准备工作
if (_steps == ESteps.Prepare) if (_steps == ESteps.Prepare)
{ {
// 检测加载地址是否为空 if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.None)
if (string.IsNullOrEmpty(_bundleInfo.LocalPath))
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = "Local path is null or empty."; Error = $"Bundle info is invalid : {_bundleInfo.BundleName}";
return; return;
} }
if (string.IsNullOrEmpty(_bundleInfo.RemoteMainURL)) if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
_steps = ESteps.CheckFile; {
else
_steps = ESteps.DownloadFromWeb; _steps = ESteps.DownloadFromWeb;
}
else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
_steps = ESteps.DownloadFromApk;
}
else if (_bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
_steps = ESteps.CheckAndCopyFile;
}
else
{
throw new System.NotImplementedException(_bundleInfo.LoadMode.ToString());
}
} }
// 2. 从服务器下载 // 2. 从服务器下载
@ -85,65 +105,20 @@ namespace YooAsset
} }
else else
{ {
// 注意:当文件更新之后,需要删除旧文件 _steps = ESteps.CheckAndCopyFile;
if (File.Exists(_savePath))
File.Delete(_savePath);
_steps = ESteps.CheckFile;
} }
} }
// 4. 检测文件 // 4. 从APK拷贝文件
if (_steps == ESteps.CheckFile)
{
// 注意:如果原生文件已经存在,则验证其完整性
if (File.Exists(_savePath))
{
bool result = DownloadSystem.CheckContentIntegrity(_savePath, _bundleInfo.SizeBytes, _bundleInfo.CRC);
if (result)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
return;
}
else
{
File.Delete(_savePath);
}
}
if (_bundleInfo.IsBuildinJarFile())
{
_steps = ESteps.DownloadFromApk;
}
else
{
try
{
File.Copy(_bundleInfo.LocalPath, _savePath, true);
}
catch (System.Exception e)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = e.ToString();
return;
}
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
// 5. 从APK拷贝文件
if (_steps == ESteps.DownloadFromApk) if (_steps == ESteps.DownloadFromApk)
{ {
string downloadURL = PathHelper.ConvertToWWWPath(_bundleInfo.LocalPath); string downloadURL = PathHelper.ConvertToWWWPath(_bundleInfo.GetStreamingLoadPath());
_fileRequester = new UnityWebFileRequester(); _fileRequester = new UnityWebFileRequester();
_fileRequester.SendRequest(downloadURL, _savePath); _fileRequester.SendRequest(downloadURL, _bundleInfo.GetCacheLoadPath());
_steps = ESteps.CheckDownloadFromApk; _steps = ESteps.CheckDownloadFromApk;
} }
// 6. 检测APK拷贝文件结果 // 5. 检测APK拷贝文件结果
if (_steps == ESteps.CheckDownloadFromApk) if (_steps == ESteps.CheckDownloadFromApk)
{ {
if (_fileRequester.IsDone() == false) if (_fileRequester.IsDone() == false)
@ -157,11 +132,51 @@ namespace YooAsset
} }
else else
{ {
_steps = ESteps.CheckAndCopyFile;
}
_fileRequester.Dispose();
}
// 6. 检测并拷贝原生文件
if (_steps == ESteps.CheckAndCopyFile)
{
// 如果不需要保存文件
if (string.IsNullOrEmpty(CopyPath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
return;
}
// 如果原生文件已经存在,则验证其完整性
if (File.Exists(CopyPath))
{
bool result = DownloadSystem.CheckContentIntegrity(CopyPath, _bundleInfo.SizeBytes, _bundleInfo.CRC);
if (result)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
return;
}
else
{
File.Delete(CopyPath);
}
}
try
{
FileUtility.CreateFileDirectory(CopyPath);
File.Copy(_bundleInfo.GetCacheLoadPath(), CopyPath, true);
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
} }
catch (System.Exception e)
_fileRequester.Dispose(); {
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = e.ToString();
}
} }
} }
@ -170,9 +185,10 @@ namespace YooAsset
/// </summary> /// </summary>
public byte[] GetFileData() public byte[] GetFileData()
{ {
if (File.Exists(_savePath) == false) string cachePath = _bundleInfo.GetCacheLoadPath();
if (File.Exists(cachePath) == false)
return null; return null;
return File.ReadAllBytes(_savePath); return File.ReadAllBytes(cachePath);
} }
/// <summary> /// <summary>
@ -180,9 +196,10 @@ namespace YooAsset
/// </summary> /// </summary>
public string GetFileText() public string GetFileText()
{ {
if (File.Exists(_savePath) == false) string cachePath = _bundleInfo.GetCacheLoadPath();
if (File.Exists(cachePath) == false)
return string.Empty; return string.Empty;
return File.ReadAllText(_savePath, System.Text.Encoding.UTF8); return File.ReadAllText(cachePath, System.Text.Encoding.UTF8);
} }
} }
} }

View File

@ -84,7 +84,7 @@ namespace YooAsset
// 创建新的下载器 // 创建新的下载器
{ {
YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}"); YooLogger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}");
FileUtility.CreateFileDirectory(bundleInfo.LocalPath); FileUtility.CreateFileDirectory(bundleInfo.GetCacheLoadPath());
DownloaderBase newDownloader; DownloaderBase newDownloader;
if (bundleInfo.SizeBytes >= _breakpointResumeFileSize) if (bundleInfo.SizeBytes >= _breakpointResumeFileSize)
newDownloader = new HttpDownloader(bundleInfo); newDownloader = new HttpDownloader(bundleInfo);
@ -146,7 +146,7 @@ namespace YooAsset
// 验证文件完整性 // 验证文件完整性
public static bool CheckContentIntegrity(BundleInfo bundleInfo) public static bool CheckContentIntegrity(BundleInfo bundleInfo)
{ {
return CheckContentIntegrity(bundleInfo.LocalPath, bundleInfo.SizeBytes, bundleInfo.CRC); return CheckContentIntegrity(bundleInfo.GetCacheLoadPath(), bundleInfo.SizeBytes, bundleInfo.CRC);
} }
public static bool CheckContentIntegrity(PatchBundle patchBundle) public static bool CheckContentIntegrity(PatchBundle patchBundle)
{ {

View File

@ -50,9 +50,6 @@ namespace YooAsset
} }
public void SendRequest(int failedTryAgain, int timeout) public void SendRequest(int failedTryAgain, int timeout)
{ {
if (string.IsNullOrEmpty(_bundleInfo.LocalPath))
throw new System.ArgumentNullException();
if (_steps == ESteps.None) if (_steps == ESteps.None)
{ {
_failedTryAgain = failedTryAgain; _failedTryAgain = failedTryAgain;
@ -62,7 +59,7 @@ namespace YooAsset
} }
public abstract void Update(); public abstract void Update();
public abstract void Abort(); public abstract void Abort();
/// <summary> /// <summary>
/// 获取网络请求地址 /// 获取网络请求地址
/// </summary> /// </summary>

View File

@ -42,7 +42,7 @@ namespace YooAsset
_requestURL = GetRequestURL(); _requestURL = GetRequestURL();
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET); _webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.LocalPath); DownloadHandlerFile handler = new DownloadHandlerFile(_bundleInfo.GetCacheLoadPath());
handler.removeFileOnAbort = true; handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler; _webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.disposeDownloadHandlerOnDispose = true;
@ -97,8 +97,9 @@ namespace YooAsset
{ {
ReportError(); ReportError();
if (File.Exists(_bundleInfo.LocalPath)) string cacheFilePath = _bundleInfo.GetCacheLoadPath();
File.Delete(_bundleInfo.LocalPath); if (File.Exists(cacheFilePath))
File.Delete(cacheFilePath);
// 失败后重新尝试 // 失败后重新尝试
if (_failedTryAgain > 0) if (_failedTryAgain > 0)

View File

@ -197,7 +197,7 @@ namespace YooAsset
_requestURL = GetRequestURL(); _requestURL = GetRequestURL();
_threadDownloader = new ThreadDownloader(); _threadDownloader = new ThreadDownloader();
_threadDownloader.Run(_requestURL, _bundleInfo.LocalPath, _bundleInfo.Hash, _bundleInfo.CRC, _bundleInfo.SizeBytes, _timeout); _threadDownloader.Run(_requestURL, _bundleInfo.GetCacheLoadPath(), _bundleInfo.Hash, _bundleInfo.CRC, _bundleInfo.SizeBytes, _timeout);
_steps = ESteps.CheckDownload; _steps = ESteps.CheckDownload;
} }

View File

@ -3,18 +3,25 @@ namespace YooAsset
{ {
public class BundleInfo public class BundleInfo
{ {
internal enum ELoadMode
{
None,
LoadFromStreaming,
LoadFromCache,
LoadFromRemote,
}
private readonly PatchBundle _patchBundle; private readonly PatchBundle _patchBundle;
internal readonly ELoadMode LoadMode;
private string _streamingPath;
private string _cachePath;
/// <summary> /// <summary>
/// 资源包名称 /// 资源包名称
/// </summary> /// </summary>
public string BundleName { private set; get; } public string BundleName { private set; get; }
/// <summary>
/// 本地存储的路径
/// </summary>
public string LocalPath { private set; get; }
/// <summary> /// <summary>
/// 远端下载地址 /// 远端下载地址
/// </summary> /// </summary>
@ -96,30 +103,57 @@ namespace YooAsset
} }
/// <summary>
/// 获取流文件夹的加载路径
/// </summary>
public string GetStreamingLoadPath()
{
if (_patchBundle == null)
return string.Empty;
if (string.IsNullOrEmpty(_streamingPath))
_streamingPath = PathHelper.MakeStreamingLoadPath(_patchBundle.Hash);
return _streamingPath;
}
/// <summary>
/// 获取缓存文件夹的加载路径
/// </summary>
public string GetCacheLoadPath()
{
if (_patchBundle == null)
return string.Empty;
if (string.IsNullOrEmpty(_cachePath))
_cachePath = SandboxHelper.MakeSandboxCacheFilePath(_patchBundle.Hash);
return _cachePath;
}
private BundleInfo() private BundleInfo()
{ {
} }
internal BundleInfo(PatchBundle patchBundle, string localPath, string mainURL, string fallbackURL) internal BundleInfo(PatchBundle patchBundle, ELoadMode loadMode, string mainURL, string fallbackURL)
{ {
_patchBundle = patchBundle; _patchBundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName; BundleName = patchBundle.BundleName;
LocalPath = localPath;
RemoteMainURL = mainURL; RemoteMainURL = mainURL;
RemoteFallbackURL = fallbackURL; RemoteFallbackURL = fallbackURL;
} }
internal BundleInfo(PatchBundle patchBundle, string localPath) internal BundleInfo(PatchBundle patchBundle, ELoadMode loadMode)
{ {
_patchBundle = patchBundle; _patchBundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName; BundleName = patchBundle.BundleName;
LocalPath = localPath;
RemoteMainURL = string.Empty; RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty; RemoteFallbackURL = string.Empty;
} }
internal BundleInfo(string bundleName, string localPath) internal BundleInfo(string bundleName)
{ {
_patchBundle = null; _patchBundle = null;
LoadMode = ELoadMode.None;
BundleName = bundleName; BundleName = bundleName;
LocalPath = localPath;
RemoteMainURL = string.Empty; RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty; RemoteFallbackURL = string.Empty;
} }
@ -127,9 +161,9 @@ namespace YooAsset
/// <summary> /// <summary>
/// 是否为JAR包内文件 /// 是否为JAR包内文件
/// </summary> /// </summary>
public bool IsBuildinJarFile() public static bool IsBuildinJarFile(string streamingPath)
{ {
return LocalPath.StartsWith("jar:"); return streamingPath.StartsWith("jar:");
} }
} }
} }

View File

@ -28,7 +28,7 @@ namespace YooAsset
BundleInfo IBundleServices.GetBundleInfo(string bundleName) BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{ {
YooLogger.Warning($"Editor play mode can not get bundle info."); YooLogger.Warning($"Editor play mode can not get bundle info.");
BundleInfo bundleInfo = new BundleInfo(bundleName, bundleName); BundleInfo bundleInfo = new BundleInfo(bundleName);
return bundleInfo; return bundleInfo;
} }
string IBundleServices.GetBundleName(string assetPath) string IBundleServices.GetBundleName(string assetPath)

View File

@ -196,10 +196,9 @@ namespace YooAsset
private BundleInfo ConvertToDownloadInfo(PatchBundle patchBundle) private BundleInfo ConvertToDownloadInfo(PatchBundle patchBundle)
{ {
// 注意:资源版本号只用于确定下载路径 // 注意:资源版本号只用于确定下载路径
string sandboxPath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
string remoteMainURL = GetPatchDownloadMainURL(patchBundle.Hash); string remoteMainURL = GetPatchDownloadMainURL(patchBundle.Hash);
string remoteFallbackURL = GetPatchDownloadFallbackURL(patchBundle.Hash); string remoteFallbackURL = GetPatchDownloadFallbackURL(patchBundle.Hash);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxPath, remoteMainURL, remoteFallbackURL); BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromRemote, remoteMainURL, remoteFallbackURL);
return bundleInfo; return bundleInfo;
} }
@ -207,36 +206,34 @@ namespace YooAsset
BundleInfo IBundleServices.GetBundleInfo(string bundleName) BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{ {
if (string.IsNullOrEmpty(bundleName)) if (string.IsNullOrEmpty(bundleName))
return new BundleInfo(string.Empty, string.Empty); return new BundleInfo(string.Empty);
if (LocalPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle)) if (LocalPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle))
{ {
// 查询沙盒资源
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
{
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromCache);
return bundleInfo;
}
// 查询APP资源 // 查询APP资源
if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle appPatchBundle)) if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle appPatchBundle))
{ {
if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash) if (appPatchBundle.IsBuildin && appPatchBundle.Hash == patchBundle.Hash)
{ {
string appLoadPath = PathHelper.MakeStreamingLoadPath(appPatchBundle.Hash); BundleInfo bundleInfo = new BundleInfo(appPatchBundle, BundleInfo.ELoadMode.LoadFromStreaming);
BundleInfo bundleInfo = new BundleInfo(appPatchBundle, appLoadPath);
return bundleInfo; return bundleInfo;
} }
} }
// 查询沙盒资源
if (DownloadSystem.ContainsVerifyFile(patchBundle.Hash))
{
string sandboxLoadPath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxLoadPath);
return bundleInfo;
}
// 从服务端下载 // 从服务端下载
return ConvertToDownloadInfo(patchBundle); return ConvertToDownloadInfo(patchBundle);
} }
else else
{ {
YooLogger.Warning($"Not found bundle in patch manifest : {bundleName}"); YooLogger.Warning($"Not found bundle in patch manifest : {bundleName}");
BundleInfo bundleInfo = new BundleInfo(bundleName, string.Empty); BundleInfo bundleInfo = new BundleInfo(bundleName);
return bundleInfo; return bundleInfo;
} }
} }

View File

@ -42,18 +42,17 @@ namespace YooAsset
BundleInfo IBundleServices.GetBundleInfo(string bundleName) BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{ {
if (string.IsNullOrEmpty(bundleName)) if (string.IsNullOrEmpty(bundleName))
return new BundleInfo(string.Empty, string.Empty); return new BundleInfo(string.Empty);
if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle)) if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle))
{ {
string localPath = PathHelper.MakeStreamingLoadPath(patchBundle.Hash); BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromStreaming);
BundleInfo bundleInfo = new BundleInfo(patchBundle, localPath);
return bundleInfo; return bundleInfo;
} }
else else
{ {
YooLogger.Warning($"Not found bundle in patch manifest : {bundleName}"); YooLogger.Warning($"Not found bundle in patch manifest : {bundleName}");
BundleInfo bundleInfo = new BundleInfo(bundleName, string.Empty); BundleInfo bundleInfo = new BundleInfo(bundleName);
return bundleInfo; return bundleInfo;
} }
} }

View File

@ -150,6 +150,7 @@ namespace YooAsset
/// </summary> /// </summary>
public static List<BundleInfo> GetUnpackListByTags(PatchManifest appPatchManifest, string[] tags) public static List<BundleInfo> GetUnpackListByTags(PatchManifest appPatchManifest, string[] tags)
{ {
// 注意:离线运行模式也依赖下面逻辑,所以判断沙盒内文件是否存在不能通过缓存系统去验证。
List<PatchBundle> downloadList = new List<PatchBundle>(1000); List<PatchBundle> downloadList = new List<PatchBundle>(1000);
foreach (var patchBundle in appPatchManifest.BundleList) foreach (var patchBundle in appPatchManifest.BundleList)
{ {
@ -162,19 +163,11 @@ namespace YooAsset
if (patchBundle.IsBuildin == false) if (patchBundle.IsBuildin == false)
continue; continue;
// 如果是纯内置资源 // 查询DLC资源
if (patchBundle.IsPureBuildin()) if (patchBundle.HasTag(tags))
{ {
downloadList.Add(patchBundle); downloadList.Add(patchBundle);
} }
else
{
// 查询DLC资源
if (patchBundle.HasTag(tags))
{
downloadList.Add(patchBundle);
}
}
} }
return ConvertToUnpackList(downloadList); return ConvertToUnpackList(downloadList);
@ -191,9 +184,10 @@ namespace YooAsset
} }
private static BundleInfo ConvertToUnpackInfo(PatchBundle patchBundle) private static BundleInfo ConvertToUnpackInfo(PatchBundle patchBundle)
{ {
string sandboxPath = SandboxHelper.MakeSandboxCacheFilePath(patchBundle.Hash); // 注意:我们把流加载路径指定为远端下载地址
string streamingLoadPath = PathHelper.MakeStreamingLoadPath(patchBundle.Hash); string streamingPath = PathHelper.MakeStreamingLoadPath(patchBundle.Hash);
BundleInfo bundleInfo = new BundleInfo(patchBundle, sandboxPath, streamingLoadPath, streamingLoadPath); streamingPath = PathHelper.ConvertToWWWPath(streamingPath);
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromRemote, streamingPath, streamingPath);
return bundleInfo; return bundleInfo;
} }
} }

View File

@ -364,10 +364,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步加载原生文件 /// 异步加载原生文件
/// </summary> /// </summary>
public static RawFileOperation LoadRawFileAsync(string location, string savePath) /// <param name="location">资源的定位地址</param>
/// <param name="copyPath">拷贝路径</param>
public static RawFileOperation LoadRawFileAsync(string location, string copyPath = null)
{ {
string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location); string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
return AssetSystem.LoadRawFileAsync(assetPath, savePath); return AssetSystem.LoadRawFileAsync(assetPath, copyPath);
} }
@ -375,7 +377,7 @@ namespace YooAsset
/// 同步加载资源对象 /// 同步加载资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam> /// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class
{ {
return LoadAssetInternal(location, typeof(TObject), true); return LoadAssetInternal(location, typeof(TObject), true);
@ -384,7 +386,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 同步加载资源对象 /// 同步加载资源对象
/// </summary> /// </summary>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
/// <param name="type">资源类型</param> /// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetSync(string location, System.Type type) public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
{ {
@ -395,7 +397,7 @@ namespace YooAsset
/// 同步加载子资源对象 /// 同步加载子资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam> /// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location) public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location)
{ {
return LoadSubAssetsInternal(location, typeof(TObject), true); return LoadSubAssetsInternal(location, typeof(TObject), true);
@ -404,7 +406,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 同步加载子资源对象 /// 同步加载子资源对象
/// </summary> /// </summary>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param> /// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type)
{ {
@ -416,7 +418,7 @@ namespace YooAsset
/// 异步加载资源对象 /// 异步加载资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam> /// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetAsync<TObject>(string location) public static AssetOperationHandle LoadAssetAsync<TObject>(string location)
{ {
return LoadAssetInternal(location, typeof(TObject), false); return LoadAssetInternal(location, typeof(TObject), false);
@ -425,7 +427,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步加载资源对象 /// 异步加载资源对象
/// </summary> /// </summary>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
/// <param name="type">资源类型</param> /// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
{ {
@ -436,7 +438,7 @@ namespace YooAsset
/// 异步加载子资源对象 /// 异步加载子资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam> /// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location) public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location)
{ {
return LoadSubAssetsInternal(location, typeof(TObject), false); return LoadSubAssetsInternal(location, typeof(TObject), false);
@ -445,7 +447,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步加载子资源对象 /// 异步加载子资源对象
/// </summary> /// </summary>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param> /// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type) public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type)
{ {