Added preemptive updater downloading again

This commit is contained in:
Travis Nickles 2020-04-11 09:46:34 -05:00
parent 92bd1bd53f
commit 7685b95db0
2 changed files with 100 additions and 17 deletions

View File

@ -190,5 +190,46 @@ namespace DS4Windows
} }
}); });
} }
public static int ElevatedCopyUpdater(string tmpUpdaterPath, bool deleteUpdatesDir=false)
{
int result = -1;
string tmpPath = Path.Combine(Path.GetTempPath(), "updatercopy.bat");
// Create temporary bat script that will later be executed
using (StreamWriter w = new StreamWriter(new FileStream(tmpPath,
FileMode.Create, FileAccess.Write)))
{
w.WriteLine("@echo off"); // Turn off echo
w.WriteLine("@echo Attempting to replace updater, please wait...");
// Copy temp downloaded file to destination
w.WriteLine($"@mov /Y \"{tmpUpdaterPath}\" {Global.exedirpath}\\DS4Updater.exe");
if (deleteUpdatesDir)
{
w.WriteLine($"@del {Global.exedirpath}\\Update Files\\DS4Windows\\DS4Updater.exe");
}
w.WriteLine("@DEL \"%~f0\""); // Attempt to delete myself without opening a time paradox.
w.Close();
}
// Execute temp batch script with admin privileges
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = tmpPath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = true;
try
{
// Launch process, wait and then save exit code
using (Process temp = Process.Start(startInfo))
{
temp.WaitForExit();
result = temp.ExitCode;
}
}
catch { }
return result;
}
} }
} }

View File

@ -55,6 +55,9 @@ namespace DS4WinWPF.DS4Forms
public ProfileList ProfileListHolder { get => profileListHolder; } public ProfileList ProfileListHolder { get => profileListHolder; }
private const string UPDATER_VERSION = "1.4.1";
private string updaterExe = Environment.Is64BitProcess ? "DS4Updater.exe" : "DS4Updater_x86.exe";
public MainWindow(ArgumentParser parser) public MainWindow(ArgumentParser parser)
{ {
InitializeComponent(); InitializeComponent();
@ -184,23 +187,47 @@ Properties.Resources.DS4Update, MessageBoxButton.YesNo, MessageBoxImage.Question
if (result == MessageBoxResult.Yes) if (result == MessageBoxResult.Yes)
{ {
bool launch = false; bool launch = true;
using (Process p = new Process()) if (!File.Exists(Global.exedirpath + "\\DS4Updater.exe") ||
(File.Exists(Global.exedirpath + "\\DS4Updater.exe")
&& (FileVersionInfo.GetVersionInfo(Global.exedirpath + "\\DS4Updater.exe").FileVersion.CompareTo(UPDATER_VERSION) != 0)))
{ {
p.StartInfo.FileName = System.IO.Path.Combine(Global.exedirpath, "DS4Updater.exe"); Uri url2 = new Uri($"https://github.com/Ryochan7/DS4Updater/releases/download/v{UPDATER_VERSION}/{updaterExe}");
bool isAdmin = Global.IsAdministrator(); string filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "DS4Updater.exe");
List<string> argList = new List<string>(); using (var downloadStream = new FileStream(filename, FileMode.Create))
argList.Add("-autolaunch");
if (!isAdmin)
{ {
argList.Add("-user"); Task<System.Net.Http.HttpResponseMessage> temp =
App.requestClient.GetAsync(url2.ToString(), downloadStream);
temp.Wait();
if (!temp.Result.IsSuccessStatusCode) launch = false;
} }
p.StartInfo.Arguments = string.Join(" ", argList);
if (Global.AdminNeeded())
p.StartInfo.Verb = "runas";
try { launch = p.Start(); } if (launch)
catch (InvalidOperationException) { } {
int copyStatus = Util.ElevatedCopyUpdater(filename);
if (copyStatus != 0) launch = false;
}
}
if (launch)
{
using (Process p = new Process())
{
p.StartInfo.FileName = System.IO.Path.Combine(Global.exedirpath, "DS4Updater.exe");
bool isAdmin = Global.IsAdministrator();
List<string> argList = new List<string>();
argList.Add("-autolaunch");
if (!isAdmin)
{
argList.Add("-user");
}
p.StartInfo.Arguments = string.Join(" ", argList);
if (Global.AdminNeeded())
p.StartInfo.Verb = "runas";
try { launch = p.Start(); }
catch (InvalidOperationException) { }
}
} }
if (launch) if (launch)
@ -211,6 +238,14 @@ Properties.Resources.DS4Update, MessageBoxButton.YesNo, MessageBoxImage.Question
Close(); Close();
})); }));
} }
else
{
Dispatcher.Invoke(() =>
{
MessageBox.Show(Properties.Resources.PleaseDownloadUpdater);
});
//Process.Start($"https://github.com/Ryochan7/DS4Updater/releases/download/v{UPDATER_VERSION}/{updaterExe}");
}
} }
else else
{ {
@ -436,10 +471,17 @@ Suspend support not enabled.", true);
processes = Process.GetProcessesByName("DS4Updater"); processes = Process.GetProcessesByName("DS4Updater");
} }
File.Delete(Global.exedirpath + "\\DS4Updater.exe"); if (!Global.AdminNeeded())
File.Move(Global.exedirpath + "\\Update Files\\DS4Windows\\DS4Updater.exe", {
Global.exedirpath + "\\DS4Updater.exe"); File.Delete(Global.exedirpath + "\\DS4Updater.exe");
Directory.Delete(Global.exedirpath + "\\Update Files", true); File.Move(Global.exedirpath + "\\Update Files\\DS4Windows\\DS4Updater.exe",
Global.exedirpath + "\\DS4Updater.exe");
Directory.Delete(Global.exedirpath + "\\Update Files", true);
}
else
{
Util.ElevatedCopyUpdater(Global.exedirpath + "\\Update Files\\DS4Windows\\DS4Updater.exe", true);
}
} }
} }