Skip to content

Commit

Permalink
Updated with suggestions from marzent:
Browse files Browse the repository at this point in the history
* Moved download funcs to CompatibilityTools
* Moved hud funcs to DxvkSettings
* Simplified MergeLDPreload
* Cleaned up formatting, deleted a commented-out function
  • Loading branch information
rankynbass committed Aug 2, 2023
1 parent 7d21042 commit 3025d25
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 123 deletions.
68 changes: 45 additions & 23 deletions src/XIVLauncher.Common.Unix/Compatibility/CompatibilityTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand Down
32 changes: 30 additions & 2 deletions src/XIVLauncher.Common.Unix/Compatibility/DxvkSettings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
96 changes: 0 additions & 96 deletions src/XIVLauncher.Common.Unix/Compatibility/UnixHelpers.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/XIVLauncher.Common.Unix/Compatibility/WineSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 3025d25

Please sign in to comment.