commit 71e819ed52c040b5a0f03254fde8fc71db4f1061 Author: katboi01 Date: Wed Oct 11 08:33:27 2023 +0800 Upload files to "/" diff --git a/TranslationUpdater.cs b/TranslationUpdater.cs new file mode 100644 index 0000000..32a1d23 --- /dev/null +++ b/TranslationUpdater.cs @@ -0,0 +1,20 @@ +using BepInEx; +using HarmonyLib; +using System.Reflection; +using UnityEngine; + +namespace TranslationUpdater +{ + [BepInPlugin(pluginGuid, pluginName, pluginVersion)] + class TranslationUpdater : BaseUnityPlugin + { + public const string pluginGuid = "katboi01.TranslationUpdater"; + public const string pluginName = "KF3 Translation Updater"; + public const string pluginVersion = "1.0.0"; + + public void Awake() + { + new GameObject(pluginName).AddComponent(); + } + } +} diff --git a/TranslationUpdater.csproj b/TranslationUpdater.csproj new file mode 100644 index 0000000..da6018d --- /dev/null +++ b/TranslationUpdater.csproj @@ -0,0 +1,81 @@ + + + + + Debug + AnyCPU + {58FF4482-CC2C-435A-B3AB-E7602D1D83B3} + Library + Properties + TranslationUpdater + TranslationUpdater + v4.7.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + ..\Build\ + TRACE + prompt + 4 + + + + ..\Libs\0Harmony.dll + + + ..\Libs\Assembly-CSharp.dll + + + ..\Libs\BepInEx.dll + + + + + + + + + + + + + ..\Libs\UnityEngine.dll + + + ..\Libs\UnityEngine.CoreModule.dll + + + ..\Libs\UnityEngine.IMGUIModule.dll + + + ..\Libs\UnityEngine.JSONSerializeModule.dll + + + ..\Libs\UnityEngine.TextRenderingModule.dll + + + ..\Libs\UnityEngine.UIModule.dll + + + ..\Libs\UnityEngine.UnityWebRequestModule.dll + + + + + + + + + \ No newline at end of file diff --git a/UpdateHandler.cs b/UpdateHandler.cs new file mode 100644 index 0000000..8e3dd56 --- /dev/null +++ b/UpdateHandler.cs @@ -0,0 +1,174 @@ +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; + } +} \ No newline at end of file