pull/513/head
何冠峰 2025-03-14 16:50:48 +08:00
parent 6155256fb6
commit 8556e071fa
8 changed files with 143 additions and 99 deletions

View File

@ -1,13 +1,14 @@
using System; using System;
using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
namespace YooAsset namespace YooAsset
{ {
/// <summary> /// <summary>
/// 内置资源清单目录 /// 内置资源清单目录
/// </summary> /// </summary>
internal class DefaultBuildinFileCatalog : ScriptableObject [Serializable]
internal class DefaultBuildinFileCatalog
{ {
[Serializable] [Serializable]
public class FileWrapper public class FileWrapper

View File

@ -338,8 +338,7 @@ namespace YooAsset
} }
public string GetCatalogFileLoadPath() public string GetCatalogFileLoadPath()
{ {
string fileName = Path.GetFileNameWithoutExtension(DefaultBuildinFileSystemDefine.BuildinCatalogFileName); return PathUtility.Combine(_packageRoot, DefaultBuildinFileSystemDefine.BuildinCatalogFileName);
return YooAssetSettingsData.GetYooResourcesLoadPath(PackageName, fileName);
} }
/// <summary> /// <summary>

View File

@ -17,10 +17,6 @@ namespace YooAsset
{ {
YooLogger.Log("Begin to create catalog file !"); YooLogger.Log("Begin to create catalog file !");
string savePath = YooAssetSettingsData.GetYooResourcesFullPath();
if (UnityEditor.AssetDatabase.DeleteAsset(savePath))
UnityEditor.AssetDatabase.Refresh();
string rootPath = YooAssetSettingsData.GetYooDefaultBuildinRoot(); string rootPath = YooAssetSettingsData.GetYooDefaultBuildinRoot();
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath); DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists == false) if (rootDirectory.Exists == false)
@ -87,10 +83,23 @@ namespace YooAsset
} }
// 创建内置清单实例 // 创建内置清单实例
var buildinFileCatalog = ScriptableObject.CreateInstance<DefaultBuildinFileCatalog>(); var buildinFileCatalog = new DefaultBuildinFileCatalog();
buildinFileCatalog.PackageName = packageName; buildinFileCatalog.PackageName = packageName;
buildinFileCatalog.PackageVersion = packageVersion; buildinFileCatalog.PackageVersion = packageVersion;
// 创建白名单查询集合
HashSet<string> whiteFileList = new HashSet<string>
{
"link.xml",
"buildlogtep.json",
$"{packageName}.version",
$"{packageName}_{packageVersion}.bytes",
$"{packageName}_{packageVersion}.hash",
$"{packageName}_{packageVersion}.json",
$"{packageName}_{packageVersion}.report",
DefaultBuildinFileSystemDefine.BuildinCatalogFileName
};
// 记录所有内置资源文件 // 记录所有内置资源文件
DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory); DirectoryInfo rootDirectory = new DirectoryInfo(pacakgeDirectory);
FileInfo[] fileInfos = rootDirectory.GetFiles(); FileInfo[] fileInfos = rootDirectory.GetFiles();
@ -99,17 +108,7 @@ namespace YooAsset
if (fileInfo.Extension == ".meta") if (fileInfo.Extension == ".meta")
continue; continue;
if (fileInfo.Name == "link.xml" || fileInfo.Name == "buildlogtep.json") if (whiteFileList.Contains(fileInfo.Name))
continue;
if (fileInfo.Name == $"{packageName}.version")
continue;
if (fileInfo.Name == $"{packageName}_{packageVersion}.bytes")
continue;
if (fileInfo.Name == $"{packageName}_{packageVersion}.hash")
continue;
if (fileInfo.Name == $"{packageName}_{packageVersion}.json")
continue;
if (fileInfo.Name == $"{packageName}_{packageVersion}.report")
continue; continue;
string fileName = fileInfo.Name; string fileName = fileInfo.Name;
@ -125,18 +124,13 @@ namespace YooAsset
} }
// 创建输出目录 // 创建输出目录
string fullPath = YooAssetSettingsData.GetYooResourcesFullPath(); string saveFilePath = $"{pacakgeDirectory}/{DefaultBuildinFileSystemDefine.BuildinCatalogFileName}";
string saveFilePath = $"{fullPath}/{packageName}/{DefaultBuildinFileSystemDefine.BuildinCatalogFileName}"; if (File.Exists(saveFilePath))
FileUtility.CreateFileDirectory(saveFilePath); File.Delete(saveFilePath);
// 创建输出文件 // 创建输出文件
UnityEditor.AssetDatabase.CreateAsset(buildinFileCatalog, saveFilePath); File.WriteAllText(saveFilePath, JsonUtility.ToJson(buildinFileCatalog, false));
UnityEditor.EditorUtility.SetDirty(buildinFileCatalog); UnityEditor.AssetDatabase.Refresh();
#if UNITY_2019
UnityEditor.AssetDatabase.SaveAssets();
#else
UnityEditor.AssetDatabase.SaveAssetIfDirty(buildinFileCatalog);
#endif
Debug.Log($"Succeed to save buildin file catalog : {saveFilePath}"); Debug.Log($"Succeed to save buildin file catalog : {saveFilePath}");
return true; return true;

