Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
more error handling
Browse files Browse the repository at this point in the history
change scope of variables
formatting
  • Loading branch information
TBirdSoars committed Dec 19, 2021
1 parent dfa1289 commit 6304586
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ internal class Program
private static readonly Uri baseURL = new Uri(@"https://buildbot.orphis.net");
private static readonly string urlParam = @"/pcsx2/index.php";
private static readonly string zipFile = @"pcsx2.7z";
private static string currentDir = "";
private static string pcsx2FullDir = "";
private static string zipFullDir = "";

private static async Task Main()
{
string currentDir = string.Empty;
string pcsx2FullDir = string.Empty;
string zipFullDir = string.Empty;

// Begin
Console.WriteLine("Simple PCSX2 Updater - By TBirdSoars");
Console.WriteLine("```");
Expand Down Expand Up @@ -55,7 +56,7 @@ private static async Task Main()
}
catch (Exception ex)
{
Console.WriteLine("Exception Finding PCSX2: " + ex.Message);
Console.WriteLine($"Exception Finding PCSX2: {ex.Message}");
}

// Set full path of zip file
Expand All @@ -80,10 +81,10 @@ private static async Task Main()
Console.WriteLine("Building download url... ");
string build_Path_and_Query = buildTable.Rows[0]["build"].ToString().Replace("amp;", "");
Uri downloadURL = new Uri(baseURL, build_Path_and_Query);
if (HttpUtility.ParseQueryString(downloadURL.Query).Get("rev") == null
|| HttpUtility.ParseQueryString(downloadURL.Query).Get("platform") == null)
if (HttpUtility.ParseQueryString(downloadURL.Query).Get("rev") == null ||
HttpUtility.ParseQueryString(downloadURL.Query).Get("platform") == null)
{
Console.WriteLine("Failed to get download URL data, exiting...");
Console.WriteLine("Failed to get download URL, exiting...");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return;
Expand All @@ -99,11 +100,25 @@ private static async Task Main()
// Get download from URL
Console.WriteLine($"Downloading version {folderName}... ");
await DownloadArchive(downloadURL, zipFullDir);
if (!File.Exists(zipFullDir))
{
Console.WriteLine("Failed to download, exiting...");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return;
}


// Extract 7zip archive
Console.WriteLine("Extracting PCSX2... ");
ExtractArchive(zipFullDir);
ExtractArchive(zipFullDir, currentDir);
if (!Directory.Exists(extractFolder))
{
Console.WriteLine("Failed to extract archive, exiting...");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return;
}


// Move files into pcsx2.exe directory
Expand Down Expand Up @@ -134,7 +149,7 @@ private static async Task<DataTable> GetBuildTable()
}
catch (Exception ex)
{
Console.WriteLine("GetBuildTable Exception: " + ex.Message);
Console.WriteLine($"GetBuildTable Exception: {ex.Message}");
}

// Get table with all recent releases
Expand Down Expand Up @@ -196,7 +211,7 @@ private static async Task<DataTable> GetBuildTable()
}
else
{
Console.WriteLine($"{baseURL} returned null");
Console.WriteLine($"GetBuildTable Error: '{baseURL}' returned null.");
}

return output;
Expand Down Expand Up @@ -230,12 +245,12 @@ private static async Task DownloadArchive(Uri uri, string dest)
}
catch (Exception ex)
{
Console.WriteLine("DownloadArchive Exception: " + ex.Message);
Console.WriteLine($"DownloadArchive Exception: {ex.Message}");
}
}

// Extracts 7z archive, then deletes archive
private static void ExtractArchive(string src)
private static void ExtractArchive(string src, string dest)
{
try
{
Expand All @@ -244,20 +259,20 @@ private static void ExtractArchive(string src)
using (SevenZipArchive sevenZipArchive = SevenZipArchive.Open(src))
using (IReader reader = sevenZipArchive.ExtractAllEntries())
{
reader.WriteAllToDirectory(currentDir, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
reader.WriteAllToDirectory(dest, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
}

// Cleanup
File.Delete(src);
}
else
{
Console.WriteLine($"Download file '{zipFile}' not found in current directory.");
Console.WriteLine($"ExtractArchive Error: Download file '{src}' not found in current directory.");
}
}
catch (Exception ex)
{
Console.WriteLine("ExtractArchive Exception: " + ex.Message);
Console.WriteLine($"ExtractArchive Exception: {ex.Message}");
}
}

Expand Down Expand Up @@ -324,12 +339,12 @@ private static void MoveAll(string src, string dest)
}
else
{
Console.WriteLine($"MoveAll Error: {src} or {dest} does not exist!");
Console.WriteLine($"MoveAll Error: '{src}' or '{dest}' does not exist.");
}
}
catch (Exception ex)
{
Console.WriteLine("MoveAll Exception: " + ex.Message);
Console.WriteLine($"MoveAll Exception: {ex.Message}");
}
}
}
Expand Down

0 comments on commit 6304586

Please sign in to comment.