Update runtime code

This commit is contained in:
hevinci
2022-09-29 18:40:43 +08:00
parent 46c219505f
commit a71921cdd1
36 changed files with 844 additions and 1625 deletions
@@ -1,111 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 清理未使用的缓存资源操作类
/// </summary>
public abstract class ClearUnusedCacheFilesOperation : AsyncOperationBase
{
}
/// <summary>
/// 编辑器模式
/// </summary>
internal sealed class EditorPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
/// <summary>
/// 离线模式
/// </summary>
internal sealed class OfflinePlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
/// <summary>
/// 联机模式
/// </summary>
internal sealed class HostPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
private enum ESteps
{
None,
GetUnusedCacheFiles,
ClearUnusedCacheFiles,
Done,
}
private ESteps _steps = ESteps.None;
private List<string> _unusedCacheFilePaths;
private int _unusedFileTotalCount = 0;
private HostPlayModeImpl _impl;
internal HostPlayModeClearUnusedCacheFilesOperation(HostPlayModeImpl impl)
{
_impl = impl;
}
internal override void Start()
{
_steps = ESteps.GetUnusedCacheFiles;
}
internal override void Update()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetUnusedCacheFiles)
{
_unusedCacheFilePaths = _impl.ClearUnusedCacheFilePaths();
_unusedFileTotalCount = _unusedCacheFilePaths.Count;
YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}");
_steps = ESteps.ClearUnusedCacheFiles;
}
if (_steps == ESteps.ClearUnusedCacheFiles)
{
for (int i = _unusedCacheFilePaths.Count - 1; i >= 0; i--)
{
string filePath = _unusedCacheFilePaths[i];
if (File.Exists(filePath))
{
YooLogger.Log($"Delete unused cache file : {filePath}");
File.Delete(filePath);
}
_unusedCacheFilePaths.RemoveAt(i);
if (OperationSystem.IsBusy)
break;
}
if (_unusedFileTotalCount == 0)
Progress = 1.0f;
else
Progress = 1.0f - (_unusedCacheFilePaths.Count / _unusedFileTotalCount);
if (_unusedCacheFilePaths.Count == 0)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 5d188c50fd00bf941b2eeebb374dc0d1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -20,7 +20,6 @@ namespace YooAsset
private enum ESteps
{
None,
Builder,
Load,
Done,
}
@@ -36,26 +35,10 @@ namespace YooAsset
}
internal override void Start()
{
if (string.IsNullOrEmpty(_simulatePatchManifestPath))
_steps = ESteps.Builder;
else
_steps = ESteps.Load;
_steps = ESteps.Load;
}
internal override void Update()
{
if (_steps == ESteps.Builder)
{
_simulatePatchManifestPath = EditorSimulateModeHelper.SimulateBuild();
if (string.IsNullOrEmpty(_simulatePatchManifestPath))
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Simulate build failed, see the detail info on the console window.";
return;
}
_steps = ESteps.Load;
}
if (_steps == ESteps.Load)
{
if (File.Exists(_simulatePatchManifestPath) == false)
@@ -3,20 +3,22 @@ using System.Reflection;
namespace YooAsset
{
internal static class EditorSimulateModeHelper
public static class EditorSimulateModeHelper
{
private static System.Type _classType;
public static string SimulateBuild()
/// <summary>
/// 编辑器下模拟构建补丁清单
/// </summary>
public static string SimulateBuild(string packageName, bool enableAddressable)
{
_classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder");
InvokePublicStaticMethod(_classType, "SimulateBuild");
return GetPatchManifestFilePath();
}
private static string GetPatchManifestFilePath()
{
return (string)InvokePublicStaticMethod(_classType, "GetPatchManifestPath");
if (_classType == null)
_classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder");
string manifestFilePath = (string)InvokePublicStaticMethod(_classType, "SimulateBuild", packageName, enableAddressable);
return manifestFilePath;
}
private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters)
{
var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static);
@@ -30,8 +32,14 @@ namespace YooAsset
}
}
#else
internal static class EditorSimulateModeHelper
namespace YooAsset
{
public static class EditorSimulateModeHelper
{
public static string SimulateBuild() { throw new System.Exception("Only support in unity editor !"); }
/// <summary>
/// 编辑器下模拟构建补丁清单
/// </summary>
public static string SimulateBuild(string packageName, bool enableAddressable) { throw new System.Exception("Only support in unity editor !"); }
}
}
#endif
@@ -57,6 +57,10 @@ namespace YooAsset
{
return _simulatePatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _simulatePatchManifest.PackageName;
}
#endregion
}
}
@@ -71,35 +71,6 @@ namespace YooAsset
return operation;
}
/// <summary>
/// 获取未被使用的缓存文件路径集合
/// </summary>
public List<string> ClearUnusedCacheFilePaths()
{
string cacheFolderPath = SandboxHelper.GetCacheFolderPath();
if (Directory.Exists(cacheFolderPath) == false)
return new List<string>();
DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath);
FileInfo[] fileInfos = directoryInfo.GetFiles();
List<string> result = new List<string>(fileInfos.Length);
foreach (FileInfo fileInfo in fileInfos)
{
bool used = false;
foreach (var patchBundle in LocalPatchManifest.BundleList)
{
if (fileInfo.Name == patchBundle.FileName)
{
used = true;
break;
}
}
if (used == false)
result.Add(fileInfo.FullName);
}
return result;
}
/// <summary>
/// 创建下载器
/// </summary>
@@ -379,6 +350,10 @@ namespace YooAsset
{
return LocalPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return LocalPatchManifest.PackageName;
}
#endregion
}
}
@@ -85,6 +85,10 @@ namespace YooAsset
{
return _appPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _appPatchManifest.PackageName;
}
#endregion
}
}