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

117 lines
3.8 KiB
C#
Raw Normal View History

using System.Diagnostics;
using System.IO;
2021-08-01 00:36:37 -04:00
using System.Reflection;
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>
2021-12-17 16:02:31 -05:00
public static string PatchFolder = "Aki_Patches";
private static string SevenZExe = "7za.exe";
2021-08-01 00:36:37 -04:00
/// <summary>
/// The path to the 7za.exe file in the <see cref="TempDir"/>
/// </summary>
public static string SevenZExePath = $"{TempDir}\\{SevenZExe}";
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>
private static void StreamResourceOut(string ResourceName, string OutputFilePath)
{
FileInfo outputFile = new FileInfo(OutputFilePath);
if (outputFile.Exists) return;
if (!outputFile.Directory.Exists)
{
Directory.CreateDirectory(outputFile.Directory.FullName);
}
using (FileStream fs = File.Create(OutputFilePath))
using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName))
{
s.CopyTo(fs);
}
}
/// <summary>
/// Checks the resources in the assembly and streams them to the temp directory for later use.
/// </summary>
public static void PrepTempDir()
{
foreach (string resource in Assembly.GetExecutingAssembly().GetManifestResourceNames())
2021-08-01 00:36:37 -04:00
{
switch (resource)
2021-08-01 00:36:37 -04:00
{
case string a when a.EndsWith(SevenZExe):
{
StreamResourceOut(resource, SevenZExePath);
break;
}
case string a when a.EndsWith(PatcherClient):
{
StreamResourceOut(resource, PatcherClientPath);
break;
}
2021-12-17 16:02:31 -05:00
case string a when a.EndsWith(XDelta3EXE):
{
StreamResourceOut(resource, XDelta3Path);
break;
}
2021-08-01 00:36:37 -04:00
}
}
}
public static void StartZipProcess(string SourcePath, string DestinationPath)
{
ProcessStartInfo procInfo = new ProcessStartInfo()
{
FileName = SevenZExePath,
Arguments = $"a {DestinationPath} {SourcePath}"
};
Process.Start(procInfo);
}
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);
}
}
}
}