Upload files to "/"

This commit is contained in:
katboi01 2023-10-11 08:33:27 +08:00
commit 71e819ed52
3 changed files with 275 additions and 0 deletions

20
TranslationUpdater.cs Normal file
View File

@ -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<UpdateHandler>();
}
}
}

81
TranslationUpdater.csproj Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{58FF4482-CC2C-435A-B3AB-E7602D1D83B3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TranslationUpdater</RootNamespace>
<AssemblyName>TranslationUpdater</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\Build\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\Libs\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\Libs\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\Libs\BepInEx.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>..\Libs\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\Libs\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.IMGUIModule">
<HintPath>..\Libs\UnityEngine.IMGUIModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.JSONSerializeModule">
<HintPath>..\Libs\UnityEngine.JSONSerializeModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.TextRenderingModule">
<HintPath>..\Libs\UnityEngine.TextRenderingModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UIModule">
<HintPath>..\Libs\UnityEngine.UIModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UnityWebRequestModule">
<HintPath>..\Libs\UnityEngine.UnityWebRequestModule.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="TranslationUpdater.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdateHandler.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

174
UpdateHandler.cs Normal file
View File

@ -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<JsonSmall>(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<byte[]> callback, System.Action<float> 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<string> 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;
}
}