String and EVORE cleanups

This commit is contained in:
Matt Nadareski
2021-08-23 22:05:18 -07:00
parent 97c9c7e5ed
commit 56aeded8eb
6 changed files with 150 additions and 207 deletions

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using BurnOutSharp.Matching;

View File

@@ -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);
}
}

View File

@@ -270,7 +270,7 @@ namespace BurnOutSharp
/// <returns>Version string, null on error</returns>
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
/// <param name="fileContent">Byte array representing the file contents</param>
/// <param name="positions">Last matched positions in the contents</param>
/// <returns>Version string, null on error</returns>
public static string GetFileVersion(string file, byte[] fileContent, List<int> positions)
{
return GetFileVersion(file);
}
public static string GetFileVersion(string file, byte[] fileContent, List<int> positions) => GetFileVersion(file);
/// <summary>
/// Wrapper for GetFileVersion for use in path matching
@@ -297,10 +294,7 @@ namespace BurnOutSharp
/// <param name="firstMatchedString">File to check for version</param>
/// <param name="files">Full list of input paths</param>
/// <returns>Version string, null on error</returns>
public static string GetFileVersion(string firstMatchedString, IEnumerable<string> files)
{
return GetFileVersion(firstMatchedString);
}
public static string GetFileVersion(string firstMatchedString, IEnumerable<string> files) => GetFileVersion(firstMatchedString);
/// <summary>
/// Get the assembly version as determined by an embedded assembly manifest
@@ -410,5 +404,41 @@ namespace BurnOutSharp
}
#endregion
#region IO Helpers
/// <summary>
/// Safely attempt to delete a path
/// </summary>
/// <param name="path">Path to be deleted</param>
/// <param name="isDirectory">True to treat the path as a directory, false for a file</param>
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
}
}
/// <summary>
/// Safely attempt to delete a path in the temp directory
/// </summary>
/// <param name="path">Path in the temp directory to be deleted</param>
/// <param name="isDirectory">True to treat the path as a directory, false for a file</param>
public static void SafeTempDelete(string path, bool isDirectory = false) => SafeDelete(Path.Combine(Path.GetTempPath(), path), isDirectory);
#endregion
}
}