/// <summary> /// 获取远程文件列表 /// </summary> /// <param name="fileListString"></param> /// <returns></returns> public static List <FileInfo> GetRemoteFileList(AHelper.AppendString appendString) { try { return(GetRemoteFileList(GetRemoteFileListString(appendString))); } catch (Exception) { throw; } }
/// <summary> /// 获取远程文件列表文本 /// </summary> /// <returns></returns> public static string GetRemoteFileListString(AHelper.AppendString appendString) { string fileListBody = ""; HttpWebRequest request = WebRequest.Create(RemoteFileListUrl) as HttpWebRequest; try { using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream())) fileListBody = reader.ReadToEnd(); } catch (Exception e) { appendString("[错误] 获取文件列表失败,请检查网络状况。", AInstruction.ReportType.ERROR); throw; } return(fileListBody); }
/// <summary> /// 获取远程版本号 /// </summary> /// <returns></returns> public static string GetRemoteVersionNumber(AHelper.AppendString appendString) { HttpWebRequest request = WebRequest.Create(RemoteVersionUrl) as HttpWebRequest; try { using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream())) { string versionInfoBody = reader.ReadToEnd(); return(versionInfoBody.Substring(versionInfoBody.IndexOf("=") + 1)); } } catch (Exception) { // Do nothing, skip update checking. appendString("检查更新出错,请检查网络连接状况,\n可使用@restart来重启Alterful检查更新,或使用@update来检查并执行更新。", AInstruction.ReportType.ERROR); return("0.0.0.0"); } }
public static List <FileInfo> GetFilesDiffer(AHelper.AppendString appendString) { List <FileInfo> differFiles = new List <FileInfo>(); try { foreach (FileInfo remoteFile in GetRemoteFileList(appendString)) { string localFileMd5 = AHelper.GetFileMd5(remoteFile.FileRoute + remoteFile.FileName).ToLower(); if (!localFileMd5.Equals(remoteFile.FileMd5.ToLower())) { differFiles.Add(remoteFile); } } } catch (Exception) { throw; } return(differFiles); }
/// <summary> /// 更新并重启Alterful,若无更新则不操作 /// </summary> /// <exception cref="WebException"></exception> public static void UpdateAndRestart(object msgHandler) { // 已是最新或内测版本 // if (AVersion.GetVersionNumberDiffer() >= 0) return; float count = 0.0f; bool updateSelf = false; AHelper.AppendString handler = msgHandler as AHelper.AppendString; using (var client = new WebClient()) { handler("[更新] 正在获取文件列表...", AInstruction.ReportType.OK); //handler("[update] Getting file list...", AInstruction.ReportType.NONE); List <FileInfo> differFiles = new List <FileInfo>(); try { differFiles = GetFilesDiffer(handler); } catch (Exception) { return; } if (0 == differFiles.Count) { handler("当前版本已经是最新版。", AInstruction.ReportType.OK); //handler("The current version is already the latest version.", AInstruction.ReportType.OK); return; } Console.WriteLine((100 * count / differFiles.Count) + "%"); try { foreach (FileInfo differFile in differFiles) { if ("Alterful.exe".ToLower() == differFile.FileName.Trim().ToLower()) { updateSelf = true; client.DownloadFile(AHelper.RemoteUrl + differFile.FileName, differFile.FileRoute + "Alterful.exe.temp"); } else { Directory.CreateDirectory(differFile.FileRoute); client.DownloadFile(AHelper.RemoteUrl + differFile.FileName, differFile.FileRoute + differFile.FileName); } count++; handler("[更新] 正在更新 " + differFile.FileName + "... (" + count + "/" + differFiles.Count + ", " + (100 * count / differFiles.Count) + "%)", AInstruction.ReportType.NONE); //handler("[update] Updating " + differFile.FileName + "... (" + count + "/" + differFiles.Count + ", " + (100 * count / differFiles.Count) + "%)", AInstruction.ReportType.NONE); } handler("更新已完成。", AInstruction.ReportType.OK); //handler("Update finished.", AInstruction.ReportType.OK); } catch (Exception e) { handler("[错误] " + e.Message, AInstruction.ReportType.ERROR); } } if (updateSelf) { // Restart. handler("此次更新需要重新启动 Alterful 才能完全生效。", AInstruction.ReportType.WARNING); handler("[重启] Alterful 将在 10 秒后自动重新启动。", AInstruction.ReportType.NONE); //handler("The update just now needs to be restarted to take full effect.", AInstruction.ReportType.WARNING); //handler("[restart] Alterful will auto restart in 10 seconds.", AInstruction.ReportType.NONE); Thread.Sleep(10000); System.IO.File.Delete(@".\restart.bat"); using (StreamWriter writer = new StreamWriter(@".\restart.bat", true)) { // Write out restart.bat writer.WriteLine(@"@echo off & cls"); writer.WriteLine(@"taskkill /f /im alterful.exe"); writer.WriteLine(@"ping 127.0.01 -n 1"); writer.WriteLine(@"copy /y " + AHelper.BASE_PATH + @"\Alterful.exe.temp " + AHelper.BASE_PATH + @"\Alterful.exe"); writer.WriteLine(@"del /f /s /q " + AHelper.BASE_PATH + @"\Alterful.exe.temp"); writer.WriteLine(@"start " + AHelper.BASE_PATH + @"\Alterful.exe"); // Execute restart.bat Process newProcess = new Process() { StartInfo = new ProcessStartInfo() { WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = true, Arguments = "", FileName = @".\restart.bat", } }; newProcess.StartInfo.Verb = "runas"; newProcess.Start(); } } else { handler("你可以执行 @restart 来看看更新了些什么内容。", AInstruction.ReportType.NONE); //handler("Execute @restart to list what's new.", AInstruction.ReportType.NONE); } }
/// <summary> /// 获取版本号差异。版本偏低返回负数,最新版本返回0,内测版本返回正数 /// </summary> /// <param name="local"></param> /// <param name="remote"></param> /// <returns></returns> public static int GetVersionNumberDiffer(AHelper.AppendString appendString) => GetVersionNumberDiffer(GetLocalVersionNumber(), GetRemoteVersionNumber(appendString));
/// <summary> /// 执行指令框中的指令 /// </summary> private void ExecuteTextBoxInstrution() { UpdateMaxWidth(InstructionTextBox.Text); // Execute instruction. string retnInfo = ExecuteInstruction(InstructionTextBox.Text); InitializeGUI(ATheme.GetThemeConfig()); if (AInstruction.ADD_CONST_INSTRUCTION == retnInfo) { constInstructionInputMode = true; TestRichTextbox.IsReadOnly = false; InstructionTextBox.IsEnabled = false; //AppendRTBLine(TestRichTextbox, "确认编辑: Alt + S / 取消编辑: Alt + Esc", themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); AppendRTBLine(TestRichTextbox, "Confirm: Alt + S / Cancel: Alt + Esc", themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); TestRichTextbox.BorderThickness = new Thickness(1); // Content Start TestRichTextbox.CaretPosition = TestRichTextbox.Document.ContentEnd; constInstructionContentRange.ContentStart = TestRichTextbox.CaretPosition.Paragraph.ContentStart; TestRichTextbox.CaretPosition.Paragraph.IsEnabled = false; // If ci already exist string constInstruction = AInstruction_Const.GetConstInstructionFromMacroInstruction(InstructionTextBox.Text); if (AConstInstruction.Exist(constInstruction)) { ConstInstruction ci = new ConstInstruction(); if (AConstInstruction.GetConstInstructionFrame(constInstruction, ref ci)) { OldConstInstructionFileName = AConstInstruction.GetFileNameFromConstInstruction(ci); foreach (string insLine in ci.instructionLines) { AppendRTBLine(TestRichTextbox, insLine, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); UpdateMaxWidth(insLine); } } } // Set CaretPosition. TestRichTextbox.CaretPosition = TestRichTextbox.Document.ContentEnd; //UpdateMaxWidth("确认编辑: Alt + S / 取消编辑: Alt + Esc"); UpdateMaxWidth("Confirm: Alt + S / Cancel: Alt + Esc"); Resize(true, constInstructionInputWidthBias); TestRichTextbox.Focus(); showOutput = true; return; } else if (AInstruction.UPDATE_INSTRUCTION == retnInfo) { UpdateMaxWidth(InstructionTextBox.Text); AppendRTBLine(TestRichTextbox, InstructionTextBox.Text, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); showOutput = true; InstructionTextBox.Text = ""; Resize(); AHelper.AppendString appendString = delegate(string content, AInstruction.ReportType type) { // InstructionTextBox 被主线程占用,利用 Dispatcher 进行操作 TestRichTextbox.Dispatcher.BeginInvoke((Action)(() => { switch (type) { case AInstruction.ReportType.NONE: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); break; case AInstruction.ReportType.WARNING: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); break; case AInstruction.ReportType.OK: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputOk, themeConfig.BackgroundOutputOk); break; case AInstruction.ReportType.ERROR: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputError, themeConfig.BackgroundOutputError); break; } InstructionTextBox.Text = ""; UpdateMaxWidth(content); Resize(); })); }; new Thread(AUpdate.UpdateAndRestart).Start(appendString); return; } // Append instruction line. AppendRTBLine(TestRichTextbox, InstructionTextBox.Text, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); // Print report. foreach (var reportInfo in AInstruction.ReportInfo) { AppendRTBLine(TestRichTextbox, reportInfo, themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); UpdateMaxWidth(reportInfo); // If have reportInfo, then show outputBox. showOutput = true; } // Print return information. SolidColorBrush bgcolor; SolidColorBrush fgcolor; switch (AInstruction.reportType) { case AInstruction.ReportType.OK: bgcolor = themeConfig.BackgroundOutputOk; fgcolor = themeConfig.ForegroundOutputOk; if (AInstruction.ReportInfo.Count == 0) { showOutput = false; } break; case AInstruction.ReportType.WARNING: bgcolor = themeConfig.BackgroundOutputWarning; fgcolor = themeConfig.ForegroundOutputWarning; showOutput = true; break; case AInstruction.ReportType.ERROR: bgcolor = themeConfig.BackgroundOutputError; fgcolor = themeConfig.ForegroundOutputError; showOutput = true; break; default: bgcolor = themeConfig.BackgroundOutputWarning; fgcolor = themeConfig.ForegroundOutputWarning; break; } if (retnInfo != "") { AppendRTBLine(TestRichTextbox, retnInfo.Trim(), fgcolor, bgcolor); } if (AInstruction.GetType(InstructionTextBox.Text) == InstructionType.CMD) { InstructionTextBox.Text = "> "; showOutput = true; } else { InstructionTextBox.Text = ""; if (AInstruction.ReportType.OK == AInstruction.reportType && 0 == AInstruction.ReportInfo.Count) { Visibility = Visibility.Hidden; } } InstructionTextBox.SelectionStart = InstructionTextBox.Text.Length; // Update width and resize. UpdateMaxWidth(retnInfo); Resize(); }
public MainWindow() { /* * TODO: Automatically initialize default startup items for users who first use Alterful. * foreach (AHelper.SoftwareInstalled software in AHelper.GetInstalledSoftwareList()) * { * Console.WriteLine(software.DisplayName + " (" + software.InstallLocation + ")"); * } */ if (!GotMutex) { AHelper.ShowANotification("Alterful 已在运行中", "Alterful is already running"); Environment.Exit(1);//退出程序 } // Global Initialization. InitializeComponent(); AHelper.Initialize(delegate(string content, AInstruction.ReportType type) { // InstructionTextBox 被主线程占用,利用 Dispatcher 进行操作 TestRichTextbox.Dispatcher.BeginInvoke((Action)(() => { switch (type) { case AInstruction.ReportType.NONE: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutput, themeConfig.BackgroundOutput); break; case AInstruction.ReportType.WARNING: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); break; case AInstruction.ReportType.OK: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputOk, themeConfig.BackgroundOutputOk); break; case AInstruction.ReportType.ERROR: AppendRTBLine(TestRichTextbox, content, themeConfig.ForegroundOutputError, themeConfig.BackgroundOutputError); break; } InstructionTextBox.Text = ""; UpdateMaxWidth(content); Resize(); })); }); InitializeGUI(ATheme.GetThemeConfig(), !AHelper.IS_FIRST_START && !AHelper.HAS_ANEW); InitializePipe(); CheckCommandLine(); appendStringDelegate = AppendStringFunction; Thread thread = new Thread(new ThreadStart(CheckUpdate)); thread.Start(); // If is first startup, give user some tips. if (AHelper.IS_FIRST_START) { string outInfo = "欢迎来到 Alterful,以下是初次上手帮助!\n- 在[指令框输入框]按 Alt 来隐藏/显示回显框;\n- 使用组合键 [Alt+A] 来唤醒/隐藏指令框;\n- 右键任意文件/文件夹来将其添加为启动项;\n- 在指令输入框键入启动项名并回车来启动它;\n- 需要时键入波浪号(~)并回车来结束 Alterful;\n- 前往 help.alterful.com 了解详细功能和使用技巧。"; TestRichTextbox.Dispatcher.BeginInvoke((Action)(() => { UpdateMaxWidth(outInfo); AppendRTBLine(TestRichTextbox, outInfo, themeConfig.ForegroundOutputWarning, themeConfig.BackgroundOutputWarning); Visibility = Visibility.Visible; showOutput = true; Resize(); })); } // Instruction Test. // MainTest(); // Close(); }