Skip to content

Commit

Permalink
fixed retry counter bug issue
Browse files Browse the repository at this point in the history
  • Loading branch information
michael811125 committed Sep 10, 2023
1 parent 071fe7f commit d2b97a3
Show file tree
Hide file tree
Showing 24 changed files with 215 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public RetryCounter(byte maxRetryCount)

public bool IsRetryValid()
{
return this.retryCount < maxRetryCount;
// 嘗試次數先++, 後判斷, 所以需使用 <= 進行判斷
return this.retryCount <= maxRetryCount;
}

public void AddRetryCount()
Expand All @@ -30,9 +31,9 @@ public void AddRetryCount()

protected Dictionary<string, RetryCounter> _loadingFlags;

public float reqSize { get; protected set; }
public float currentCount { get; protected set; }

public float totalSize { get; protected set; }
public float totalCount { get; protected set; }

public int Count { get { return this._cacher.Count; } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,45 @@ public T GetOperationHandle<T>() where T : OperationHandleBase
#region Raw
public string GetRawFileText()
{
return this.GetOperationHandle<RawFileOperationHandle>()?.GetRawFileText();
if (!this.IsValid()) return null;
return this.GetOperationHandle<RawFileOperationHandle>().GetRawFileText();
}

public byte[] GetRawFileData()
{
return this.GetOperationHandle<RawFileOperationHandle>()?.GetRawFileData();
if (!this.IsValid()) return null;
return this.GetOperationHandle<RawFileOperationHandle>().GetRawFileData();
}

public void UnloadRawFile()
{
if (this.IsValid()) this.GetOperationHandle<RawFileOperationHandle>()?.Release();
if (this.IsValid()) this.GetOperationHandle<RawFileOperationHandle>().Release();
}
#endregion

#region Scene
public Scene GetScene()
{
if (this.operationHandle == null) return new Scene();
if (!this.IsValid()) return new Scene();
return this.GetOperationHandle<SceneOperationHandle>().SceneObject;
}

public void UnloadScene()
{
if (this.IsValid()) this.GetOperationHandle<SceneOperationHandle>()?.UnloadAsync();
if (this.IsValid()) this.GetOperationHandle<SceneOperationHandle>().UnloadAsync();
}
#endregion

#region Asset
public T GetAsset<T>() where T : Object
{
if (!this.IsValid()) return null;
return this.GetOperationHandle<AssetOperationHandle>()?.AssetObject as T;
}

public void UnloadAsset()
{
if (this.IsValid()) this.GetOperationHandle<AssetOperationHandle>()?.Release();
if (this.IsValid()) this.GetOperationHandle<AssetOperationHandle>().Release();
}
#endregion

Expand Down
161 changes: 84 additions & 77 deletions Assets/OxGFrame/AssetLoader/Scripts/Runtime/Cacher/CacheBundle.cs

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions Assets/OxGFrame/AssetLoader/Scripts/Runtime/Cacher/CacheResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public async UniTask PreloadAssetAsync<T>(string[] assetNames, Progression progr
if (assetNames == null || assetNames.Length == 0) return;

// 先初始加載進度
this.reqSize = 0;
this.totalSize = assetNames.Length;
this.currentCount = 0;
this.totalCount = assetNames.Length;

for (int i = 0; i < assetNames.Length; i++)
{
Expand All @@ -58,8 +58,8 @@ public async UniTask PreloadAssetAsync<T>(string[] assetNames, Progression progr
// 如果有在緩存中就不進行預加載
if (this.HasInCache(assetName))
{
this.reqSize++;
progression?.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount++;
progression?.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);
this.RemoveLoadingFlags(assetName);
continue;
}
Expand All @@ -71,14 +71,14 @@ public async UniTask PreloadAssetAsync<T>(string[] assetNames, Progression progr

if (req != null)
{
float lastSize = 0;
float lastCount = 0;
do
{
if (progression != null)
{
this.reqSize += (req.progress - lastSize);
lastSize = req.progress;
progression.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount += (req.progress - lastCount);
lastCount = req.progress;
progression.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);
}

if (req.isDone)
Expand Down Expand Up @@ -119,8 +119,8 @@ public void PreloadAsset<T>(string[] assetNames, Progression progression, byte m
if (assetNames == null || assetNames.Length == 0) return;

// 先初始加載進度
this.reqSize = 0;
this.totalSize = assetNames.Length;
this.currentCount = 0;
this.totalCount = assetNames.Length;

for (int i = 0; i < assetNames.Length; i++)
{
Expand All @@ -142,8 +142,8 @@ public void PreloadAsset<T>(string[] assetNames, Progression progression, byte m
// 如果有在緩存中就不進行預加載
if (this.HasInCache(assetName))
{
this.reqSize++;
progression?.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount++;
progression?.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);
this.RemoveLoadingFlags(assetName);
continue;
}
Expand All @@ -154,8 +154,8 @@ public void PreloadAsset<T>(string[] assetNames, Progression progression, byte m
var asset = Resources.Load<T>(assetName);
if (asset != null)
{
this.reqSize++;
progression?.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount++;
progression?.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);

loaded = true;
pack.SetPack(assetName, asset);
Expand Down Expand Up @@ -197,8 +197,8 @@ public async UniTask<T> LoadAssetAsync<T>(string assetName, Progression progress
}

// 初始加載進度
this.reqSize = 0;
this.totalSize = 1;
this.currentCount = 0;
this.totalCount = 1;

// Loading 標記
this.AddLoadingFlags(assetName, maxRetryCount);
Expand All @@ -215,14 +215,14 @@ public async UniTask<T> LoadAssetAsync<T>(string assetName, Progression progress

if (req != null)
{
float lastSize = 0;
float lastCount = 0;
do
{
if (progression != null)
{
this.reqSize += (req.progress - lastSize);
lastSize = req.progress;
progression.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount += (req.progress - lastCount);
lastCount = req.progress;
progression.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);
}

if (req.isDone)
Expand All @@ -244,8 +244,8 @@ public async UniTask<T> LoadAssetAsync<T>(string assetName, Progression progress
}
else
{
this.reqSize = this.totalSize;
progression?.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount = this.totalCount;
progression?.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);
}

var asset = pack.GetAsset<T>();
Expand Down Expand Up @@ -281,8 +281,8 @@ public T LoadAsset<T>(string assetName, Progression progression, byte maxRetryCo
}

// 初始加載進度
this.reqSize = 0;
this.totalSize = 1;
this.currentCount = 0;
this.totalCount = 1;

// Loading 標記
this.AddLoadingFlags(assetName, maxRetryCount);
Expand All @@ -299,8 +299,8 @@ public T LoadAsset<T>(string assetName, Progression progression, byte maxRetryCo
asset = Resources.Load<T>(assetName);
if (asset != null)
{
this.reqSize = this.totalSize;
progression?.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount = this.totalCount;
progression?.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);

loaded = true;
pack.SetPack(assetName, asset);
Expand All @@ -315,8 +315,8 @@ public T LoadAsset<T>(string assetName, Progression progression, byte maxRetryCo
}
else
{
this.reqSize = this.totalSize;
progression?.Invoke(this.reqSize / this.totalSize, this.reqSize, this.totalSize);
this.currentCount = this.totalCount;
progression?.Invoke(this.currentCount / this.totalCount, this.currentCount, this.totalCount);
}

asset = pack.GetAsset<T>();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace OxGFrame.AssetLoader
namespace OxGFrame.AssetLoader.Cacher
{
public delegate void Progression(float progress, float reqSize, float totalSize);

public interface ICache<T>
{
bool HasInCache(string name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;

namespace OxGFrame.AssetLoader.GroupCacher
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public async UniTask<T> LoadRawFileAsync<T>(int id, string packageName, string a
if (keyGroup != null)
{
keyGroup.AddRef();

Logging.Print<Logger>($"【Load】 => Current << GroupBundle >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");
}
}
Expand All @@ -85,7 +84,6 @@ public T LoadRawFile<T>(int id, string packageName, string assetName, Progressio
if (keyGroup != null)
{
keyGroup.AddRef();

Logging.Print<Logger>($"【Load】 => Current << GroupBundle >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");
}
}
Expand All @@ -99,21 +97,18 @@ public void UnloadRawFile(int id, string assetName, bool forceUnload)
if (keyGroup != null)
{
keyGroup.DelRef();

Logging.Print<Logger>($"【Unload】 => Current << GroupBundle >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");

// 強制釋放
if (forceUnload)
{
this.DelFromCache(id, keyGroup.assetName);

Logging.Print<Logger>($"【Force Unload Completes】 => Current << GroupBundle >> Cache Count: {this.Count}, GroupId: {id}");
}
// 使用引用計數釋放
else if (keyGroup.refCount <= 0)
{
this.DelFromCache(id, keyGroup.assetName);

Logging.Print<Logger>($"【Unload Completes】 => Current << GroupBundle >> Cache Count: {this.Count}, GroupId: {id}");
}

Expand Down Expand Up @@ -192,7 +187,6 @@ public async UniTask<T> LoadAssetAsync<T>(int id, string packageName, string ass
if (keyGroup != null)
{
keyGroup.AddRef();

Logging.Print<Logger>($"【Load】 => Current << GroupBundle >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");
}
}
Expand All @@ -213,7 +207,6 @@ public T LoadAsset<T>(int id, string packageName, string assetName, Progression
if (keyGroup != null)
{
keyGroup.AddRef();

Logging.Print<Logger>($"【Load】 => Current << GroupBundle >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");
}
}
Expand All @@ -227,21 +220,18 @@ public void UnloadAsset(int id, string assetName, bool forceUnload)
if (keyGroup != null)
{
keyGroup.DelRef();

Logging.Print<Logger>($"【Unload】 => Current << GroupBundle >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");

// 強制釋放
if (forceUnload)
{
this.DelFromCache(id, keyGroup.assetName);

Logging.Print<Logger>($"【Force Unload Completes】 => Current << GroupBundle >> Cache Count: {this.Count}, GroupId: {id}");
}
// 使用引用計數釋放
else if (keyGroup.refCount <= 0)
{
this.DelFromCache(id, keyGroup.assetName);

Logging.Print<Logger>($"【Unload Completes】 => Current << GroupBundle >> Cache Count: {this.Count}, GroupId: {id}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public async UniTask<T> LoadAssetAsync<T>(int id, string assetName, Progression
if (keyGroup != null)
{
keyGroup.AddRef();

Logging.Print<Logger>($"【Load】 => Current << GroupResource >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");
}
}
Expand All @@ -84,7 +83,6 @@ public T LoadAsset<T>(int id, string assetName, Progression progression, byte ma
if (keyGroup != null)
{
keyGroup.AddRef();

Logging.Print<Logger>($"【Load】 => Current << GroupResource >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");
}
}
Expand All @@ -98,21 +96,18 @@ public void UnloadAsset(int id, string assetName, bool forceUnload)
if (keyGroup != null)
{
keyGroup.DelRef();

Logging.Print<Logger>($"【Unload】 => Current << GroupResource >> Cache Count: {this.Count}, KeyRef: {keyGroup.refCount}, GroupId: {id}");

// 強制釋放
if (forceUnload)
{
this.DelFromCache(id, keyGroup.assetName);

Logging.Print<Logger>($"【Force Unload Completes】 => Current << GroupResource >> Cache Count: {this.Count}, GroupId: {id}");
}
// 使用引用計數釋放
else if (keyGroup.refCount <= 0)
{
this.DelFromCache(id, keyGroup.assetName);

Logging.Print<Logger>($"【Unload Completes】 => Current << GroupResource >> Cache Count: {this.Count}, GroupId: {id}");
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OxGFrame.AssetLoader
namespace OxGFrame.AssetLoader.GroupCacher
{
public interface IGroupCache<T>
{
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions Assets/OxGFrame/AssetLoader/Scripts/Runtime/Progression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace OxGFrame.AssetLoader
{
public delegate void Progression(float progress, float currentCount, float totalCount);
}
11 changes: 11 additions & 0 deletions Assets/OxGFrame/AssetLoader/Scripts/Runtime/Progression.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d2b97a3

Please sign in to comment.