Skip to content

Commit

Permalink
most of the way through implementing a heads up loading bar
Browse files Browse the repository at this point in the history
  • Loading branch information
dooly123 committed Jan 29, 2025
1 parent 4a8786b commit 8f93484
Show file tree
Hide file tree
Showing 30 changed files with 799 additions and 3,204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ MonoBehaviour:
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 2a27b24eacd47d84ba39d6ee782dd1e8
m_Address: Packages/com.basis.sdk/Prefabs/UI/Loading Bar.prefab
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 356a8bb117010ea4084b0dd6463d28b0
m_Address: LoggerUI
m_ReadOnly: 0
Expand Down Expand Up @@ -160,11 +165,6 @@ MonoBehaviour:
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 851cc482c802fed4388550f1c5749169
m_Address: oldnameplate
m_ReadOnly: 0
m_SerializedLabels: []
FlaggedDuringContentUpdateRestriction: 0
- m_GUID: 861fed068fe3fd4449deba6697749c16
m_Address: UserNamePanel
m_ReadOnly: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace Basis.Scripts.Avatar
Expand Down Expand Up @@ -161,8 +162,9 @@ public static async Task LoadAvatarRemote(BasisRemotePlayer Player,byte Mode, Ba
}
public static async Task<GameObject> DownloadAndLoadAvatar(BasisLoadableBundle BasisLoadableBundle, BasisPlayer BasisPlayer)
{
Guid UniqueID = new Guid();
GameObject Output = await BasisLoadHandler.LoadGameObjectBundle(BasisLoadableBundle, true, BasisPlayer.ProgressReportAvatarLoad, new CancellationToken(), BasisPlayer.transform.position, Quaternion.identity,Vector3.one,false, BasisPlayer.transform);
BasisPlayer.ProgressReportAvatarLoad.ReportProgress(100, "Setting Position");
BasisPlayer.ProgressReportAvatarLoad.ReportProgress(UniqueID.ToString(), 100, "Setting Position");
Output.transform.SetPositionAndRotation(BasisPlayer.transform.position, Quaternion.identity);
return Output;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Basis.Scripts.BasisSdk;
using System;
using System.Threading.Tasks;
using System.Xml;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class BasisBundleLoadAsset
Expand Down Expand Up @@ -48,6 +50,7 @@ public static async Task<GameObject> LoadFromWrapper(BasisTrackedBundleWrapper B
}
public static async Task<Scene> LoadSceneFromBundleAsync(BasisTrackedBundleWrapper bundle, bool MakeActiveScene, BasisProgressReport progressCallback)
{
Guid UniqueID = new Guid();
bool AssignedIncrement = false;
string[] scenePaths = bundle.AssetBundle.GetAllScenePaths();
if (scenePaths.Length == 0)
Expand All @@ -63,7 +66,7 @@ public static async Task<Scene> LoadSceneFromBundleAsync(BasisTrackedBundleWrapp
// Track scene loading progress
while (!asyncLoad.isDone)
{
progressCallback.ReportProgress(50 + asyncLoad.progress * 50, "loading scene"); // Progress from 50 to 100 during scene load
progressCallback.ReportProgress(UniqueID.ToString(),50 + asyncLoad.progress * 50, "loading scene"); // Progress from 50 to 100 during scene load
await Task.Yield();
}

Expand All @@ -79,7 +82,7 @@ public static async Task<Scene> LoadSceneFromBundleAsync(BasisTrackedBundleWrapp
AssignedIncrement = bundle.Increment();
}
BasisDebug.Log("Scene set as active: " + loadedScene.name);
progressCallback.ReportProgress(100, "loading scene"); // Set progress to 100 when done
progressCallback.ReportProgress(UniqueID.ToString(), 100, "loading scene"); // Set progress to 100 when done
return loadedScene;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class BasisIOManagement
public static async Task DownloadFile(string url, string localFilePath, BasisProgressReport progressCallback, CancellationToken cancellationToken = default)
{
BasisDebug.Log($"Starting file download from {url}");

Guid UniqueID = new Guid();
// Null or empty URL check
if (string.IsNullOrWhiteSpace(url))
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public static async Task DownloadFile(string url, string localFilePath, BasisPro
}

// Report progress (0% to 100%)
progressCallback.ReportProgress(asyncOperation.webRequest.downloadProgress * 100, "downloading data");
progressCallback.ReportProgress(UniqueID.ToString(), asyncOperation.webRequest.downloadProgress * 100, "downloading data");
// BasisDebug.Log("downloading file " + asyncOperation.webRequest.downloadProgress);
await Task.Yield();
}
Expand Down Expand Up @@ -168,6 +168,7 @@ public static async Task<bool> CopyFileAsync(string sourceFilePath, string desti
BasisDebug.LogError($"Source file not found: {sourceFilePath}");
return false;
}
Guid UniqueID = new Guid();

try
{
Expand All @@ -189,7 +190,7 @@ public static async Task<bool> CopyFileAsync(string sourceFilePath, string desti

// Calculate and report progress
float progress = (float)totalBytesCopied / totalBytes;
Report.ReportProgress(progress * 100, "copying data");
Report.ReportProgress(UniqueID.ToString(), progress * 100, "copying data");

// Allow other tasks to run
await Task.Yield();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BasisSerializer.OdinSerializer;
using System;
using System.Threading.Tasks;
using UnityEngine;

Expand All @@ -11,9 +12,9 @@ public static async Task<AssetBundleCreateRequest> GenerateBundleFromFile(string
{
VP = Password
};

Guid UniqueID = new Guid();
// Decrypt the file asynchronously
byte[] LoadedBundleData = await BasisEncryptionWrapper.DecryptFileAsync(BasisPassword, FilePath, progressCallback, 8388608);
byte[] LoadedBundleData = await BasisEncryptionWrapper.DecryptFileAsync(UniqueID.ToString(), BasisPassword, FilePath, progressCallback, 8388608);

// Start the AssetBundle loading process from memory with CRC check
AssetBundleCreateRequest assetBundleCreateRequest = AssetBundle.LoadFromMemoryAsync(LoadedBundleData, CRC);
Expand All @@ -33,15 +34,15 @@ public static async Task<AssetBundleCreateRequest> GenerateBundleFromFile(string
lastReportedProgress = progress;

// Call the progress callback with the current progress
progressCallback.ReportProgress(progress, "loading bundle");
progressCallback.ReportProgress(UniqueID.ToString(),progress, "loading bundle");
}

// Wait a short period before checking again to avoid busy waiting
await Task.Delay(100); // Adjust delay as needed (e.g., 100ms)
}

// Ensure progress reaches 100% after completion
progressCallback.ReportProgress(100, "loading bundle");
progressCallback.ReportProgress(UniqueID.ToString(), 100, "loading bundle");

// Await the request completion
await assetBundleCreateRequest;
Expand All @@ -54,8 +55,9 @@ public static async Task<BasisLoadableBundle> GenerateMetaFromFile(BasisLoadable
{
VP = BasisLoadableBundle.UnlockPassword
};
Guid UniqueID = new Guid();
// BasisDebug.Log("BasisLoadableBundle.UnlockPassword" + BasisLoadableBundle.UnlockPassword);
byte[] LoadedMetaData = await BasisEncryptionWrapper.DecryptFileAsync(BasisPassword, FilePath, progressCallback, 81920);
byte[] LoadedMetaData = await BasisEncryptionWrapper.DecryptFileAsync(UniqueID.ToString(), BasisPassword, FilePath, progressCallback, 81920);
BasisDebug.Log("Converting decrypted meta file to BasisBundleInformation...", BasisDebug.LogTag.Event);
BasisLoadableBundle.BasisBundleInformation = ConvertBytesToJson(LoadedMetaData);
return BasisLoadableBundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Basis.Scripts.Device_Management
public partial class BasisDeviceManagement : MonoBehaviour
{
public bool FireOffNetwork = true;
public bool HasEvents = false;
public static bool HasEvents = false;
public const string InvalidConst = "Invalid";
public string[] BakedInCommandLineArgs = new string[] { };
public static string NetworkManagement = "NetworkManagement";
Expand Down Expand Up @@ -61,7 +61,7 @@ public static bool IsUserInDesktop()
public event Action<string> OnBootModeChanged;
public event Action<string> OnBootModeStopped;
public delegate Task InitializationCompletedHandler();
public event InitializationCompletedHandler OnInitializationCompleted;
public static event InitializationCompletedHandler OnInitializationCompleted;
public BasisDeviceNameMatcher BasisDeviceNameMatcher;
[SerializeField]
public BasisObservableList<BasisInput> AllInputDevices = new BasisObservableList<BasisInput>();
Expand Down Expand Up @@ -106,6 +106,7 @@ void OnDestroy()
BasisXRManagement.CheckForPass -= CheckForPass;

OnInitializationCompleted -= RunAfterInitialized;
HasEvents = false;
}
}
public static void UnassignFBTrackers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,28 @@ public void DeInitalizeCallToRender()
BasisRemotePlayer.FaceRenderer.DestroyCalled -= AvatarUnloaded;
}
}
private void OnProgressStart()
private void OnProgressStart(string UniqueID)
{
EnqueueOnMainThread(() =>
{
Loadingbar.gameObject.SetActive(true);
Loadingtext.gameObject.SetActive(true);
});
}
private void OnProgressComplete()
private void OnProgressComplete(string UniqueID)
{
EnqueueOnMainThread(() =>
{
Loadingtext.gameObject.SetActive(false);
Loadingbar.gameObject.SetActive(false);
});
}
public void ProgresReport(float progress, string info)
public void ProgresReport(string UniqueID, float progress, string info)
{
EnqueueOnMainThread(() =>
{
Loadingtext.text = info;
UpdateProgressBar(progress);
UpdateProgressBar( UniqueID, progress);
});
}
private static void EnqueueOnMainThread(Action action)
Expand All @@ -200,7 +200,7 @@ private static void EnqueueOnMainThread(Action action)
RemoteNamePlateDriver.actions.Enqueue(action);
}
}
public void UpdateProgressBar(float progress)
public void UpdateProgressBar(string UniqueID,float progress)
{
Vector2 scale = Loadingbar.size;
scale.x = progress/2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
Expand All @@ -8,7 +6,6 @@
using Basis.Scripts.Common;
using Basis.Scripts.Networking;
using System.Threading.Tasks;

namespace Basis.Scripts.UI.UI_Panels
{
public class BasisSetUserName : MonoBehaviour
Expand All @@ -17,31 +14,14 @@ public class BasisSetUserName : MonoBehaviour
public Button Ready;
public static string LoadFileName = "CachedUserName.BAS";
public bool UseAddressables;
public Image Loadingbar;
public Button AdvancedSettings;
public GameObject AdvancedSettingsPanel;
[Header("Advanced Settings")]
public TMP_InputField IPaddress;
public TMP_InputField Port;
public TMP_InputField Password;
public TMP_Text displayloadinginfo;
public Button UseLocalhost;
public Toggle HostMode;
// Queue to hold actions that need to be run on the main thread
private static readonly Queue<Action> mainThreadActions = new Queue<Action>();

private void Update()
{
// Process actions on the main thread
lock (mainThreadActions)
{
while (mainThreadActions.Count != 0)
{
mainThreadActions.Dequeue()?.Invoke();
}
}
}

public void Start()
{
UserNameTMP_InputField.text = BasisDataStore.LoadString(LoadFileName, string.Empty);
Expand All @@ -52,27 +32,6 @@ public void Start()
UseLocalhost.onClick.AddListener(UseLocalHost);
}
BasisNetworkManagement.OnEnableInstanceCreate += LoadCurrentSettings;
BasisSceneLoadDriver.progressCallback.OnProgressReport += ProgresReport;
BasisSceneLoadDriver.progressCallback.OnProgressStart += StartProgress;
BasisSceneLoadDriver.progressCallback.OnProgressComplete += OnProgressComplete;
}

private void StartProgress()
{
EnqueueOnMainThread(() =>
{
displayloadinginfo.gameObject.SetActive(true);
Loadingbar.gameObject.SetActive(true);
});
}

private void OnProgressComplete()
{
EnqueueOnMainThread(() =>
{
displayloadinginfo.gameObject.SetActive(false);
Loadingbar.gameObject.SetActive(false);
});
}

public void OnDestroy()
Expand All @@ -82,19 +41,6 @@ public void OnDestroy()
AdvancedSettings.onClick.RemoveListener(ToggleAdvancedSettings);
UseLocalhost.onClick.RemoveListener(UseLocalHost);
}
BasisSceneLoadDriver.progressCallback.OnProgressReport -= ProgresReport;
BasisSceneLoadDriver.progressCallback.OnProgressStart -= StartProgress;
BasisSceneLoadDriver.progressCallback.OnProgressComplete -= OnProgressComplete;
}

private void ProgresReport(float progress, string info)
{
// Ensure this method is executed on the main thread
EnqueueOnMainThread(() =>
{
displayloadinginfo.text = info;
Loadingbar.rectTransform.localScale = new Vector3(progress / 100, 1f, 1f);
});
}
public void UseLocalHost()
{
Expand Down Expand Up @@ -157,14 +103,5 @@ public void ToggleAdvancedSettings()
AdvancedSettingsPanel.SetActive(!AdvancedSettingsPanel.activeSelf);
}
}

// Helper method to enqueue actions to be executed on the main thread
private static void EnqueueOnMainThread(Action action)
{
lock (mainThreadActions)
{
mainThreadActions.Enqueue(action);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public void Initalize()
}
}
}
}
}
Loading

0 comments on commit 8f93484

Please sign in to comment.