View File

@ -6,6 +6,6 @@ namespace YooAsset
/// <summary> /// <summary>
/// 内置清单文件名称 /// 内置清单文件名称
/// </summary> /// </summary>
public const string BuildinCatalogFileName = "BuildinCatalog.asset"; public const string BuildinCatalogFileName = "BuildinCatalog.json";
} }
} }

View File

@ -1,4 +1,5 @@
using UnityEngine; using System;
using UnityEngine;
namespace YooAsset namespace YooAsset
{ {
@ -7,12 +8,15 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
RequestData,
LoadCatalog, LoadCatalog,
Done, Done,
} }
private readonly DefaultBuildinFileSystem _fileSystem; private readonly DefaultBuildinFileSystem _fileSystem;
private UnityWebTextRequestOperation _webTextRequestOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private string _textData = null;
internal LoadBuildinCatalogFileOperation(DefaultBuildinFileSystem fileSystem) internal LoadBuildinCatalogFileOperation(DefaultBuildinFileSystem fileSystem)
@ -21,25 +25,54 @@ namespace YooAsset
} }
internal override void InternalStart() internal override void InternalStart()
{ {
_steps = ESteps.LoadCatalog; _steps = ESteps.RequestData;
} }
internal override void InternalUpdate() internal override void InternalUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.LoadCatalog) if (_steps == ESteps.RequestData)
{ {
string catalogFilePath = _fileSystem.GetCatalogFileLoadPath(); if (_webTextRequestOp == null)
var catalog = Resources.Load<DefaultBuildinFileCatalog>(catalogFilePath); {
if (catalog == null) string filePath = _fileSystem.GetCatalogFileLoadPath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
_webTextRequestOp.StartOperation();
AddChildOperation(_webTextRequestOp);
}
_webTextRequestOp.UpdateOperation();
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadCatalog;
_textData = _webTextRequestOp.Result;
}
else
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {catalogFilePath}"; Error = _webTextRequestOp.Error;
}
}
if (_steps == ESteps.LoadCatalog)
{
if (string.IsNullOrEmpty(_textData))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Buildin catalog file content is empty !";
return; return;
} }
try
{
var catalog = JsonUtility.FromJson<DefaultBuildinFileCatalog>(_textData);
if (catalog.PackageName != _fileSystem.PackageName) if (catalog.PackageName != _fileSystem.PackageName)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
@ -58,6 +91,13 @@ namespace YooAsset
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
} }
catch (Exception e)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {e.Message}";
}
}
} }
} }
} }

View File

@ -200,8 +200,7 @@ namespace YooAsset
} }
public string GetCatalogFileLoadPath() public string GetCatalogFileLoadPath()
{ {
string fileName = Path.GetFileNameWithoutExtension(DefaultBuildinFileSystemDefine.BuildinCatalogFileName); return PathUtility.Combine(_webPackageRoot, DefaultBuildinFileSystemDefine.BuildinCatalogFileName);
return YooAssetSettingsData.GetYooResourcesLoadPath(PackageName, fileName);
} }
/// <summary> /// <summary>

