Implement update checker.

This commit is contained in:
2023-06-22 13:23:24 +08:00
parent ea02fc22bd
commit 864ea91be0
3 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7b40db90d3ad439498fc7ff01a5ea5f4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,105 @@
using Cryville.Common.Network;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using UnityEngine;
using Logger = Cryville.Common.Logging.Logger;
using ThreadPriority = System.Threading.ThreadPriority;
namespace Cryville.Crtr.Network {
public class UpdateChecker : MonoBehaviour {
string _currentVersion;
void Start() {
_currentVersion = Application.version;
new Thread(CheckVersion) { IsBackground = true, Priority = ThreadPriority.BelowNormal }.Start();
}
static readonly Uri BaseUri = new Uri("https://www.cryville.world/api/crtr/index");
List<VersionInfo> _versions;
public void CheckVersion() {
try {
var client = new HttpsClient(BaseUri);
client.Connect();
var response = client.Request("GET", new Uri(BaseUri, "versions"));
var data = Encoding.UTF8.GetString(response.MessageBody.ReadToEnd());
_versions = JsonConvert.DeserializeObject<List<VersionInfo>>(data, Game.GlobalJsonSerializerSettings);
}
catch (Exception ex) {
Logger.Log("main", 4, "Network", "An error occurred while checking for update: {0}", ex);
Dialog.Show(null, "Failed to check for update.");
return;
}
var availableVersions = (from v in _versions where v.platforms.ContainsKey(PlatformConfig.Name) select v).ToArray();
var versionIndex = new Dictionary<string, int>(availableVersions.Length);
for (int i = 0; i < availableVersions.Length; i++) versionIndex.Add(availableVersions[i].name, i);
var currentVersion = (from v in availableVersions where v.name == _currentVersion select v).SingleOrDefault();
var latestVersion = availableVersions.Last();
if (currentVersion == null) {
Dialog.Show(null, string.Format("You are playing an unknown version of Cosmo Resona: {0}\nThe latest version is: {1}", _currentVersion, latestVersion.name));
return;
}
if (latestVersion.name != _currentVersion) {
var latestResources = latestVersion.platforms[PlatformConfig.Name].resources;
VersionResourceInfo fullPackage = null;
if (latestResources != null) {
fullPackage = (from r in latestResources where r.upstream == null select r).SingleOrDefault();
}
if (fullPackage == null) {
Dialog.Show(null, string.Format("A new version is present: {0}\nUpdate is not available.", latestVersion.name));
return;
}
long totalDiffSize = 0;
int searchIndex = versionIndex[latestVersion.name], targetIndex = versionIndex[currentVersion.name];
while (searchIndex != targetIndex) {
var searchVersion = availableVersions[searchIndex];
VersionResourceInfo matchedUpstream = null;
var resources = searchVersion.platforms[PlatformConfig.Name].resources;
if (resources == null) {
totalDiffSize = 0;
break;
}
foreach (var r in resources) {
if (r.upstream == null) continue;
var upstreamIndex = versionIndex[r.upstream];
if (upstreamIndex >= targetIndex && upstreamIndex < searchIndex) {
matchedUpstream = r;
searchIndex = upstreamIndex;
}
}
if (matchedUpstream != null) {
totalDiffSize += matchedUpstream.size;
}
else {
totalDiffSize = 0;
break;
}
}
if (totalDiffSize == 0 || totalDiffSize >= fullPackage.size) {
Dialog.ShowAndWait(string.Format("A new version is available: {0}\nYou have to download the full package.\nOpen the download link now?", latestVersion.name), "Yes", "No");
}
else {
Dialog.ShowAndWait(string.Format("A new version is available: {0}\nDo you want to update?", latestVersion.name), "Yes", "No");
}
}
}
class VersionInfo {
[JsonRequired]
public string name;
public string type;
public Dictionary<string, PlatformVersionInfo> platforms;
}
class PlatformVersionInfo {
public List<VersionResourceInfo> resources;
}
class VersionResourceInfo {
public bool external;
public string upstream;
[JsonRequired]
public string url;
[JsonRequired]
public long size;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1a07bd0d5ae9f7b4fa13443638f4b7e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: