Files
BinaryObjectScanner/BurnOutSharp/FileType/Valve.cs

90 lines
2.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.IO;
2022-05-01 17:41:50 -07:00
using BurnOutSharp.Interfaces;
2021-08-25 15:09:42 -07:00
using BurnOutSharp.Tools;
2022-05-15 14:18:45 -07:00
using HLLib.Directory;
using HLLib.Packages;
namespace BurnOutSharp.FileType
{
public class Valve : IScannable
{
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
{
2022-05-15 14:18:45 -07:00
return Package.GetPackageType(magic) != PackageType.HL_PACKAGE_NONE;
}
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
2021-02-26 09:26:23 -08:00
{
if (!File.Exists(file))
return null;
using (var fs = File.OpenRead(file))
{
return Scan(scanner, fs, file);
}
}
// TODO: Add stream opening support
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
// If the Valve archive itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
2022-05-15 14:18:45 -07:00
// Get the package type
byte[] magic = stream.ReadBytes(16);
PackageType packageType = Package.GetPackageType(magic);
if (packageType == PackageType.HL_PACKAGE_NONE)
return null;
// Create a new package from the file
var pkg = Package.CreatePackage(packageType);
2022-07-08 14:50:03 -07:00
FileModeFlags mode = FileModeFlags.HL_MODE_READ | FileModeFlags.HL_MODE_WRITE | FileModeFlags.HL_MODE_NO_FILEMAPPING | FileModeFlags.HL_MODE_VOLATILE;
2022-05-15 14:18:45 -07:00
bool opened = pkg.Open(file, mode, overwriteFiles: true);
if (!opened)
return null;
2022-05-15 14:18:45 -07:00
// Create the root directory
var rootDirectory = pkg.GetRoot();
// Extract all files
rootDirectory.Extract(tempPath, readEncrypted: true, overwrite: true);
// Close the package explicitly
pkg.Close();
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);
// If temp directory cleanup fails
try
{
Directory.Delete(tempPath, true);
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
Utilities.StripFromKeys(protections, tempPath);
return protections;
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
return null;
}
}
}