From bd3587314653f09afb7e36adcb1f914a0530031e Mon Sep 17 00:00:00 2001 From: Miepee <38186597+Miepee@users.noreply.github.com> Date: Thu, 29 Jul 2021 22:18:36 +0200 Subject: [PATCH] Use an enum for CheckIZipAM2R11 return codes --- .../AM2RLauncher/MainForm/MainForm.Events.cs | 8 ++--- .../AM2RLauncher/MainForm/MainForm.Methods.cs | 35 ++++++++++--------- .../AM2RLauncher/MainForm/MainForm.UI.cs | 11 ++++++ 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs index fc46e49..6dabb09 100644 --- a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs +++ b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Events.cs @@ -282,11 +282,11 @@ namespace AM2RLauncher { if (Directory.Exists(fileFinder.FileName)) return; // this can happen on linux, and maybe windows as well - - if(!CheckIfZipIsAM2R11(fileFinder.FileName)) + IsZipAM2R11ReturnCodes errorCode = CheckIfZipIsAM2R11(fileFinder.FileName); + if (errorCode != IsZipAM2R11ReturnCodes.Successful) { - log.Error("User tried to input invalid AM2R_11.zip file. Cancelling import."); - MessageBox.Show(Language.Text.ZipIsNotAM2R11, Language.Text.ErrorWindowTitle, MessageBoxType.Error); + log.Error("User tried to input invalid AM2R_11.zip file (" + errorCode + "). Cancelling import."); + MessageBox.Show(Language.Text.ZipIsNotAM2R11 + "\n\nError Code: " + errorCode, Language.Text.ErrorWindowTitle, MessageBoxType.Error); return; } diff --git a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs index 911bad2..ad6aadd 100644 --- a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs +++ b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.Methods.cs @@ -1107,13 +1107,13 @@ namespace AM2RLauncher /// Checks if a Zip file is a valid AM2R_1.1 zip. /// /// Full Path to the Zip file to check. - /// if the Zip file is a valid AM2R_1.1 zip, if not. - private bool CheckIfZipIsAM2R11(string zipPath) + /// if the zip file is a valid AM2R_1.1 zip

+ /// if the zip does not contain AM2R.exe

+ /// if the zip does not contain a valid D3D dll

+ /// if the zip does not contain a valid data.win file
+ private IsZipAM2R11ReturnCodes CheckIfZipIsAM2R11(string zipPath) { - bool doesExeExist = false; - bool isD3dDllCorrect = false; const string d3dHash = "86e39e9161c3d930d93822f1563c280d"; - bool isDataWinCorrect = false; const string dataWinHash = "f2b84fe5ba64cb64e284be1066ca08ee"; string tmpPath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(zipPath); @@ -1122,34 +1122,35 @@ namespace AM2RLauncher ZipFile.ExtractToDirectory(zipPath, tmpPath); // check if exe exists - if (File.Exists(tmpPath + "/AM2R.exe")) - doesExeExist = true; + if (!File.Exists(tmpPath + "/AM2R.exe")) + return IsZipAM2R11ReturnCodes.MissingAM2RExe; // check if d3d.dll exists, then check the hash of d3d.dll - if (File.Exists(tmpPath + "/D3DX9_43.dll") && CalculateMD5(tmpPath + "/D3DX9_43.dll") == d3dHash) - isD3dDllCorrect = true; + if (CalculateMD5(tmpPath + "/D3DX9_43.dll") != d3dHash) + return IsZipAM2R11ReturnCodes.MissingOrInvalidD3DX9_43Dll; // check if data.win exists and check its hash - if (File.Exists(tmpPath + "/data.win") && CalculateMD5(tmpPath + "/data.win") == dataWinHash) - isDataWinCorrect = true; + if (CalculateMD5(tmpPath + "/data.win") != dataWinHash) + return IsZipAM2R11ReturnCodes.MissingOrInvalidDataWin; // clean up Directory.Delete(tmpPath, true); - // if everything is true, return true - if (doesExeExist && isD3dDllCorrect && isDataWinCorrect) - return true; - - return false; + // if we didn't exist before, everything is fine + return IsZipAM2R11ReturnCodes.Successful; } /// /// Calculates an MD5 hash from a given file. /// /// Full Path to the file whose MD5 hash is supposed to be calculated. - /// The MD5 hash as a . + /// The MD5 hash as a , empty string if file does not exist. private string CalculateMD5(string filename) { + // Check if File exists first + if (!File.Exists(filename)) + return ""; + using (var stream = File.OpenRead(filename)) { using (var md5 = MD5.Create()) diff --git a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.UI.cs b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.UI.cs index 24d0e57..3d0563f 100644 --- a/AM2RLauncher/AM2RLauncher/MainForm/MainForm.UI.cs +++ b/AM2RLauncher/AM2RLauncher/MainForm/MainForm.UI.cs @@ -48,6 +48,17 @@ namespace AM2RLauncher Creating } + /// + /// An enum, that has possible return codes for + /// + enum IsZipAM2R11ReturnCodes + { + Successful, + MissingAM2RExe, + MissingOrInvalidD3DX9_43Dll, + MissingOrInvalidDataWin + } + // Splashes /// /// A - of custom splashes.