0
0
mirror of https://github.com/sp-tarkov/patcher.git synced 2025-02-13 06:30:45 -05:00
patcher/Patcher/PatcherUtils/LazyOperations.cs

160 lines
5.6 KiB
C#
Raw Normal View History

2024-03-22 13:03:07 -04:00
using System;
using PatcherUtils.Model;
using System.IO;
2021-08-01 00:36:37 -04:00
using System.Reflection;
2024-03-22 13:03:07 -04:00
using SevenZip;
2021-08-01 00:36:37 -04:00
namespace PatcherUtils
{
public class LazyOperations
{
/// <summary>
/// A directory to store temporary data.
/// </summary>
public static string TempDir = "PATCHER_TEMP".FromCwd();
/// <summary>
/// The folder that the patches will be stored in
/// </summary>
2024-05-23 20:07:47 -04:00
public static string PatchFolder = "SPT_Patches";
2021-12-17 16:02:31 -05:00
2024-03-22 13:03:07 -04:00
private static string SevenZDll = "7z.dll";
2021-08-01 00:36:37 -04:00
/// <summary>
/// The path to the 7za.exe file in the <see cref="TempDir"/>
/// </summary>
2024-03-22 13:03:07 -04:00
public static string SevenZDllPath = $"{TempDir}\\{SevenZDll}";
2021-08-01 00:36:37 -04:00
private static string PatcherClient = "PatchClient.exe";
2021-08-01 00:36:37 -04:00
/// <summary>
/// The path to the patcher.exe file in the <see cref="TempDir"/>
/// </summary>
public static string PatcherClientPath = $"{TempDir}\\{PatcherClient}";
2021-12-17 16:02:31 -05:00
private static string XDelta3EXE = "xdelta3.exe";
/// <summary>
/// The path to the xdelta3.exe flie in the <see cref="TempDir"/>
/// </summary>
public static string XDelta3Path = $"{TempDir}\\{XDelta3EXE}";
2021-08-01 00:36:37 -04:00
/// <summary>
/// Streams embedded resources out of the assembly
/// </summary>
/// <param name="ResourceName"></param>
/// <param name="OutputFilePath"></param>
/// <remarks>The resource will not be streamed out if the <paramref name="OutputFilePath"/> already exists</remarks>
2021-12-29 11:04:02 -05:00
private static void StreamResourceOut(Assembly assembly, string ResourceName, string OutputFilePath)
2021-08-01 00:36:37 -04:00
{
FileInfo outputFile = new FileInfo(OutputFilePath);
if (outputFile.Exists)
{
2022-06-14 21:26:23 -04:00
PatchLogger.LogInfo($"Deleting Existing Resource: {outputFile.Name}");
outputFile.Delete();
}
2021-08-01 00:36:37 -04:00
if (!outputFile.Directory.Exists)
{
2022-06-14 21:26:23 -04:00
PatchLogger.LogInfo($"Creating Resource Directory: {outputFile.Directory.Name}");
2021-08-01 00:36:37 -04:00
Directory.CreateDirectory(outputFile.Directory.FullName);
}
using (FileStream fs = File.Create(OutputFilePath))
2021-12-29 11:04:02 -05:00
using (Stream s = assembly.GetManifestResourceStream(ResourceName))
2021-08-01 00:36:37 -04:00
{
s.CopyTo(fs);
2022-06-14 21:26:23 -04:00
PatchLogger.LogInfo($"Resourced streamed out of assembly: {outputFile.Name}");
2021-08-01 00:36:37 -04:00
}
}
/// <summary>
/// Checks the resources in the assembly and streams them to the temp directory for later use.
/// </summary>
2021-12-29 11:04:02 -05:00
public static void ExtractResourcesToTempDir(Assembly assembly = null)
2021-08-01 00:36:37 -04:00
{
2021-12-29 11:04:02 -05:00
if(assembly == null) assembly = Assembly.GetExecutingAssembly();
foreach (string resource in assembly.GetManifestResourceNames())
2021-08-01 00:36:37 -04:00
{
switch (resource)
2021-08-01 00:36:37 -04:00
{
2024-03-22 13:03:07 -04:00
case string a when a.EndsWith(SevenZDll):
2021-08-01 00:36:37 -04:00
{
2024-03-22 13:03:07 -04:00
StreamResourceOut(assembly, resource, SevenZDllPath);
2021-08-01 00:36:37 -04:00
break;
}
case string a when a.EndsWith(PatcherClient):
{
2021-12-29 11:04:02 -05:00
StreamResourceOut(assembly, resource, PatcherClientPath);
2021-08-01 00:36:37 -04:00
break;
}
2021-12-17 16:02:31 -05:00
case string a when a.EndsWith(XDelta3EXE):
{
2021-12-29 11:04:02 -05:00
StreamResourceOut(assembly, resource, XDelta3Path);
2021-12-17 16:02:31 -05:00
break;
}
2021-08-01 00:36:37 -04:00
}
}
}
2024-03-22 13:03:07 -04:00
public static void CompressDirectory(string SourceDirectoryPath, string DestinationFilePath, IProgress<int> progress)
{
2024-05-03 14:57:08 -04:00
try
{
2024-05-03 14:57:08 -04:00
PatchLogger.LogInfo($"Compressing: {SourceDirectoryPath}");
PatchLogger.LogInfo($"Output file: {DestinationFilePath}");
var outputFile = new FileInfo(DestinationFilePath);
SevenZipBase.SetLibraryPath(SevenZDllPath);
PatchLogger.LogInfo($"7z.dll set: {SevenZDllPath}");
var compressor = new SevenZipCompressor()
{
ArchiveFormat = OutArchiveFormat.SevenZip,
CompressionMethod = CompressionMethod.Lzma2,
2024-05-04 10:56:49 -04:00
CompressionLevel = CompressionLevel.Normal,
PreserveDirectoryRoot = true
2024-05-03 14:57:08 -04:00
};
2024-05-03 14:57:08 -04:00
compressor.Compressing += (_, args) => { progress.Report(args.PercentDone); };
using var outputStream = outputFile.OpenWrite();
PatchLogger.LogInfo("Starting compression");
compressor.CompressDirectory(SourceDirectoryPath, outputStream);
PatchLogger.LogInfo("Compression complete");
2024-03-22 13:03:07 -04:00
2024-05-03 14:57:08 -04:00
outputFile.Refresh();
2022-06-14 21:26:23 -04:00
2024-05-03 14:57:08 -04:00
// failed to compress data
if (!outputFile.Exists || outputFile.Length == 0)
{
PatchLogger.LogError("Failed to compress patcher");
}
}
catch (Exception ex)
2024-03-22 13:03:07 -04:00
{
2024-05-03 14:57:08 -04:00
PatchLogger.LogException(ex);
2024-03-22 13:03:07 -04:00
}
}
2021-08-01 00:36:37 -04:00
/// <summary>
/// Deletes the <see cref="TempDir"/> recursively
/// </summary>
public static void CleanupTempDir()
2021-08-01 00:36:37 -04:00
{
DirectoryInfo dir = new DirectoryInfo(TempDir);
if (dir.Exists)
2021-08-01 00:36:37 -04:00
{
dir.Delete(true);
2024-05-03 14:57:08 -04:00
PatchLogger.LogInfo("Temp directory deleted");
2021-08-01 00:36:37 -04:00
}
}
}
}