From 3025d2500b71272bd6cdace718de36ebb20b58c2 Mon Sep 17 00:00:00 2001 From: Rankyn Bass Date: Tue, 1 Aug 2023 20:08:44 -0700 Subject: [PATCH] Updated with suggestions from marzent: * Moved download funcs to CompatibilityTools * Moved hud funcs to DxvkSettings * Simplified MergeLDPreload * Cleaned up formatting, deleted a commented-out function --- .../Compatibility/CompatibilityTools.cs | 68 ++++++++----- .../Compatibility/DxvkSettings.cs | 32 ++++++- .../Compatibility/UnixHelpers.cs | 96 ------------------- .../Compatibility/WineSettings.cs | 4 +- 4 files changed, 77 insertions(+), 123 deletions(-) delete mode 100644 src/XIVLauncher.Common.Unix/Compatibility/UnixHelpers.cs diff --git a/src/XIVLauncher.Common.Unix/Compatibility/CompatibilityTools.cs b/src/XIVLauncher.Common.Unix/Compatibility/CompatibilityTools.cs index 77924015..afe43885 100644 --- a/src/XIVLauncher.Common.Unix/Compatibility/CompatibilityTools.cs +++ b/src/XIVLauncher.Common.Unix/Compatibility/CompatibilityTools.cs @@ -4,6 +4,8 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using System.Net.Http; +using XIVLauncher.Common.Util; using Serilog; #if FLATPAK @@ -63,30 +65,59 @@ public async Task EnsureTool(DirectoryInfo tempPath) { if (!File.Exists(Settings.WinePath)) { - Log.Information("Compatibility tool does not exist, downloading"); - await UnixHelpers.DownloadWine(wineDirectory, Settings.FolderName, Settings.DownloadUrl).ConfigureAwait(false); + Log.Information($"Compatibility tool does not exist, downloading {Settings.DownloadUrl}"); + await DownloadTool(wineDirectory, Settings.DownloadUrl).ConfigureAwait(false); } EnsurePrefix(); if (DxvkSettings.Enabled) - await UnixHelpers.InstallDxvk(Settings.Prefix, dxvkDirectory, DxvkSettings.FolderName, DxvkSettings.DownloadUrl).ConfigureAwait(false); + await InstallDxvk().ConfigureAwait(false); IsToolReady = true; } - // private async Task DownloadTool(DirectoryInfo tempPath) - // { - // using var client = new HttpClient(); - // var tempFilePath = Path.Combine(tempPath.FullName, $"{Guid.NewGuid()}"); + private async Task InstallDxvk() + { + var dxvkPath = Path.Combine(dxvkDirectory.FullName, DxvkSettings.FolderName, "x64"); + if (!Directory.Exists(dxvkPath)) + { + Log.Information($"DXVK does not exist, downloading {DxvkSettings.DownloadUrl}"); + await DownloadTool(dxvkDirectory, DxvkSettings.DownloadUrl).ConfigureAwait(false); + } + + var system32 = Path.Combine(Settings.Prefix.FullName, "drive_c", "windows", "system32"); + var files = Directory.GetFiles(dxvkPath); + + foreach (string fileName in files) + { + File.Copy(fileName, Path.Combine(system32, Path.GetFileName(fileName)), true); + } + + // 32-bit files for Directx9. + var dxvkPath32 = Path.Combine(dxvkDirectory.FullName, DxvkSettings.FolderName, "x32"); + var syswow64 = Path.Combine(Settings.Prefix.FullName, "drive_c", "windows", "syswow64"); + + if (Directory.Exists(dxvkPath32)) + { + files = Directory.GetFiles(dxvkPath32); - // await File.WriteAllBytesAsync(tempFilePath, await client.GetByteArrayAsync(Settings.DownloadUrl).ConfigureAwait(false)).ConfigureAwait(false); + foreach (string fileName in files) + { + File.Copy(fileName, Path.Combine(syswow64, Path.GetFileName(fileName)), true); + } + } + } - // PlatformHelpers.Untar(tempFilePath, this.wineDirectory.FullName); + private async Task DownloadTool(DirectoryInfo installDirectory, string downloadUrl) + { + using var client = new HttpClient(); + var tempPath = Path.GetTempFileName(); - // Log.Information("Compatibility tool successfully extracted to {Path}", this.wineDirectory.FullName); + File.WriteAllBytes(tempPath, await client.GetByteArrayAsync(downloadUrl)); + PlatformHelpers.Untar(tempPath, installDirectory.FullName); - // File.Delete(tempFilePath); - // } + File.Delete(tempPath); + } private void ResetPrefix() { @@ -146,16 +177,7 @@ private string MergeLDPreload(string a, string b) { a ??= ""; b ??= ""; - var alist = a.Split(':'); - var blist = b.Split(':'); - - var merged = alist.Union(blist); - - var ldpreload = ""; - foreach (var item in merged) - ldpreload += item + ":"; - - return ldpreload.TrimEnd(':'); + return (a.Trim(':') + ":" + b.Trim(':')).Trim(':'); } public void AddEnvironmentVar(string key, string value) @@ -283,7 +305,7 @@ private string GetProcessName(Int32 winePid) var output = wineDbg.StandardOutput.ReadToEnd(); var matchingLines = output.Split('\n', StringSplitOptions.RemoveEmptyEntries).Skip(1).Where( l => int.Parse(l.Substring(1, 8), System.Globalization.NumberStyles.HexNumber) == winePid); - var processNames = matchingLines.Select( l => l.Substring(20).Trim('\'')).ToArray(); + var processNames = matchingLines.Select(l => l.Substring(20).Trim('\'')).ToArray(); return processNames.FirstOrDefault(); } diff --git a/src/XIVLauncher.Common.Unix/Compatibility/DxvkSettings.cs b/src/XIVLauncher.Common.Unix/Compatibility/DxvkSettings.cs index cfd421db..78ad0388 100644 --- a/src/XIVLauncher.Common.Unix/Compatibility/DxvkSettings.cs +++ b/src/XIVLauncher.Common.Unix/Compatibility/DxvkSettings.cs @@ -1,5 +1,7 @@ using System.IO; using System.Collections.Generic; +using System.Text.RegularExpressions; +using System.Linq; using Serilog; namespace XIVLauncher.Common.Unix.Compatibility; @@ -38,9 +40,9 @@ public DxvkSettings(string folder, string url, string storageFolder, bool async, Environment.Add("DXVK_STATE_CACHE_PATH", Path.Combine(dxvkCachePath.FullName, folder)); if (dxvkHudEnabled) - Environment.Add("DXVK_HUD", UnixHelpers.DxvkHudStringIsValid(dxvkHudString) ? dxvkHudString : "1"); + Environment.Add("DXVK_HUD", DxvkHudStringIsValid(dxvkHudString) ? dxvkHudString : "1"); - if (mangoHudEnabled && UnixHelpers.MangoHudIsInstalled()) + if (mangoHudEnabled && MangoHudIsInstalled()) { Environment.Add("MANGOHUD", "1"); if (mangoHudCustomIsFile) @@ -56,4 +58,30 @@ public DxvkSettings(string folder, string url, string storageFolder, bool async, } } } + + public static bool DxvkHudStringIsValid(string customHud) + { + var ALLOWED_CHARS = "^[0-9a-zA-Z,=.]+$"; + var ALLOWED_WORDS = "^(?:devinfo|fps|frametimes|submissions|drawcalls|pipelines|descriptors|memory|gpuload|version|api|cs|compiler|samplers|scale=(?:[0-9])*(?:.(?:[0-9])+)?)$"; + + if (string.IsNullOrWhiteSpace(customHud)) return false; + if (customHud == "full") return true; + if (customHud == "1") return true; + if (!Regex.IsMatch(customHud, ALLOWED_CHARS)) return false; + + string[] hudvars = customHud.Split(","); + + return hudvars.All(hudvar => Regex.IsMatch(hudvar, ALLOWED_WORDS)); + } + + public static bool MangoHudIsInstalled() + { + var usrLib = Path.Combine("/", "usr", "lib", "mangohud", "libMangoHud.so"); // fedora uses this + var usrLib64 = Path.Combine("/", "usr", "lib64", "mangohud", "libMangoHud.so"); // arch and openSUSE use this + var flatpak = Path.Combine("/", "usr", "lib", "extensions", "vulkan", "MangoHud", "lib", "x86_64-linux-gnu", "libMangoHud.so"); + var debuntu = Path.Combine("/", "usr", "lib", "x86_64-linux-gnu", "mangohud", "libMangoHud.so"); + if (File.Exists(usrLib64) || File.Exists(usrLib) || File.Exists(flatpak) || File.Exists(debuntu)) + return true; + return false; + } } \ No newline at end of file diff --git a/src/XIVLauncher.Common.Unix/Compatibility/UnixHelpers.cs b/src/XIVLauncher.Common.Unix/Compatibility/UnixHelpers.cs deleted file mode 100644 index 61159d5d..00000000 --- a/src/XIVLauncher.Common.Unix/Compatibility/UnixHelpers.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System.IO; -using System.Net.Http; -using System.Threading.Tasks; -using System.Linq; -using System.Text.RegularExpressions; -using Serilog; -using XIVLauncher.Common.Util; - -namespace XIVLauncher.Common.Unix.Compatibility; - -public enum DistroPackage -{ - ubuntu, - - fedora, - - arch, - - none, -} - -public static class UnixHelpers -{ - public static async Task InstallDxvk(DirectoryInfo prefix, DirectoryInfo installDirectory, string folder, string downloadUrl) - { - var dxvkPath = Path.Combine(installDirectory.FullName, folder, "x64"); - if (!Directory.Exists(dxvkPath)) - { - Log.Information("DXVK does not exist, downloading"); - await DownloadTool(installDirectory, downloadUrl).ConfigureAwait(false); - } - - var system32 = Path.Combine(prefix.FullName, "drive_c", "windows", "system32"); - var files = Directory.GetFiles(dxvkPath); - - foreach (string fileName in files) - { - File.Copy(fileName, Path.Combine(system32, Path.GetFileName(fileName)), true); - } - - // 32-bit files for Directx9. - var dxvkPath32 = Path.Combine(installDirectory.FullName, folder, "x32"); - if (Directory.Exists(dxvkPath32)) - { - var syswow64 = Path.Combine(prefix.FullName, "drive_c", "windows", "syswow64"); - files = Directory.GetFiles(dxvkPath32); - - foreach (string fileName in files) - { - File.Copy(fileName, Path.Combine(syswow64, Path.GetFileName(fileName)), true); - } - } - } - - public static async Task DownloadWine(DirectoryInfo installDirectory, string folder, string downloadUrl) - { - if (!Directory.Exists(Path.Combine(installDirectory.FullName, folder))) - await DownloadTool(installDirectory, downloadUrl).ConfigureAwait(false); - } - - private static async Task DownloadTool(DirectoryInfo installDirectory, string downloadUrl) - { - using var client = new HttpClient(); - var tempPath = Path.GetTempFileName(); - - File.WriteAllBytes(tempPath, await client.GetByteArrayAsync(downloadUrl)); - PlatformHelpers.Untar(tempPath, installDirectory.FullName); - - File.Delete(tempPath); - } - public static bool DxvkHudStringIsValid(string customHud) - { - var ALLOWED_CHARS = "^[0-9a-zA-Z,=.]+$"; - var ALLOWED_WORDS = "^(?:devinfo|fps|frametimes|submissions|drawcalls|pipelines|descriptors|memory|gpuload|version|api|cs|compiler|samplers|scale=(?:[0-9])*(?:.(?:[0-9])+)?)$"; - - if (string.IsNullOrWhiteSpace(customHud)) return false; - if (customHud == "full") return true; - if (customHud == "1") return true; - if (!Regex.IsMatch(customHud, ALLOWED_CHARS)) return false; - - string[] hudvars = customHud.Split(","); - - return hudvars.All(hudvar => Regex.IsMatch(hudvar, ALLOWED_WORDS)); - } - - public static bool MangoHudIsInstalled() - { - var usrLib = Path.Combine("/usr", "lib", "mangohud", "libMangoHud.so"); // fedora uses this - var usrLib64 = Path.Combine("/usr", "lib64", "mangohud", "libMangoHud.so"); // arch and openSUSE use this - var flatpak = Path.Combine(new string[] { "/usr", "lib", "extensions", "vulkan", "MangoHud", "lib", "x86_64-linux-gnu", "libMangoHud.so"}); - var debuntu = Path.Combine(new string[] { "/usr", "lib", "x86_64-linux-gnu", "mangohud", "libMangoHud.so"}); - if (File.Exists(usrLib64) || File.Exists(usrLib) || File.Exists(flatpak) || File.Exists(debuntu)) - return true; - return false; - } -} \ No newline at end of file diff --git a/src/XIVLauncher.Common.Unix/Compatibility/WineSettings.cs b/src/XIVLauncher.Common.Unix/Compatibility/WineSettings.cs index 0dcac216..ed8d16df 100644 --- a/src/XIVLauncher.Common.Unix/Compatibility/WineSettings.cs +++ b/src/XIVLauncher.Common.Unix/Compatibility/WineSettings.cs @@ -26,13 +26,13 @@ public class WineSettings public DirectoryInfo Prefix { get; private set; } - public WineSettings(bool isManaged, string customBinPath, string managedFolder, string managedUrl, string storageFolder, string debugVars, FileInfo logFile, DirectoryInfo prefix, bool? esyncOn, bool? fsyncOn) + public WineSettings(bool isManaged, string customBinPath, string managedFolder, string managedUrl, DirectoryInfo storageFolder, string debugVars, FileInfo logFile, DirectoryInfo prefix, bool? esyncOn, bool? fsyncOn) { // storageFolder is the path to .xlcore folder. managedFolder is the foldername inside the tarball that will be downloaded from managedUrl. IsManaged = isManaged; FolderName = managedFolder; DownloadUrl = managedUrl; - BinPath = (isManaged) ? Path.Combine(new [] {storageFolder, "compatibilitytool", "wine", managedFolder, "bin"}) : customBinPath; + BinPath = (isManaged) ? Path.Combine(storageFolder.FullName, "compatibilitytool", "wine", managedFolder, "bin") : customBinPath; this.EsyncOn = (esyncOn ?? false) ? "1" : "0"; this.FsyncOn = (fsyncOn ?? false) ? "1" : "0";