2021-12-25 20:24:00 -05:00
|
|
|
|
using System.Diagnostics;
|
2021-08-01 15:12:22 -04:00
|
|
|
|
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();
|
|
|
|
|
|
2021-08-01 15:12:22 -04:00
|
|
|
|
|
|
|
|
|
/// <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 15:12:22 -04:00
|
|
|
|
|
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}";
|
|
|
|
|
|
2021-08-01 15:12:22 -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>
|
|
|
|
|
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()
|
|
|
|
|
{
|
2021-12-25 20:24:00 -05:00
|
|
|
|
foreach (string resource in Assembly.GetExecutingAssembly().GetManifestResourceNames())
|
2021-08-01 00:36:37 -04:00
|
|
|
|
{
|
2021-12-25 20:24:00 -05: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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 15:12:22 -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>
|
2021-08-01 15:12:22 -04:00
|
|
|
|
public static void CleanupTempDir()
|
2021-08-01 00:36:37 -04:00
|
|
|
|
{
|
|
|
|
|
DirectoryInfo dir = new DirectoryInfo(TempDir);
|
|
|
|
|
|
2021-12-25 20:24:00 -05:00
|
|
|
|
if (dir.Exists)
|
2021-08-01 00:36:37 -04:00
|
|
|
|
{
|
|
|
|
|
dir.Delete(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|