Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
APIUpdateManager.cs
浏览该文件的文档.
1using System.Text.Json;
2using System.Text.Json.Serialization;
3using System.Text.RegularExpressions;
4
5namespace Game {
6 public static class APIUpdateManager {
10 public static bool? IsNeedUpdate {
11 get;
12 // ReSharper disable UnusedAutoPropertyAccessor.Local
13 private set;
14 // ReSharper restore UnusedAutoPropertyAccessor.Local
15 }
16
21
25 public static string LatestVersion { get; private set; }
26
27 public static void Initialize() {
28#if !DEBUG
29 Task.Run(async () => { IsNeedUpdate = await GetIsNeedUpdate(); });
30#endif
31 }
32
37 public static async Task<bool> GetIsNeedUpdate() {
38 LatestVersion = await GetLatestVersion(true);
39 string currentVersion = ModsManager.APIVersionString;
40 return CompareVersion(currentVersion, LatestVersion) == -1;
41 }
42
49
56
60 public static Regex VersionRegex = new(@"(\d+)\.?(\d+)?\.?(\d+)?\.?(\d+)?");
61
68 public static uint ParseVersionFromString(string version) {
69 Match match = VersionRegex.Match(version);
70 if (match.Success) {
71 uint result = 0;
72 for (int i = 1; i < Math.Min(5, match.Groups.Count); i++) {
73 Group group = match.Groups[i];
74 if (group.Success) {
75 if (byte.TryParse(group.Value, out byte value)) {
76 result |= (uint)value << (8 * (4 - i));
77 }
78 else {
79 throw new FormatException($"Invalid version format: {version}");
80 }
81 }
82 }
83 return result;
84 }
85 throw new FormatException($"Invalid version format: {version}");
86 }
87
92 //当 direct 为真,将获取的 json 直接当做数组中的一个元素
93 //常用于链接自带 latest 标识的情况
94 public static async Task<string> GetLatestVersion(bool direct) {
95 using (JsonDocument remoteDoc = await GetLatestAPIJsonDocument()) {
96 JsonElement root = remoteDoc.RootElement;
97 // 假设 API 返回的版本信息在第一个 release 的 tag_name 字段
98 string input = direct
99 ? root.GetProperty("tag_name").GetString()
100 : root[root.GetArrayLength() - 1].GetProperty("tag_name").GetString();
101 return input;
102 }
103 }
104
114 public static int CompareVersion(string current, string target) {
115 if (target == "API_OLD") {
116 return current == "API_OLD" ? 0 : 1;
117 }
118 if (current == "API_OLD") {
119 return -1;
120 }
121 uint currentVersion = ParseVersionFromString(current);
122 uint targetVersion = ParseVersionFromString(target);
123 return currentVersion.CompareTo(targetVersion);
124 }
125 }
126
127 /*
128 这里面的字段名称由于涉及到反序列化解析千万不能改!!
129 这里面的字段名称由于涉及到反序列化解析千万不能改!!
130 这里面的字段名称由于涉及到反序列化解析千万不能改!!
131 */
132 public class ReleaseInfo {
133 public long id { get; set; }
134 public string tag_name { get; set; }
135 public string target_commitish { get; set; }
136 public bool prerelease { get; set; }
137 public string name { get; set; }
138 public string body { get; set; }
139 public Author author { get; set; }
140 public string created_at { get; set; }
141 public List<Asset> assets { get; set; }
142 }
143
144 /*
145 这里面的字段名称由于涉及到反序列化解析千万不能改!!
146 这里面的字段名称由于涉及到反序列化解析千万不能改!!
147 这里面的字段名称由于涉及到反序列化解析千万不能改!!
148 */
149 public class Author {
150 public long id { get; set; }
151 public string login { get; set; }
152 public string name { get; set; }
153 public string avatar_url { get; set; }
154 public string url { get; set; }
155 public string html_url { get; set; }
156 public string remark { get; set; }
157 public string followers_url { get; set; }
158 public string following_url { get; set; }
159 public string gists_url { get; set; }
160 public string starred_url { get; set; }
161 public string subscriptions_url { get; set; }
162 public string organizations_url { get; set; }
163 public string repos_url { get; set; }
164 public string events_url { get; set; }
165 public string received_events_url { get; set; }
166 public string type { get; set; }
167 }
168
169 /*
170 这里面的字段名称由于涉及到反序列化解析千万不能改!!
171 这里面的字段名称由于涉及到反序列化解析千万不能改!!
172 这里面的字段名称由于涉及到反序列化解析千万不能改!!
173 */
174 public class Asset {
175 public string browser_download_url { get; set; }
176 public string name { get; set; }
177 }
178
179 [JsonSourceGenerationOptions(
180 PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower
181 )]
182 [JsonSerializable(typeof(List<ReleaseInfo>))]
183 [JsonSerializable(typeof(ReleaseInfo))]
184 [JsonSerializable(typeof(Author))]
185 [JsonSerializable(typeof(List<Asset>))]
186 [JsonSerializable(typeof(Asset))]
187 public partial class GiteeReleaseInfoJsonContext : JsonSerializerContext
188 {
189 }
190}
static uint ParseVersionFromString(string version)
将API版本字符串以点分十进制数转为uint
static ? bool IsNeedUpdate
API是否需要更新?ture:需要;false:不需要;null:正在获取
static async Task< string > GetLatestVersion(bool direct)
获取 Gitee release最后一个版本的版本号
static string CurrentVersion
当前API版本
static async Task< bool > GetIsNeedUpdate()
对比API版本,判断是否需要更新
static async Task< JsonDocument > GetLatestAPIJsonDocument()
获取 Gitee release最后一个版本的Json文件数据
static Regex VersionRegex
版本号X.X.X.X的正则表达式s
static async Task< JsonDocument > GetAPIReleasesJsonDocument()
获取 Gitee 所有release版本的Json文件数据
static string LatestVersion
网络上最新的API的版本
static int CompareVersion(string current, string target)
比较两个版本的新旧关系。 current大于target,返回1 current小于target,返回-1 版本相等,返回0
static async Task< JsonDocument > GetJsonFromUrlAsync(string url)
从链接获取 Json 文档
static string APILatestReleaseLink_API
static string APIVersionString
static string APIReleasesLink_API