View File

@ -11,17 +11,15 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
RequestData,
LoadCatalog, LoadCatalog,
Done, Done,
} }
private readonly DefaultWebServerFileSystem _fileSystem; private readonly DefaultWebServerFileSystem _fileSystem;
private UnityWebTextRequestOperation _webTextRequestOp;
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private string _textData = null;
/// <summary>
/// 内置清单版本
/// </summary>
public string PackageVersion { private set; get; }
internal LoadWebServerCatalogFileOperation(DefaultWebServerFileSystem fileSystem) internal LoadWebServerCatalogFileOperation(DefaultWebServerFileSystem fileSystem)
{ {
@ -29,44 +27,79 @@ namespace YooAsset
} }
internal override void InternalStart() internal override void InternalStart()
{ {
_steps = ESteps.LoadCatalog; _steps = ESteps.RequestData;
} }
internal override void InternalUpdate() internal override void InternalUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.LoadCatalog) if (_steps == ESteps.RequestData)
{ {
string catalogFilePath = _fileSystem.GetCatalogFileLoadPath(); if (_webTextRequestOp == null)
var catalog = Resources.Load<DefaultBuildinFileCatalog>(catalogFilePath); {
if (catalog == null) string filePath = _fileSystem.GetCatalogFileLoadPath();
string url = DownloadSystemHelper.ConvertToWWWPath(filePath);
_webTextRequestOp = new UnityWebTextRequestOperation(url);
_webTextRequestOp.StartOperation();
AddChildOperation(_webTextRequestOp);
}
_webTextRequestOp.UpdateOperation();
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadCatalog;
_textData = _webTextRequestOp.Result;
}
else
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Failed to load web server catalog file : {catalogFilePath}"; Error = _webTextRequestOp.Error;
}
}
if (_steps == ESteps.LoadCatalog)
{
if (string.IsNullOrEmpty(_textData))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Buildin catalog file content is empty !";
return; return;
} }
try
{
var catalog = JsonUtility.FromJson<DefaultBuildinFileCatalog>(_textData);
if (catalog.PackageName != _fileSystem.PackageName) if (catalog.PackageName != _fileSystem.PackageName)
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Web server catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}"; Error = $"Catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
return; return;
} }
PackageVersion = catalog.PackageVersion;
foreach (var wrapper in catalog.Wrappers) foreach (var wrapper in catalog.Wrappers)
{ {
var fileWrapper = new DefaultWebServerFileSystem.FileWrapper(wrapper.FileName); var fileWrapper = new DefaultWebServerFileSystem.FileWrapper(wrapper.FileName);
_fileSystem.RecordCatalogFile(wrapper.BundleGUID, fileWrapper); _fileSystem.RecordCatalogFile(wrapper.BundleGUID, fileWrapper);
} }
YooLogger.Log($"Package '{_fileSystem.PackageName}' catalog files count : {catalog.Wrappers.Count}"); YooLogger.Log($"Package '{_fileSystem.PackageName}' buildin catalog files count : {catalog.Wrappers.Count}");
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
} }
catch (Exception e)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {e.Message}";
}
}
} }
} }
} }

View File

@ -106,28 +106,6 @@ namespace YooAsset
} }
#region 路径相关 #region 路径相关
/// <summary>
/// 获取YOO的Resources目录的加载路径
/// </summary>
internal static string GetYooResourcesLoadPath(string packageName, string fileName)
{
if (string.IsNullOrEmpty(Setting.DefaultYooFolderName))
return PathUtility.Combine(packageName, fileName);
else
return PathUtility.Combine(Setting.DefaultYooFolderName, packageName, fileName);
}
/// <summary>
/// 获取YOO的Resources目录的全路径
/// </summary>
internal static string GetYooResourcesFullPath()
{
if (string.IsNullOrEmpty(Setting.DefaultYooFolderName))
return $"Assets/Resources";
else
return $"Assets/Resources/{Setting.DefaultYooFolderName}";
}
/// <summary> /// <summary>
/// 获取YOO的编辑器下缓存文件根目录 /// 获取YOO的编辑器下缓存文件根目录
/// </summary> /// </summary>