using System; using System.Diagnostics; using System.IO; using System.Reflection; namespace PatcherUtils { public class LazyOperations { /// /// A directory to store temporary data. /// public static string TempDir = "PATCHER_TEMP".FromCwd(); /// /// The folder that the patches will be stored in /// public static string PatchFolder = "Aki_Patches"; private static string SevenZExe = "7za.exe"; /// /// The path to the 7za.exe file in the /// public static string SevenZExePath = $"{TempDir}\\{SevenZExe}"; private static string PatcherClient = "PatchClient.exe"; /// /// The path to the patcher.exe file in the /// public static string PatcherClientPath = $"{TempDir}\\{PatcherClient}"; private static string XDelta3EXE = "xdelta3.exe"; /// /// The path to the xdelta3.exe flie in the /// public static string XDelta3Path = $"{TempDir}\\{XDelta3EXE}"; /// /// Streams embedded resources out of the assembly /// /// /// /// The resource will not be streamed out if the already exists 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); } } /// /// Checks the resources in the assembly and streams them to the temp directory for later use. /// public static void PrepTempDir() { foreach(string resource in Assembly.GetExecutingAssembly().GetManifestResourceNames()) { switch(resource) { case string a when a.EndsWith(SevenZExe): { StreamResourceOut(resource, SevenZExePath); break; } case string a when a.EndsWith(PatcherClient): { StreamResourceOut(resource, PatcherClientPath); break; } case string a when a.EndsWith(XDelta3EXE): { StreamResourceOut(resource, XDelta3Path); break; } } } } public static void StartZipProcess(string SourcePath, string DestinationPath) { ProcessStartInfo procInfo = new ProcessStartInfo() { FileName = SevenZExePath, Arguments = $"a {DestinationPath} {SourcePath}" }; Process.Start(procInfo); } /// /// Deletes the recursively /// public static void CleanupTempDir() { DirectoryInfo dir = new DirectoryInfo(TempDir); if(dir.Exists) { dir.Delete(true); } } } }