diff --git a/Dotnet/Program.cs b/Dotnet/Program.cs index 036b3f6a..a4b20137 100644 --- a/Dotnet/Program.cs +++ b/Dotnet/Program.cs @@ -8,6 +8,7 @@ using NLog; using NLog.Targets; using System; using System.IO; +using System.Threading; using System.Windows.Forms; namespace VRCX @@ -104,10 +105,37 @@ namespace VRCX { Run(); } + #region Handle CEF Explosion + catch (FileNotFoundException e) + { + logger.Error(e, "Handled Exception, Missing file found in Handle Cef Explosion."); + + var result = MessageBox.Show("VRCX Has encountered an error with the CefSharp backend, \nthis is typically caused by missing files or dependencies\nWould you like to try an autofix and automatically install vc_redist?.", "VRCX CefSharp not found.", MessageBoxButtons.YesNo, MessageBoxIcon.Error); + + switch (result) + { + case DialogResult.Yes: + logger.Fatal("Handled Exception, User selected auto install of vc_redist."); + Update.DownloadInstallRedist(); + MessageBox.Show( + "vc_redist has finished installing, if the issue continues upon next restart, please reinstall reinstall VRCX From GitHub,\nVRCX Will now restart.", "vc_redist installation complete", MessageBoxButtons.OK); + Thread.Sleep(5000); + AppApi.Instance.RestartApplication(); + break; + + case DialogResult.No: + logger.Fatal("Handled Exception, User choose manual."); + MessageBox.Show("VRCX will now close, try reinstalling VRCX from Github setup exe as a possible fix.", "VRCX CefSharp not found", MessageBoxButtons.OK, MessageBoxIcon.Error); + Thread.Sleep(5000); + Environment.Exit(0); + break; + } + } + #endregion catch (Exception e) { logger.Fatal(e, "Unhandled Exception, program dying"); - MessageBox.Show(e.ToString(), "PLEASE REPORT IN https://vrcx.app/discord", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(e.ToString(), "PLEASE REPORT IN https://vrcx.pypy.moe/discord", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(0); } } diff --git a/Dotnet/Update.cs b/Dotnet/Update.cs index 84eff256..648a30d8 100644 --- a/Dotnet/Update.cs +++ b/Dotnet/Update.cs @@ -8,6 +8,7 @@ using System; using System.IO; using System.Windows.Forms; using System.Diagnostics; +using System.Net.Http; namespace VRCX { @@ -55,5 +56,89 @@ namespace VRCX MessageBox.Show(e.ToString(), "Update failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + public static void DownloadInstallRedist() + { + try + { + var filePath = DownloadFile("https://aka.ms/vs/17/release/vc_redist.x64.exe"); + var installRedist = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = filePath, + Arguments = "/install /quiet /norestart" + } + }; + installRedist.Start(); + installRedist.WaitForExit(); + } + catch (Exception e) + { + MessageBox.Show(e.ToString(), "Update failed", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + + private static string DownloadFile(string fileUrl) + { + HttpClient client = new HttpClient(); + + try + { + HttpResponseMessage response = client.GetAsync(fileUrl).Result; + + if (response.IsSuccessStatusCode) + { + string fileName = GetFileNameFromContentDisposition(response); + + string tempPath = Path.Combine(Path.GetTempPath(), "VRCX"); + Directory.CreateDirectory(tempPath); + + string filePath = Path.Combine(tempPath, fileName); + + using (FileStream fileStream = File.Create(filePath)) + { + response.Content.CopyToAsync(fileStream).Wait(); + } + + return filePath; + } + else + { + throw new Exception($"Failed to download the file. Status code: {response.StatusCode}"); + } + } + catch (Exception ex) + { + throw new Exception($"Error downloading the file: {ex.Message}"); + } + finally + { + client.Dispose(); + } + } + + private static string GetFileNameFromContentDisposition(HttpResponseMessage response) + { + string contentDisposition = response.Content.Headers.ContentDisposition?.ToString(); + if (contentDisposition != null) + { + int startIndex = contentDisposition.IndexOf("filename=", StringComparison.OrdinalIgnoreCase); + if (startIndex >= 0) + { + startIndex += "filename=".Length; + int endIndex = contentDisposition.IndexOf(";", startIndex); + if (endIndex == -1) + { + endIndex = contentDisposition.Length; + } + + string fileName = contentDisposition.Substring(startIndex, endIndex - startIndex).Trim(' ', '"'); + return fileName; + } + } + + throw new Exception("Unable to extract file name from content-disposition header."); + } } } \ No newline at end of file