Files
community/Ink Canvas/Helpers/AutoUpdateHelper.cs
T

1378 lines
72 KiB
C#
Raw Normal View History

2025-05-25 09:29:48 +08:00
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Text;
using System.Collections.Generic;
2025-07-22 18:25:04 +08:00
using System.Threading;
2025-07-16 15:28:36 +08:00
using Newtonsoft.Json;
2025-07-22 17:07:27 +08:00
using Newtonsoft.Json.Linq;
2025-05-25 09:29:48 +08:00
namespace Ink_Canvas.Helpers
{
internal class AutoUpdateHelper
{
2025-06-29 11:56:38 +08:00
// 定义超时时间为10秒
private static readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(10);
2025-07-19 15:36:05 +08:00
private static string updatesFolderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "AutoUpdate");
private static string statusFilePath = null;
// 线路组结构体(包含版本、下载、日志地址)
public class UpdateLineGroup
2025-05-25 09:29:48 +08:00
{
2025-07-19 15:36:05 +08:00
public string GroupName { get; set; } // 组名
public string VersionUrl { get; set; } // 版本检测地址
public string DownloadUrlFormat { get; set; } // 下载地址格式(带{0}占位符)
public string LogUrl { get; set; } // 更新日志地址
}
// 通道-线路组映射
2025-07-22 18:02:29 +08:00
public static readonly Dictionary<UpdateChannel, List<UpdateLineGroup>> ChannelLineGroups = new Dictionary<UpdateChannel, List<UpdateLineGroup>>
2025-07-19 15:36:05 +08:00
{
{ UpdateChannel.Release, new List<UpdateLineGroup>
2025-06-29 11:56:38 +08:00
{
2025-07-19 15:36:05 +08:00
new UpdateLineGroup
{
GroupName = "GitHub主线",
VersionUrl = "https://github.com/InkCanvasForClass/community/raw/refs/heads/main/AutomaticUpdateVersionControl.txt",
DownloadUrlFormat = "https://github.com/InkCanvasForClass/community/releases/download/{0}/InkCanvasForClass.CE.{0}.zip",
LogUrl = "https://github.com/InkCanvasForClass/community/raw/refs/heads/main/UpdateLog.md"
},
new UpdateLineGroup
{
GroupName = "bgithub备用",
VersionUrl = "https://bgithub.xyz/InkCanvasForClass/community/raw/refs/heads/main/AutomaticUpdateVersionControl.txt",
DownloadUrlFormat = "https://bgithub.xyz/InkCanvasForClass/community/releases/download/{0}/InkCanvasForClass.CE.{0}.zip",
LogUrl = "https://bgithub.xyz/InkCanvasForClass/community/raw/refs/heads/main/UpdateLog.md"
},
new UpdateLineGroup
{
GroupName = "kkgithub线路",
VersionUrl = "https://kkgithub.com/InkCanvasForClass/community/raw/refs/heads/main/AutomaticUpdateVersionControl.txt",
DownloadUrlFormat = "https://kkgithub.com/InkCanvasForClass/community/releases/download/{0}/InkCanvasForClass.CE.{0}.zip",
LogUrl = "https://kkgithub.com/InkCanvasForClass/community/raw/refs/heads/main/UpdateLog.md"
2025-07-24 22:16:38 +08:00
},
new UpdateLineGroup
{
GroupName = "智教联盟",
DownloadUrlFormat = "https://get.smart-teach.cn/d/Ningbo-S3/shared/jiangling/community/InkCanvasForClass.CE.{0}.zip",
2025-07-19 15:36:05 +08:00
}
2025-06-29 11:56:38 +08:00
}
2025-07-19 15:36:05 +08:00
},
{ UpdateChannel.Beta, new List<UpdateLineGroup>
2025-06-29 11:56:38 +08:00
{
2025-07-19 15:36:05 +08:00
new UpdateLineGroup
{
GroupName = "GitHub主线",
VersionUrl = "https://github.com/InkCanvasForClass/community-beta/raw/refs/heads/main/AutomaticUpdateVersionControl.txt",
DownloadUrlFormat = "https://github.com/InkCanvasForClass/community-beta/releases/download/{0}/InkCanvasForClass.CE.{0}.zip",
LogUrl = "https://github.com/InkCanvasForClass/community-beta/raw/refs/heads/main/UpdateLog.md"
},
new UpdateLineGroup
{
GroupName = "bgithub备用",
VersionUrl = "https://bgithub.xyz/InkCanvasForClass/community-beta/raw/refs/heads/main/AutomaticUpdateVersionControl.txt",
DownloadUrlFormat = "https://bgithub.xyz/InkCanvasForClass/community-beta/releases/download/{0}/InkCanvasForClass.CE.{0}.zip",
LogUrl = "https://bgithub.xyz/InkCanvasForClass/community-beta/raw/refs/heads/main/UpdateLog.md"
},
new UpdateLineGroup
{
GroupName = "kkgithub线路",
VersionUrl = "https://kkgithub.com/InkCanvasForClass/community-beta/raw/refs/heads/main/AutomaticUpdateVersionControl.txt",
DownloadUrlFormat = "https://kkgithub.com/InkCanvasForClass/community-beta/releases/download/{0}/InkCanvasForClass.CE.{0}.zip",
LogUrl = "https://kkgithub.com/InkCanvasForClass/community-beta/raw/refs/heads/main/UpdateLog.md"
2025-07-24 22:16:38 +08:00
},
new UpdateLineGroup
{
GroupName = "智教联盟",
DownloadUrlFormat = "https://get.smart-teach.cn/d/Ningbo-S3/shared/jiangling/community-beta/InkCanvasForClass.CE.{0}.zip",
2025-07-19 15:36:05 +08:00
}
2025-06-29 11:56:38 +08:00
}
2025-07-19 15:36:05 +08:00
}
};
2025-05-25 09:29:48 +08:00
2025-07-22 18:45:43 +08:00
// 区块任务结构体(移到类体内)
private class BlockTask
{
public int Index;
public long Start;
public long End;
public int RetryCount;
}
2025-07-19 15:36:05 +08:00
// 检测URL延迟
private static async Task<long> GetUrlDelay(string url)
{
try
{
2025-07-19 16:51:03 +08:00
// 检测是否为Windows 7
var osVersion = Environment.OSVersion;
bool isWindows7 = osVersion.Version.Major == 6 && osVersion.Version.Minor == 1;
if (isWindows7)
{
// Windows 7使用特殊配置
using (var handler = new HttpClientHandler())
{
// 配置HttpClientHandler以支持Windows 7
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromSeconds(5);
var sw = Stopwatch.StartNew();
var resp = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
sw.Stop();
if (resp.IsSuccessStatusCode)
return sw.ElapsedMilliseconds;
}
}
}
else
2025-06-18 22:40:48 +08:00
{
2025-07-19 16:51:03 +08:00
// 其他Windows版本使用标准配置
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(5);
var sw = Stopwatch.StartNew();
var resp = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
sw.Stop();
if (resp.IsSuccessStatusCode)
return sw.ElapsedMilliseconds;
}
2025-06-18 22:40:48 +08:00
}
2025-07-19 15:36:05 +08:00
}
catch { }
return -1;
}
2025-06-18 22:40:48 +08:00
2025-07-19 16:03:45 +08:00
// 检测线路组延迟,返回最快组(保持向后兼容)
2025-07-19 15:36:05 +08:00
private static async Task<UpdateLineGroup> GetFastestLineGroup(UpdateChannel channel)
2025-07-19 16:03:45 +08:00
{
var availableGroups = await GetAvailableLineGroupsOrdered(channel);
return availableGroups.Count > 0 ? availableGroups[0] : null;
}
// 获取所有可用线路组,按延迟排序
public static async Task<List<UpdateLineGroup>> GetAvailableLineGroupsOrdered(UpdateChannel channel)
2025-07-19 15:36:05 +08:00
{
var groups = ChannelLineGroups[channel];
2025-07-19 16:03:45 +08:00
var availableGroups = new List<(UpdateLineGroup group, long delay)>();
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 开始检测通道 {channel} 下所有线路组延迟...");
foreach (var group in groups)
{
2025-07-24 22:16:38 +08:00
// 跳过“智教联盟”线路组,不参与延迟检测和排序
if (group.GroupName == "智教联盟")
{
LogHelper.WriteLogToFile($"AutoUpdate | 跳过智教联盟线路组延迟检测");
continue;
}
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 检测线路组: {group.GroupName} ({group.VersionUrl})");
var delay = await GetUrlDelay(group.VersionUrl);
if (delay >= 0)
2025-05-25 09:29:48 +08:00
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 线路组 {group.GroupName} 延迟: {delay}ms");
2025-07-19 16:03:45 +08:00
availableGroups.Add((group, delay));
2025-05-25 09:29:48 +08:00
}
else
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 线路组 {group.GroupName} 不可用", LogHelper.LogType.Warning);
2025-05-25 09:29:48 +08:00
}
}
2025-07-19 15:36:05 +08:00
2025-07-19 16:03:45 +08:00
// 按延迟排序,延迟最小的排在前面
var orderedGroups = availableGroups
.OrderBy(x => x.delay)
.Select(x => x.group)
.ToList();
2025-07-24 22:16:38 +08:00
// 将“智教联盟”线路组插入到最前面(如果存在)
var zhiJiaoGroup = groups.FirstOrDefault(g => g.GroupName == "智教联盟");
if (zhiJiaoGroup != null)
{
orderedGroups.Insert(0, zhiJiaoGroup);
LogHelper.WriteLogToFile($"AutoUpdate | 智教联盟线路组已插入到首位");
}
2025-07-19 16:03:45 +08:00
if (orderedGroups.Count > 0)
2025-05-25 09:29:48 +08:00
{
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 找到 {orderedGroups.Count} 个可用线路组,按延迟排序:");
for (int i = 0; i < orderedGroups.Count; i++)
{
LogHelper.WriteLogToFile($"AutoUpdate | {i + 1}. {orderedGroups[i].GroupName}");
}
2025-07-19 15:36:05 +08:00
}
else
{
LogHelper.WriteLogToFile("AutoUpdate | 所有线路组均不可用", LogHelper.LogType.Error);
2025-05-25 09:29:48 +08:00
}
2025-07-19 15:36:05 +08:00
2025-07-19 16:03:45 +08:00
return orderedGroups;
2025-05-25 09:29:48 +08:00
}
2025-07-19 15:36:05 +08:00
// 获取远程版本号
private static async Task<string> GetRemoteVersion(string fileUrl)
2025-05-25 09:29:48 +08:00
{
2025-07-19 16:51:03 +08:00
// 检测是否为Windows 7
var osVersion = Environment.OSVersion;
bool isWindows7 = osVersion.Version.Major == 6 && osVersion.Version.Minor == 1;
if (isWindows7)
2025-05-25 09:29:48 +08:00
{
2025-07-19 16:51:03 +08:00
// Windows 7使用特殊配置
using (var handler = new HttpClientHandler())
2025-05-25 09:29:48 +08:00
{
2025-07-19 16:51:03 +08:00
try
{
// 配置HttpClientHandler以支持Windows 7
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
using (HttpClient client = new HttpClient(handler))
{
client.Timeout = RequestTimeout;
LogHelper.WriteLogToFile($"AutoUpdate | 发送HTTP请求到: {fileUrl}");
var downloadTask = client.GetAsync(fileUrl);
var timeoutTask = Task.Delay(RequestTimeout);
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask == timeoutTask)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时 ({RequestTimeout.TotalSeconds}秒)", LogHelper.LogType.Error);
return null;
}
HttpResponseMessage response = await downloadTask;
LogHelper.WriteLogToFile($"AutoUpdate | HTTP响应状态: {response.StatusCode}");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
content = content.Trim();
// 如果内容包含HTML(可能是GitHub页面而不是原始内容),尝试提取版本号
if (content.Contains("<html") || content.Contains("<!DOCTYPE"))
{
LogHelper.WriteLogToFile($"AutoUpdate | 收到HTML内容而不是原始版本号 - 尝试提取版本");
int startPos = content.IndexOf("<table");
if (startPos > 0)
{
int endPos = content.IndexOf("</table>", startPos);
if (endPos > startPos)
{
string tableContent = content.Substring(startPos, endPos - startPos);
var match = System.Text.RegularExpressions.Regex.Match(tableContent, @"(\d+\.\d+\.\d+(\.\d+)?)");
if (match.Success)
{
content = match.Groups[1].Value;
LogHelper.WriteLogToFile($"AutoUpdate | 从HTML提取版本: {content}");
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | 无法从HTML内容提取版本");
return null;
}
}
}
}
LogHelper.WriteLogToFile($"AutoUpdate | 响应内容: {content}");
return content;
}
}
catch (HttpRequestException ex)
2025-06-18 22:40:48 +08:00
{
2025-07-19 16:51:03 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | HTTP请求错误: {ex.Message}", LogHelper.LogType.Error);
}
catch (TaskCanceledException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时: {ex.Message}", LogHelper.LogType.Error);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 错误: {ex.Message}", LogHelper.LogType.Error);
2025-06-18 22:40:48 +08:00
}
2025-05-25 09:29:48 +08:00
2025-07-19 16:51:03 +08:00
return null;
}
}
else
{
// 其他Windows版本使用标准配置
using (HttpClient client = new HttpClient())
{
try
2025-06-18 16:08:26 +08:00
{
2025-07-19 16:51:03 +08:00
client.Timeout = RequestTimeout;
LogHelper.WriteLogToFile($"AutoUpdate | 发送HTTP请求到: {fileUrl}");
var downloadTask = client.GetAsync(fileUrl);
var timeoutTask = Task.Delay(RequestTimeout);
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask == timeoutTask)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时 ({RequestTimeout.TotalSeconds}秒)", LogHelper.LogType.Error);
return null;
}
HttpResponseMessage response = await downloadTask;
LogHelper.WriteLogToFile($"AutoUpdate | HTTP响应状态: {response.StatusCode}");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
content = content.Trim();
// 如果内容包含HTML(可能是GitHub页面而不是原始内容),尝试提取版本号
if (content.Contains("<html") || content.Contains("<!DOCTYPE"))
2025-06-18 16:08:26 +08:00
{
2025-07-19 16:51:03 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 收到HTML内容而不是原始版本号 - 尝试提取版本");
int startPos = content.IndexOf("<table");
if (startPos > 0)
2025-06-18 16:08:26 +08:00
{
2025-07-19 16:51:03 +08:00
int endPos = content.IndexOf("</table>", startPos);
if (endPos > startPos)
2025-06-18 16:08:26 +08:00
{
2025-07-19 16:51:03 +08:00
string tableContent = content.Substring(startPos, endPos - startPos);
var match = System.Text.RegularExpressions.Regex.Match(tableContent, @"(\d+\.\d+\.\d+(\.\d+)?)");
if (match.Success)
{
content = match.Groups[1].Value;
LogHelper.WriteLogToFile($"AutoUpdate | 从HTML提取版本: {content}");
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | 无法从HTML内容提取版本");
return null;
}
2025-06-18 16:08:26 +08:00
}
}
}
2025-07-19 16:51:03 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 响应内容: {content}");
return content;
}
catch (HttpRequestException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | HTTP请求错误: {ex.Message}", LogHelper.LogType.Error);
}
catch (TaskCanceledException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时: {ex.Message}", LogHelper.LogType.Error);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 错误: {ex.Message}", LogHelper.LogType.Error);
2025-06-18 16:08:26 +08:00
}
2025-05-25 09:29:48 +08:00
2025-07-19 16:51:03 +08:00
return null;
}
2025-05-25 09:29:48 +08:00
}
}
2025-07-22 17:07:27 +08:00
// 通过GitHub API获取最新Release信息
private static async Task<(string version, string downloadUrl, string releaseNotes)> GetLatestGithubRelease(UpdateChannel channel)
{
try
{
string apiUrl = channel == UpdateChannel.Beta
? "https://api.github.com/repos/InkCanvasForClass/community-beta/releases/latest"
: "https://api.github.com/repos/InkCanvasForClass/community/releases/latest";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "ICC-CE Auto Updater");
var response = await client.GetStringAsync(apiUrl);
var json = JObject.Parse(response);
string version = json["tag_name"]?.ToString();
string releaseNotes = json["body"]?.ToString();
string downloadUrl = json["assets"]?.First?["browser_download_url"]?.ToString();
if (!string.IsNullOrEmpty(version) && !string.IsNullOrEmpty(downloadUrl))
return (version, downloadUrl, releaseNotes);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | GitHub Releases API 获取失败: {ex.Message}", LogHelper.LogType.Warning);
}
return (null, null, null);
}
2025-07-19 16:03:45 +08:00
// 主要的更新检测方法(优先检测延迟,失败时自动切换线路组)
2025-07-22 18:02:29 +08:00
// 仅检测新版本时用GitHub API,实际下载时只用线路组
public static async Task<(string remoteVersion, UpdateLineGroup lineGroup, string releaseNotes)> CheckForUpdates(UpdateChannel channel = UpdateChannel.Release, bool alwaysGetRemote = false)
2025-07-19 15:36:05 +08:00
{
try
{
string localVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
LogHelper.WriteLogToFile($"AutoUpdate | 本地版本: {localVersion}");
2025-07-22 17:07:27 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 优先通过GitHub Releases API检测...");
// 1. 优先通过GitHub Releases API获取
2025-07-22 18:02:29 +08:00
var (apiVersion, _, apiReleaseNotes) = await GetLatestGithubRelease(channel);
if (!string.IsNullOrEmpty(apiVersion))
2025-07-22 17:07:27 +08:00
{
Version local = new Version(localVersion);
Version remote = new Version(apiVersion.TrimStart('v', 'V'));
if (remote > local || alwaysGetRemote)
{
LogHelper.WriteLogToFile($"AutoUpdate | 通过GitHub Releases API发现新版本: {apiVersion}");
2025-07-22 18:02:29 +08:00
// 只返回版本号和日志,不返回直链
var group = (await GetAvailableLineGroupsOrdered(channel)).FirstOrDefault();
return (apiVersion, group, apiReleaseNotes);
2025-07-22 17:07:27 +08:00
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | 当前版本已是最新 (GitHub Releases API)");
2025-07-22 18:02:29 +08:00
var group = (await GetAvailableLineGroupsOrdered(channel)).FirstOrDefault();
return (null, group, apiReleaseNotes);
2025-07-22 17:07:27 +08:00
}
}
// 2. 回退到原有txt方案
LogHelper.WriteLogToFile($"AutoUpdate | GitHub Releases API获取失败,回退到txt方案...");
2025-07-19 16:03:45 +08:00
var availableGroups = await GetAvailableLineGroupsOrdered(channel);
if (availableGroups.Count == 0)
2025-07-19 15:36:05 +08:00
{
LogHelper.WriteLogToFile("AutoUpdate | 所有线路组均不可用", LogHelper.LogType.Error);
2025-07-22 18:02:29 +08:00
return (null, null, null);
2025-07-19 15:36:05 +08:00
}
2025-07-19 16:03:45 +08:00
foreach (var group in availableGroups)
2025-07-19 15:36:05 +08:00
{
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 尝试使用线路组获取版本信息: {group.GroupName}");
string remoteVersion = await GetRemoteVersion(group.VersionUrl);
if (remoteVersion != null)
2025-07-19 15:36:05 +08:00
{
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 成功从线路组 {group.GroupName} 获取远程版本: {remoteVersion}");
Version local = new Version(localVersion);
Version remote = new Version(remoteVersion);
if (remote > local || alwaysGetRemote)
{
LogHelper.WriteLogToFile($"AutoUpdate | 发现新版本或强制获取: {remoteVersion}");
2025-07-22 18:02:29 +08:00
return (remoteVersion, group, null);
2025-07-19 16:03:45 +08:00
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | 当前版本已是最新");
2025-07-22 18:02:29 +08:00
return (null, group, null);
2025-07-19 16:03:45 +08:00
}
2025-07-19 15:36:05 +08:00
}
else
{
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 线路组 {group.GroupName} 获取版本失败,尝试下一个线路组", LogHelper.LogType.Warning);
2025-07-19 15:36:05 +08:00
}
}
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile("AutoUpdate | 所有线路组均无法获取版本信息", LogHelper.LogType.Error);
2025-07-22 18:02:29 +08:00
return (null, null, null);
2025-07-19 15:36:05 +08:00
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | CheckForUpdates错误: {ex.Message}", LogHelper.LogType.Error);
2025-07-22 18:02:29 +08:00
return (null, null, null);
2025-07-19 15:36:05 +08:00
}
}
2025-05-25 09:29:48 +08:00
2025-07-19 15:36:05 +08:00
// 使用指定线路组下载新版
public static async Task<bool> DownloadSetupFile(string version, UpdateLineGroup group)
2025-07-19 16:03:45 +08:00
{
return await DownloadSetupFileWithFallback(version, new List<UpdateLineGroup> { group });
}
2025-07-24 22:16:38 +08:00
// 获取智教联盟真实下载地址
private static async Task<string> GetZhijiaoRealDownloadUrl(string url)
{
try
{
using (var handler = new HttpClientHandler { AllowAutoRedirect = false })
using (var client = new HttpClient(handler))
{
client.Timeout = RequestTimeout;
var resp = await client.GetAsync(url);
// 优先取Location头
if (resp.StatusCode == System.Net.HttpStatusCode.Found || resp.StatusCode == System.Net.HttpStatusCode.Redirect || resp.StatusCode == System.Net.HttpStatusCode.MovedPermanently)
{
if (resp.Headers.Location != null)
{
var realUrl = resp.Headers.Location.ToString();
if (realUrl.Contains(" ")) realUrl = realUrl.Replace(" ", "%20");
return realUrl;
}
}
// 有些服务器直接返回真实地址在内容里
var content = await resp.Content.ReadAsStringAsync();
if (Uri.IsWellFormedUriString(content.Trim(), UriKind.Absolute))
{
var realUrl = content.Trim();
if (realUrl.Contains(" ")) realUrl = realUrl.Replace(" ", "%20");
return realUrl;
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 获取智教联盟真实下载地址失败: {ex.Message}", LogHelper.LogType.Error);
}
return null;
}
2025-07-19 16:03:45 +08:00
// 使用多线路组下载新版(支持自动切换)
2025-07-22 18:02:29 +08:00
public static async Task<bool> DownloadSetupFileWithFallback(string version, List<UpdateLineGroup> groups, Action<double, string> progressCallback = null)
2025-05-25 09:29:48 +08:00
{
try
{
statusFilePath = Path.Combine(updatesFolderPath, $"DownloadV{version}Status.txt");
if (File.Exists(statusFilePath) && File.ReadAllText(statusFilePath).Trim().ToLower() == "true")
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile("AutoUpdate | 安装包已下载");
2025-07-22 18:02:29 +08:00
progressCallback?.Invoke(100, "已下载完成");
2025-05-25 09:29:48 +08:00
return true;
}
2025-07-19 15:36:05 +08:00
// 确保更新目录存在
if (!Directory.Exists(updatesFolderPath))
{
Directory.CreateDirectory(updatesFolderPath);
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 创建更新目录: {updatesFolderPath}");
}
2025-07-19 15:36:05 +08:00
string zipFilePath = Path.Combine(updatesFolderPath, $"InkCanvasForClass.CE.{version}.zip");
LogHelper.WriteLogToFile($"AutoUpdate | 目标文件路径: {zipFilePath}");
2025-05-25 09:29:48 +08:00
SaveDownloadStatus(false);
2025-07-19 16:03:45 +08:00
2025-07-24 22:16:38 +08:00
// 优先尝试“智教联盟”线路组
var zhiJiaoGroup = groups.FirstOrDefault(g => g.GroupName == "智教联盟");
if (zhiJiaoGroup != null)
{
groups = new List<UpdateLineGroup> { zhiJiaoGroup }.Concat(groups.Where(g => g.GroupName != "智教联盟")).ToList();
LogHelper.WriteLogToFile($"AutoUpdate | 下载时优先尝试智教联盟线路组");
}
2025-07-19 16:03:45 +08:00
// 依次尝试每个线路组
foreach (var group in groups)
{
2025-07-19 16:03:45 +08:00
string url = string.Format(group.DownloadUrlFormat, version);
2025-07-24 22:16:38 +08:00
// 智教联盟需要先获取真实下载地址
if (group.GroupName == "智教联盟")
{
LogHelper.WriteLogToFile($"AutoUpdate | 获取智教联盟真实下载地址: {url}");
var realUrl = await GetZhijiaoRealDownloadUrl(url);
if (string.IsNullOrEmpty(realUrl))
{
LogHelper.WriteLogToFile($"AutoUpdate | 智教联盟真实下载地址获取失败,跳过", LogHelper.LogType.Warning);
progressCallback?.Invoke(0, "智教联盟真实下载地址获取失败,跳过");
continue;
}
url = realUrl;
LogHelper.WriteLogToFile($"AutoUpdate | 智教联盟真实下载地址: {url}");
}
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 尝试从线路组 {group.GroupName} 下载: {url}");
2025-07-22 18:36:37 +08:00
2025-07-22 18:02:29 +08:00
bool downloadSuccess = await DownloadFile(url, zipFilePath, progressCallback);
2025-07-22 18:36:37 +08:00
2025-07-19 16:03:45 +08:00
if (downloadSuccess)
{
SaveDownloadStatus(true);
LogHelper.WriteLogToFile($"AutoUpdate | 从线路组 {group.GroupName} 下载成功");
2025-07-22 18:02:29 +08:00
progressCallback?.Invoke(100, "下载完成");
2025-07-19 16:03:45 +08:00
return true;
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | 线路组 {group.GroupName} 下载失败,尝试下一个线路组", LogHelper.LogType.Warning);
}
}
2025-07-22 18:36:37 +08:00
2025-07-19 16:03:45 +08:00
LogHelper.WriteLogToFile("AutoUpdate | 所有线路组下载均失败", LogHelper.LogType.Error);
2025-07-22 18:02:29 +08:00
progressCallback?.Invoke(0, "所有线路组下载均失败");
2025-07-19 16:03:45 +08:00
return false;
2025-05-25 09:29:48 +08:00
}
catch (Exception ex)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 下载更新时出错: {ex.Message}", LogHelper.LogType.Error);
if (ex.InnerException != null)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 内部异常: {ex.InnerException.Message}", LogHelper.LogType.Error);
}
2025-05-25 09:29:48 +08:00
SaveDownloadStatus(false);
2025-07-22 18:02:29 +08:00
progressCallback?.Invoke(0, $"下载异常: {ex.Message}");
2025-05-25 09:29:48 +08:00
return false;
}
}
2025-07-19 15:36:05 +08:00
// 下载文件的具体实现
2025-07-22 18:25:04 +08:00
public static async Task<bool> DownloadFile(string fileUrl, string destinationPath, Action<double, string> progressCallback = null)
2025-05-25 09:29:48 +08:00
{
2025-07-22 18:25:04 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 正在尝试多线程下载: {fileUrl}");
2025-07-22 18:08:22 +08:00
int maxRetry = 3;
2025-07-22 21:01:25 +08:00
int[] threadOptions = new int[] { 32, 4 };
2025-07-24 22:16:38 +08:00
// 检查服务器是否支持Range分块下载
bool supportRange = false;
long totalSize = -1;
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
var req = new HttpRequestMessage(HttpMethod.Head, fileUrl);
req.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(0, 0);
var resp = await client.SendAsync(req);
if (resp.StatusCode == System.Net.HttpStatusCode.PartialContent)
{
supportRange = true;
if (resp.Content.Headers.ContentRange != null && resp.Content.Headers.ContentRange.Length.HasValue)
{
totalSize = resp.Content.Headers.ContentRange.Length.Value;
}
else if (resp.Content.Headers.ContentLength.HasValue)
{
totalSize = resp.Content.Headers.ContentLength.Value;
}
}
else if (resp.StatusCode == System.Net.HttpStatusCode.OK)
{
supportRange = false;
if (resp.Content.Headers.ContentLength.HasValue)
{
totalSize = resp.Content.Headers.ContentLength.Value;
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 检查Range支持时异常: {ex.Message}", LogHelper.LogType.Warning);
}
if (!supportRange)
{
LogHelper.WriteLogToFile($"AutoUpdate | 服务器不支持分块下载,自动降级为单线程下载");
progressCallback?.Invoke(0, "服务器不支持分块下载,自动降级为单线程下载");
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
using (var resp = await client.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead))
{
resp.EnsureSuccessStatusCode();
using (var fs = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
var stream = await resp.Content.ReadAsStreamAsync();
byte[] buffer = new byte[8192];
int read;
long downloaded = 0;
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fs.WriteAsync(buffer, 0, read);
downloaded += read;
if (totalSize > 0)
{
double percent = (double)downloaded / totalSize * 100;
progressCallback?.Invoke(percent, $"单线程下载中: {percent:F1}%");
}
}
}
}
}
progressCallback?.Invoke(100, "单线程下载完成");
return true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 单线程下载失败: {ex.Message}", LogHelper.LogType.Error);
progressCallback?.Invoke(0, $"单线程下载失败: {ex.Message}");
return false;
}
}
2025-07-22 21:01:25 +08:00
foreach (int threadCount in threadOptions)
2025-05-25 09:29:48 +08:00
{
2025-07-24 22:16:38 +08:00
if (totalSize <= 0)
{
totalSize = await GetContentLength(fileUrl);
}
2025-07-22 21:01:25 +08:00
if (totalSize <= 0)
{
progressCallback?.Invoke(0, "无法获取文件大小,取消下载");
return false;
}
int blockSize = (int)Math.Ceiling((double)totalSize / threadCount);
int blockCount = (int)Math.Ceiling((double)totalSize / blockSize);
var blockQueue = new System.Collections.Concurrent.ConcurrentQueue<BlockTask>();
var finishedBlocks = new System.Collections.Concurrent.ConcurrentDictionary<int, bool>();
long[] blockDownloaded = new long[blockCount];
for (int i = 0; i < blockCount; i++)
{
long start = i * blockSize;
long end = Math.Min(start + blockSize - 1, totalSize - 1);
blockQueue.Enqueue(new BlockTask { Index = i, Start = start, End = end, RetryCount = 0 });
}
CancellationTokenSource cts = new CancellationTokenSource();
var tasks = new List<Task>();
for (int t = 0; t < threadCount; t++)
2025-05-25 09:29:48 +08:00
{
2025-07-22 21:01:25 +08:00
tasks.Add(Task.Run(async () =>
2025-05-25 09:29:48 +08:00
{
2025-07-22 21:01:25 +08:00
while (blockQueue.TryDequeue(out var block))
{
2025-07-22 21:01:25 +08:00
bool success = false;
for (int retry = block.RetryCount; retry < maxRetry && !success; retry++)
2025-07-19 16:51:03 +08:00
{
2025-07-22 21:01:25 +08:00
try
{
2025-07-22 21:01:25 +08:00
using (var client = new HttpClient())
2025-07-19 16:51:03 +08:00
{
2025-07-24 22:16:38 +08:00
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
2025-07-22 21:01:25 +08:00
var req = new HttpRequestMessage(HttpMethod.Get, fileUrl);
req.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(block.Start, block.End);
2025-07-23 19:29:44 +08:00
// 新增:分块下载超时机制
var downloadCts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token);
var lastReadTime = DateTime.UtcNow;
bool dataReceived = false;
using (var resp = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, downloadCts.Token))
2025-07-22 18:08:22 +08:00
{
2025-07-24 22:16:38 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 分块{block.Index} 响应状态: {resp.StatusCode}");
2025-07-22 21:01:25 +08:00
resp.EnsureSuccessStatusCode();
string tempPath = destinationPath + $".part{block.Index}";
using (var fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None))
2025-07-22 18:45:43 +08:00
{
2025-07-22 21:01:25 +08:00
var stream = await resp.Content.ReadAsStreamAsync();
byte[] buffer = new byte[8192];
int read;
2025-07-23 19:29:44 +08:00
while (true)
2025-07-22 21:01:25 +08:00
{
2025-07-23 19:29:44 +08:00
var readTask = stream.ReadAsync(buffer, 0, buffer.Length, downloadCts.Token);
var timeoutTask = Task.Delay(15000, downloadCts.Token); // 15秒超时
var completed = await Task.WhenAny(readTask, timeoutTask);
if (completed == timeoutTask)
{
// 超时未收到数据,取消本线程,重新入队
LogHelper.WriteLogToFile($"AutoUpdate | 分块{block.Index} 15秒无数据,线程超时重试", LogHelper.LogType.Warning);
progressCallback?.Invoke(0, $"分块{block.Index} 15秒无数据,线程超时重试");
downloadCts.Cancel();
break;
}
read = await readTask;
if (read <= 0) break;
await fs.WriteAsync(buffer, 0, read, downloadCts.Token);
2025-07-22 21:01:25 +08:00
blockDownloaded[block.Index] += read;
2025-07-23 19:29:44 +08:00
lastReadTime = DateTime.UtcNow;
dataReceived = true;
2025-07-22 21:01:25 +08:00
// 合并所有块进度
long totalDownloaded = blockDownloaded.Sum();
double percent = (double)totalDownloaded / totalSize * 100;
progressCallback?.Invoke(percent, $"多线程下载中({threadCount}线程): {percent:F1}%");
}
2025-07-22 18:45:43 +08:00
}
2025-07-22 18:08:22 +08:00
}
2025-07-23 19:29:44 +08:00
// 如果因超时break且未完成,success为false,重新入队
if (!dataReceived)
{
throw new IOException("分块下载超时无数据");
}
2025-06-18 22:40:48 +08:00
}
2025-07-22 21:01:25 +08:00
success = true;
}
2025-07-23 19:29:44 +08:00
catch (Exception ex) when (ex is HttpRequestException || ex is IOException || ex is TaskCanceledException)
2025-07-22 21:01:25 +08:00
{
LogHelper.WriteLogToFile($"AutoUpdate | 分块{block.Index}下载失败,第{retry + 1}次: {ex.Message}", LogHelper.LogType.Warning);
progressCallback?.Invoke(0, $"分块{block.Index}下载失败,第{retry + 1}次: {ex.Message}");
await Task.Delay(15000);
}
2025-06-18 22:40:48 +08:00
}
2025-07-22 21:01:25 +08:00
if (success)
2025-07-19 16:51:03 +08:00
{
2025-07-22 21:01:25 +08:00
finishedBlocks[block.Index] = true;
}
else if (block.RetryCount + 1 < maxRetry)
{
// 失败但未超最大重试,重新入队
block.RetryCount++;
blockQueue.Enqueue(block);
}
else
{
// 超过最大重试,取消所有任务
cts.Cancel();
break;
2025-07-19 16:51:03 +08:00
}
}
2025-07-22 21:01:25 +08:00
}));
}
await Task.WhenAll(tasks);
if (cts.IsCancellationRequested || finishedBlocks.Count != blockCount)
2025-07-22 18:25:04 +08:00
{
2025-07-22 21:01:25 +08:00
progressCallback?.Invoke(0, $"多线程下载失败({threadCount}线程)");
// 清理分块文件
for (int i = 0; i < blockCount; i++)
{
string tempPath = destinationPath + $".part{i}";
if (File.Exists(tempPath)) File.Delete(tempPath);
}
if (threadCount == threadOptions.Last())
{
// 已经是最后一次尝试
return false;
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | {threadCount}线程下载失败,尝试降级为{threadOptions.Last()}线程");
progressCallback?.Invoke(0, $"{threadCount}线程下载失败,尝试降级为{threadOptions.Last()}线程");
continue;
}
2025-07-22 18:08:22 +08:00
}
2025-07-22 21:01:25 +08:00
// 合并所有块
using (var output = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
2025-07-22 18:08:22 +08:00
{
2025-07-22 21:01:25 +08:00
for (int i = 0; i < blockCount; i++)
{
2025-07-22 21:01:25 +08:00
string tempPath = destinationPath + $".part{i}";
using (var input = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
{
await input.CopyToAsync(output);
}
File.Delete(tempPath);
}
2025-07-22 18:08:22 +08:00
}
2025-07-22 21:01:25 +08:00
progressCallback?.Invoke(100, $"多线程下载完成({threadCount}线程)");
return true;
2025-07-22 18:25:04 +08:00
}
2025-07-22 21:01:25 +08:00
// 理论上不会到这里
return false;
2025-07-22 18:25:04 +08:00
}
// 获取文件总大小
private static async Task<long> GetContentLength(string fileUrl)
{
try
{
using (var client = new HttpClient())
2025-07-22 18:08:22 +08:00
{
2025-07-22 18:25:04 +08:00
var req = new HttpRequestMessage(HttpMethod.Head, fileUrl);
var resp = await client.SendAsync(req);
if (resp.IsSuccessStatusCode && resp.Content.Headers.ContentLength.HasValue)
return resp.Content.Headers.ContentLength.Value;
2025-07-22 18:36:37 +08:00
}
}
catch { }
2025-07-22 18:25:04 +08:00
return -1;
2025-05-25 09:29:48 +08:00
}
2025-07-19 15:36:05 +08:00
// 保存下载状态
2025-05-25 09:29:48 +08:00
private static void SaveDownloadStatus(bool isSuccess)
{
try
{
if (statusFilePath == null) return;
string directory = Path.GetDirectoryName(statusFilePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
File.WriteAllText(statusFilePath, isSuccess.ToString());
}
catch (Exception ex)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 保存下载状态时出错: {ex.Message}", LogHelper.LogType.Error);
2025-05-25 09:29:48 +08:00
}
}
2025-07-19 15:36:05 +08:00
// 安装新版本应用
2025-05-25 09:29:48 +08:00
public static void InstallNewVersionApp(string version, bool isInSilence)
{
try
{
2025-07-16 15:28:36 +08:00
// 在更新前备份设置文件
try
{
if (MainWindow.Settings.Advanced.IsAutoBackupBeforeUpdate)
{
string backupDir = Path.Combine(App.RootPath, "Backups");
if (!Directory.Exists(backupDir))
{
Directory.CreateDirectory(backupDir);
LogHelper.WriteLogToFile($"创建备份目录: {backupDir}", LogHelper.LogType.Info);
}
string backupFileName = $"Settings_BeforeUpdate_v{version}_{DateTime.Now:yyyyMMdd_HHmmss}.json";
string backupPath = Path.Combine(backupDir, backupFileName);
2025-07-19 15:36:05 +08:00
string settingsJson = JsonConvert.SerializeObject(MainWindow.Settings, Formatting.Indented);
2025-07-16 15:28:36 +08:00
File.WriteAllText(backupPath, settingsJson);
LogHelper.WriteLogToFile($"更新前自动备份设置成功: {backupPath}", LogHelper.LogType.Info);
}
else
{
LogHelper.WriteLogToFile("更新前自动备份功能已禁用,跳过备份", LogHelper.LogType.Info);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新前自动备份设置时出错: {ex.Message}", LogHelper.LogType.Error);
}
2025-06-18 15:40:07 +08:00
string zipFilePath = Path.Combine(updatesFolderPath, $"InkCanvasForClass.CE.{version}.zip");
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 检查ZIP文件: {zipFilePath}");
2025-05-25 09:29:48 +08:00
2025-06-18 15:40:07 +08:00
if (!File.Exists(zipFilePath))
2025-05-25 09:29:48 +08:00
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | ZIP文件未找到: {zipFilePath}", LogHelper.LogType.Error);
2025-05-25 09:29:48 +08:00
return;
}
FileInfo fileInfo = new FileInfo(zipFilePath);
if (fileInfo.Length == 0)
2025-05-25 09:29:48 +08:00
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | ZIP文件为空,无法继续", LogHelper.LogType.Error);
return;
2025-06-18 15:40:07 +08:00
}
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | ZIP文件大小: {fileInfo.Length} 字节");
2025-06-18 15:40:07 +08:00
string currentAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
int currentProcessId = Process.GetCurrentProcess().Id;
string appPath = Assembly.GetExecutingAssembly().Location;
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 当前应用程序目录: {currentAppDir}");
LogHelper.WriteLogToFile($"AutoUpdate | 当前进程ID: {currentProcessId}");
LogHelper.WriteLogToFile($"AutoUpdate | 静默更新模式: {isInSilence}");
2025-06-18 15:40:07 +08:00
string batchFilePath = Path.Combine(Path.GetTempPath(), "UpdateICC_" + Guid.NewGuid().ToString().Substring(0, 8) + ".bat");
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 创建更新批处理文件: {batchFilePath}");
StringBuilder batchContent = new StringBuilder();
batchContent.AppendLine("@echo off");
batchContent.AppendLine("echo Set objShell = CreateObject(\"WScript.Shell\") > \"%temp%\\hideme.vbs\"");
batchContent.AppendLine("echo objShell.Run \"cmd /c \"\"\" ^& WScript.Arguments(0) ^& \"\"\"\", 0, True >> \"%temp%\\hideme.vbs\"");
batchContent.AppendLine($"echo Wscript.Sleep 100 >> \"%temp%\\hideme.vbs\"");
string updateBatPath = Path.Combine(Path.GetTempPath(), "ICCUpdate_" + Guid.NewGuid().ToString().Substring(0, 8) + ".bat");
batchContent.AppendLine($"echo @echo off > \"{updateBatPath}\"");
batchContent.AppendLine($"echo set PROC_ID={currentProcessId} >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo :CHECK_PROCESS >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo tasklist /fi \"PID eq %PROC_ID%\" ^| find \"%PROC_ID%\" ^> nul >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo if %%ERRORLEVEL%% == 0 ( >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo timeout /t 1 /nobreak ^> nul >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo goto CHECK_PROCESS >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo ) >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo Application closed, starting update process... >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo timeout /t 2 /nobreak ^> nul >> \"{updateBatPath}\"");
string extractPath = Path.Combine(updatesFolderPath, $"Extract_{version}");
batchContent.AppendLine($"echo echo Extracting update files... >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo if exist \"{extractPath}\" rd /s /q \"{extractPath}\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo mkdir \"{extractPath}\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo powershell -command \"Expand-Archive -Path '{zipFilePath.Replace("'", "''")}' -DestinationPath '{extractPath.Replace("'", "''")}' -Force\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo if %%ERRORLEVEL%% neq 0 ( >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo goto ERROR_EXIT >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo ) >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo Copying updated files to application directory... >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo xcopy /s /y /e \"{extractPath}\\*\" \"{currentAppDir}\\\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo if %%ERRORLEVEL%% neq 0 ( >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo goto ERROR_EXIT >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo ) >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo Cleaning up temporary files... >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo if exist \"{extractPath}\" rd /s /q \"{extractPath}\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo if exist \"{zipFilePath}\" del /f /q \"{zipFilePath}\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo Update completed successfully! >> \"{updateBatPath}\"");
2025-06-19 11:47:16 +08:00
if (isInSilence)
{
batchContent.AppendLine($"echo echo 自动启动应用程序... >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo start \"\" \"{appPath}\" >> \"{updateBatPath}\"");
}
else
{
2025-07-23 21:45:29 +08:00
batchContent.AppendLine($"echo taskkill /F /IM \"InkCanvasForClass.exe\" >nul 2>nul >> \"{updateBatPath}\"");
2025-06-19 11:47:16 +08:00
batchContent.AppendLine($"echo :: 检查应用程序是否已经在运行 >> \"{updateBatPath}\"");
2025-07-23 21:45:29 +08:00
batchContent.AppendLine($"echo tasklist /FI \"IMAGENAME eq InkCanvasForClass.exe\" | find /i \"InkCanvasForClass.exe\" > nul >> \"{updateBatPath}\"");
2025-06-19 11:47:16 +08:00
batchContent.AppendLine($"echo if %%ERRORLEVEL%% neq 0 ( >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo 启动应用程序... >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo start \"\" \"{appPath}\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo ) else ( >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo 应用程序已经在运行,不再重复启动 >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo ) >> \"{updateBatPath}\"");
}
batchContent.AppendLine($"echo exit /b 0 >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo goto EXIT >> \"{updateBatPath}\"");
2025-06-19 11:47:16 +08:00
if (isInSilence)
{
batchContent.AppendLine($"echo :ERROR_EXIT >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo echo Update failed! >> \"%temp%\\icc_update_error.log\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo exit /b 1 >> \"{updateBatPath}\"");
}
else
{
batchContent.AppendLine($"echo :ERROR_EXIT >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo start \"\" cmd /c \"echo Update failed! ^& pause\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo exit /b 1 >> \"{updateBatPath}\"");
}
batchContent.AppendLine($"echo :EXIT >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo del \"{updateBatPath}\" >> \"{updateBatPath}\"");
batchContent.AppendLine($"echo exit >> \"{updateBatPath}\"");
batchContent.AppendLine($"wscript \"%temp%\\hideme.vbs\" \"{updateBatPath}\"");
batchContent.AppendLine("del \"%temp%\\hideme.vbs\"");
batchContent.AppendLine("exit");
File.WriteAllText(batchFilePath, batchContent.ToString());
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 创建更新批处理文件完成");
Process.Start(new ProcessStartInfo
2025-06-18 15:40:07 +08:00
{
FileName = batchFilePath,
CreateNoWindow = true,
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden
});
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 启动更新批处理进程(隐藏窗口)");
2025-05-25 09:29:48 +08:00
}
catch (Exception ex)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 准备更新安装时出错: {ex.Message}", LogHelper.LogType.Error);
if (ex.InnerException != null)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 内部异常: {ex.InnerException.Message}", LogHelper.LogType.Error);
2025-05-25 09:29:48 +08:00
}
}
}
2025-07-19 15:36:05 +08:00
// 获取远程内容的通用方法
public static async Task<string> GetRemoteContent(string fileUrl)
2025-06-29 11:56:38 +08:00
{
2025-07-19 16:51:03 +08:00
// 检测是否为Windows 7
var osVersion = Environment.OSVersion;
bool isWindows7 = osVersion.Version.Major == 6 && osVersion.Version.Minor == 1;
if (isWindows7)
2025-06-29 11:56:38 +08:00
{
2025-07-19 16:51:03 +08:00
// Windows 7使用特殊配置
using (var handler = new HttpClientHandler())
2025-06-29 11:56:38 +08:00
{
2025-07-19 16:51:03 +08:00
try
2025-06-29 11:56:38 +08:00
{
2025-07-19 16:51:03 +08:00
// 配置HttpClientHandler以支持Windows 7
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
using (HttpClient client = new HttpClient(handler))
{
client.Timeout = RequestTimeout;
LogHelper.WriteLogToFile($"AutoUpdate | 发送HTTP请求到: {fileUrl}");
var downloadTask = client.GetAsync(fileUrl);
var timeoutTask = Task.Delay(RequestTimeout);
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask == timeoutTask)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时 ({RequestTimeout.TotalSeconds}秒)", LogHelper.LogType.Error);
return null;
}
HttpResponseMessage response = await downloadTask;
LogHelper.WriteLogToFile($"AutoUpdate | HTTP响应状态: {response.StatusCode}");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
return content;
}
2025-06-29 11:56:38 +08:00
}
2025-07-19 16:51:03 +08:00
catch (HttpRequestException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | HTTP请求错误: {ex.Message}", LogHelper.LogType.Error);
}
catch (TaskCanceledException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时: {ex.Message}", LogHelper.LogType.Error);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 错误: {ex.Message}", LogHelper.LogType.Error);
}
return null;
2025-06-29 11:56:38 +08:00
}
2025-07-19 16:51:03 +08:00
}
else
{
// 其他Windows版本使用标准配置
using (HttpClient client = new HttpClient())
2025-06-29 11:56:38 +08:00
{
2025-07-19 16:51:03 +08:00
try
{
client.Timeout = RequestTimeout;
LogHelper.WriteLogToFile($"AutoUpdate | 发送HTTP请求到: {fileUrl}");
var downloadTask = client.GetAsync(fileUrl);
var timeoutTask = Task.Delay(RequestTimeout);
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask == timeoutTask)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时 ({RequestTimeout.TotalSeconds}秒)", LogHelper.LogType.Error);
return null;
}
HttpResponseMessage response = await downloadTask;
LogHelper.WriteLogToFile($"AutoUpdate | HTTP响应状态: {response.StatusCode}");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
return content;
}
catch (HttpRequestException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | HTTP请求错误: {ex.Message}", LogHelper.LogType.Error);
}
catch (TaskCanceledException ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 请求超时: {ex.Message}", LogHelper.LogType.Error);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 错误: {ex.Message}", LogHelper.LogType.Error);
}
return null;
2025-06-29 11:56:38 +08:00
}
}
}
2025-07-19 15:36:05 +08:00
// 使用指定线路组获取更新日志
public static async Task<string> GetUpdateLogWithLineGroup(UpdateLineGroup group)
{
return await GetRemoteContent(group.LogUrl);
}
// 获取更新日志(自动选择最快线路组)
public static async Task<string> GetUpdateLog(UpdateChannel channel = UpdateChannel.Release)
{
var group = await GetFastestLineGroup(channel);
if (group == null) return "# 无法获取更新日志\n\n所有线路均不可用。";
return await GetUpdateLogWithLineGroup(group);
}
2025-07-19 15:36:05 +08:00
// 删除更新文件夹
public static void DeleteUpdatesFolder()
{
try
{
if (Directory.Exists(updatesFolderPath))
{
foreach (string file in Directory.GetFiles(updatesFolderPath, "*", SearchOption.AllDirectories))
{
try { File.Delete(file); } catch { }
}
foreach (string dir in Directory.GetDirectories(updatesFolderPath))
{
try { Directory.Delete(dir, true); } catch { }
}
try { Directory.Delete(updatesFolderPath, true); } catch { }
}
}
catch { }
}
2025-07-19 15:36:05 +08:00
// 版本修复方法,强制下载并安装指定通道的最新版本
public static async Task<bool> FixVersion(UpdateChannel channel = UpdateChannel.Release)
{
try
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 开始修复版本,通道: {channel}");
// 获取远程版本号(自动选择最快线路组,始终下载远程版本)
2025-07-22 18:02:29 +08:00
var (remoteVersion, group, _) = await CheckForUpdates(channel, true);
if (string.IsNullOrEmpty(remoteVersion) || group == null)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile("AutoUpdate | 修复版本时获取远程版本失败", LogHelper.LogType.Error);
return false;
}
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | 修复版本远程版本: {remoteVersion}");
// 无论版本是否为最新,都下载远程版本
2025-07-19 15:36:05 +08:00
bool downloadResult = await DownloadSetupFile(remoteVersion, group);
if (!downloadResult)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile("AutoUpdate | 修复版本时下载更新失败", LogHelper.LogType.Error);
return false;
}
2025-07-19 15:36:05 +08:00
// 执行安装,非静默模式
InstallNewVersionApp(remoteVersion, false);
App.IsAppExitByUser = true;
Application.Current.Dispatcher.Invoke(() => {
Application.Current.Shutdown();
});
return true;
}
catch (Exception ex)
{
2025-07-19 15:36:05 +08:00
LogHelper.WriteLogToFile($"AutoUpdate | FixVersion错误: {ex.Message}", LogHelper.LogType.Error);
return false;
}
}
2025-07-19 16:51:03 +08:00
2025-07-22 17:07:27 +08:00
// 获取所有GitHub历史版本(Release
public static async Task<List<(string version, string downloadUrl, string releaseNotes)>> GetAllGithubReleases(UpdateChannel channel = UpdateChannel.Release)
{
var result = new List<(string, string, string)>();
try
{
string apiUrl = channel == UpdateChannel.Beta
? "https://api.github.com/repos/InkCanvasForClass/community-beta/releases"
: "https://api.github.com/repos/InkCanvasForClass/community/releases";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "ICC-CE Auto Updater");
var response = await client.GetStringAsync(apiUrl);
var arr = JArray.Parse(response);
foreach (var item in arr)
{
string version = item["tag_name"]?.ToString();
string releaseNotes = item["body"]?.ToString();
string downloadUrl = item["assets"]?.First?["browser_download_url"]?.ToString();
if (!string.IsNullOrEmpty(version) && !string.IsNullOrEmpty(downloadUrl))
result.Add((version, downloadUrl, releaseNotes));
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 获取历史版本失败: {ex.Message}", LogHelper.LogType.Error);
}
return result;
}
2025-07-19 16:51:03 +08:00
// 测试Windows 7 TLS连接的方法
public static async Task<bool> TestWindows7TlsConnection()
{
try
{
// 检测是否为Windows 7
var osVersion = Environment.OSVersion;
bool isWindows7 = osVersion.Version.Major == 6 && osVersion.Version.Minor == 1;
if (!isWindows7)
{
LogHelper.WriteLogToFile("AutoUpdate | 当前系统不是Windows 7,跳过TLS连接测试", LogHelper.LogType.Info);
return true; // 非Windows 7系统直接返回成功
}
LogHelper.WriteLogToFile("AutoUpdate | 开始测试Windows 7 TLS连接...");
// 测试GitHub连接
var testUrl = "https://github.com/InkCanvasForClass/community/raw/refs/heads/main/AutomaticUpdateVersionControl.txt";
using (var handler = new HttpClientHandler())
{
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromSeconds(10);
var response = await client.GetAsync(testUrl);
if (response.IsSuccessStatusCode)
{
LogHelper.WriteLogToFile("AutoUpdate | Windows 7 TLS连接测试成功");
return true;
}
else
{
LogHelper.WriteLogToFile($"AutoUpdate | Windows 7 TLS连接测试失败,状态码: {response.StatusCode}", LogHelper.LogType.Error);
return false;
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | Windows 7 TLS连接测试异常: {ex.Message}", LogHelper.LogType.Error);
return false;
}
}
2025-07-22 18:36:37 +08:00
/// <summary>
/// 启动手动指定版本的多线路多线程下载并自动安装(用于历史版本回滚等场景)
/// </summary>
public static async Task<bool> StartManualDownloadAndInstall(string version, UpdateChannel channel, Action<double, string> progressCallback = null)
{
try
{
2025-07-22 22:39:02 +08:00
// 先检测并排序所有可用线路组
var groups = await GetAvailableLineGroupsOrdered(channel);
2025-07-22 18:36:37 +08:00
bool downloadSuccess = await DownloadSetupFileWithFallback(version, groups, progressCallback);
if (!downloadSuccess)
{
LogHelper.WriteLogToFile($"AutoUpdate | 手动下载版本{version}失败");
return false;
}
LogHelper.WriteLogToFile($"AutoUpdate | 手动安装版本: {version}");
InstallNewVersionApp(version, false);
App.IsAppExitByUser = true;
Application.Current.Dispatcher.Invoke(() => {
Application.Current.Shutdown();
});
return true;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 手动下载或安装异常: {ex.Message}", LogHelper.LogType.Error);
progressCallback?.Invoke(0, $"下载异常: {ex.Message}");
return false;
}
}
2025-05-25 09:29:48 +08:00
}
internal class AutoUpdateWithSilenceTimeComboBox
{
public static ObservableCollection<string> Hours { get; set; } = new ObservableCollection<string>();
public static ObservableCollection<string> Minutes { get; set; } = new ObservableCollection<string>();
public static void InitializeAutoUpdateWithSilenceTimeComboBoxOptions(ComboBox startTimeComboBox, ComboBox endTimeComboBox)
{
for (int hour = 0; hour <= 23; ++hour)
{
Hours.Add(hour.ToString("00"));
}
for (int minute = 0; minute <= 59; minute += 20)
{
Minutes.Add(minute.ToString("00"));
}
startTimeComboBox.ItemsSource = Hours.SelectMany(h => Minutes.Select(m => $"{h}:{m}"));
endTimeComboBox.ItemsSource = Hours.SelectMany(h => Minutes.Select(m => $"{h}:{m}"));
}
public static bool CheckIsInSilencePeriod(string startTime, string endTime)
{
if (startTime == endTime) return true;
DateTime currentTime = DateTime.Now;
DateTime StartTime = DateTime.ParseExact(startTime, "HH:mm", null);
DateTime EndTime = DateTime.ParseExact(endTime, "HH:mm", null);
if (StartTime <= EndTime)
{ // 单日时间段
return currentTime >= StartTime && currentTime <= EndTime;
}
else
{ // 跨越两天的时间段
return currentTime >= StartTime || currentTime <= EndTime;
}
}
}
}