using System.Collections; using System.Collections.Generic; using System.IO.Compression; using System.IO; using UnityEngine; using UnityEngine.Networking; public class UpdateHandler : MonoBehaviour { private string _message = ""; private string _serverVersion = ""; private float _messageTime = 0; private bool _updating = false; private bool _updateSuccess = false; private const string translationVersion = "lastVersion"; private const string repositoryUrl = "https://git.japari.cafe/api/v1/repos/katboi01/Vorked_Translation"; public IEnumerator Start() { string currentVersion = PlayerPrefs.GetString(translationVersion, "0"); string newVersion = currentVersion; yield return DownloadText(repositoryUrl, (json) => { var newData = JsonUtility.FromJson(json); newVersion = newData.updated_at; } ); Debug.Log("local version: " + currentVersion); Debug.Log("server version: " + newVersion); if (newVersion != currentVersion) { _serverVersion = newVersion; } else { //Destroy(this.gameObject); } } public void OnGUI() { if (_updating || _messageTime > 0) { var style = GUI.skin.GetStyle("Label"); style.normal.textColor = Color.black; style.normal.background = Texture2D.whiteTexture; style.alignment = TextAnchor.MiddleCenter; GUI.Label(new Rect(Screen.width / 2 - 10, Screen.height / 2 - 25, 200, 100), _message, style); } else { var style = GUI.skin.GetStyle("Button"); style.alignment = TextAnchor.UpperCenter; if (GUI.Button(new Rect(Screen.width / 2 - 100, 0, 200, 50), "Translation Update Available", style)) { StartCoroutine(UpdateTranslation()); } } } private void Update() { if (_messageTime > 0) { _messageTime -= Time.deltaTime; } else if (_updateSuccess) { Destroy(this.gameObject); } } public IEnumerator UpdateTranslation() { _updating = true; byte[] bytes = null; yield return DownloadBytes(repositoryUrl + "/archive/master.zip", (newBytes) => { bytes = newBytes; }, (progress) => { SetMessage($"Downloading {progress * 100}%"); } ); if (bytes.Length <= 0) { SetMessage("Error. Download failed"); _updateSuccess = false; yield break; } using (var compressedFileStream = new MemoryStream(bytes)) { using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Read, false)) { foreach (var file in zipArchive.Entries) { if (file.Name != "") { //remove repo name from file path var outFilePath = file.FullName.Split(new[] { '/' }, 2)[1]; Directory.CreateDirectory(Path.GetDirectoryName(outFilePath)); file.ExtractToFile(outFilePath, true); } } } } PlayerPrefs.SetString(translationVersion, _serverVersion); SetMessage("Update completed! Please restart the game to apply it.", 5); _updateSuccess = true; _updating = false; } private void SetMessage(string message, float time = 2) { _message = message; _messageTime = time; } public static IEnumerator DownloadBytes(string url, System.Action callback, System.Action onProgressChanged = null) { UnityWebRequest www = UnityWebRequest.Get(url); var request = www.SendWebRequest(); while (!request.isDone) { onProgressChanged?.Invoke(request.progress); yield return 0; } if (www.result != UnityWebRequest.Result.Success) { Debug.Log(url); Debug.Log(www.error); callback(null); } else { callback(www.downloadHandler.data); } } public static IEnumerator DownloadText(string url, System.Action callback) { UnityWebRequest www = UnityWebRequest.Get(url); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(url); Debug.Log(www.error); callback(null); } else { callback(www.downloadHandler.text); } } public class JsonSmall { public string updated_at; } }