refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-17 16:12:03 +08:00
parent 3e3661ad95
commit da222c6179
9 changed files with 269 additions and 315 deletions
@@ -15,13 +15,13 @@ namespace YooAsset
}
#endif
// 全局调度器名称
public const string GLOBAL_SCHEDULER_NAME = "YOOASSET_GLOBAL_SCHEDULER";
public const string GlobalSchedulerName = "YOOASSET_GLOBAL_SCHEDULER"; // 全局调度器名称
private const long MinTimeSlice = 10; // 最小时间片(毫秒)
private static readonly Dictionary<string, OperationScheduler> _schedulerDic = new Dictionary<string, OperationScheduler>(100);
private static readonly List<OperationScheduler> _schedulerList = new List<OperationScheduler>(100);
private static bool _isInitialize = false;
private static int _createIndex = 0;
private static bool _isInitialized;
private static int _createIndex;
// 计时器相关
private static Stopwatch _watch;
@@ -33,12 +33,16 @@ namespace YooAsset
/// </summary>
public static long MaxTimeSlice
{
get
{
return _maxTimeSlice;
}
set
{
if (value < 10)
if (value < MinTimeSlice)
{
_maxTimeSlice = 10;
YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds.");
_maxTimeSlice = MinTimeSlice;
YooLogger.Warning($"MaxTimeSlice minimum value is {MinTimeSlice} milliseconds.");
}
else
{
@@ -71,14 +75,17 @@ namespace YooAsset
/// </summary>
public static void Initialize()
{
if (_isInitialize == false)
if (_isInitialized)
{
_isInitialize = true;
_watch = Stopwatch.StartNew();
// 创建全局调度器
CreatePackageScheduler(GLOBAL_SCHEDULER_NAME, uint.MaxValue);
YooLogger.Warning("Operation system is already initialized!");
return;
}
_isInitialized = true;
_watch = Stopwatch.StartNew();
// 创建全局调度器
CreatePackageScheduler(GlobalSchedulerName, uint.MaxValue);
}
/// <summary>
@@ -86,7 +93,7 @@ namespace YooAsset
/// </summary>
public static void Update()
{
if (_isInitialize == false)
if (_isInitialized == false)
return;
// 检测是否需要执行排序
@@ -122,8 +129,7 @@ namespace YooAsset
/// </summary>
public static void DestroyAll()
{
_isInitialize = false;
YooLogger.Log("Operation system destroy all !");
_isInitialized = false;
// 清空所有调度器
foreach (var scheduler in _schedulerList)
@@ -136,7 +142,7 @@ namespace YooAsset
_watch = null;
_frameTime = 0;
MaxTimeSlice = long.MaxValue;
_maxTimeSlice = long.MaxValue;
}
/// <summary>
@@ -144,7 +150,7 @@ namespace YooAsset
/// </summary>
public static OperationScheduler CreatePackageScheduler(string packageName, uint priority)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
if (_schedulerDic.ContainsKey(packageName))
{
@@ -163,10 +169,10 @@ namespace YooAsset
/// </summary>
public static void DestroyPackageScheduler(string packageName)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
// 不允许销毁默认调度器
if (packageName == GLOBAL_SCHEDULER_NAME)
if (packageName == GlobalSchedulerName)
{
throw new YooInternalException("Cannot destroy the global package scheduler!");
}
@@ -184,7 +190,7 @@ namespace YooAsset
/// </summary>
public static void ClearPackageOperation(string packageName)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
var scheduler = GetScheduler(packageName);
scheduler.ClearAll();
@@ -195,7 +201,7 @@ namespace YooAsset
/// </summary>
public static void StartOperation(string packageName, AsyncOperationBase operation)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
var scheduler = GetScheduler(packageName);
scheduler.StartOperation(operation);
@@ -206,7 +212,7 @@ namespace YooAsset
/// </summary>
public static void SetSchedulerPriority(string packageName, uint priority)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
var scheduler = GetScheduler(packageName);
scheduler.Priority = priority;
@@ -217,7 +223,7 @@ namespace YooAsset
/// </summary>
public static uint GetSchedulerPriority(string packageName)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
var scheduler = GetScheduler(packageName);
return scheduler.Priority;
@@ -240,7 +246,7 @@ namespace YooAsset
#region 调试信息
internal static List<DebugOperationInfo> GetDebugOperationInfos(string packageName)
{
DebugCheckInitialize(packageName);
DebugEnsureInitialized(packageName);
var scheduler = GetScheduler(packageName);
return scheduler.GetDebugOperationInfos();
@@ -249,12 +255,12 @@ namespace YooAsset
#region 调试方法
[Conditional("DEBUG")]
private static void DebugCheckInitialize(string packageName)
private static void DebugEnsureInitialized(string packageName)
{
if (string.IsNullOrWhiteSpace(packageName))
throw new YooInternalException("Package name is null or empty.");
if (_isInitialize == false)
if (_isInitialized == false)
throw new YooInternalException($"{nameof(OperationSystem)} not initialized !");
}
#endregion