Support fast build mode.

支持快速构建模式,真实的实现EditorPlayMode的运行时环境。
This commit is contained in:
hevinci
2022-05-02 23:15:09 +08:00
parent 8d02406211
commit 665a16fe60
39 changed files with 492 additions and 383 deletions
@@ -9,6 +9,7 @@ namespace YooAsset
LoadFromStreaming,
LoadFromCache,
LoadFromRemote,
LoadFromEditor,
}
private readonly PatchBundle _patchBundle;
@@ -32,6 +33,11 @@ namespace YooAsset
/// </summary>
internal string RemoteFallbackURL { private set; get; }
/// <summary>
/// 编辑器资源路径
/// </summary>
public string EditorAssetPath { private set; get; }
/// <summary>
/// 文件哈希值
/// </summary>
@@ -113,6 +119,16 @@ namespace YooAsset
BundleName = patchBundle.BundleName;
RemoteMainURL = mainURL;
RemoteFallbackURL = fallbackURL;
EditorAssetPath = string.Empty;
}
internal BundleInfo(PatchBundle patchBundle, ELoadMode loadMode, string editorAssetPath)
{
_patchBundle = patchBundle;
LoadMode = loadMode;
BundleName = patchBundle.BundleName;
RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty;
EditorAssetPath = editorAssetPath;
}
internal BundleInfo(PatchBundle patchBundle, ELoadMode loadMode)
{
@@ -121,6 +137,7 @@ namespace YooAsset
BundleName = patchBundle.BundleName;
RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty;
EditorAssetPath = string.Empty;
}
internal BundleInfo(string bundleName)
{
@@ -129,6 +146,7 @@ namespace YooAsset
BundleName = bundleName;
RemoteMainURL = string.Empty;
RemoteFallbackURL = string.Empty;
EditorAssetPath = string.Empty;
}
/// <summary>
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
@@ -16,12 +17,50 @@ namespace YooAsset
/// </summary>
internal sealed class EditorPlayModeInitializationOperation : InitializationOperation
{
private enum ESteps
{
None,
Builder,
Done,
}
private readonly EditorPlayModeImpl _impl;
private ESteps _steps = ESteps.None;
internal EditorPlayModeInitializationOperation(EditorPlayModeImpl impl)
{
_impl = impl;
}
internal override void Start()
{
Status = EOperationStatus.Succeed;
_steps = ESteps.Builder;
}
internal override void Update()
{
if (_steps == ESteps.Builder)
{
string manifestFilePath = EditorPlayModeHelper.DryRunBuild();
if (string.IsNullOrEmpty(manifestFilePath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Dry run build failed, see the detail info on the console window.";
return;
}
if (File.Exists(manifestFilePath) == false)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Manifest file not found : {manifestFilePath}";
return;
}
YooLogger.Log($"Load manifest file in editor play mode : {manifestFilePath}");
string jsonContent = FileUtility.ReadFile(manifestFilePath);
_impl.AppPatchManifest = PatchManifest.Deserialize(jsonContent);
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
@@ -152,6 +191,7 @@ namespace YooAsset
}
}
/// <summary>
/// 内置补丁清单加载器
/// </summary>
@@ -51,10 +51,10 @@ namespace YooAsset
public readonly Dictionary<string, PatchAsset> Assets = new Dictionary<string, PatchAsset>();
/// <summary>
/// 可寻址地址映射集合
/// 资源路径映射集合
/// </summary>
[NonSerialized]
public readonly Dictionary<string, string> AddressDic = new Dictionary<string, string>();
public readonly Dictionary<string, string> AssetPathMapping = new Dictionary<string, string>();
/// <summary>
@@ -120,17 +120,39 @@ namespace YooAsset
}
/// <summary>
/// 可寻址地址转换为资源路径
/// 尝试获取资源包的主资源路径
/// </summary>
public string ConvertAddress(string address)
public string TryGetBundleMainAssetPath(string bundleName)
{
if (AddressDic.TryGetValue(address, out string assetPath))
foreach (var patchAsset in AssetList)
{
int bundleID = patchAsset.BundleID;
if (bundleID >= 0 && bundleID < BundleList.Count)
{
var patchBundle = BundleList[bundleID];
if (patchBundle.BundleName == bundleName)
return patchAsset.AssetPath;
}
else
{
throw new Exception($"Invalid depend id : {bundleID} Asset path : {patchAsset.AssetPath}");
}
}
return string.Empty;
}
/// <summary>
/// 映射为资源路径
/// </summary>
public string MappingToAssetPath(string location)
{
if (AssetPathMapping.TryGetValue(location, out string assetPath))
{
return assetPath;
}
else
{
YooLogger.Warning($"Not found address in patch manifest : {address}");
YooLogger.Warning($"Failed to mapping location to asset path : {location}");
return string.Empty;
}
}
@@ -162,36 +184,47 @@ namespace YooAsset
// AssetList
foreach (var patchAsset in patchManifest.AssetList)
{
string assetPath = patchAsset.AssetPath;
// 添加原始路径
// 注意:我们不允许原始路径存在重名
string assetPath = patchAsset.AssetPath;
if (patchManifest.Assets.ContainsKey(assetPath))
throw new Exception($"Asset path have existed : {assetPath}");
throw new Exception($"AssetPath have existed : {assetPath}");
else
patchManifest.Assets.Add(assetPath, patchAsset);
// 添加去掉后缀名的路径
if (Path.HasExtension(assetPath))
{
string assetPathWithoutExtension = StringUtility.RemoveExtension(assetPath);
if (patchManifest.Assets.ContainsKey(assetPathWithoutExtension))
YooLogger.Warning($"Asset path have existed : {assetPathWithoutExtension}");
else
patchManifest.Assets.Add(assetPathWithoutExtension, patchAsset);
}
}
// Address
// AssetPathMapping
if (patchManifest.EnableAddressable)
{
foreach (var patchAsset in patchManifest.AssetList)
{
string address = patchAsset.Address;
if (patchManifest.AddressDic.ContainsKey(address))
if (patchManifest.AssetPathMapping.ContainsKey(address))
throw new Exception($"Address have existed : {address}");
else
patchManifest.AddressDic.Add(address, patchAsset.AssetPath);
patchManifest.AssetPathMapping.Add(address, patchAsset.AssetPath);
}
}
else
{
foreach (var patchAsset in patchManifest.AssetList)
{
string assetPath = patchAsset.AssetPath;
// 添加原生路径的映射
if (patchManifest.AssetPathMapping.ContainsKey(assetPath))
throw new Exception($"AssetPath have existed : {assetPath}");
else
patchManifest.AssetPathMapping.Add(assetPath, assetPath);
// 添加无后缀名路径的映射
if (Path.HasExtension(assetPath))
{
string assetPathWithoutExtension = StringUtility.RemoveExtension(assetPath);
if (patchManifest.AssetPathMapping.ContainsKey(assetPathWithoutExtension))
YooLogger.Warning($"AssetPath have existed : {assetPathWithoutExtension}");
else
patchManifest.AssetPathMapping.Add(assetPathWithoutExtension, assetPath);
}
}
}
@@ -0,0 +1,37 @@
#if UNITY_EDITOR
using System.Reflection;
namespace YooAsset
{
internal static class EditorPlayModeHelper
{
private static System.Type _classType;
public static string DryRunBuild()
{
_classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleRuntimeBuilder");
InvokePublicStaticMethod(_classType, "FastBuild");
return GetPatchManifestFilePath();
}
private static string GetPatchManifestFilePath()
{
return (string)InvokePublicStaticMethod(_classType, "GetPatchManifestFilePath");
}
private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters)
{
var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null)
{
UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}");
return null;
}
return methodInfo.Invoke(null, parameters);
}
}
}
#else
internal static class EditorPlayModeHelper
{
public static string DryRunBuild() { throw new System.Exception("Only support in unity editor !"); }
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5b09fe4b7aa2d2143bc538976541b039
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -6,12 +6,14 @@ namespace YooAsset
{
internal class EditorPlayModeImpl : IBundleServices
{
internal PatchManifest AppPatchManifest;
/// <summary>
/// 异步初始化
/// </summary>
public InitializationOperation InitializeAsync()
{
var operation = new EditorPlayModeInitializationOperation();
var operation = new EditorPlayModeInitializationOperation(this);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
@@ -21,27 +23,41 @@ namespace YooAsset
/// </summary>
public int GetResourceVersion()
{
return 0;
if (AppPatchManifest == null)
return 0;
return AppPatchManifest.ResourceVersion;
}
#region IBundleServices接口
BundleInfo IBundleServices.GetBundleInfo(string bundleName)
{
YooLogger.Warning($"Editor play mode can not get bundle info.");
BundleInfo bundleInfo = new BundleInfo(bundleName);
return bundleInfo;
if (string.IsNullOrEmpty(bundleName))
return new BundleInfo(string.Empty);
if (AppPatchManifest.Bundles.TryGetValue(bundleName, out PatchBundle patchBundle))
{
string mainAssetPath = AppPatchManifest.TryGetBundleMainAssetPath(bundleName);
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromEditor, mainAssetPath);
return bundleInfo;
}
else
{
YooLogger.Warning($"Not found bundle in patch manifest : {bundleName}");
BundleInfo bundleInfo = new BundleInfo(bundleName);
return bundleInfo;
}
}
string IBundleServices.ConvertAddress(string address)
string IBundleServices.MappingToAssetPath(string location)
{
throw new Exception($"Editor play mode not support addressable.");
return AppPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetBundleName(string assetPath)
{
return assetPath;
return AppPatchManifest.GetBundleName(assetPath);
}
string[] IBundleServices.GetAllDependencies(string assetPath)
{
return new string[] { };
return AppPatchManifest.GetAllDependencies(assetPath);
}
#endregion
}
@@ -309,9 +309,9 @@ namespace YooAsset
return bundleInfo;
}
}
string IBundleServices.ConvertAddress(string address)
string IBundleServices.MappingToAssetPath(string location)
{
return LocalPatchManifest.ConvertAddress(address);
return LocalPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetBundleName(string assetPath)
{
@@ -56,9 +56,9 @@ namespace YooAsset
return bundleInfo;
}
}
string IBundleServices.ConvertAddress(string address)
string IBundleServices.MappingToAssetPath(string location)
{
return AppPatchManifest.ConvertAddress(address);
return AppPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetBundleName(string assetPath)
{