Singularity/Assets/Scripts/UpdateChecker.cs

96 lines
3.3 KiB
C#
Raw Permalink Normal View History

2024-05-06 14:45:45 -04:00
using System;
using System.Collections;
using System.IO;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class UpdateJSON {
[JsonProperty("current_version")] public float Version { get; set; }
[JsonProperty("download_link")] public string DownloadLink { get; set; }
}
public class UpdateChecker : MonoBehaviour
{
[SerializeField] private GameObject UpdateCover;
[SerializeField] private string JSONURL;
[SerializeField] private TextMeshProUGUI Message;
[SerializeField] private GameObject ProgressContainer;
[SerializeField] private Slider Progress;
[SerializeField] private TextMeshProUGUI ProgressText;
UpdateJSON UpdateJSON;
bool doDownload = false;
// Start is called before the first frame update
void Start() {
StartCoroutine(GetUpdateA());
}
private IEnumerator GetUpdateA() {
UpdateCover.SetActive(true);
Message.text = "Checking for update...";
using (UnityWebRequest uwr = UnityWebRequest.Get(JSONURL)) {
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.ConnectionError) {
Debug.Log("Error While Sending for User info: " + uwr.error);
} else {
UpdateJSON JSON = JsonConvert.DeserializeObject<UpdateJSON>(uwr.downloadHandler.text);
UpdateJSON = JSON;
if (JSON.Version > float.Parse(Application.version)) {// Update Needed!
float OnlineVersion = JSON.Version;
string message = $"This application is out of date. Please update.\nCurrent Version: {Application.version}\nAvailable Version: {OnlineVersion}\n(Click anywhere to Download and install)";
Message.text = message;
doDownload = true;
} else if (JSON.Version == float.Parse(Application.version)) {// We are up to date!
UpdateCover.SetActive(false);
}
}
}
}
public void DownloadUpdate() {
if (doDownload) {
StartCoroutine(DownloadUpdateA());
}
}
private IEnumerator DownloadUpdateA() {
Debug.Log("DownloadFileA Called");
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+@"\Schaken-Mods®\Mod Downloader\";
string fullPath = Path.Combine(path, "Update.exe");
var dh = new DownloadHandlerFile(fullPath) {
removeFileOnAbort = true
};
var uwr = new UnityWebRequest(UpdateJSON.DownloadLink, UnityWebRequest.kHttpVerbGET) {
downloadHandler = dh
};
var operation = uwr.SendWebRequest();
Debug.Log("Web request sent");
while (!operation.isDone) {
ProgressContainer.SetActive(true);
Progress.value = uwr.downloadProgress;
ProgressText.text = string.Format("{0:P1}", uwr.downloadProgress);
yield return new WaitForSeconds(0.5f);
}
if (uwr.result == UnityWebRequest.Result.Success) {
Progress.value = 1;
ProgressText.text = "Starting Install...";
} else {
Debug.LogError($"Download failed: {uwr.error}");
}
yield return new WaitForSeconds(0.5f);
Debug.Log("Writing File");
uwr.Dispose();
Application.OpenURL(fullPath);
yield return new WaitForSeconds(2.0f);
#if !UNITY_EDITOR
Application.Quit();
#endif
}
}