some code cleanup

This commit is contained in:
atomsk-0
2025-02-27 17:19:35 +02:00
parent 95904bde96
commit 9782ce8e77
2 changed files with 103 additions and 91 deletions

View File

@@ -29,13 +29,13 @@ namespace WinDurango.UI.Utils
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; GitHubAPI/1.0)");
}
public static async Task<bool> UnpatchPackage(Package package, ProgressController controller)
public static Task<bool> UnpatchPackage(Package package, ProgressController controller)
{
installedPackage pkg = App.InstalledPackages.GetPackage(package);
if (pkg == null)
return false;
return Task.FromResult(false);
return await UnpatchPackage(pkg, controller);
return UnpatchPackage(pkg, controller);
}
public static async Task<bool> PatchPackage(Package package, bool forceRedownload,
@@ -48,29 +48,40 @@ namespace WinDurango.UI.Utils
return await PatchPackage(pkg, forceRedownload, controller);
}
// todo: clean this up
// Cleaned lil bit
public static async Task<bool> PatchPackage(installedPackage package, bool forceRedownload,
ProgressController controller)
{
string patchesPath = Path.Combine(App.DataDir, "WinDurangoCore");
// Artifact dll package link
const string dllLink =
"https://nightly.link/WinDurango/WinDurango/workflows/msbuild/main/WinDurango-DEBUG.zip";
controller?.Update($"Patching {package.FamilyName}", 0);
string curDate = DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss");
string installPath = package.InstallPath;
controller?.Update("Getting latest release", 10);
await GetOrReuseRelease(); // don't use the return value since wdRelease is set regardless
string dlLink = wdRelease.DownloadLink;
string relName = wdRelease.Name;
string dlPath = $"WinDurangoCore.zip";
// This should be further improved...
bool isDownloadSourceArtifact = App.Settings.Settings.DownloadSource == UiConfigData.PatchSource.Artifact;
// see this is quite messy but just needed to get it to work
if (App.Settings.Settings.DownloadSource == UiConfigData.PatchSource.Artifact)
{
dlLink = "https://nightly.link/WinDurango/WinDurango/workflows/msbuild/main/WinDurango-DEBUG.zip";
dlPath = $"WinDurangoCore-ARTIFACT.zip";
patchesPath = Path.Combine(App.DataDir, "WinDurangoCore-ARTIFACT");
relName = $"latest GitHub Actions artifact";
}
string dlLink = isDownloadSourceArtifact
? dllLink
: wdRelease.DownloadLink;
string relName = isDownloadSourceArtifact
? "latest GitHub Actions artifact"
: wdRelease.Name;
string dlPath = isDownloadSourceArtifact
? "WinDurangoCore-ARTIFACT.zip"
: "WinDurangoCore.zip";
string patchesPath = isDownloadSourceArtifact
? Path.Combine(App.DataDir, "WinDurangoCore-ARTIFACT")
: Path.Combine(App.DataDir, "WinDurangoCore");
if (!Path.Exists(patchesPath) || forceRedownload)
{
@@ -98,94 +109,94 @@ namespace WinDurango.UI.Utils
try
{
Directory.CreateDirectory(patchesPath);
controller?.Update($"Extracting", 40);
controller?.Update("Extracting", 40);
ZipFile.ExtractToDirectory(archivePath, patchesPath);
}
catch (Exception ex)
{
await controller.Fail(ex.Message, "Failed to extract");
await controller!.Fail(ex.Message, "Failed to extract");
return false;
}
}
controller?.Update("Extracting", 50);
if (Path.Exists(patchesPath))
{
DirectoryInfo patchesDir = new(patchesPath);
FileInfo[] patchFiles = patchesDir.GetFiles("*.dll");
float progressPerFile = (90 - 50) / (float)patchFiles.Length;
DirectoryInfo packageDir = new(installPath);
foreach (FileInfo file in patchFiles)
{
int progress = (int)Math.Round(50 + progressPerFile * Array.IndexOf(patchFiles, file));
controller?.Update($"Copying {file.Name}", progress);
FileInfo oldFile = packageDir.GetFiles("*.dll").FirstOrDefault(f => f.Name == file.Name);
if (oldFile != null)
{
Logger.WriteInformation($"Backing up old {oldFile.Name}");
string dllBackup = Path.Combine(installPath, "WDDllBackup", curDate);
if (!Path.Exists(dllBackup))
Directory.CreateDirectory(dllBackup);
try
{
File.Move(oldFile.FullName, Path.Combine(dllBackup, oldFile.Name));
}
catch (Exception e)
{
await controller.Fail(e.Message, "Failed to move backed-up file " + oldFile.Name);
return false;
}
if (!package.OriginalDlls.Contains(Path.Combine(dllBackup, oldFile.Name)))
package.OriginalDlls.Add(Path.Combine(dllBackup, oldFile.Name));
}
string patchPath = Path.Combine(installPath, file.Name);
if (Path.Exists(Path.Combine(installPath, file.Name)))
File.Delete(Path.Combine(installPath, file.Name));
try
{
File.Copy(file.FullName, Path.Combine(installPath, file.Name));
}
catch (Exception e)
{
await controller.Fail(e.Message, "Failed to copy file " + file.Name);
return false;
}
Logger.WriteInformation($"Added {file.Name}");
if (!package.PatchedDlls.Contains(patchPath))
package.PatchedDlls.Add(patchPath);
}
}
else
if (!Path.Exists(patchesPath))
{
Logger.WriteError("How did this happen???? patchDir should exist.");
return false;
}
controller?.Update($"Writing patch txt", 93);
DirectoryInfo patchesDir = new(patchesPath);
FileInfo[] patchFiles = patchesDir.GetFiles("*.dll");
float progressPerFile = (90 - 50) / (float)patchFiles.Length;
DirectoryInfo packageDir = new(installPath);
foreach (FileInfo file in patchFiles)
{
int progress = (int)Math.Round(50 + progressPerFile * Array.IndexOf(patchFiles, file));
controller?.Update($"Copying {file.Name}", progress);
FileInfo oldFile = packageDir.GetFiles("*.dll").FirstOrDefault(f => f.Name == file.Name);
if (oldFile != null)
{
Logger.WriteInformation($"Backing up old {oldFile.Name}");
string dllBackup = Path.Combine(installPath, "WDDllBackup", curDate);
if (!Path.Exists(dllBackup))
Directory.CreateDirectory(dllBackup);
try
{
File.Move(oldFile.FullName, Path.Combine(dllBackup, oldFile.Name));
}
catch (Exception e)
{
await controller!.Fail(e.Message, "Failed to move backed-up file " + oldFile.Name);
return false;
}
if (!package.OriginalDlls.Contains(Path.Combine(dllBackup, oldFile.Name)))
package.OriginalDlls.Add(Path.Combine(dllBackup, oldFile.Name));
}
string patchPath = Path.Combine(installPath, file.Name);
if (Path.Exists(Path.Combine(installPath, file.Name)))
File.Delete(Path.Combine(installPath, file.Name));
try
{
File.Copy(file.FullName, Path.Combine(installPath, file.Name));
}
catch (Exception e)
{
await controller!.Fail(e.Message, "Failed to copy file " + file.Name);
return false;
}
Logger.WriteInformation($"Added {file.Name}");
if (!package.PatchedDlls.Contains(patchPath))
package.PatchedDlls.Add(patchPath);
}
controller?.Update("Writing patch txt", 93);
StringBuilder builder = new();
builder.AppendLine($"# This package was patched by WinDurango.UI with WinDurango release \"{relName}\".");
builder.AppendLine("# If you want to unpatch manually, delete this file and edit %appdata%\\WinDurango\\UI\\InstalledPackages.json and set IsPatched to false.");
builder.AppendLine(
"# If you want to unpatch manually, delete this file and edit %appdata%\\WinDurango\\UI\\InstalledPackages.json and set IsPatched to false.");
builder.AppendLine("# Format is ReleaseName;VerPacked");
builder.AppendLine($"{relName.Replace(";", "-")};{App.VerPacked}");
await File.WriteAllTextAsync(Path.Combine(installPath, "installed.txt"), builder.ToString());
controller?.Update($"Updating package list", 95);
controller?.Update("Updating package list", 95);
package.IsPatched = true;
App.InstalledPackages.UpdatePackage(package);
controller?.Update($"Done!", 100);
controller?.Update("Done!", 100);
return true;
}
public static async Task<bool> UnpatchPackage(installedPackage package,
public static Task<bool> UnpatchPackage(installedPackage package,
ProgressController controller)
{
controller?.Update($"Unpatching {package.FamilyName}", 0);
@@ -198,7 +209,8 @@ namespace WinDurango.UI.Utils
float progressPerRemove = (0 - 50) / (float)package.PatchedDlls.Count;
foreach (string dll in dlls)
{
int progress = (int)Math.Round(50 + progressPerRemove * Array.IndexOf(package.PatchedDlls.ToArray(), dll));
int progress =
(int)Math.Round(50 + progressPerRemove * Array.IndexOf(package.PatchedDlls.ToArray(), dll));
controller?.Update($"Removing {dll}", progress);
try
{
@@ -209,7 +221,7 @@ namespace WinDurango.UI.Utils
{
Logger.WriteError($"Failed to delete {dll}.");
}
};
}
float progressPerRevert = (98 - 50) / (float)package.OriginalDlls.Count;
foreach (string dll in originalDlls)
@@ -225,18 +237,18 @@ namespace WinDurango.UI.Utils
{
Logger.WriteError($"Failed to copy {dll}.");
}
};
}
package.IsPatched = false;
controller?.Update($"Removing patched.txt", 99);
controller?.Update("Removing patched.txt", 99);
if (Path.Exists(Path.Combine(installPath, "installed.txt")))
File.Delete(Path.Combine(installPath, "installed.txt"));
controller?.Update($"Updating package list", 99);
controller?.Update("Updating package list", 99);
App.InstalledPackages.UpdatePackage(package);
controller?.Update($"Done!", 100);
return true;
controller?.Update("Done!", 100);
return Task.FromResult(true);
}
// don't wanna use all the api shits lol
@@ -252,7 +264,7 @@ namespace WinDurango.UI.Utils
{
GitHubRelease release = new();
const string url = $"https://api.github.com/repos/WinDurango/WinDurango/releases";
const string url = "https://api.github.com/repos/WinDurango/WinDurango/releases";
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();

View File

@@ -57,7 +57,7 @@ namespace WinDurango.UI.Utils
public static List<Package> GetXPackages(List<Package> packages)
{
// first try implementation and it worked hell yeah
List<Package> result = new();
List<Package> result = [];
foreach (Package package in packages)
{
string installPath = package.InstalledPath;
@@ -67,9 +67,9 @@ namespace WinDurango.UI.Utils
continue;
string manifest;
using (var stream = File.OpenRead(manifestPath))
using (FileStream stream = File.OpenRead(manifestPath))
{
var reader = new StreamReader(stream);
StreamReader reader = new(stream);
manifest = reader.ReadToEnd();
}
@@ -82,7 +82,7 @@ namespace WinDurango.UI.Utils
XElement osName = prerequisites?.Descendants().FirstOrDefault(e => e.Name.LocalName == "OSName");
XElement osPackageDependency = dependencies?.Descendants().FirstOrDefault(e => e.Name.LocalName == "OSPackageDependency");
if ((osName != null && osName.Value.ToLower() == "era") || (osPackageDependency != null && osPackageDependency.Attribute("Name")?.Value == "Microsoft.GameOs"))
if ((osName != null && osName.Value.Equals("era", System.StringComparison.InvariantCultureIgnoreCase)) || (osPackageDependency != null && osPackageDependency.Attribute("Name")?.Value == "Microsoft.GameOs"))
{
result.Add(package);
}