diff --git a/BurnOutSharp/ProtectionType/LaserLock.cs b/BurnOutSharp/ProtectionType/LaserLock.cs index 9d0d5bc7..b077652a 100644 --- a/BurnOutSharp/ProtectionType/LaserLock.cs +++ b/BurnOutSharp/ProtectionType/LaserLock.cs @@ -148,7 +148,7 @@ namespace BurnOutSharp.ProtectionType if (char.IsNumber(version[0]) && char.IsNumber(version[2]) && char.IsNumber(version[3])) return $"{version[0]}.{version[2]}{version[3]}"; - return ""; + return string.Empty; } } } diff --git a/BurnOutSharp/ProtectionType/ProtectDisc.cs b/BurnOutSharp/ProtectionType/ProtectDisc.cs index c96d186a..f8e2799f 100644 --- a/BurnOutSharp/ProtectionType/ProtectDisc.cs +++ b/BurnOutSharp/ProtectionType/ProtectDisc.cs @@ -179,81 +179,67 @@ namespace BurnOutSharp.ProtectionType return $"7.6-10.x (Build {irefBuild})"; } - return ""; + return string.Empty; } // TODO: Analyze this method and figure out if this can be done without attempting execution private static string SearchProtectDiscVersion(string file, byte[] fileContent) { - string version = ""; - DateTime timestart; + // If the file isn't executable, don't even bother if (!EVORE.IsEXE(fileContent)) - return ""; + return string.Empty; + // Get some of the required paths string tempexe = EVORE.MakeTempFile(fileContent); - string[] DependentDlls = EVORE.CopyDependentDlls(file, fileContent); - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "a*.tmp")); - } - catch { } - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "PCD*.sys")); - } - catch { } - if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"))) - { - try - { - File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc", "p*.dll")); - } - catch { } - } + string[] dependentDlls = EVORE.CopyDependentDlls(file, fileContent); + string pdPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"); + // Clean up any temp files before attempting to run + Utilities.SafeTempDelete("a*.tmp"); + Utilities.SafeTempDelete("PCD*.sys"); + if (Directory.Exists(pdPath)) + Utilities.SafeDelete(Path.Combine(pdPath, "p*.dll")); + + // Try to safely start the temp executable Process exe = EVORE.StartSafe(tempexe); if (exe == null) - return ""; + return string.Empty; + string version = ""; Process[] processes = new Process[0]; - timestart = DateTime.Now; + DateTime timestart = DateTime.Now; do { exe.Refresh(); - string[] files = null; + string[] files = new string[0]; - //check for ProtectDisc 8.2-x - if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"))) - { - files = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"), "p*.dll"); - } + // Check for ProtectDisc 8.2-x + if (Directory.Exists(pdPath)) + files = Directory.GetFiles(pdPath, "p*.dll"); - if (files != null) + if (files.Any()) { - if (files.Length > 0) + string fileVersion = Utilities.GetFileVersion(files[0]); + if (!string.IsNullOrWhiteSpace(fileVersion)) { - var fvinfo = Utilities.GetFileVersionInfo(files[0]); - if (!string.IsNullOrWhiteSpace(fvinfo?.FileVersion)) - { - version = fvinfo.FileVersion.Replace(" ", "").Replace(",", "."); - //ProtectDisc 9 uses a ProtectDisc-Core dll version 8.0.x - if (version.StartsWith("8.0")) - version = ""; - fvinfo = null; - break; - } + version = fileVersion; + + // ProtectDisc 9 uses a ProtectDisc-Core dll version 8.0.x + if (version.StartsWith("8.0")) + version = string.Empty; + + break; } } //check for ProtectDisc 7.1-8.1 files = Directory.GetFiles(Path.GetTempPath(), "a*.tmp"); - if (files.Length > 0) + if (files.Any()) { - var fvinfo = Utilities.GetFileVersionInfo(files[0]); - if (!string.IsNullOrWhiteSpace(fvinfo?.FileVersion)) + string fileVersion = Utilities.GetFileVersion(files[0]); + if (!string.IsNullOrWhiteSpace(fileVersion)) { - version = fvinfo.FileVersion.Replace(" ", "").Replace(",", "."); - fvinfo = null; + version = fileVersion; break; } } @@ -304,39 +290,19 @@ namespace BurnOutSharp.ProtectionType exe.Close(); Thread.Sleep(500); - if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"))) - { - try - { - File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc", "p*.dll")); - } - catch { } - } - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "a*.tmp")); - } - catch { } + // Clean up any temp files after running + Utilities.SafeDelete(tempexe); + Utilities.SafeTempDelete("a*.tmp"); + Utilities.SafeTempDelete("PCD*.sys"); + if (Directory.Exists(pdPath)) + Utilities.SafeDelete(Path.Combine(pdPath, "p*.dll")); - try + if (dependentDlls != null) { - File.Delete(Path.Combine(Path.GetTempPath(), "PCD*.sys")); - } - catch { } - File.Delete(tempexe); - if (DependentDlls != null) - { - for (int i = 0; i < DependentDlls.Length; i++) + foreach (string dll in dependentDlls) { - try - { - File.Delete(DependentDlls[i]); - } - catch (Exception ex) - { - Console.WriteLine("!error while deleting file " + DependentDlls[i] + "; " + ex.Message); - } + Utilities.SafeDelete(dll); } } diff --git a/BurnOutSharp/ProtectionType/SafeDisc.cs b/BurnOutSharp/ProtectionType/SafeDisc.cs index 35c38238..b5c6366d 100644 --- a/BurnOutSharp/ProtectionType/SafeDisc.cs +++ b/BurnOutSharp/ProtectionType/SafeDisc.cs @@ -223,30 +223,25 @@ namespace BurnOutSharp.ProtectionType // TODO: Analyze this method and figure out if this can be done without attempting execution private static string SearchSafeDiscVersion(string file, byte[] fileContent) { - Process exe; - string version = ""; - DateTime timestart; + // If the file isn't executable, don't even bother if (!EVORE.IsEXE(fileContent)) - return ""; + return string.Empty; + // Get some of the required paths string tempexe = EVORE.MakeTempFile(fileContent); - string[] DependentDlls = EVORE.CopyDependentDlls(file, fileContent); - try - { - Directory.Delete(Path.Combine(Path.GetTempPath(), "~e*"), true); - } - catch { } - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "~e*")); - } - catch { } + string[] dependentDlls = EVORE.CopyDependentDlls(file, fileContent); - exe = EVORE.StartSafe(tempexe); + // Clean up any temp files before attempting to run + Utilities.SafeTempDelete("~e*", isDirectory: true); + Utilities.SafeTempDelete("~e*", isDirectory: false); + + // Try to safely start the temp executable + Process exe = EVORE.StartSafe(tempexe); if (exe == null) - return ""; + return string.Empty; - timestart = DateTime.Now; + string version = ""; + DateTime timestart = DateTime.Now; do { if (Directory.GetDirectories(Path.GetTempPath(), "~e*").Length > 0) @@ -258,11 +253,11 @@ namespace BurnOutSharp.ProtectionType try { sr = new StreamReader(files[0], Encoding.Default); - string FileContent = sr.ReadToEnd(); + string localFileContent = sr.ReadToEnd(); sr.Close(); - int position = FileContent.IndexOf("%ld.%ld.%ld, %ld, %s,") - 1; + int position = localFileContent.IndexOf("%ld.%ld.%ld, %ld, %s,") - 1; if (position > -1) - version = FileContent.Substring(position + 28, 12); + version = localFileContent.Substring(position + 28, 12); break; } catch { } @@ -272,32 +267,19 @@ namespace BurnOutSharp.ProtectionType if (!exe.HasExited) exe.Kill(); + exe.Close(); - try - { - Directory.Delete(Path.Combine(Path.GetTempPath(), "~e*"), true); - } - catch { } - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "~e*")); - File.Delete(tempexe); - } - catch { } + // Clean up any temp files after running + Utilities.SafeDelete(tempexe); + Utilities.SafeTempDelete("~e*", isDirectory: true); + Utilities.SafeTempDelete("~e*", isDirectory: false); - if (DependentDlls != null) + if (dependentDlls != null) { - for (int i = 0; i < DependentDlls.Length; i--) + foreach (string dll in dependentDlls) { - try - { - File.Delete(DependentDlls[i]); - } - catch (Exception ex) - { - Console.WriteLine("!error while deleting file " + DependentDlls[i] + "; " + ex.Message); - } + Utilities.SafeDelete(dll); } } diff --git a/BurnOutSharp/ProtectionType/SolidShield.cs b/BurnOutSharp/ProtectionType/SolidShield.cs index 1efa0640..bfac9dfc 100644 --- a/BurnOutSharp/ProtectionType/SolidShield.cs +++ b/BurnOutSharp/ProtectionType/SolidShield.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using BurnOutSharp.Matching; diff --git a/BurnOutSharp/ProtectionType/VOBProtectCDDVD.cs b/BurnOutSharp/ProtectionType/VOBProtectCDDVD.cs index b2199bd1..4f3e761b 100644 --- a/BurnOutSharp/ProtectionType/VOBProtectCDDVD.cs +++ b/BurnOutSharp/ProtectionType/VOBProtectCDDVD.cs @@ -86,7 +86,7 @@ namespace BurnOutSharp.ProtectionType private static string GetBuild(byte[] fileContent, int position) { if (!char.IsNumber((char)fileContent[position - 13])) - return ""; //Build info removed + return string.Empty; //Build info removed int build = BitConverter.ToInt16(fileContent, position - 4); // Check if this is supposed to be a 4-byte read return $" (Build {build})"; @@ -103,81 +103,67 @@ namespace BurnOutSharp.ProtectionType return $"5.{subVersion}.{subsubVersion}"; } - return ""; + return string.Empty; } // TODO: Analyze this method and figure out if this can be done without attempting execution private static string SearchProtectDiscVersion(string file, byte[] fileContent) { - string version = ""; - DateTime timestart; + // If the file isn't executable, don't even bother if (!EVORE.IsEXE(fileContent)) - return ""; + return string.Empty; + // Get some of the required paths string tempexe = EVORE.MakeTempFile(fileContent); - string[] DependentDlls = EVORE.CopyDependentDlls(file, fileContent); - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "a*.tmp")); - } - catch { } - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "PCD*.sys")); - } - catch { } - if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"))) - { - try - { - File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc", "p*.dll")); - } - catch { } - } + string[] dependentDlls = EVORE.CopyDependentDlls(file, fileContent); + string pdPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"); + // Clean up any temp files before attempting to run + Utilities.SafeTempDelete("a*.tmp"); + Utilities.SafeTempDelete("PCD*.sys"); + if (Directory.Exists(pdPath)) + Utilities.SafeDelete(Path.Combine(pdPath, "p*.dll")); + + // Try to safely start the temp executable Process exe = EVORE.StartSafe(tempexe); if (exe == null) - return ""; + return string.Empty; + string version = ""; Process[] processes = new Process[0]; - timestart = DateTime.Now; + DateTime timestart = DateTime.Now; do { exe.Refresh(); string[] files = null; - //check for ProtectDisc 8.2-x - if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"))) - { - files = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"), "p*.dll"); - } + // Check for ProtectDisc 8.2-x + if (Directory.Exists(pdPath)) + files = Directory.GetFiles(pdPath, "p*.dll"); - if (files != null) + if (files.Any()) { - if (files.Length > 0) + string fileVersion = Utilities.GetFileVersion(files[0]); + if (!string.IsNullOrWhiteSpace(fileVersion)) { - var fvinfo = Utilities.GetFileVersionInfo(files[0]); - if (!string.IsNullOrWhiteSpace(fvinfo?.FileVersion)) - { - version = fvinfo.FileVersion.Replace(" ", "").Replace(",", "."); - //ProtectDisc 9 uses a ProtectDisc-Core dll version 8.0.x - if (version.StartsWith("8.0")) - version = ""; - fvinfo = null; - break; - } + version = fileVersion; + + // ProtectDisc 9 uses a ProtectDisc-Core dll version 8.0.x + if (version.StartsWith("8.0")) + version = string.Empty; + + break; } } //check for ProtectDisc 7.1-8.1 files = Directory.GetFiles(Path.GetTempPath(), "a*.tmp"); - if (files.Length > 0) + if (files.Any()) { - var fvinfo = Utilities.GetFileVersionInfo(files[0]); - if (!string.IsNullOrWhiteSpace(fvinfo?.FileVersion)) + string fileVersion = Utilities.GetFileVersion(files[0]); + if (!string.IsNullOrWhiteSpace(fileVersion)) { - version = fvinfo.FileVersion.Replace(" ", "").Replace(",", "."); - fvinfo = null; + version = fileVersion; break; } } @@ -228,39 +214,19 @@ namespace BurnOutSharp.ProtectionType exe.Close(); Thread.Sleep(500); - if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc"))) - { - try - { - File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ProtectDisc", "p*.dll")); - } - catch { } - } - try - { - File.Delete(Path.Combine(Path.GetTempPath(), "a*.tmp")); - } - catch { } + // Clean up any temp files after running + Utilities.SafeDelete(tempexe); + Utilities.SafeTempDelete("a*.tmp"); + Utilities.SafeTempDelete("PCD*.sys"); + if (Directory.Exists(pdPath)) + Utilities.SafeDelete(Path.Combine(pdPath, "p*.dll")); - try + if (dependentDlls != null) { - File.Delete(Path.Combine(Path.GetTempPath(), "PCD*.sys")); - } - catch { } - File.Delete(tempexe); - if (DependentDlls != null) - { - for (int i = 0; i < DependentDlls.Length; i++) + foreach (string dll in dependentDlls) { - try - { - File.Delete(DependentDlls[i]); - } - catch (Exception ex) - { - Console.WriteLine("!error while deleting file " + DependentDlls[i] + "; " + ex.Message); - } + Utilities.SafeDelete(dll); } } diff --git a/BurnOutSharp/Utilities.cs b/BurnOutSharp/Utilities.cs index 1b20b086..a7f61cd5 100644 --- a/BurnOutSharp/Utilities.cs +++ b/BurnOutSharp/Utilities.cs @@ -270,7 +270,7 @@ namespace BurnOutSharp /// Version string, null on error public static string GetFileVersion(string file) { - var fvinfo = GetFileVersionInfo (file); + var fvinfo = GetFileVersionInfo(file); if (fvinfo?.FileVersion == null) return string.Empty; if (fvinfo.FileVersion != "") @@ -286,10 +286,7 @@ namespace BurnOutSharp /// Byte array representing the file contents /// Last matched positions in the contents /// Version string, null on error - public static string GetFileVersion(string file, byte[] fileContent, List positions) - { - return GetFileVersion(file); - } + public static string GetFileVersion(string file, byte[] fileContent, List positions) => GetFileVersion(file); /// /// Wrapper for GetFileVersion for use in path matching @@ -297,10 +294,7 @@ namespace BurnOutSharp /// File to check for version /// Full list of input paths /// Version string, null on error - public static string GetFileVersion(string firstMatchedString, IEnumerable files) - { - return GetFileVersion(firstMatchedString); - } + public static string GetFileVersion(string firstMatchedString, IEnumerable files) => GetFileVersion(firstMatchedString); /// /// Get the assembly version as determined by an embedded assembly manifest @@ -410,5 +404,41 @@ namespace BurnOutSharp } #endregion + + #region IO Helpers + + /// + /// Safely attempt to delete a path + /// + /// Path to be deleted + /// True to treat the path as a directory, false for a file + public static void SafeDelete(string path, bool isDirectory = false) + { + // No valid path means we can't delete + if (string.IsNullOrWhiteSpace(path)) + return; + + // Attempt to delete the path + try + { + if (!isDirectory && File.Exists(path)) + File.Delete(path); + else if (isDirectory && Directory.Exists(path)) + Directory.Delete(path, true); + } + catch + { + // Absorb any errors in deletion + } + } + + /// + /// Safely attempt to delete a path in the temp directory + /// + /// Path in the temp directory to be deleted + /// True to treat the path as a directory, false for a file + public static void SafeTempDelete(string path, bool isDirectory = false) => SafeDelete(Path.Combine(Path.GetTempPath(), path), isDirectory); + + #endregion } }