Skip to content

Commit

Permalink
added DownloadSpeedCalculator
Browse files Browse the repository at this point in the history
  • Loading branch information
michael811125 committed May 15, 2023
1 parent 503e38f commit 39e906a
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 28 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<root Version="2.4">
<Common AutoAddressable="True" UniqueBundleName="False" ShowPackageView="True" ShowEditorAlias="False" />
<Package PackageName="DefaultPackage" PackageDesc="">
<Group GroupActiveRule="EnableGroup" GroupName="ModelGroup" GroupDesc="" AssetTags="models">
<Collector CollectPath="Assets/OxGFrame/AssetLoader/Example/Prefabs/model_01.prefab" CollectGUID="b4ea3356cddbd8b41926c28adefdfbc5" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
</Group>
<Group GroupActiveRule="EnableGroup" GroupName="SceneGroup" GroupDesc="" AssetTags="scenes">
<Collector CollectPath="Assets/OxGFrame/CoreFrame/Example/USFrameDemo/Scenes/LevelDemo01.unity" CollectGUID="97102f27cdcc74846bb0909829b431cd" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
<Collector CollectPath="Assets/OxGFrame/CoreFrame/Example/USFrameDemo/Scenes/LevelDemo02.unity" CollectGUID="9f25400e0da4e034294d5e153b0533ab" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
<Collector CollectPath="Assets/OxGFrame/CoreFrame/Example/USFrameDemo/Scenes/LevelDemo03.unity" CollectGUID="42b42b7f9b5995748ba85a54431669b3" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
</Group>
</Package>
<Package PackageName="OtherPackage" PackageDesc="">
<Group GroupActiveRule="EnableGroup" GroupName="ModelGroup" GroupDesc="" AssetTags="models">
<Collector CollectPath="Assets/OxGFrame/AssetLoader/Example/Prefabs/model_01.prefab" CollectGUID="b4ea3356cddbd8b41926c28adefdfbc5" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
</Group>
<Group GroupActiveRule="EnableGroup" GroupName="SceneGroup" GroupDesc="" AssetTags="scenes">
<Collector CollectPath="Assets/OxGFrame/CoreFrame/Example/USFrameDemo/Scenes/LevelDemo01.unity" CollectGUID="97102f27cdcc74846bb0909829b431cd" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
<Collector CollectPath="Assets/OxGFrame/CoreFrame/Example/USFrameDemo/Scenes/LevelDemo02.unity" CollectGUID="9f25400e0da4e034294d5e153b0533ab" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
<Collector CollectPath="Assets/OxGFrame/CoreFrame/Example/USFrameDemo/Scenes/LevelDemo03.unity" CollectGUID="42b42b7f9b5995748ba85a54431669b3" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
</Group>
</Package>
<Package PackageName="DlcPackage" PackageDesc="">
<Group GroupActiveRule="EnableGroup" GroupName="ModelGroup" GroupDesc="" AssetTags="models">
<Collector CollectPath="Assets/OxGFrame/AssetLoader/Example/Prefabs/model_01.prefab" CollectGUID="b4ea3356cddbd8b41926c28adefdfbc5" CollectType="MainAssetCollector" AddressRule="AddressByFileName" PackRule="PackSeparately" FilterRule="CollectAll" UserData="" AssetTags="" />
</Group>
</Package>
</root>

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

Binary file not shown.

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

4 changes: 4 additions & 0 deletions Assets/OxGFrame/AssetLoader/Example/BundleDemo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. Unzip BundleDemo_CDN.7z and then upload CDN folder to server (Don't forget double check url path in burlconfig.conf).
2. Open BundleDemo scene.
3. Select HostMode on PatchLauncher.
4. Play
7 changes: 7 additions & 0 deletions Assets/OxGFrame/AssetLoader/Example/BundleDemo/README.md.meta

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
Expand Up @@ -60,6 +60,7 @@ private void _InitPatchEvents()
// 7. PatchVersionUpdateFailed
// 8. PatchManifestUpdateFailed
// 9. PatchDownloadFailed
// 10. PatchDownloadCanceled

#region Add PatchEvents Handle
this._patchEvents.AddListener<PatchEvents.PatchRepairFailed>(this._OnHandleEventMessage);
Expand All @@ -72,6 +73,7 @@ private void _InitPatchEvents()
this._patchEvents.AddListener<PatchEvents.PatchVersionUpdateFailed>(this._OnHandleEventMessage);
this._patchEvents.AddListener<PatchEvents.PatchManifestUpdateFailed>(this._OnHandleEventMessage);
this._patchEvents.AddListener<PatchEvents.PatchDownloadFailed>(this._OnHandleEventMessage);
this._patchEvents.AddListener<PatchEvents.PatchDownloadCanceled>(this._OnHandleEventMessage);
#endregion
}

