Use an enum for CheckIZipAM2R11 return codes

pull/21/head
Miepee 5 years ago
parent 61ccb4f1a1
commit bd35873146

@ -282,11 +282,11 @@ namespace AM2RLauncher
{ {
if (Directory.Exists(fileFinder.FileName)) return; // this can happen on linux, and maybe windows as well if (Directory.Exists(fileFinder.FileName)) return; // this can happen on linux, and maybe windows as well
IsZipAM2R11ReturnCodes errorCode = CheckIfZipIsAM2R11(fileFinder.FileName);
if(!CheckIfZipIsAM2R11(fileFinder.FileName)) if (errorCode != IsZipAM2R11ReturnCodes.Successful)
{ {
log.Error("User tried to input invalid AM2R_11.zip file. Cancelling import."); log.Error("User tried to input invalid AM2R_11.zip file (" + errorCode + "). Cancelling import.");
MessageBox.Show(Language.Text.ZipIsNotAM2R11, Language.Text.ErrorWindowTitle, MessageBoxType.Error); MessageBox.Show(Language.Text.ZipIsNotAM2R11 + "\n\nError Code: " + errorCode, Language.Text.ErrorWindowTitle, MessageBoxType.Error);
return; return;
} }

@ -1107,13 +1107,13 @@ namespace AM2RLauncher
/// Checks if a Zip file is a valid AM2R_1.1 zip. /// Checks if a Zip file is a valid AM2R_1.1 zip.
/// </summary> /// </summary>
/// <param name="zipPath">Full Path to the Zip file to check.</param> /// <param name="zipPath">Full Path to the Zip file to check.</param>
/// <returns><see langword="true"/> if the Zip file is a valid AM2R_1.1 zip, <see langword="false"/> if not. </returns> /// <returns><see langword="0"/> if the zip file is a valid AM2R_1.1 zip <br></br>
private bool CheckIfZipIsAM2R11(string zipPath) /// <see langword="-1"/> if the zip does not contain AM2R.exe <br></br>
/// <see langword="-2"/> if the zip does not contain a valid D3D dll <br></br>
/// <see langword="-3"/> if the zip does not contain a valid data.win file</returns>
private IsZipAM2R11ReturnCodes CheckIfZipIsAM2R11(string zipPath)
{ {
bool doesExeExist = false;
bool isD3dDllCorrect = false;
const string d3dHash = "86e39e9161c3d930d93822f1563c280d"; const string d3dHash = "86e39e9161c3d930d93822f1563c280d";
bool isDataWinCorrect = false;
const string dataWinHash = "f2b84fe5ba64cb64e284be1066ca08ee"; const string dataWinHash = "f2b84fe5ba64cb64e284be1066ca08ee";
string tmpPath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(zipPath); string tmpPath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(zipPath);
@ -1122,34 +1122,35 @@ namespace AM2RLauncher
ZipFile.ExtractToDirectory(zipPath, tmpPath); ZipFile.ExtractToDirectory(zipPath, tmpPath);
// check if exe exists // check if exe exists
if (File.Exists(tmpPath + "/AM2R.exe")) if (!File.Exists(tmpPath + "/AM2R.exe"))
doesExeExist = true; return IsZipAM2R11ReturnCodes.MissingAM2RExe;
// check if d3d.dll exists, then check the hash of d3d.dll // 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) if (CalculateMD5(tmpPath + "/D3DX9_43.dll") != d3dHash)
isD3dDllCorrect = true; return IsZipAM2R11ReturnCodes.MissingOrInvalidD3DX9_43Dll;
// check if data.win exists and check its hash // check if data.win exists and check its hash
if (File.Exists(tmpPath + "/data.win") && CalculateMD5(tmpPath + "/data.win") == dataWinHash) if (CalculateMD5(tmpPath + "/data.win") != dataWinHash)
isDataWinCorrect = true; return IsZipAM2R11ReturnCodes.MissingOrInvalidDataWin;
// clean up // clean up
Directory.Delete(tmpPath, true); Directory.Delete(tmpPath, true);
// if everything is true, return true // if we didn't exist before, everything is fine
if (doesExeExist && isD3dDllCorrect && isDataWinCorrect) return IsZipAM2R11ReturnCodes.Successful;
return true;
return false;
} }
/// <summary> /// <summary>
/// Calculates an MD5 hash from a given file. /// Calculates an MD5 hash from a given file.
/// </summary> /// </summary>
/// <param name="filename">Full Path to the file whose MD5 hash is supposed to be calculated.</param> /// <param name="filename">Full Path to the file whose MD5 hash is supposed to be calculated.</param>
/// <returns>The MD5 hash as a <see cref="string"/>.</returns> /// <returns>The MD5 hash as a <see cref="string"/>, empty string if file does not exist.</returns>
private string CalculateMD5(string filename) private string CalculateMD5(string filename)
{ {
// Check if File exists first
if (!File.Exists(filename))
return "";
using (var stream = File.OpenRead(filename)) using (var stream = File.OpenRead(filename))
{ {
using (var md5 = MD5.Create()) using (var md5 = MD5.Create())

@ -48,6 +48,17 @@ namespace AM2RLauncher
Creating Creating
} }
/// <summary>
/// An enum, that has possible return codes for <see cref="CheckIfZipIsAM2R11(string)"./>
/// </summary>
enum IsZipAM2R11ReturnCodes
{
Successful,
MissingAM2RExe,
MissingOrInvalidD3DX9_43Dll,
MissingOrInvalidDataWin
}
// Splashes // Splashes
/// <summary> /// <summary>
/// A <see cref="string"/>-<see cref="Array"/> of custom splashes. /// A <see cref="string"/>-<see cref="Array"/> of custom splashes.

Loading…
Cancel
Save