Skip to content

Commit 894a267

Browse files
committed
fix(core/mobile): 修复 Android 端版本号显示为 v0.0.0.0
Android 没有托管 Main,Assembly.GetEntryAssembly() 恒为 null,版本元数据因此回退到不含 GitInfo 的 SecRandom.Core,导致设置页右上角、导出文件名、归档 producer_version、Sentry 版本串与移动端更新检查全部拿到 v0.0.0.0(Android 上导出的设置与全量数据还会被自己的 major==3 版本校验拒绝)。现在各平台头在启动时通过 GlobalConstants.SetVersionAssembly 显式发布承载版本元数据的程序集(SecRandom.Desktop/Program.Main、Android MobileApplication.CustomizeAppBuilder、iOS MobileEntryPoint.Main),Core 保留入口程序集回退供桌面与测试宿主使用,并把解析逻辑抽成可测试的 ReadMetadata。 同时把桌面与移动的更新版本比较收敛到新的 UpdateVersionComparer(先去 v 前缀,再去 -/+ 后缀),使 3.0.0-beta.1 这类预发布版本号也能正确比较;MobileUpdateService 改用 GlobalConstants.Version,不再自行读取入口程序集。 新增 GlobalConstantsTests 与 UpdateVersionComparerTests 覆盖头程序集发布、占位回退与版本比较规则,并同步更新根/SecRandom/SecRandom.Core 的 AGENTS.md 与 CHANGELOG(#289)。
1 parent 297c566 commit 894a267

13 files changed

Lines changed: 218 additions & 43 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Keep this map short and stable. When code moves, AI agents should re-read the mo
212212
- Settings-page explanation values (`*_D`, including `S_*_D` and `C_*_D`) must not use sentence-ending or sentence-separating full stops (`。` or `.`); preserve technical dots in file names, domains, process names, and version identifiers.
213213
- Views commonly set `DataContext = this` and expose `ViewModel`; bindings use `ViewModel.*`.
214214
- ViewModels use CommunityToolkit MVVM (`ObservableRecipient`, `[ObservableProperty]`); app VMs inherit `SecRandom.ViewModels.ViewModelBase`.
215-
- `Global.props` carries the main MSBuild behavior: unsafe enabled, Windows targeting enabled, SourceLink, full debug symbols, and default exclusion of project-local `artifacts/` / `publish/` output trees from SDK item globbing. The shared `AssemblyInfo.cs` is enabled only by entry assemblies that explicitly set `EnableGitInfoGenerator` (`SecRandom.Desktop`, `SecRandom.Android`, and `SecRandom.iOS`); `SecRandom.Core` reads that entry assembly metadata rather than generating a second public `SecRandom.GitInfo` type.
215+
- `Global.props` carries the main MSBuild behavior: unsafe enabled, Windows targeting enabled, SourceLink, full debug symbols, and default exclusion of project-local `artifacts/` / `publish/` output trees from SDK item globbing. The shared `AssemblyInfo.cs` is enabled only by entry assemblies that explicitly set `EnableGitInfoGenerator` (`SecRandom.Desktop`, `SecRandom.Android`, and `SecRandom.iOS`); `SecRandom.Core` reads that head-assembly metadata rather than generating a second public `SecRandom.GitInfo` type. Every head must publish itself through `GlobalConstants.SetVersionAssembly` during startup (`SecRandom.Desktop/Program.Main`, `SecRandom.Android` `MobileApplication.CustomizeAppBuilder`, `SecRandom.iOS/MobileEntryPoint.Main`) **before** any version value is read: Android has no managed `Main`, so `Assembly.GetEntryAssembly()` is null there and the lookup would otherwise fall back to `SecRandom.Core` and render the whole version surface — settings title bar, `producer_version`, Sentry release, update checks — as `v0.0.0.0`. A new platform head must therefore set its own assembly rather than rely on the entry-assembly fallback.
216216
- `Directory.Build.props` only pins `AvaloniaVersion`.
217217
- Standalone verification scripts live under `scripts/`; keep them self-contained and write outputs under `artifacts/`.
218218
- Release CI keeps generated material under `artifacts/release/`: RID publish trees under `publish/`, portable ZIP assembly under `portable/`, Windows installer staging under `installer/` and `setup/`, platform package workspaces under `linux/` / `macos/`, upload candidates under `dist/`, and release-job downloads/final signed assets under `downloaded/` / `output/`. Every Android build must receive the same configured release keystore secrets, rejects missing signing configuration, and verifies the produced APK certificate against that keystore before upload. The Android job stages signed arm64 and x64 APKs separately; the release job includes the supported APK artifacts in `output/`, the signed manifest, and the GitHub release. Portable ZIP contents must remain a root `SecRandomLauncher` plus one valid `app-*` payload directory; do not rearrange that runtime package contract.

‎CHANGELOG/v3/v3.0.0-release.1/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
- 修复 **抽取证明上报**,证明导出或上报失败会连带本次抽取失败的问题;现在抽取成功后才会发布证明,发布失败只记录警告并等待重试
3737
- 修复 **备份导入与插件安装**,异常备份包可能覆盖软件配置文件、插件替换失败会留下不完整插件目录的问题;现在失败时都会还原原有文件
3838
- 修复 **账号登录状态**,刷新登录令牌时先把新的访问令牌与刷新令牌一起原子写入磁盘再使用,多个请求同时刷新只会发出一次请求;服务端返回 `invalid_grant` 等终态错误时立即停止重试并清空本地令牌,访问令牌被吊销时不再白白消耗刷新令牌,限流与服务不可用则按提示退避后重试同一次刷新,避免旧令牌被反复提交导致账号被反复退出登录
39+
- 修复 **Android 版本号显示**,平板与手机上的版本号显示为 `v0.0.0.0`,导出的设置与备份也会因版本不符被自己拒绝导回的问题[#289](https://github.com/SECTL/SecRandom/issues/289)
40+
- 修复 **移动端更新检查**,无法识别 `3.0.0-beta.1` 这类预发布版本号,导致始终提示「已是最新」的问题
3941

4042
## 🙏 贡献者
4143

‎SecRandom.Android/MobileEntryPoint.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Avalonia.Android;
99
using CameraView;
1010
using CameraView.Platforms.Android;
11+
using SecRandom.Core;
1112
using SecRandom.Core.Abstraction;
1213
using SecRandom.Platforms;
1314
using SecRandom.Platforms.Abstractions;
@@ -28,6 +29,9 @@ protected MobileApplication(nint javaReference, JniHandleOwnership transfer)
2829

2930
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
3031
{
32+
// Android 没有托管 Main,Assembly.GetEntryAssembly() 恒为 null;不显式发布头程序集时,
33+
// 版本元数据会回退到不含 git 信息的 SecRandom.Core 并显示为 v0.0.0.0
34+
GlobalConstants.SetVersionAssembly(typeof(MobileApplication).Assembly);
3135
RegisterUnhandledExceptionHooks();
3236
var screenLayout = Resources?.Configuration?.ScreenLayout ?? ScreenLayout.SizeNormal;
3337
var isTablet = (screenLayout & ScreenLayout.SizeMask) >= ScreenLayout.SizeLarge;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Reflection;
2+
using System.Reflection.Emit;
3+
using SecRandom.Core;
4+
5+
namespace SecRandom.Core.Tests;
6+
7+
/// <summary>
8+
/// Version metadata is compiled into the platform head assembly (root AssemblyInfo.cs plus the GitInfo
9+
/// generator). Android has no managed entry point, so Assembly.GetEntryAssembly() is null there and the head
10+
/// publishes its own assembly instead; without that hand-off the settings title bar rendered v0.0.0.0 because
11+
/// the lookup fell back to this library, which carries no build metadata.
12+
/// </summary>
13+
public sealed class GlobalConstantsTests
14+
{
15+
[Fact]
16+
public void PublishedHeadAssemblySuppliesTheDisplayedVersion()
17+
{
18+
var originalSource = GlobalConstants.VersionSource;
19+
try
20+
{
21+
GlobalConstants.SetVersionAssembly(
22+
CreateProbeAssembly("v3.0.0-beta.1+0123456789abcdef0123456789abcdef01234567"));
23+
24+
Assert.Equal("v3.0.0-beta.1", GlobalConstants.Version);
25+
Assert.Equal("v3.0.0-beta.1", GlobalConstants.Tag);
26+
Assert.Equal("0123456", GlobalConstants.CommitHash);
27+
Assert.Equal("v3.0.0-beta.1-Nonomi-0123456(Unknown)", GlobalConstants.VersionLong);
28+
}
29+
finally
30+
{
31+
GlobalConstants.SetVersionAssembly(originalSource);
32+
}
33+
}
34+
35+
[Fact]
36+
public void AssemblyWithoutInformationalVersionKeepsThePlaceholder()
37+
{
38+
var originalSource = GlobalConstants.VersionSource;
39+
try
40+
{
41+
GlobalConstants.SetVersionAssembly(CreateProbeAssembly(null));
42+
43+
Assert.Equal("v0.0.0.0", GlobalConstants.Version);
44+
Assert.Equal("0.0.0.0", GlobalConstants.Tag);
45+
Assert.Equal("Unknown", GlobalConstants.Branch);
46+
}
47+
finally
48+
{
49+
GlobalConstants.SetVersionAssembly(originalSource);
50+
}
51+
}
52+
53+
[Fact]
54+
public void RepublishingRestoresMetadataFromTheOriginalSource()
55+
{
56+
var originalSource = GlobalConstants.VersionSource;
57+
var originalVersion = GlobalConstants.Version;
58+
try
59+
{
60+
GlobalConstants.SetVersionAssembly(CreateProbeAssembly("v9.9.9+ffffffffffffffffffffffffffffffffffffffff"));
61+
Assert.Equal("v9.9.9", GlobalConstants.Version);
62+
63+
GlobalConstants.SetVersionAssembly(originalSource);
64+
Assert.Equal(originalVersion, GlobalConstants.Version);
65+
}
66+
finally
67+
{
68+
GlobalConstants.SetVersionAssembly(originalSource);
69+
}
70+
}
71+
72+
private static Assembly CreateProbeAssembly(string? informationalVersion)
73+
{
74+
var builder = AssemblyBuilder.DefineDynamicAssembly(
75+
new AssemblyName($"SecRandom.VersionProbe.{Guid.NewGuid():N}"),
76+
AssemblyBuilderAccess.Run);
77+
if (informationalVersion is not null)
78+
{
79+
var constructor = typeof(AssemblyInformationalVersionAttribute).GetConstructor([typeof(string)])!;
80+
builder.SetCustomAttribute(new CustomAttributeBuilder(constructor, [informationalVersion]));
81+
}
82+
83+
return builder;
84+
}
85+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using SecRandom.Services.Updates;
2+
3+
namespace SecRandom.Core.Tests;
4+
5+
/// <summary>
6+
/// Signed update-manifest versions are release tags such as v3.0.0-beta.1, which System.Version cannot parse.
7+
/// Desktop and mobile update checks must share one rule so a pre-release train still sees newer releases.
8+
/// </summary>
9+
public sealed class UpdateVersionComparerTests
10+
{
11+
[Theory]
12+
[InlineData("3.1.0", "v3.0.0-beta.1", true)]
13+
[InlineData("3.0.1", "3.0.0", true)]
14+
[InlineData("v4.0.0", "v3.0.0-beta.1", true)]
15+
[InlineData("3.0.0", "v3.0.0-beta.1", false)]
16+
[InlineData("3.0.0-beta.2", "v3.0.0-beta.1", false)]
17+
[InlineData("3.0.0-beta.1", "v3.0.0-beta.1", false)]
18+
[InlineData("3.0.0+build.7", "3.0.0", false)]
19+
[InlineData("", "3.0.0", false)]
20+
[InlineData("3.0.0", "", false)]
21+
public void IsNewerComparesTheNumericCoreOfReleaseTags(string candidate, string current, bool expected) =>
22+
Assert.Equal(expected, UpdateVersionComparer.IsNewer(candidate, current));
23+
24+
[Theory]
25+
[InlineData("v3.0.0-beta.1", "3.0.0")]
26+
[InlineData("3.0.0+build.7", "3.0.0")]
27+
[InlineData(" v3.1.2 ", "3.1.2")]
28+
public void TryParseDropsThePrefixAndSuffix(string text, string expected)
29+
{
30+
Assert.True(UpdateVersionComparer.TryParse(text, out var version));
31+
Assert.Equal(Version.Parse(expected), version);
32+
}
33+
}

‎SecRandom.Core/AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ SecRandom.Core/
8080
- `IProfileService.LoadStudentProfile(name)` switches the app-layer active point-call student list and matching history; callers should use it instead of constructing profile configs directly when changing the active roll-call list.
8181
- Profile startup/switching validates nonempty names with `ProfileConfigBase` before path access or saving the active profile. Archive settings validation checks roll-call/quick-draw `DefaultClass` and lottery `DefaultPool` before snapshots or commit; the exact empty string means no selection, not a file name.
8282
- Registration helpers are responsible for both keyed DI and `PagesRegistryService` metadata.
83+
- Build metadata is never generated inside Core: the platform head assembly carries it (root `AssemblyInfo.cs` plus `EnableGitInfoGenerator`), and every head publishes itself through `GlobalConstants.SetVersionAssembly` before any version value is read. Android has no managed entry point, so `Assembly.GetEntryAssembly()` is null there and the version would otherwise fall back to this library and render as `v0.0.0.0` across the settings title bar, `producer_version`, export file names, and telemetry. Keep the entry-assembly fallback for desktop and test hosts, and never reintroduce a Core-local `SecRandom.GitInfo` type.
8384
- `DrawEngine` is partial: keep filtering in `DrawEngine.Filter.cs`, weight math in `DrawEngine.WeightCalculator.cs`,
8485
orchestration in `DrawEngine.cs`.
8586
- Weighted drawing validates count, candidates, and weights before sampling; preserve explicit `DrawStatus` returns over

‎SecRandom.Core/GlobalConstants.cs‎

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,58 @@ namespace SecRandom.Core;
55

66
public static class GlobalConstants
77
{
8-
private static readonly Assembly VersionAssembly = Assembly.GetEntryAssembly() ?? typeof(GlobalConstants).Assembly;
9-
private static readonly (string Tag, string Branch, string CommitHash) VersionParts = GetVersionParts();
8+
private static readonly object VersionSourceGate = new();
9+
private static Assembly? _versionAssembly;
10+
private static VersionMetadata? _versionMetadata;
1011

11-
public static string Tag => VersionParts.Tag;
12-
public static string Branch => VersionParts.Branch;
13-
public static string CommitHash => VersionParts.CommitHash[..Math.Min(7, VersionParts.CommitHash.Length)];
14-
public static string FullCommitHash => VersionParts.CommitHash;
12+
/// <summary>
13+
/// Assembly this build's version metadata is read from. The metadata is compiled into the platform head
14+
/// assembly (root AssemblyInfo.cs plus the GitInfo generator), and Android has no managed entry point, so
15+
/// <see cref="Assembly.GetEntryAssembly" /> returns null there and the head must publish itself through
16+
/// <see cref="SetVersionAssembly" />; otherwise the version falls back to this library, which carries no
17+
/// build metadata and renders as v0.0.0.0.
18+
/// </summary>
19+
private static Assembly VersionAssembly =>
20+
_versionAssembly ?? Assembly.GetEntryAssembly() ?? typeof(GlobalConstants).Assembly;
21+
22+
private static VersionMetadata Metadata
23+
{
24+
get
25+
{
26+
var cached = _versionMetadata;
27+
if (cached is not null)
28+
return cached;
29+
30+
lock (VersionSourceGate)
31+
return _versionMetadata ??= ReadMetadata(VersionAssembly);
32+
}
33+
}
34+
35+
internal static Assembly VersionSource => VersionAssembly;
36+
37+
/// <summary>
38+
/// Publishes the platform head assembly that carries this build's
39+
/// <see cref="AssemblyInformationalVersionAttribute" />. Platform heads call this during startup, before any
40+
/// version value is read; re-publishing clears the cached metadata.
41+
/// </summary>
42+
public static void SetVersionAssembly(Assembly assembly)
43+
{
44+
ArgumentNullException.ThrowIfNull(assembly);
45+
lock (VersionSourceGate)
46+
{
47+
_versionAssembly = assembly;
48+
_versionMetadata = null;
49+
}
50+
}
51+
52+
public static string Tag => Metadata.Tag;
53+
public static string Branch => Metadata.Branch;
54+
public static string CommitHash => Metadata.CommitHash[..Math.Min(7, Metadata.CommitHash.Length)];
55+
public static string FullCommitHash => Metadata.CommitHash;
1556

1657
public static string CodeName => @"Nonomi";
1758
public static string Version => Tag.StartsWith("v", StringComparison.OrdinalIgnoreCase) ? Tag : $@"v{Tag}";
18-
public static string AssemblyVersion => VersionAssembly
59+
public static string AssemblyVersion => VersionSource
1960
.GetCustomAttribute<AssemblyVersionAttribute>()?.Version ?? "0.0.0.0";
2061
public static string DisplayVersion => $@"{Version} (Codename {CodeName})";
2162
public static string VersionLong => $@"{Version}-{CodeName}-{CommitHash}({Branch})";
@@ -44,21 +85,23 @@ public static class GlobalConstants
4485
public static FontFamily DefaultAvaFontFamily { get; } =
4586
new(@"avares://SecRandom/Assets/Fonts/MiSans/#MiSans");
4687

47-
private static (string Tag, string Branch, string CommitHash) GetVersionParts()
88+
internal static VersionMetadata ReadMetadata(Assembly assembly)
4889
{
49-
var informationalVersion = VersionAssembly
90+
var informationalVersion = assembly
5091
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
5192
.InformationalVersion;
5293
if (string.IsNullOrWhiteSpace(informationalVersion))
53-
return ("0.0.0.0", "Unknown", "Unknown");
94+
return new VersionMetadata("0.0.0.0", "Unknown", "Unknown");
5495

5596
var separator = informationalVersion.IndexOf('+');
56-
var generatedGitInfo = VersionAssembly.GetType("SecRandom.GitInfo");
97+
var generatedGitInfo = assembly.GetType("SecRandom.GitInfo");
5798
var branch = generatedGitInfo?.GetField("Branch", BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as string
5899
?? generatedGitInfo?.GetProperty("Branch", BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as string
59100
?? "Unknown";
60101
return separator < 0
61-
? (informationalVersion, branch, "Unknown")
62-
: (informationalVersion[..separator], branch, informationalVersion[(separator + 1)..]);
102+
? new VersionMetadata(informationalVersion, branch, "Unknown")
103+
: new VersionMetadata(informationalVersion[..separator], branch, informationalVersion[(separator + 1)..]);
63104
}
64105
}
106+
107+
internal sealed record VersionMetadata(string Tag, string Branch, string CommitHash);

‎SecRandom.Desktop/Program.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Avalonia.Media;
77
using Avalonia.Platform;
88
using SecRandom;
9+
using SecRandom.Core;
910
using SecRandom.Extensions;
1011
using SecRandom.Services.CrashRecovery;
1112
using SecRandom.Services.Desktop;
@@ -30,6 +31,8 @@ internal sealed class Program
3031
[STAThread]
3132
public static void Main(string[] args)
3233
{
34+
// 版本元数据由头程序集承载;显式发布可让桌面端与移动头共用同一条解析路径
35+
GlobalConstants.SetVersionAssembly(typeof(Program).Assembly);
3336
// UiAccess startup reads the persisted topmost setting, so data-root selection must precede it.
3437
Utils.PrepareDesktopDataRoot();
3538
if (!UiAccessStartup.ShouldContinue(args))

‎SecRandom.iOS/MobileEntryPoint.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Avalonia.iOS;
44
using CoreGraphics;
55
using Foundation;
6+
using SecRandom.Core;
67
using SecRandom.Core.Abstraction;
78
using SecRandom.Platforms;
89
using SecRandom.Platforms.Abstractions;
@@ -18,6 +19,8 @@ public static class MobileEntryPoint
1819
{
1920
public static void Main(string[] args)
2021
{
22+
// iOS 有托管入口,GetEntryAssembly() 可用;这里仍显式发布头程序集,与 Android 头保持同一契约
23+
GlobalConstants.SetVersionAssembly(typeof(MobileEntryPoint).Assembly);
2124
UIApplication.Main(args, null, typeof(AppDelegate));
2225
}
2326
}

0 commit comments

Comments
 (0)