mirror of
https://github.com/D1GQ/BetterAmongUs.git
synced 2026-10-28 09:39:13 +00:00
97 lines
3.3 KiB
C#
97 lines
3.3 KiB
C#
using BetterAmongUs.Managers;
|
|
using Il2CppInterop.Runtime.Attributes;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace BetterAmongUs.Network;
|
|
|
|
internal class GitHubFile
|
|
{
|
|
/// <summary>
|
|
/// Downloads an individual visor-related file from the remote repository and saves it locally.
|
|
/// Handles errors and logs any failed downloads to avoid missing assets.
|
|
/// </summary>
|
|
[HideFromIl2Cpp]
|
|
internal static IEnumerator CoDownloadFile(string fileUrl, string localFilePath, Action<string>? callback = null, bool showProgress = false)
|
|
{
|
|
var www = new UnityWebRequest(fileUrl, UnityWebRequest.kHttpVerbGET)
|
|
{
|
|
downloadHandler = new DownloadHandlerBuffer()
|
|
};
|
|
|
|
if (showProgress)
|
|
{
|
|
CustomLoadingBarManager.ToggleLoadingBar(true);
|
|
CustomLoadingBarManager.SetLoadingPercent(0f, "Starting download...");
|
|
}
|
|
|
|
var operation = www.SendWebRequest();
|
|
|
|
// Track progress while downloading
|
|
while (!operation.isDone)
|
|
{
|
|
if (showProgress)
|
|
{
|
|
int dotCount = (int)(Time.time * 2f) % 4;
|
|
string dots = new('.', dotCount);
|
|
float progress = www.downloadProgress * 100f;
|
|
if (progress < 1f)
|
|
{
|
|
CustomLoadingBarManager.SetLoadingPercent(0f, $"Starting Download{dots}");
|
|
}
|
|
CustomLoadingBarManager.SetLoadingPercent(progress, $"Downloading{dots}");
|
|
}
|
|
yield return null;
|
|
}
|
|
|
|
if (www.result == UnityWebRequest.Result.Success)
|
|
{
|
|
CustomLoadingBarManager.SetLoadingPercent(100f, "Saving File!");
|
|
yield return new WaitForSeconds(1f);
|
|
CustomLoadingBarManager.ToggleLoadingBar(false);
|
|
}
|
|
else
|
|
{
|
|
Logger.Error($"Error downloading file from URL '{fileUrl}': {www.error} (Response Code: {(int)www.responseCode})");
|
|
if (showProgress)
|
|
{
|
|
CustomLoadingBarManager.SetLoadingPercent(100f, "Download Failed!");
|
|
yield return new WaitForSeconds(2f);
|
|
CustomLoadingBarManager.ToggleLoadingBar(false);
|
|
}
|
|
yield break;
|
|
}
|
|
|
|
byte[] bytes = www.downloadHandler.GetNativeData().ToArray();
|
|
File.WriteAllBytes(localFilePath, bytes);
|
|
|
|
Logger.Log($"Saved file: {localFilePath}");
|
|
callback?.Invoke(localFilePath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Downloads an individual visor-related file from the remote repository and saves it locally.
|
|
/// Handles errors and logs any failed downloads to avoid missing assets.
|
|
/// </summary>
|
|
[HideFromIl2Cpp]
|
|
internal static IEnumerator CoDownloadManifest(string fileUrl, string fileName, Action<string> Callback)
|
|
{
|
|
var www = new UnityWebRequest($"{fileUrl}/{fileName}", UnityWebRequest.kHttpVerbGET)
|
|
{
|
|
downloadHandler = new DownloadHandlerBuffer()
|
|
};
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Logger.Error($"Error downloading {fileUrl}/{fileName}: {www.error}");
|
|
yield break;
|
|
}
|
|
|
|
var response = www.downloadHandler.text;
|
|
www.Dispose();
|
|
Callback.Invoke(response);
|
|
}
|
|
}
|