-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Diagnostics; | ||
using System.IO; | ||
using log4net; | ||
using StoPasswordBook; | ||
using StoPasswordBook.Generic; | ||
|
||
namespace STOTool.Feature | ||
{ | ||
public class AutoUpdate | ||
{ | ||
private static readonly string Author = "Xkaguya"; | ||
private static readonly string Project = "StoPasswordBook"; | ||
private static readonly string ExeName = "StoPasswordBook.exe"; | ||
private static readonly string CurrentExePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ExeName); | ||
private static readonly string NewExePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StoPasswordBook-New.exe"); | ||
private static readonly CancellationTokenSource CancellationTokenSource = new CancellationTokenSource(); | ||
private static readonly ILog Log = LogManager.GetLogger(typeof(AutoUpdate)); | ||
|
||
public static void StartAutoUpdateTask() | ||
{ | ||
Task.Run(async () => await AutoUpdateTask(CancellationTokenSource.Token)); | ||
} | ||
|
||
private static async Task AutoUpdateTask(CancellationToken token) | ||
{ | ||
while (!token.IsCancellationRequested) | ||
{ | ||
CheckAndUpdate(); | ||
await Task.Delay(TimeSpan.FromHours(1), token); | ||
} | ||
} | ||
|
||
public static void CheckAndUpdate() | ||
{ | ||
if (!GlobalVariables.AutoUpdate) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
string commonUpdaterPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CommonUpdater.exe"); | ||
|
||
if (!File.Exists(commonUpdaterPath)) | ||
{ | ||
Log.Info("There's no CommonUpdater in the folder. Failed to update."); | ||
return; | ||
} | ||
|
||
string arguments = $"{Project} {ExeName} {Author} {MainWindow.Version} \"{CurrentExePath}\" \"{NewExePath}\""; | ||
|
||
var startInfo = new ProcessStartInfo | ||
{ | ||
FileName = commonUpdaterPath, | ||
Arguments = arguments, | ||
UseShellExecute = false, | ||
RedirectStandardOutput = false, | ||
RedirectStandardError = true | ||
}; | ||
|
||
Log.Debug($"Starting CommonUpdater with arguments: {arguments}"); | ||
using var process = Process.Start(startInfo); | ||
if (process == null) | ||
{ | ||
Log.Error("Failed to start CommonUpdater: Process.Start returned null."); | ||
return; | ||
} | ||
|
||
string error = process.StandardError.ReadToEnd(); | ||
process.WaitForExit(); | ||
|
||
if (!string.IsNullOrEmpty(error)) | ||
{ | ||
Log.Error($"CommonUpdater error: {error}"); | ||
} | ||
|
||
if (process.ExitCode != 0) | ||
{ | ||
Log.Error($"CommonUpdater exited with code {process.ExitCode}"); | ||
} | ||
else | ||
{ | ||
Log.Debug("CommonUpdater started successfully."); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Error($"Failed to start CommonUpdater: {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters