Skip to content

Commit

Permalink
Get rid of file find functions
Browse files Browse the repository at this point in the history
  I didn't realize there was a way to limit recursion in GetFiles(), so I was using linux find.
  EnumerationOptions was the solution:
  * set RecurseSubdirectories = true
  * set MaxRecursionDepth
  • Loading branch information
rankynbass committed Nov 9, 2024
1 parent 8fa6c39 commit 8a2e854
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 75 deletions.
16 changes: 15 additions & 1 deletion src/XIVLauncher.Common.Unix/Compatibility/DxvkSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@ public static bool MangoHudIsInstalled()
{
if (mangoHudFound is null)
{
mangoHudFound = LinuxInfo.IsFileFound(LinuxInfo.LibraryPaths, "libMangoHud.so");
mangoHudFound = false;
var options = new EnumerationOptions();
options.RecurseSubdirectories = true;
options.MaxRecursionDepth = 5;
foreach (var path in LinuxInfo.LibraryPaths)
{
if (!Directory.Exists(path))
continue;

if (Directory.GetFiles(path, "libMangoHud.so", options).Length > 0)
{
mangoHudFound = true;
break;
}
}
}
return mangoHudFound ?? false;
}
Expand Down
83 changes: 9 additions & 74 deletions src/XIVLauncher.Common.Unix/LinuxInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static LinuxInfo()
LibraryPaths.Add(Path.Combine("/", "usr", "lib"));
LibraryPaths.Add(Path.Combine("/", "lib64"));
LibraryPaths.Add(Path.Combine("/", "lib"));
Console.WriteLine($"Distro = {Package}, Container = {Container}");
return;
}
var osRelease = File.ReadAllLines("/etc/os-release");
Expand Down Expand Up @@ -77,6 +78,7 @@ static LinuxInfo()
{
if (osInfo["ID"] == "ubuntu-core" && osInfo["HOME_URL"] == "https://snapcraft.io")
{
Console.WriteLine("Running snap container check");
LibraryPaths.Add(Path.Combine("/", "usr", "lib", "x86_64-linux-gnu"));
LibraryPaths.Add(Path.Combine("/", "usr", "lib"));
// nvidia host path, needed for dlss on steam snap. These paths look on the host distro.
Expand All @@ -99,24 +101,28 @@ static LinuxInfo()
Package = LinuxDistro.fedora;
break;
}
if (kvp.Value.ToLower().Contains("tumbleweed"))
else if (kvp.Value.ToLower().Contains("tumbleweed"))
{
LibraryPaths.Add(Path.Combine("/", "usr", "lib64"));
Package = LinuxDistro.fedora;
break;
}
if (kvp.Value.ToLower().Contains("arch"))
else if (kvp.Value.ToLower().Contains("arch"))
{
LibraryPaths.Add(Path.Combine("/", "usr", "lib"));
Package = LinuxDistro.arch;
break;
}
if (kvp.Value.ToLower().Contains("ubuntu") || kvp.Value.ToLower().Contains("debian"))
else if (kvp.Value.ToLower().Contains("ubuntu") || kvp.Value.ToLower().Contains("debian"))
{
LibraryPaths.Add(Path.Combine("/", "usr", "lib", "x86_64-linux-gnu"));
break;
}
}
if (LibraryPaths.Count == 0)
{
// Unknown distro, add extra library search paths
LibraryPaths.Add(Path.Combine("/", "usr", "lib", "x86_64-linux-gnu"));
LibraryPaths.Add(Path.Combine("/", "usr", "lib64"));
LibraryPaths.Add(Path.Combine("/", "usr", "lib"));
LibraryPaths.Add(Path.Combine("/", "lib64"));
Expand All @@ -136,75 +142,4 @@ static LinuxInfo()
LibraryPaths.Add(Path.Combine("/", "lib"));
}
}

public static List<string> FileFind(string searchPath, string file, bool followSymlinks = false)
{
if (!IsLinux)
return new List<string>();

var found = new List<string>();
var psi = new ProcessStartInfo("find");
psi.Arguments = $"{(followSymlinks ? "-L " : "")}{searchPath} -name \"{file}\"";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
var findCmd = new Process();
findCmd.StartInfo = psi;
try
{
findCmd.Start();
var output = findCmd.StandardOutput.ReadToEnd();
if (!string.IsNullOrWhiteSpace(output))
{
var outputArray = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (string foundfile in outputArray)
found.Add(foundfile);
}
}
catch (System.ComponentModel.Win32Exception ex)
{
Console.WriteLine("Error: could not execute \"find\" command. Is it installed?");
Console.WriteLine(ex.Message);
}
finally
{
findCmd.Dispose();
}
return found;
}

public static List<string> FileFind(IEnumerable<string> searchPaths, string file, bool followSymlinks = false)
{
if (!IsLinux)
return new List<string>();

var found = new List<string>();
foreach (string searchPath in searchPaths)
{
found.AddRange(FileFind(searchPath, file, followSymlinks));
}
return found;
}

public static bool IsFileFound(string searchPath, string file, bool followSymlinks = false)
{
if (!IsLinux)
return false;

if (FileFind(searchPath, file, followSymlinks).Count > 0)
return true;
return false;
}

public static bool IsFileFound(IEnumerable<string> searchPaths, string file, bool followSymlinks = false)
{
if (!IsLinux)
return false;

foreach (string searchPath in searchPaths)
{
if (IsFileFound(searchPath, file, followSymlinks))
return true;
}
return false;
}
}

0 comments on commit 8a2e854

Please sign in to comment.