update yooasset2.0

pull/189/head
hevinci 2023-09-21 17:23:02 +08:00
parent 006d4c6f09
commit 9c0f9557e8
15 changed files with 161 additions and 21 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
@ -61,5 +62,14 @@ namespace YooAsset.Editor
protected abstract string GetBundleFileHash(string filePath, BuildParametersContext buildParametersContext);
protected abstract string GetBundleFileCRC(string filePath, BuildParametersContext buildParametersContext);
protected abstract long GetBundleFileSize(string filePath, BuildParametersContext buildParametersContext);
protected string GetFilePathTempHash(string filePath)
{
byte[] bytes = Encoding.UTF8.GetBytes(filePath);
return HashUtility.BytesMD5(bytes);
// 注意:在文件路径的哈希值冲突的情况下,可以使用下面的方法
//return $"{HashUtility.BytesMD5(bytes)}-{Guid.NewGuid():N}";
}
}
}

View File

@ -55,7 +55,7 @@ namespace YooAsset.Editor
{
var buildMode = buildParametersContext.Parameters.BuildMode;
if (buildMode == EBuildMode.DryRunBuild || buildMode == EBuildMode.SimulateBuild)
return "00000000000000000000000000000000"; //32位
return GetFilePathTempHash(filePath);
else
return HashUtility.FileMD5(filePath);
}

View File

@ -37,7 +37,7 @@ namespace YooAsset.Editor
{
var buildMode = buildParametersContext.Parameters.BuildMode;
if (buildMode == EBuildMode.SimulateBuild)
return "00000000000000000000000000000000"; //32位
return GetFilePathTempHash(filePath);
else
return HashUtility.FileMD5(filePath);
}

View File

@ -55,7 +55,7 @@ namespace YooAsset.Editor
{
var buildMode = buildParametersContext.Parameters.BuildMode;
if (buildMode == EBuildMode.SimulateBuild)
return "00000000000000000000000000000000"; //32位
return GetFilePathTempHash(filePath);
else
return HashUtility.FileMD5(filePath);
}

View File

@ -176,23 +176,34 @@ namespace YooAsset
}
/// <summary>
/// 批量转换为解压BundleInfo
/// 批量创建解压BundleInfo
/// </summary>
public static List<BundleInfo> ConvertToUnpackInfos(ResourceAssist assist, List<PackageBundle> unpackList)
public static List<BundleInfo> CreateUnpackInfos(ResourceAssist assist, List<PackageBundle> unpackList)
{
List<BundleInfo> result = new List<BundleInfo>(unpackList.Count);
foreach (var packageBundle in unpackList)
{
var bundleInfo = ConvertToUnpackInfo(assist, packageBundle);
var bundleInfo = CreateUnpackInfo(assist, packageBundle);
result.Add(bundleInfo);
}
return result;
}
private static BundleInfo ConvertToUnpackInfo(ResourceAssist assist, PackageBundle packageBundle)
private static BundleInfo CreateUnpackInfo(ResourceAssist assist, PackageBundle packageBundle)
{
string streamingPath = PersistentHelper.ConvertToWWWPath(assist.Persistent.GetBuildinFilePath(packageBundle));
BundleInfo newBundleInfo = new BundleInfo(assist, packageBundle, ELoadMode.LoadFromStreaming, streamingPath, streamingPath);
return newBundleInfo;
}
/// <summary>
/// 创建导入BundleInfo
/// </summary>
public static BundleInfo CreateImportInfo(ResourceAssist assist, PackageBundle packageBundle, string filePath)
{
// 注意:我们把本地文件路径指定为远端下载地址
string persistentPath = PersistentHelper.ConvertToWWWPath(filePath);
BundleInfo bundleInfo = new BundleInfo(assist, packageBundle, BundleInfo.ELoadMode.None, persistentPath, persistentPath);
return bundleInfo;
}
}
}

View File

@ -36,5 +36,8 @@ namespace YooAsset
// 解压相关
ResourceUnpackerOperation CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout);
ResourceUnpackerOperation CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout);
// 导入相关
ResourceImporterOperation CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout);
}
}

View File