Expand Down Expand Up @@ -121,7 +123,7 @@ private void _OnHandleEventMessage(IEventMessage message)
case PatchFsmStates.FsmBeginDownload:
this.msg.text = "Begin Download Files";
if (!this.progressGroup.activeSelf) this.progressGroup.SetActive(true);
if (!this.downloadBtns.activeSelf) this.downloadBtns.SetActive(true);
//if (!this.downloadBtns.activeSelf) this.downloadBtns.SetActive(true);
break;
case PatchFsmStates.FsmDownloadOver:
this.msg.text = "Download Over";
Expand Down Expand Up @@ -193,7 +195,8 @@ private void _OnHandleEventMessage(IEventMessage message)
$"TotalCount: {downloadInfo.totalDownloadCount}, " +
$"TotalSize: {BundleUtility.GetBytesToString((ulong)downloadInfo.totalDownloadSizeBytes)}, " +
$"CurrentCount: {downloadInfo.currentDownloadCount}, " +
$"CurrentSize: {BundleUtility.GetBytesToString((ulong)downloadInfo.currentDownloadSizeBytes)}"
$"CurrentSize: {BundleUtility.GetBytesToString((ulong)downloadInfo.currentDownloadSizeBytes)}" +
$"DownloadSpeed: {BundleUtility.GetSpeedBytesToString((ulong)downloadInfo.downloadSpeedBytes)}"
);

this._UpdateDownloadInfo
Expand All @@ -202,7 +205,8 @@ private void _OnHandleEventMessage(IEventMessage message)
downloadInfo.currentDownloadCount,
downloadInfo.currentDownloadSizeBytes,
downloadInfo.totalDownloadCount,
downloadInfo.totalDownloadSizeBytes
downloadInfo.totalDownloadSizeBytes,
downloadInfo.downloadSpeedBytes
);
#endregion
}
Expand All @@ -228,20 +232,27 @@ private void _OnHandleEventMessage(IEventMessage message)
this._retryType = 5;
this.ShowRetryWindow("Patch Download Failed");
}
else if (message is PatchEvents.PatchDownloadCanceled)
{
// Show Patch Download Canceled Retry UI

this._retryType = 5;
this.ShowRetryWindow("Patch Download Canceled");
}
else
{
throw new System.NotImplementedException($"{message.GetType()}");
}
}

private void _UpdateDownloadInfo(float progress, int dlCount, long dlBytes, int totalCount, long totalBytes)
private void _UpdateDownloadInfo(float progress, int dlCount, long dlBytes, int totalCount, long totalBytes, long dlSpeedBytes)
{
if (!this.progressGroup.activeSelf) this.progressGroup.SetActive(true);

var strBuilder = new StringBuilder();
strBuilder.Append($"Patch Size: {BundleUtility.GetBytesToString((ulong)totalBytes)}");
strBuilder.Append($", {dlCount} (DC) / {totalCount} (PC)");
strBuilder.Append($"\nCurrent Download Size: {BundleUtility.GetBytesToString((ulong)dlBytes)}");
strBuilder.Append($"\nCurrent Download Size: {BundleUtility.GetBytesToString((ulong)dlBytes)}, Download Speed: {BundleUtility.GetSpeedBytesToString((ulong)dlSpeedBytes)}");
this.info.text = strBuilder.ToString();

this.progress.size = progress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace OxGFrame.AssetLoader.PatchEvent
// 7. PatchCreateDownloader
// 8. PatchDownloadProgression
// 9. PatchDownloadFailed
// 10. PatchDownloadCanceled

public static class PatchEvents
{
Expand Down Expand Up @@ -129,14 +130,16 @@ public class PatchDownloadProgression : IEventMessage
public int currentDownloadCount;
public long totalDownloadSizeBytes;
public long currentDownloadSizeBytes;
public long downloadSpeedBytes;

public static void SendEventMessage(int totalDownloadCount, int currentDownloadCount, long totalDownloadSizeBytes, long currentDownloadSizeBytes)
public static void SendEventMessage(int totalDownloadCount, int currentDownloadCount, long totalDownloadSizeBytes, long currentDownloadSizeBytes, long downloadSpeedBytes)
{
var msg = new PatchDownloadProgression();
msg.totalDownloadCount = totalDownloadCount;
msg.currentDownloadCount = currentDownloadCount;
msg.totalDownloadSizeBytes = totalDownloadSizeBytes;
msg.currentDownloadSizeBytes = currentDownloadSizeBytes;
msg.downloadSpeedBytes = downloadSpeedBytes;
msg.progress = (msg.currentDownloadSizeBytes * 1f) / (msg.totalDownloadSizeBytes * 1f);
UniEvent.SendMessage(msg);
}
Expand All @@ -158,5 +161,17 @@ public static void SendEventMessage(string fileName, string error)
UniEvent.SendMessage(msg);
}
}

