using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace UltraParadise.Linux { internal static class Mac { public static bool On => OperatingSystem.IsMacOS(); public const string OverrideKey = "\"winhttp\"=\"native,builtin\""; public static string AppSupport => Path.Combine(Dirs.Home, "Library", "Application Support"); public static List<(string prefix, string kind)> Bottles() { List<(string, string)> o = new(); HashSet seen = new(StringComparer.Ordinal); void Add(string p, string kind) { try { if (Directory.Exists(Path.Combine(p, "drive_c")) && seen.Add(Path.GetFullPath(p))) o.Add((p, kind)); } catch (Exception) { } } void Each(string dir, string kind, Func map) { try { if (!Directory.Exists(dir)) return; foreach (string d in Directory.GetDirectories(dir)) Add(map(d), kind); } catch (Exception) { } } string h = Dirs.Home; Each(Path.Combine(AppSupport, "CrossOver", "Bottles"), "CrossOver", d => d); Each(Path.Combine(h, "Library", "Containers", "com.isaacmarovitz.Whisky", "Bottles"), "Whisky", d => d); Each(Path.Combine(h, "Library", "Containers", "com.isaacmarovitz.Whisky", "Data", "Library", "Application Support", "com.isaacmarovitz.Whisky", "Bottles"), "Whisky", d => d); Each(Path.Combine(h, "Games", "Heroic", "Prefixes"), "Heroic", d => d); Each(Path.Combine(h, "Games", "Heroic", "Prefixes", "default"), "Heroic", d => d); foreach (string apps in new[] { Path.Combine(h, "Applications"), Path.Combine(h, "Applications", "Wineskin"), Path.Combine(h, "Applications", "Sikarugir"), "/Applications/Wineskin" }) Each(apps, "Wineskin", d => Path.Combine(d, "Contents", "SharedSupport", "prefix")); Add(Path.Combine(h, ".wine"), "Wine"); return o; } public static string BottleOf(string game) { try { DirectoryInfo d = new(game); while (d != null) { if (Directory.Exists(Path.Combine(d.FullName, "drive_c")) && File.Exists(Path.Combine(d.FullName, "user.reg"))) return d.FullName; d = d.Parent; } } catch (Exception) { } return null; } public static string KindOf(string bottle) { if (bottle == null) return null; if (bottle.Contains("/CrossOver/Bottles/", StringComparison.Ordinal)) return "CrossOver"; if (bottle.Contains("com.isaacmarovitz.Whisky", StringComparison.Ordinal)) return "Whisky"; if (bottle.Contains("/Heroic/", StringComparison.Ordinal)) return "Heroic"; if (bottle.Contains("/SharedSupport/prefix", StringComparison.Ordinal)) return "Wineskin"; return "Wine"; } public static string FromWindows(string bottle, string winPath) { if (string.IsNullOrEmpty(winPath) || winPath.Length < 2 || winPath[1] != ':') return null; char drive = char.ToLowerInvariant(winPath[0]); string rest = winPath[2..].Replace("\\\\", "\\").Replace('\\', '/').TrimStart('/'); string root = drive == 'c' ? Path.Combine(bottle, "drive_c") : Path.Combine(bottle, "dosdevices", drive + ":"); try { FileSystemInfo t = new DirectoryInfo(root).ResolveLinkTarget(true); if (t != null) root = t.FullName; } catch (Exception) { } return Path.Combine(root, rest); } public static List Libraries() { List libs = new(); HashSet seen = new(StringComparer.Ordinal); foreach (var (b, _) in Bottles()) { foreach (string s in new[] { Path.Combine(b, "drive_c", "Program Files (x86)", "Steam"), Path.Combine(b, "drive_c", "Program Files", "Steam") }) { if (!Directory.Exists(s)) continue; if (seen.Add(s)) libs.Add(s); try { string f = Path.Combine(s, "steamapps", "libraryfolders.vdf"); if (!File.Exists(f)) continue; VNode v = Vdf.Parse(File.ReadAllText(f)); VNode lf = v.Child("libraryfolders") ?? v; foreach (VNode n in lf.Kids ?? new List()) { string p = FromWindows(b, n.Kids != null ? n.Child("path")?.Value : n.Value); if (p != null && Directory.Exists(p) && seen.Add(p)) libs.Add(p); } } catch (Exception) { } } } return libs; } public static string Find() { foreach (string lib in Libraries()) { try { string acf = Path.Combine(lib, "steamapps", "appmanifest_" + Game.AppId + ".acf"); if (File.Exists(acf)) { string dir = Vdf.Parse(File.ReadAllText(acf)).Child("AppState")?.Child("installdir")?.Value; if (!string.IsNullOrEmpty(dir)) { string d = Path.Combine(lib, "steamapps", "common", dir); if (Game.IsGameDir(d)) return d; } } string guess = Path.Combine(lib, "steamapps", "common", "Casualties Unknown"); if (Game.IsGameDir(guess)) return guess; } catch (Exception) { } } return null; } public static string Ps() { try { ProcessStartInfo si = new("/bin/ps") { RedirectStandardOutput = true, UseShellExecute = false }; si.ArgumentList.Add("-axo"); si.ArgumentList.Add("command="); using Process p = Process.Start(si); string o = p.StandardOutput.ReadToEnd(); p.WaitForExit(3000); return o; } catch (Exception) { return ""; } } public static bool GameRunning() { foreach (string l in Ps().Split('\n')) if (l.Contains(Game.Exe, StringComparison.OrdinalIgnoreCase) && !l.Contains("UltraParadise", StringComparison.Ordinal)) return true; return false; } public static bool WineRunning() { foreach (string l in Ps().Split('\n')) if (l.Contains("wineserver", StringComparison.Ordinal)) return true; return false; } public static string OverrideState(string game) { string b = BottleOf(game); if (b == null) return "unknown"; try { string reg = File.ReadAllText(Path.Combine(b, "user.reg")); int s = SectionStart(reg); if (s < 0) return "unset"; int e = reg.IndexOf("\n[", s + 1, StringComparison.Ordinal); string sec = e < 0 ? reg[s..] : reg[s..e]; return Regex.IsMatch(sec, "^\"\\*?winhttp\"=\"native(,builtin)?\"", RegexOptions.Multiline | RegexOptions.IgnoreCase) ? "set" : "unset"; } catch (Exception) { return "unknown"; } } private static int SectionStart(string reg) { Match m = Regex.Match(reg, "^\\[Software\\\\\\\\Wine\\\\\\\\DllOverrides\\][^\\n]*$", RegexOptions.Multiline | RegexOptions.IgnoreCase); return m.Success ? m.Index : -1; } public static string SetOverride(string game) { string b = BottleOf(game); if (b == null) return "couldn't find the Wine bottle the game is in"; if (WineRunning()) return "something is still running in Wine. Quit Steam and anything else in " + KindOf(b) + " (or the whole app), then try again"; string f = Path.Combine(b, "user.reg"); try { string reg = File.ReadAllText(f).Replace("\r\n", "\n"); if (OverrideState(game) == "set") return null; int s = SectionStart(reg); if (s >= 0) { int eol = reg.IndexOf('\n', s); if (eol < 0) { reg += "\n"; eol = reg.Length - 1; } int at = eol + 1; if (reg.Length > at && reg[at] == '#') { int e2 = reg.IndexOf('\n', at); at = e2 < 0 ? reg.Length : e2 + 1; } string body = reg[at..]; Match old = Regex.Match(body, "^\"\\*?winhttp\"=\"[^\"\\n]*\"\\n?", RegexOptions.Multiline | RegexOptions.IgnoreCase); int end = reg.IndexOf("\n[", at, StringComparison.Ordinal); if (old.Success && (end < 0 || at + old.Index < end)) reg = reg.Remove(at + old.Index, old.Length); reg = reg.Insert(at, OverrideKey + "\n"); } else { if (!reg.EndsWith("\n\n", StringComparison.Ordinal)) reg += reg.EndsWith('\n') ? "\n" : "\n\n"; reg += "[Software\\\\Wine\\\\DllOverrides] " + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + "\n" + OverrideKey + "\n"; } string bak = f + ".ultraparadise.bak"; if (!File.Exists(bak)) File.Copy(f, bak); string tmp = f + ".tmp"; File.WriteAllText(tmp, reg, new UTF8Encoding(false)); File.Move(tmp, f, true); return OverrideState(game) == "set" ? null : "the setting didn't stick"; } catch (Exception e) { return e.Message; } } private static string CrossOverWine() { foreach (string app in new[] { "/Applications/CrossOver.app", Path.Combine(Dirs.Home, "Applications", "CrossOver.app") }) { string w = Path.Combine(app, "Contents", "SharedSupport", "CrossOver", "bin", "wine"); if (File.Exists(w)) return w; } return null; } public static async Task Start(string game) { string b = BottleOf(game); if (b == null) return "couldn't find the Wine bottle the game is in. Start it from CrossOver, Whisky or your Wine app instead."; string kind = KindOf(b); string steam = new[] { Path.Combine(b, "drive_c", "Program Files (x86)", "Steam", "steam.exe"), Path.Combine(b, "drive_c", "Program Files", "Steam", "steam.exe") }.FirstOrDefault(File.Exists); if (kind == "CrossOver") { string w = CrossOverWine(); if (w != null) { try { ProcessStartInfo si = new(w) { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true }; si.ArgumentList.Add("--bottle"); si.ArgumentList.Add(Path.GetFileName(b)); if (steam != null) { si.ArgumentList.Add(steam); si.ArgumentList.Add("-applaunch"); si.ArgumentList.Add(Game.AppId); } else si.ArgumentList.Add(Path.Combine(game, Game.Exe)); Process p = Process.Start(si); if (p != null) { _ = p.StandardOutput.ReadToEndAsync(); _ = p.StandardError.ReadToEndAsync(); await Task.Delay(300); return null; } } catch (Exception) { } } } string open = kind switch { "CrossOver" => "CrossOver", "Whisky" => "Whisky", _ => null }; if (open != null) { try { ProcessStartInfo si = new("/usr/bin/open") { UseShellExecute = false }; si.ArgumentList.Add("-a"); si.ArgumentList.Add(open); Process.Start(si); } catch (Exception) { } return open + " is opening. Start Casualties: Unknown from there (through Steam in that bottle)."; } return "start Casualties: Unknown from your Wine app (" + kind + ")."; } public static string WineVersion(string game) { string kind = KindOf(BottleOf(game)); string app = kind switch { "CrossOver" => "/Applications/CrossOver.app", "Whisky" => "/Applications/Whisky.app", _ => null }; if (app == null) return kind; try { string plist = File.ReadAllText(Path.Combine(app, "Contents", "Info.plist")); Match m = Regex.Match(plist, "CFBundleShortVersionString\\s*([^<]{1,40})"); return kind + (m.Success ? " " + m.Groups[1].Value : ""); } catch (Exception) { return kind; } } } }