Skip to content

Commit

Permalink
Merge pull request #1151 from anatawa12/new-version-checks
Browse files Browse the repository at this point in the history
New version checks
  • Loading branch information
anatawa12 authored Aug 22, 2024
2 parents d515f7d + fd8c097 commit 969c747
Show file tree
Hide file tree
Showing 25 changed files with 620 additions and 171 deletions.
30 changes: 30 additions & 0 deletions .docs/build-latest-txt.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { writeFileSync } from 'node:fs';

const baseUrl = process.argv[2];
const newVersion = process.argv[3];
const requiredUnity = "2019.4";

const supports2019 = requiredUnity.startsWith("2019");

if (supports2019) {
writeFileSync("static/latest.txt", newVersion);
}

const existingLatest2Txt = await fetch(`${baseUrl}/latest2.txt`);
if (!existingLatest2Txt.ok) throw new Error(`Failed to fetch latest2.txt: ${existingLatest2Txt.statusText}`);
const existingLatest2 = await existingLatest2Txt.text();

const parsed = existingLatest2.trimEnd().split("\n").map(x => x.split(':'));

const sameUnityIndex = parsed.findIndex(x => x[1] === requiredUnity);
if (sameUnityIndex !== -1) {
parsed[sameUnityIndex][0] = newVersion;
} else {
parsed.push([newVersion, requiredUnity]);
}

const newLatest2 = parsed.map(x => x.join(':') + '\n').join('');

writeFileSync("static/latest2.txt", newLatest2);
2 changes: 1 addition & 1 deletion .docs/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ BASE_URL="$1"
LATEST_VERSION="$2"

mkdir -p static
echo "$LATEST_VERSION" > static/latest.txt
./build-latest-txt.mjs "$BASE_URL" "$LATEST_VERSION"
hugo --minify --baseURL "$BASE_URL"

1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- Rewritten Check for Update system `#1151`

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- Rewritten Check for Update system `#1151`

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion Editor/AssetDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override void OnInspectorGUI()
EditorGUILayout.LabelField(AAOL10N.Tr("AssetDescription:Description"), EditorStyles.wordWrappedLabel);
if (GUILayout.Button(AAOL10N.Tr("AssetDescription:OpenDocs")))
{
var baseUrl = CheckForUpdate.IsBeta ? "https://vpm.anatawa12.com/avatar-optimizer/beta/" : "https://vpm.anatawa12.com/avatar-optimizer/";
var baseUrl = CheckForUpdate.Checker.IsBeta ? "https://vpm.anatawa12.com/avatar-optimizer/beta/" : "https://vpm.anatawa12.com/avatar-optimizer/";
var isJapanese = LanguagePrefs.Language == "ja";
baseUrl += isJapanese ? "ja/" : "en/";
baseUrl += "developers/asset-description/";
Expand Down
164 changes: 0 additions & 164 deletions Editor/CheckForUpdate.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/CheckForUpdate.cs.meta

This file was deleted.

3 changes: 3 additions & 0 deletions Editor/CheckForUpdate.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions Editor/CheckForUpdate/CheckForUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer.CheckForUpdate
{
[InitializeOnLoad]
internal static class Checker
{
static Checker()
{
EditorApplication.delayCall += DoCheckForUpdate;
}

public static bool OutOfDate
{
get => SessionState.GetBool("com.anatawa12.avatar-optimizer.out-of-date", false);
private set => SessionState.SetBool("com.anatawa12.avatar-optimizer.out-of-date", value);
}

public static string LatestVersionName
{
get => SessionState.GetString("com.anatawa12.avatar-optimizer.latest", "");
private set => SessionState.SetString("com.anatawa12.avatar-optimizer.latest", value);
}

public static string CurrentVersionName
{
get => SessionState.GetString("com.anatawa12.avatar-optimizer.current", "");
private set => SessionState.SetString("com.anatawa12.avatar-optimizer.current", value);
}

public static bool IsBeta => CurrentVersionName.Contains("-");

static async void DoCheckForUpdate()
{
if (!MenuItems.CheckForUpdateEnabled)
{
// if disabled, do nothing
OutOfDate = false;
return;
}

if (!(GetCurrentVersion() is Version currentVersion))
{
Debug.LogError("Avatar Optimizer CheckForUpdate: Failed to get current version");
return;
}

CurrentVersionName = currentVersion.ToString();

var isBeta = currentVersion.IsPrerelease || MenuItems.ForceBetaChannel;
if (!UnityVersion.TryParse(Application.unityVersion, out var unityVersion))
{
Debug.LogError("Avatar Optimizer CheckForUpdate: Failed to get unity version");
return;
}

var ctx = new CheckForUpdateContext(isBeta, currentVersion, unityVersion);

if (await ctx.GetLatestVersion() is Version latestVersion)
{
// there is known latest version
if (currentVersion < latestVersion)
{
OutOfDate = true;
LatestVersionName = latestVersion.ToString();

Debug.Log("AAO CheckForUpdate: Out of date detected! " +
$"current version: {currentVersion}, latest version: {latestVersion}");
}
else
{
OutOfDate = false;
}
}
else
{
OutOfDate = false;
}
}

static Version? GetCurrentVersion()
{
try
{
var packageJson =
JsonUtility.FromJson<PackageJson>(
File.ReadAllText("Packages/com.anatawa12.avatar-optimizer/package.json"));
if (packageJson.version == null) return null;
if (!Version.TryParse(packageJson.version, out var version)) return null;
return version;
}
catch (Exception e)
{
Debug.LogException(e);
return null;
}
}

[Serializable]
class PackageJson
{
public string version;
}
}
}
3 changes: 3 additions & 0 deletions Editor/CheckForUpdate/CheckForUpdate.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 969c747

Please sign in to comment.