/// <summary>
/// Download canceled
/// </summary>
public class PatchDownloadCanceled : IEventMessage
{
public static void SendEventMessage()
{
var msg = new PatchDownloadCanceled();
UniEvent.SendMessage(msg);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@ private async UniTask _StartDownload()
ResourceDownloaderOperation downloader = PatchManager.GetInstance().mainDownloader;

downloader.OnDownloadErrorCallback = PatchEvents.PatchDownloadFailed.SendEventMessage;
downloader.OnDownloadProgressCallback = PatchEvents.PatchDownloadProgression.SendEventMessage;
var downloadSpeedCalculator = new DownloadSpeedCalculator();
downloader.OnDownloadProgressCallback = downloadSpeedCalculator.OnDownloadProgress;
downloadSpeedCalculator.onDownloadSpeedProgress = PatchEvents.PatchDownloadProgression.SendEventMessage;
downloader.BeginDownload();

await downloader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MyBox;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;

namespace OxGFrame.AssetLoader.Bundle
{
Expand All @@ -23,6 +24,8 @@ internal class PatchLauncher : MonoBehaviour
[Header("Download Options")]
public int maxConcurrencyDownloadCount = BundleConfig.maxConcurrencyDownloadCount;
public int failedRetryCount = BundleConfig.failedRetryCount;
[Tooltip("If file size >= [BreakpointFileSizeThreshold] that file will enable breakpoint mechanism (for all downloaders)")]
public int breakpointFileSizeThreshold = 20 << 20;

[Header("Cryptogram Options")]
[Tooltip("AssetBundle decrypt key. \n[NONE], \n[OFFSET, dummySize], \n[XOR, key], \n[HTXOR, headKey, tailKey], \n[AES, key, iv] \nex: \n\"None\" \n\"offset, 12\" \n\"xor, 23\" \n\"htxor, 34, 45\" \n\"aes, key, iv\"")]
Expand All @@ -41,23 +44,29 @@ private async void Awake()
}
else DontDestroyOnLoad(this.gameObject.transform.root);

// Patch Options
#region Patch Options
BundleConfig.playMode = this.playMode;
if (this.playMode == BundleConfig.PlayMode.HostMode)
{
BundleConfig.semanticRule = this.semanticRule;
BundleConfig.skipCreateMainDownloder = this.skipCreateMainDownloder;
}
#endregion

// Package List
#region Package List
BundleConfig.listPackage = this.listPackage;
#endregion

// Download Options
#region Download Options
BundleConfig.maxConcurrencyDownloadCount = this.maxConcurrencyDownloadCount <= 0 ? BundleConfig.defaultMaxConcurrencyDownloadCount : this.maxConcurrencyDownloadCount;
BundleConfig.failedRetryCount = this.failedRetryCount <= 0 ? BundleConfig.defaultFailedRetryCount : this.failedRetryCount;
// Set download breakpoint size threshold
YooAssets.SetDownloadSystemBreakpointResumeFileSize(this.breakpointFileSizeThreshold);
#endregion

// Cryptogram Options
#region Cryptogram Options
BundleConfig.InitCryptogram(string.IsNullOrEmpty(this.decryptArgs) ? BundleConfig.CryptogramType.NONE : this.decryptArgs);
#endregion

// Init Settings
PackageManager.InitSetup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public void Resume()
public void Cancel()
{
this.mainDownloader?.CancelDownload();
// temp canceled callback in here
PatchEvents.PatchDownloadCanceled.SendEventMessage();
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;

namespace OxGFrame.AssetLoader.Utility
{
public sealed class DownloadSpeedCalculator
{
public delegate void OnDownloadSpeedProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes, long downloadSpeedBytes);

public OnDownloadSpeedProgress onDownloadSpeedProgress;
private DateTime _lastTime = default;
private long _lastDownloadBytes = 0;
private long _downloadSpeedBytes = 0;
private long _lastDownloadSpeedBytes = 0;
private bool _isDone = false;

public void OnDownloadProgress(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes)
{
if (this._isDone)
{
this._Reset();
return;
}

if (currentDownloadBytes >= totalDownloadBytes) this._isDone = true;

var frag = currentDownloadBytes - this._lastDownloadBytes;
this._lastDownloadBytes = currentDownloadBytes;
this._Increment(frag);
var speedBytes = this._GetSpeedBytes();
this.onDownloadSpeedProgress?.Invoke(totalDownloadCount, currentDownloadCount, totalDownloadBytes, currentDownloadBytes, speedBytes);
}

private void _Reset()
{
this._lastTime = default;
this._lastDownloadBytes = 0;
this._downloadSpeedBytes = 0;
this._lastDownloadSpeedBytes = 0;
this._isDone = false;
}

private void _Increment(long bytes)
{
lock (this)
{
this._downloadSpeedBytes += bytes;
}
}

private long _GetSpeedBytes()
{
lock (this)
{
if (DateTime.Now.Subtract(this._lastTime).TotalSeconds > 1)
{
this._lastDownloadSpeedBytes = this._downloadSpeedBytes;
this._downloadSpeedBytes = 0;
this._lastTime = DateTime.Now;
}
return this._lastDownloadSpeedBytes;
}
}
}
}

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

22 changes: 22 additions & 0 deletions Assets/OxGFrame/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# CHANGELOG

## [2.1.4] - 2023-05-15
- Added DownloadSpeedCalculator (using OxGFrame.AssetLoader.Utility).
```
var packageName = "DlcPackage";
bool isInitialized = await AssetPatcher.InitDlcPackage(packageName, "dlcVersion", true);
if (isInitialized)
{
var package = AssetPatcher.GetPackage(packageName);
var downloader = AssetPatcher.GetPackageDownloader(package);
// Create a DownloadSpeedCalculator to helps calculate download speed
var downloadSpeedCalculator = new DownloadSpeedCalculator();
downloader.OnDownloadProgressCallback = downloadSpeedCalculator.OnDownloadProgress;
downloadSpeedCalculator.onDownloadSpeedProgress = (totalCount, currentCount, totalBytes, currentBytes, speedBytes) =>
{
/*
* Display download info
*/
};
}
```
- Added BeakpointFileSizeThreshold on PatchLauncher (default is 20 MB).

## [2.1.3] - 2023-05-13
- Modified patch repair procedure (the repair only delete main default package cache files and local files).
- Modified UnloadPackageAndClearCacheFiles() method has return value (true = Successed, false = Failed).
Expand Down
2 changes: 1 addition & 1 deletion Assets/OxGFrame/Utility/Scripts/Runtime/Timer/RealTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public float GetTick()
}

/// <summary>
/// Tick觸發時間倒數, 如果超過設置的觸發時間將直接返回 0
/// Tick 觸發時間倒數, 如果超過設置的觸發時間將直接返回 0
/// </summary>
/// <returns></returns>
public float TickCountdown()
Expand Down
2 changes: 1 addition & 1 deletion Assets/OxGFrame/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.michaelo.oxgframe",
"displayName": "OxGFrame",
"description": "The OxGFrame is a lightweight framework based on Unity for accelerating game development. Supports multi-platform Win, Android, iOS, WebGL.",
"version": "2.1.3",
"version": "2.1.4",
"unity": "2021.3",
"license": "MIT",
"dependencies": {},
Expand Down

0 comments on commit 39e906a

Please sign in to comment.