@ -151,11 +151,13 @@ namespace YooAsset
}
// 填充BundleDic
manifest.BundleDic = new Dictionary<string, PackageBundle>(manifest.BundleList.Count);
manifest.BundleDic1 = new Dictionary<string, PackageBundle>(manifest.BundleList.Count);
manifest.BundleDic2 = new Dictionary<string, PackageBundle>(manifest.BundleList.Count);
foreach (var packageBundle in manifest.BundleList)
{
packageBundle.ParseBundle(manifest);
manifest.BundleDic.Add(packageBundle.BundleName, packageBundle);
manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle);
manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
}
// 填充AssetDic

View File

@ -267,7 +267,6 @@ namespace YooAsset
return operation;
}
}
public sealed class ResourceUnpackerOperation : DownloaderOperation
{
internal ResourceUnpackerOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
@ -285,4 +284,21 @@ namespace YooAsset
return operation;
}
}
public sealed class ResourceImporterOperation : DownloaderOperation
{
internal ResourceImporterOperation(string packageName, List<BundleInfo> downloadList, int downloadingMaxNumber, int failedTryAgain, int timeout)
: base(packageName, downloadList, downloadingMaxNumber, failedTryAgain, timeout)
{
}
/// <summary>
/// 创建空的导入器
/// </summary>
internal static ResourceImporterOperation CreateEmptyImporter(string packageName, int upackingMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new ResourceImporterOperation(packageName, downloadList, upackingMaxNumber, failedTryAgain, int.MaxValue);
return operation;
}
}
}

View File

@ -185,7 +185,8 @@ namespace YooAsset
{
_packageBundleCount = _buffer.ReadInt32();
Manifest.BundleList = new List<PackageBundle>(_packageBundleCount);
Manifest.BundleDic = new Dictionary<string, PackageBundle>(_packageBundleCount);
Manifest.BundleDic1 = new Dictionary<string, PackageBundle>(_packageBundleCount);
Manifest.BundleDic2 = new Dictionary<string, PackageBundle>(_packageBundleCount);
_progressTotalValue = _packageBundleCount;
_steps = ESteps.DeserializeBundleList;
}
@ -203,8 +204,9 @@ namespace YooAsset
packageBundle.Tags = _buffer.ReadUTF8Array();
packageBundle.DependIDs = _buffer.ReadInt32Array();
packageBundle.ParseBundle(Manifest);
Manifest.BundleList.Add(packageBundle);
Manifest.BundleDic.Add(packageBundle.BundleName, packageBundle);
Manifest.BundleList.Add(packageBundle);
Manifest.BundleDic1.Add(packageBundle.BundleName, packageBundle);
Manifest.BundleDic2.Add(packageBundle.FileName, packageBundle);
// 注意原始文件可能存在相同的CacheGUID
if (Manifest.CacheGUIDs.Contains(packageBundle.CacheGUID) == false)

View File

@ -67,7 +67,13 @@ namespace YooAsset
/// 资源包集合提供BundleName获取PackageBundle
/// </summary>
[NonSerialized]
public Dictionary<string, PackageBundle> BundleDic;
public Dictionary<string, PackageBundle> BundleDic1;
/// <summary>
/// 资源包集合提供FileName获取PackageBundle
/// </summary>
[NonSerialized]
public Dictionary<string, PackageBundle> BundleDic2;
/// <summary>
/// 资源映射集合提供AssetPath获取PackageAsset
@ -170,9 +176,17 @@ namespace YooAsset
/// <summary>
/// 尝试获取包裹的资源包
/// </summary>
public bool TryGetPackageBundle(string bundleName, out PackageBundle result)
public bool TryGetPackageBundleByBundleName(string bundleName, out PackageBundle result)
{
return BundleDic.TryGetValue(bundleName, out result);
return BundleDic1.TryGetValue(bundleName, out result);
}
/// <summary>
/// 尝试获取包裹的资源包
/// </summary>
public bool TryGetPackageBundleByFileName(string fileName, out PackageBundle result)
{
return BundleDic2.TryGetValue(fileName, out result);
}
/// <summary>

View File

@ -85,6 +85,11 @@ namespace YooAsset
{
return ResourceUnpackerOperation.CreateEmptyUnpacker(PackageName, upackingMaxNumber, failedTryAgain, timeout);
}
ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
{
return ResourceImporterOperation.CreateEmptyImporter(PackageName, importerMaxNumber, failedTryAgain, timeout);
}
#endregion
#region IBundleQuery接口

View File

@ -265,7 +265,7 @@ namespace YooAsset
}
}
return BundleInfo.ConvertToUnpackInfos(_assist, downloadList);
return BundleInfo.CreateUnpackInfos(_assist, downloadList);
}
ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
@ -293,7 +293,36 @@ namespace YooAsset
}
}
return BundleInfo.ConvertToUnpackInfos(_assist, downloadList);
return BundleInfo.CreateUnpackInfos(_assist, downloadList);
}
ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> importerList = GetImporterListByFilePaths(_activeManifest, filePaths);
var operation = new ResourceImporterOperation(PackageName, importerList, importerMaxNumber, failedTryAgain, timeout);
return operation;
}
private List<BundleInfo> GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths)
{
List<BundleInfo> result = new List<BundleInfo>();
foreach (var filePath in filePaths)
{
string fileName = System.IO.Path.GetFileName(filePath);
if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle))
{
// 忽略缓存文件
if (IsCachedPackageBundle(packageBundle))
continue;
var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath);
result.Add(bundleInfo);
}
else
{
YooLogger.Warning($"Not found package bundle, importer file path : {filePath}");
}
}
return result;
}
#endregion

View File

@ -109,7 +109,7 @@ namespace YooAsset
downloadList.Add(packageBundle);
}
return BundleInfo.ConvertToUnpackInfos(_assist, downloadList);
return BundleInfo.CreateUnpackInfos(_assist, downloadList);
}
ResourceUnpackerOperation IPlayMode.CreateResourceUnpackerByTags(string[] tags, int upackingMaxNumber, int failedTryAgain, int timeout)
@ -134,7 +134,36 @@ namespace YooAsset
}
}
return BundleInfo.ConvertToUnpackInfos(_assist, downloadList);
return BundleInfo.CreateUnpackInfos(_assist, downloadList);
}
ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
{
List<BundleInfo> importerList = GetImporterListByFilePaths(_activeManifest, filePaths);
var operation = new ResourceImporterOperation(PackageName, importerList, importerMaxNumber, failedTryAgain, timeout);
return operation;
}
private List<BundleInfo> GetImporterListByFilePaths(PackageManifest manifest, string[] filePaths)
{
List<BundleInfo> result = new List<BundleInfo>();
foreach (var filePath in filePaths)
{
string fileName = System.IO.Path.GetFileName(filePath);
if (manifest.TryGetPackageBundleByFileName(fileName, out PackageBundle packageBundle))
{
// 忽略缓存文件
if (IsCachedPackageBundle(packageBundle))
continue;
var bundleInfo = BundleInfo.CreateImportInfo(_assist, packageBundle, filePath);
result.Add(bundleInfo);
}
else
{
YooLogger.Warning($"Not found package bundle, importer file path : {filePath}");
}
}
return result;
}
#endregion

View File

@ -112,6 +112,11 @@ namespace YooAsset
{
return ResourceUnpackerOperation.CreateEmptyUnpacker(PackageName, upackingMaxNumber, failedTryAgain, timeout);
}
ResourceImporterOperation IPlayMode.CreateResourceImporterByFilePaths(string[] filePaths, int importerMaxNumber, int failedTryAgain, int timeout)
{
return ResourceImporterOperation.CreateEmptyImporter(PackageName, importerMaxNumber, failedTryAgain, timeout);
}
#endregion
#region IBundleQuery接口

View File

@ -352,7 +352,6 @@ namespace YooAsset
public void UnloadUnusedAssets()
{
DebugCheckInitialize();
_resourceMgr.Update();
_resourceMgr.UnloadUnusedAssets();
}
@ -1028,6 +1027,21 @@ namespace YooAsset
}
#endregion
#region 资源导入
/// <summary>
/// 创建资源导入器
/// 注意:资源文件名称必须和资源服务器部署的文件名称一致!
/// </summary>
/// <param name="filePaths">资源路径列表</param>
/// <param name="importerMaxNumber">同时导入的最大文件数</param>
/// <param name="failedTryAgain">导入失败的重试次数</param>
public ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain, int.MaxValue);
}
#endregion
#region 内部方法
/// <summary>
/// 是否包含资源文件