fix auto-updates and crash handler restart

This commit is contained in:
zznty
2023-02-17 15:47:44 +07:00
parent f349366b58
commit 32d318be5e
2 changed files with 30 additions and 17 deletions

View File

@@ -32,9 +32,25 @@ internal class UnhandledExceptionHandler
{ {
Console.WriteLine("Restarting in 5 seconds."); Console.WriteLine("Restarting in 5 seconds.");
Thread.Sleep(5000); Thread.Sleep(5000);
var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe"); var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
Process.Start(exe, $"-waitForPid {Environment.ProcessId} {_config}"); var args = Environment.GetCommandLineArgs();
for (var i = 0; i < args.Length; i++)
{
if (args[i].Contains(' '))
args[i] = $"\"{args[i]}\"";
if (!args[i].Contains("--tempAutostart", StringComparison.InvariantCultureIgnoreCase) &&
!args[i].Contains("--waitForPid", StringComparison.InvariantCultureIgnoreCase))
continue;
args[i] = string.Empty;
args[++i] = string.Empty;
}
Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}");
} }
else else
{ {

View File

@@ -58,21 +58,18 @@ namespace Torch.Managers
if (!File.Exists(source)) if (!File.Exists(source))
return; return;
try
{
File.Delete(source);
}
catch (IOException)
{
var tempFilePath = Path.Combine(path, file + ".old"); var tempFilePath = Path.Combine(path, file + ".old");
if (File.Exists(tempFilePath)) if (File.Exists(tempFilePath))
File.Delete(tempFilePath); File.Delete(tempFilePath);
var errorCode = Rename(source, tempFilePath); File.Move(source, tempFilePath);
if (Marshal.GetExceptionForHR(errorCode) is { } exception) }
throw exception; }
}
[LibraryImport("msvcrt", SetLastError = true, EntryPoint = "rename")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static partial int Rename(
[MarshalAs(UnmanagedType.LPWStr)]
string oldpath,
[MarshalAs(UnmanagedType.LPWStr)]
string newpath);
} }
} }