Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Commit c511f2f

Browse files
authored
feat: file-based version/metadata (#168)
* initial support * finish implementation
1 parent d8093e8 commit c511f2f

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

App/Basics.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,42 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Linq;
5+
using System.Reflection;
6+
using System.Text.Json;
57
using System.Threading;
68
using System.Windows;
79
using PCL.Core.Logging;
810
using PCL.Core.Utils;
911

1012
namespace PCL.Core.App;
1113

14+
/// <summary>
15+
/// 基础工具集。
16+
/// </summary>
1217
public static class Basics
1318
{
1419
#region 基本信息
1520

1621
/// <summary>
17-
/// 当前版本名称
22+
/// 启动器元数据
1823
/// </summary>
19-
public static string VersionName { get; set; } = "";
24+
public static MetadataModel Metadata { get; } = JsonSerializer.Deserialize<MetadataModel>(
25+
Assembly.GetEntryAssembly()!.GetManifestResourceStream("PCL.metadata.json")!)!;
2026

2127
/// <summary>
22-
/// 当前版本号
28+
/// 版本名称
2329
/// </summary>
24-
public static int VersionNumber { get; set; } = 0;
30+
public static string VersionName => Metadata.Version.BaseName;
31+
32+
/// <summary>
33+
/// 版本内部代号。
34+
/// </summary>
35+
public static int VersionCode => Metadata.Version.Code;
36+
37+
/// <summary>
38+
/// 版本分支名。
39+
/// </summary>
40+
public static string VersionBranch => Metadata.Version.BranchName;
2541

2642
#endregion
2743

@@ -135,6 +151,8 @@ public static void OpenPath(string path, string? workingDirectory = null)
135151
}
136152
#endregion
137153

154+
#region 应用程序操作
155+
138156
/// <summary>
139157
/// 获取程序打包资源的输入流。该资源必须声明为 <c>Resource</c> 类型,否则将会报错,<c>Images</c>
140158
/// 和 <c>Resources</c> 目录已默认声明该类型。
@@ -148,4 +166,6 @@ public static void OpenPath(string path, string? workingDirectory = null)
148166

149167
private const string AssemblyImagePath = "pack://application:,,,/Plain Craft Launcher 2;component/Images/";
150168
public static string GetAppImagePath(string imageName) => AssemblyImagePath + imageName;
169+
170+
#endregion
151171
}

App/Lifecycle.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Threading.Tasks;
1010
using System.Windows;
1111
using PCL.Core.Logging;
12-
using PCL.Core.Utils.OS;
1312

1413
namespace PCL.Core.App;
1514

App/Metadata.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Text.Json.Serialization;
2+
using PCL.Core.Utils.OS;
3+
4+
namespace PCL.Core.App;
5+
6+
/// <summary>
7+
/// 启动器版本信息模型
8+
/// </summary>
9+
/// <param name="BaseVersion">基本版本号, 如 <c>2.14.1</c></param>
10+
/// <param name="Suffix">后缀, 如 <c>beta.1</c></param>
11+
/// <param name="Code">内部版本代号, 如 <c>712</c></param>
12+
/// <param name="UpstreamVersion">上游版本, 如 <c>2.12.0</c></param>
13+
public sealed record LauncherVersionModel(
14+
[property: JsonPropertyName("base")] string BaseVersion,
15+
[property: JsonPropertyName("suffix")] string Suffix,
16+
[property: JsonPropertyName("code")] int Code,
17+
[property: JsonPropertyName("upstream")] string UpstreamVersion
18+
) {
19+
private static readonly (string Name, int Code) _CompilationBranchInfo =
20+
#if DEBUG
21+
("Debug", 100);
22+
#elif CI
23+
("CI", 50);
24+
#else
25+
("Publish", 0);
26+
#endif
27+
28+
private static readonly string? _SecretCommitInfo = EnvironmentInterop.GetSecret("GITHUB_SHA", false);
29+
30+
/// <summary>
31+
/// 基本版本名, 若 <see cref="Suffix"/> 存在实际值会附加后缀, 否则与 <see cref="BaseVersion"/> 相同
32+
/// </summary>
33+
public string BaseName { get; } = BaseVersion + (string.IsNullOrWhiteSpace(Suffix) ? "" : "-" + Suffix);
34+
35+
/// <summary>
36+
/// 发行分支名
37+
/// </summary>
38+
public string BranchName { get; } = _CompilationBranchInfo.Name;
39+
40+
/// <summary>
41+
/// 发行分支代号
42+
/// </summary>
43+
public int BranchCode { get; } = _CompilationBranchInfo.Code;
44+
45+
/// <summary>
46+
/// 标准版本号
47+
/// </summary>
48+
public string StandardVersion { get; } = BaseVersion + "." + _CompilationBranchInfo.Code;
49+
50+
/// <summary>
51+
/// 代码提交版本 hash, 若不存在 (非 CI 构建) 则为 <c>native</c>
52+
/// </summary>
53+
public string Commit { get; } = _SecretCommitInfo ?? "native";
54+
55+
/// <summary>
56+
/// 代码提交版本 hash 的摘要 (取前 7 位), 若不存在 (非 CI 构建) 则为 <c>native</c>
57+
/// </summary>
58+
public string CommitDigest { get; } = _SecretCommitInfo?[..7] ?? "native";
59+
}
60+
61+
/// <summary>
62+
/// 启动器元数据模型
63+
/// </summary>
64+
/// <param name="Name">程序名称</param>
65+
/// <param name="Version">版本信息</param>
66+
public sealed record MetadataModel(
67+
[property: JsonPropertyName("name")] string Name,
68+
[property: JsonPropertyName("version")] LauncherVersionModel Version
69+
);

Net/Http/Client/HttpRequestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private void _PrepareRequestParameters()
238238
if (_addLauncherHeader)
239239
{
240240
_request.Headers.TryAddWithoutValidation("User-Agent", $"PCL-Community/PCL2-CE/{Basics.VersionName} (pclc.cc)");
241-
_request.Headers.TryAddWithoutValidation("Referer", $"https://{Basics.VersionNumber}.ce.open.pcl2.server/");
241+
_request.Headers.TryAddWithoutValidation("Referer", $"https://{Basics.VersionCode}.ce.open.pcl2.server/");
242242
}
243243
}
244244

0 commit comments

Comments
 (0)