Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ PC방이나 관리되는 PC 가운데에는 모르는 프로그램의 실행을

### 업데이트

- **기본 버전·포터블 버전**: 런처를 열 때 하루에 한 번 GitHub에 최신 버전을 확인합니다. 새 버전이 있으면 홈에 알림이 뜨고, **지금 업데이트**를 누르면 새 zip을 내려받아 SHA-256을 확인한 뒤 같은 폴더의 `SoundVisualizer.exe` 를 바꾸고 다시 엽니다. 설정 파일은 그대로 남습니다. 업데이트는 기본 버전을 설치하며, 포터블 버전이었다면 exe 옆에 DLL 과 `AIModel` 폴더가 생깁니다. 도움말 > 앱 정보의 **업데이트 확인**으로 직접 확인할 수도 있고, 설정 > 고급의 **시작할 때 업데이트 확인**으로 자동 확인을 끌 수 있습니다.
- **기본 버전·포터블 버전**: 런처를 열 때 하루에 한 번 GitHub에 최신 버전을 확인합니다. 새 버전이 있으면 홈에 알림이 뜨고, **지금 업데이트**를 누르면 새 zip을 내려받아 SHA-256을 확인한 뒤 같은 폴더의 `SoundVisualizer.exe` 를 바꾸고 다시 엽니다. 설정 파일은 그대로 남습니다. 기본 버전은 기본 버전 zip 으로, 포터블 버전은 포터블 zip 으로 업데이트됩니다. (2.3.3 포터블은 한 번은 기본 버전을 받아 exe 옆에 DLL 과 `AIModel` 폴더가 생깁니다.) 도움말 > 앱 정보의 **업데이트 확인**으로 직접 확인할 수도 있고, 설정 > 고급의 **시작할 때 업데이트 확인**으로 자동 확인을 끌 수 있습니다.
- **Microsoft Store 판**: Store가 업데이트합니다. 앱은 인터넷에 접속하지 않습니다.
- **Chocolatey 판**: 관리자 권한 명령 프롬프트에서 `choco upgrade soundvisualizer -y` 로 업데이트합니다. 앱은 인터넷에 접속하지 않습니다.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Some PC bangs and managed PCs block programs they don't know, and Windows SmartS

### Updates

- **Standard and portable versions**: once a day, when you open the launcher, the app checks GitHub for the latest version. If there is a new one, a notice appears on Home; click **Update now** and the app downloads the new zip, checks its SHA-256, replaces `SoundVisualizer.exe` in the same folder, and reopens. Your settings file stays. The update installs the standard version; on a portable copy it adds the DLLs and the `AIModel` folder next to the exe. You can also check from Help > About > **Check for updates**, and turn off the automatic check with Settings > Advanced > **Check for updates on start**.
- **Standard and portable versions**: once a day, when you open the launcher, the app checks GitHub for the latest version. If there is a new one, a notice appears on Home; click **Update now** and the app downloads the new zip, checks its SHA-256, replaces `SoundVisualizer.exe` in the same folder, and reopens. Your settings file stays. The standard version updates to the standard zip and the portable version to the portable zip. (A 2.3.3 portable copy installs the standard version once, adding the DLLs and the `AIModel` folder next to the exe.) You can also check from Help > About > **Check for updates**, and turn off the automatic check with Settings > Advanced > **Check for updates on start**.
- **Microsoft Store version**: the Store updates the app. The app does not connect to the internet.
- **Chocolatey version**: update with `choco upgrade soundvisualizer -y` in an administrator Command Prompt. The app does not connect to the internet.

Expand Down
56 changes: 42 additions & 14 deletions SoundVisualizer.Core/Updates/ReleaseInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ namespace SoundVisualizer.Updates
/// <summary>앱이 직접 내려받을 수 있는 릴리스 파일(zip). 크기와 SHA-256 은 GitHub 이 자산마다 알려 주는 값이다.</summary>
public sealed record ReleaseAsset(string Name, Uri DownloadUrl, long Size, string Sha256Hex);

/// <summary>앱 안의 업데이트가 받을 zip 의 종류(#66).</summary>
public enum UpdatePackageKind
{
/// <summary>기본 버전 zip(SoundVisualizer_v버전.zip): exe 옆에 DLL 과 AIModel 폴더가 있다.</summary>
Standard,

/// <summary>포터블 zip(SoundVisualizer_v버전_portable.zip): 실행 파일 하나다. 포터블 판이 받는다.</summary>
Portable,
}

/// <summary>
/// GitHub 최신 릴리스 하나(#36). <see cref="Asset"/> 이 없으면 새 버전이 있다는 것만 알고, 받기는 릴리스 페이지에서 한다.
/// </summary>
Expand All @@ -23,15 +33,18 @@ public sealed record ReleaseInfo(Version Version, string Tag, Uri PageUrl, Relea
/// </summary>
public static class ReleaseParser
{
// 릴리스에 올리는 zip 이름. Chocolatey 패키지와 README 가 같은 이름을 쓴다.
// 릴리스에 올리는 기본 버전 zip 이름. Chocolatey 패키지와 README 가 같은 이름을 쓴다.
private static readonly Regex AssetName = new(@"^SoundVisualizer_v\d{1,4}(\.\d{1,5}){1,3}\.zip$", RegexOptions.CultureInvariant);
// 함께 올리는 포터블 zip 이름(#58, #64). 포터블 판만 이것을 받는다(#66).
private static readonly Regex PortableAssetName = new(@"^SoundVisualizer_v\d{1,4}(\.\d{1,5}){1,3}_portable\.zip$", RegexOptions.CultureInvariant);
private static readonly Regex Sha256Digest = new(@"^sha256:([0-9a-fA-F]{64})$", RegexOptions.CultureInvariant);

/// <summary>
/// 쓸 수 있는 릴리스면 그 정보, 아니면(형식이 다름, 초안, 시험판, 버전 없는 태그) null.
/// <paramref name="isTrusted"/> 는 믿을 주소인지 정한다. 주지 않으면 https://github.com 만 믿는다(<see cref="UpdateFeed"/>).
/// <paramref name="kind"/> 는 받을 zip 의 종류다. 포터블 판은 포터블 zip 을 고르고, 그 릴리스에 포터블 zip 이 없으면 기본 zip 을 받는다.
/// </summary>
public static ReleaseInfo? Parse(string json, Func<Uri, bool>? isTrusted = null)
public static ReleaseInfo? Parse(string json, Func<Uri, bool>? isTrusted = null, UpdatePackageKind kind = UpdatePackageKind.Standard)
{
var trusted = isTrusted ?? UpdateFeed.IsGitHubHttps;
try
Expand All @@ -46,30 +59,45 @@ public static class ReleaseParser
var page = TrustedUri(String(root, "html_url"), trusted);
if (tag is null || version is null || page is null) return null;

return new ReleaseInfo(version, tag, page, FindAsset(root, trusted));
return new ReleaseInfo(version, tag, page, FindAsset(root, trusted, kind));
}
catch (JsonException)
{
return null;
}
}

private static ReleaseAsset? FindAsset(JsonElement release, Func<Uri, bool> trusted)
private static ReleaseAsset? FindAsset(JsonElement release, Func<Uri, bool> trusted, UpdatePackageKind kind)
{
if (!release.TryGetProperty("assets", out var assets) || assets.ValueKind != JsonValueKind.Array) return null;
foreach (var asset in assets.EnumerateArray())
// 포터블 zip 이 있는데 믿을 수 없으면 기본 zip 으로 넘어가지 않고 페이지만 안내한다(다른 종류로 몰래 바꾸지 않는다).
if (kind == UpdatePackageKind.Portable && TryFind(assets, PortableAssetName, trusted, out var portable)) return portable;
return TryFind(assets, AssetName, trusted, out var standard) ? standard : null;
}

/// <summary>
/// 이름이 맞는 첫 자산을 찾으면 true 다. 그 자산의 주소·SHA-256·크기를 믿을 수 없으면 <paramref name="asset"/> 은 null 이다.
/// 이름이 맞는 자산이 없으면 false.
/// </summary>
private static bool TryFind(JsonElement assets, Regex assetName, Func<Uri, bool> trusted, out ReleaseAsset? asset)
{
asset = null;
foreach (var item in assets.EnumerateArray())
{
if (asset.ValueKind != JsonValueKind.Object) continue;
string? name = String(asset, "name");
if (name is null || !AssetName.IsMatch(name)) continue;
if (item.ValueKind != JsonValueKind.Object) continue;
string? name = String(item, "name");
if (name is null || !assetName.IsMatch(name)) continue;

var url = TrustedUri(String(asset, "browser_download_url"), trusted);
var digest = Sha256Digest.Match(String(asset, "digest") ?? "");
long size = asset.TryGetProperty("size", out var s) && s.ValueKind == JsonValueKind.Number && s.TryGetInt64(out long n) ? n : 0;
if (url is null || !digest.Success || size <= 0) return null;
return new ReleaseAsset(name, url, size, digest.Groups[1].Value.ToLowerInvariant());
var url = TrustedUri(String(item, "browser_download_url"), trusted);
var digest = Sha256Digest.Match(String(item, "digest") ?? "");
long size = item.TryGetProperty("size", out var s) && s.ValueKind == JsonValueKind.Number && s.TryGetInt64(out long n) ? n : 0;
if (url is not null && digest.Success && size > 0)
{
asset = new ReleaseAsset(name, url, size, digest.Groups[1].Value.ToLowerInvariant());
}
return true;
}
return null;
return false;
}

private static Uri? TrustedUri(string? text, Func<Uri, bool> trusted) =>
Expand Down
11 changes: 8 additions & 3 deletions SoundVisualizer.Core/Updates/UpdateClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ public sealed class UpdateClient
private readonly HttpClient _http;
private readonly Uri _latestUrl;
private readonly Func<Uri, bool> _isTrusted;
private readonly UpdatePackageKind _kind;

/// <summary><paramref name="feed"/> 를 주지 않으면 GitHub 에 묻는다(<see cref="UpdateFeed.GitHub"/>).</summary>
public UpdateClient(HttpClient http, Version currentVersion, UpdateFeed? feed = null)
/// <summary>
/// <paramref name="feed"/> 를 주지 않으면 GitHub 에 묻는다(<see cref="UpdateFeed.GitHub"/>). <paramref name="kind"/> 는 받을 zip 의
/// 종류다(포터블 판은 포터블 zip, #66).
/// </summary>
public UpdateClient(HttpClient http, Version currentVersion, UpdateFeed? feed = null, UpdatePackageKind kind = UpdatePackageKind.Standard)
{
var source = feed ?? UpdateFeed.GitHub;
_http = http;
_latestUrl = new Uri(source.ApiUrl);
_isTrusted = source.IsTrusted;
_kind = kind;
CurrentVersion = UpdateVersion.Normalize(currentVersion);
}

Expand All @@ -67,7 +72,7 @@ public async Task<UpdateCheckResult> CheckAsync(CancellationToken cancellationTo
if (!response.IsSuccessStatusCode) return new UpdateCheckResult(UpdateCheckOutcome.Failed, null);

string json = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
var release = ReleaseParser.Parse(json, _isTrusted);
var release = ReleaseParser.Parse(json, _isTrusted, _kind);
if (release is null) return new UpdateCheckResult(UpdateCheckOutcome.Failed, null);
return UpdateVersion.IsNewer(release.Version, CurrentVersion)
? new UpdateCheckResult(UpdateCheckOutcome.Available, release)
Expand Down
79 changes: 79 additions & 0 deletions SoundVisualizer.Tests/Updates/ReleaseParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,85 @@ public void 크기가_없거나_0_이면_쓰지_않는다()
Assert.Null(ReleaseParser.Parse(json.ToJsonString())!.Asset);
}

internal const string PortableDigest = "c0824bef63e57b436d632ed7f97b2c52fe9dafeb95ef57a7ec127c3b2684f165";

/// <summary>기본 zip 과 포터블 zip 을 함께 올린 릴리스(2.3.3 부터의 꼴, #64). 순서는 <paramref name="portableFirst"/> 로 정한다.</summary>
internal static JsonObject TwoZips(bool portableFirst = false)
{
var json = Sample();
var portable = new JsonObject
{
["name"] = "SoundVisualizer_v2.2_portable.zip",
["size"] = 104381910,
["digest"] = "sha256:" + PortableDigest,
["content_type"] = "application/zip",
["browser_download_url"] = "https://github.com/SOVIS-TEAM/SoundVisualizer-Windows/releases/download/SoundVisualizer_v2.2/SoundVisualizer_v2.2_portable.zip",
};
var assets = (JsonArray)json["assets"]!;
if (portableFirst) assets.Insert(0, portable);
else assets.Add(portable);
return json;
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void 포터블_판은_포터블_zip_을_기본_판은_기본_zip_을_고른다(bool portableFirst)
{
string json = TwoZips(portableFirst).ToJsonString();

var portable = ReleaseParser.Parse(json, kind: UpdatePackageKind.Portable)!.Asset!;
Assert.Equal("SoundVisualizer_v2.2_portable.zip", portable.Name);
Assert.Equal(PortableDigest, portable.Sha256Hex);
Assert.Equal(104381910, portable.Size);

var standard = ReleaseParser.Parse(json)!.Asset!;
Assert.Equal("SoundVisualizer_v2.2.zip", standard.Name);
Assert.Equal(Digest, standard.Sha256Hex);
}

[Fact]
public void 포터블_zip_이_없는_릴리스면_포터블_판도_기본_zip_을_받는다()
{
// 2.3.2 까지처럼 기본 zip 만 올린 릴리스. 포터블 판도 업데이트는 받는다(exe 옆에 DLL 과 AIModel 이 생긴다).
Assert.Equal("SoundVisualizer_v2.2.zip", ReleaseParser.Parse(Sample().ToJsonString(), kind: UpdatePackageKind.Portable)!.Asset!.Name);
}

[Fact]
public void 포터블_zip_만_있으면_기본_판은_받지_않고_페이지만_안내한다()
{
var json = Sample();
var asset = Asset(json);
asset["name"] = "SoundVisualizer_v2.2_portable.zip";
var release = ReleaseParser.Parse(json.ToJsonString());
Assert.NotNull(release);
Assert.Null(release.Asset);
}

[Fact]
public void 포터블_zip_을_믿을_수_없으면_기본_zip_으로_넘어가지_않는다()
{
var json = TwoZips();
((JsonObject)((JsonArray)json["assets"]!)[1]!)["digest"] = "sha1:0123";
var release = ReleaseParser.Parse(json.ToJsonString(), kind: UpdatePackageKind.Portable);
Assert.NotNull(release);
Assert.Null(release.Asset);
// 기본 판은 영향을 받지 않는다.
Assert.Equal("SoundVisualizer_v2.2.zip", ReleaseParser.Parse(json.ToJsonString())!.Asset!.Name);
}

[Theory]
[InlineData("SoundVisualizer_v2.2_portable.exe")]
[InlineData("SoundVisualizer_v2.2-portable.zip")]
[InlineData("SoundVisualizer_v2.2_Portable.zip")]
[InlineData("SoundVisualizer_v2.2_portable.zip.zip")]
public void 이름이_조금이라도_다른_포터블_zip_은_받지_않는다(string name)
{
var json = Sample();
Asset(json)["name"] = name;
Assert.Null(ReleaseParser.Parse(json.ToJsonString(), kind: UpdatePackageKind.Portable)!.Asset);
}

[Fact]
public void 다른_파일이_먼저_있어도_zip_을_찾는다()
{
Expand Down
15 changes: 15 additions & 0 deletions SoundVisualizer.Tests/Updates/UpdateClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public async Task 더_높은_버전이_있으면_새_버전이_있다고_답한
Assert.Equal(new Version(2, 3, 0), result.Release!.Version);
}

[Theory]
[InlineData(UpdatePackageKind.Portable, "SoundVisualizer_v2.2_portable.zip")]
[InlineData(UpdatePackageKind.Standard, "SoundVisualizer_v2.2.zip")]
public async Task 받을_zip_의_종류에_맞는_자산을_고른다(UpdatePackageKind kind, string expected)
{
var json = ReleaseParserTests.TwoZips();
json["tag_name"] = "SoundVisualizer_v2.3";
var client = new UpdateClient(new HttpClient(new FakeHandler(_ => Json(json.ToJsonString()))), Version.Parse("2.2.0"), kind: kind);

var result = await client.CheckAsync(CancellationToken.None);

Assert.Equal(UpdateCheckOutcome.Available, result.Outcome);
Assert.Equal(expected, result.Release!.Asset!.Name);
}

[Theory]
[InlineData("2.2.0")]
[InlineData("2.3.0")]
Expand Down
4 changes: 3 additions & 1 deletion SoundVisualizer/Updates/UpdateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ private UpdateController()
if (!ReferenceEquals(feed, UpdateFeed.GitHub)) AppLog.Info(Tag, "개발용 업데이트 주소를 씁니다: " + feed.ApiUrl);
// 응답 머리까지 기다리는 시간. 내려받는 본문은 끊김(60초 동안 아무것도 오지 않음)으로만 멈춘다(UpdateClient).
var http = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
_client = new UpdateClient(http, CurrentVersion, feed);
// 포터블 판(실행 파일 안에 모델이 든 빌드)은 포터블 zip 을 받아 포터블로 남는다(#66).
var kind = AppModels.IsBundled ? UpdatePackageKind.Portable : UpdatePackageKind.Standard;
_client = new UpdateClient(http, CurrentVersion, feed, kind);
}

public UpdateChannel Channel { get; }
Expand Down
Loading