Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
XKaguya authored Sep 23, 2024
1 parent 2e8cbe9 commit ead0ce4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
92 changes: 92 additions & 0 deletions StoPasswordBook/StoPasswordBook/Extern/AutoUpdate.cs
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}");
}
}
}
}
3 changes: 3 additions & 0 deletions StoPasswordBook/StoPasswordBook/Generic/GlobalVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace StoPasswordBook.Generic
{
public class GlobalVariables
{
[Description("Set to true to allow program self update. \nDefault value: true")]
public static bool AutoUpdate { get; set; } = true;

[Description("Launcher path. Do not touch this unless you're reinstalled your STO. \nDefault value: null")]
public static string LauncherPath { get; set; } = "null";

Expand Down
5 changes: 5 additions & 0 deletions StoPasswordBook/StoPasswordBook/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using log4net;
using log4net.Config;
using StoPasswordBook.Generic;
using STOTool.Feature;
using Wpf.Ui.Controls;

namespace StoPasswordBook;
Expand All @@ -16,6 +17,8 @@ namespace StoPasswordBook;
/// </summary>
public partial class MainWindow : FluentWindow
{
public static readonly string Version = "1.0.0";

private static readonly ILog Log = LogManager.GetLogger(typeof(MainWindow));

private ObservableDictionary<string, string> Accounts { get; set; }
Expand Down Expand Up @@ -126,6 +129,8 @@ private async void SubmitButton_Click(object sender, RoutedEventArgs e)

private async Task InitMethod()
{
AutoUpdate.StartAutoUpdateTask();

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "StoPasswordBook.log4net.config";

Expand Down

0 comments on commit ead0ce4

Please sign in to comment.