using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Layout; using Avalonia.Media; using Avalonia.Platform.Storage; using Avalonia.Threading; using Path = System.IO.Path; namespace UltraParadise.Linux { internal static class SysInfo { private static string Read(string f) { try { return File.Exists(f) ? File.ReadAllText(f).Trim() : null; } catch (Exception) { return null; } } private static string Run(string exe, params string[] args) { try { ProcessStartInfo si = new(exe) { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false }; foreach (string a in args) si.ArgumentList.Add(a); using Process p = Process.Start(si); string o = p.StandardOutput.ReadToEnd(); if (!p.WaitForExit(4000)) { try { p.Kill(); } catch (Exception) { } return null; } return o; } catch (Exception) { return null; } } public static List<(string label, string value)> Collect(string game) { List<(string, string)> o = new(); o.Add(("Trainer", Program.Name + " v" + Program.Version + " (" + Program.Platform + " app, " + RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant() + ")")); if (OperatingSystem.IsWindows()) { o.Add(("System", RuntimeInformation.OSDescription + " (" + RuntimeInformation.OSArchitecture + ")")); } else if (Mac.On) { string name = Run("/usr/bin/sw_vers", "-productName")?.Trim(), ver = Run("/usr/bin/sw_vers", "-productVersion")?.Trim(); o.Add(("macOS", ((name ?? "macOS") + " " + (ver ?? "?")).Trim())); o.Add(("Kernel", Run("/usr/bin/uname", "-r")?.Trim() ?? RuntimeInformation.OSDescription)); string model = Sysctl("hw.model"); if (model != null) o.Add(("Model", model)); } else { string pretty = null; string rel = Read("/etc/os-release"); if (rel != null) foreach (string line in rel.Split('\n')) if (line.StartsWith("PRETTY_NAME=", StringComparison.Ordinal)) pretty = line[12..].Trim('"'); o.Add(("Distro", pretty ?? "Unknown Linux")); o.Add(("Kernel", Read("/proc/sys/kernel/osrelease") ?? RuntimeInformation.OSDescription)); string vendor = Read("/sys/devices/virtual/dmi/id/sys_vendor"), product = Read("/sys/devices/virtual/dmi/id/product_name"), board = Read("/sys/devices/virtual/dmi/id/board_vendor"); if (board == "Valve" || vendor == "Valve") o.Add(("Device", "Steam Deck (" + (product == "Galileo" ? "OLED" : product == "Jupiter" ? "LCD" : product ?? "?") + ")")); else if (vendor != null || product != null) o.Add(("Device", ((vendor ?? "") + " " + (product ?? "")).Trim())); string desk = Environment.GetEnvironmentVariable("XDG_CURRENT_DESKTOP"), sess = Environment.GetEnvironmentVariable("XDG_SESSION_TYPE"); if (desk != null || sess != null) o.Add(("Desktop", ((desk ?? "?") + " (" + (sess ?? "?") + ")"))); } o.Add(("CPU", Cpu())); o.Add(("RAM", Ram())); foreach (string g in Gpus()) o.Add(("GPU", g)); if (game != null) { o.Add(("BepInEx", Game.BepInstalled(game) ? Game.BepVersion + (Game.BepRan(game) ? "" : " (hasn't run yet)") : "not installed")); o.Add(("Plugin", Game.PluginState(game))); o.Add((Mac.On ? "Wine setting" : "Launch option", Game.LaunchOptionState())); if (Mac.On) o.Add(("Wine", Mac.WineVersion(game))); string proton = Mac.On ? null : Proton(game); if (proton != null) o.Add(("Proton", proton)); List mods = Mods.All.Where(m => Mods.Installed(game, m)).Select(m => m.Name).ToList(); if (Game.OtherCheats(game) != null && !mods.Contains("CU Cheats - Ultra Paradise")) mods.Add("CU Cheats"); o.Add(("Other mods", mods.Count == 0 ? "none of casualexpie's" : string.Join(", ", mods))); } else o.Add(("Game", "not found")); return o; } private static string Sysctl(string key) { string v = Run("/usr/sbin/sysctl", "-n", key)?.Trim(); return string.IsNullOrEmpty(v) ? null : v; } private static string Cpu() { if (OperatingSystem.IsWindows()) return (Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER") ?? "?") + ", " + Environment.ProcessorCount + " threads"; if (Mac.On) return (Sysctl("machdep.cpu.brand_string") ?? "?") + ", " + Environment.ProcessorCount + " cores"; string c = Read("/proc/cpuinfo"); string model = c?.Split('\n').FirstOrDefault(l => l.StartsWith("model name", StringComparison.Ordinal))?.Split(':', 2)[1].Trim(); return (model ?? "?") + ", " + Environment.ProcessorCount + " threads"; } private static string Ram() { long bytes = 0; if (OperatingSystem.IsWindows()) bytes = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes; else if (Mac.On) long.TryParse(Sysctl("hw.memsize"), out bytes); else { string m = Read("/proc/meminfo"); string line = m?.Split('\n').FirstOrDefault(l => l.StartsWith("MemTotal:", StringComparison.Ordinal)); if (line != null && long.TryParse(new string(line.Where(char.IsDigit).ToArray()), out long kb)) bytes = kb * 1024; } return bytes > 0 ? (bytes / 1073741824.0).ToString("0.0", CultureInfo.InvariantCulture) + " GB" : "?"; } private static List Gpus() { List o = new(); if (OperatingSystem.IsWindows()) { o.Add("(shown on Linux)"); return o; } if (Mac.On) { string sp = Run("/usr/sbin/system_profiler", "SPDisplaysDataType"); if (sp != null) foreach (string line in sp.Split('\n')) { string t = line.Trim(); if (t.StartsWith("Chipset Model:", StringComparison.Ordinal)) o.Add(t[14..].Trim()); else if (t.StartsWith("Total Number of Cores:", StringComparison.Ordinal) && o.Count > 0) o[^1] += " (" + t[22..].Trim() + " GPU cores)"; } if (o.Count == 0) o.Add("?"); return o; } string l = Run("lspci"); if (l != null) foreach (string line in l.Split('\n')) if (line.Contains("VGA compatible", StringComparison.Ordinal) || line.Contains("3D controller", StringComparison.Ordinal) || line.Contains("Display controller", StringComparison.Ordinal)) { int i = line.IndexOf(": ", StringComparison.Ordinal); o.Add(i > 0 ? line[(i + 2)..].Trim() : line.Trim()); } if (o.Count == 0) { try { foreach (string card in Directory.GetDirectories("/sys/class/drm", "card?")) { string v = Read(Path.Combine(card, "device", "vendor")), d = Read(Path.Combine(card, "device", "device")); if (v == null) continue; string name = v == "0x10de" ? "NVIDIA" : v == "0x1002" ? "AMD" : v == "0x8086" ? "Intel" : v; o.Add(name + " (device " + d + ")"); } } catch (Exception) { } } string nv = Read("/proc/driver/nvidia/version"); if (nv != null) o.Add("NVIDIA driver: " + nv.Split('\n')[0]); if (o.Count == 0) o.Add("?"); return o; } private static string Proton(string game) { try { string lib = Directory.GetParent(game)?.Parent?.FullName; if (lib == null) return null; string cfg = Path.Combine(lib, "compatdata", Game.AppId, "config_info"); if (File.Exists(cfg)) return File.ReadLines(cfg).FirstOrDefault()?.Trim(); string ver = Path.Combine(lib, "compatdata", Game.AppId, "version"); if (File.Exists(ver)) return File.ReadAllText(ver).Trim(); } catch (Exception) { } return null; } public static string Scrub(string s) { string home = Dirs.Home; if (!string.IsNullOrEmpty(home) && home.Length > 3) s = s.Replace(home, "~"); string user = Environment.UserName; if (!string.IsNullOrEmpty(user) && user.Length > 2) s = s.Replace("/" + user + "/", "//").Replace("\\" + user + "\\", "\\\\"); return s; } } internal sealed partial class MainWindow { private readonly TextBox logBox = new() { IsReadOnly = true, AcceptsReturn = true, TextWrapping = TextWrapping.NoWrap, FontFamily = new FontFamily("monospace"), FontSize = 12, Height = 420, }; private readonly StackPanel sysRows = new() { Spacing = 4 }; private readonly TextBlock logState = T("", 12, Ui.B(Ui.Dim), false, true); private readonly List logLines = new(); private string logFile; private long logPos; private string logPartial = ""; private bool logErrorsOnly, logFollow = true; private List<(string, string)> sys; private DateTime sysAt; private Control ReportPage() { Grid g = new() { ColumnDefinitions = new ColumnDefinitions("2*,*") }; StackPanel left = new() { Margin = new Thickness(0, 0, 7, 0) }; Border ls = Section("GAME LOG (LIVE)", out StackPanel lb); lb.Children.Add(logState); lb.Children.Add(logBox); WrapPanel lw = new(); ToggleButton err = Switch("Only Warnings and Errors", "Hides the normal lines.", false, on => { logErrorsOnly = on; ShowLog(); }); err.HorizontalAlignment = HorizontalAlignment.Left; err.Margin = new Thickness(0, 0, 6, 0); lw.Children.Add(err); ToggleButton follow = Switch("Follow", "Keeps the newest lines in view.", true, on => logFollow = on); follow.HorizontalAlignment = HorizontalAlignment.Left; follow.Margin = new Thickness(0, 0, 6, 0); lw.Children.Add(follow); lw.Children.Add(Btn("Open Log Folder", () => { if (logFile != null) Game.Open(Path.GetDirectoryName(logFile)); })); lb.Children.Add(lw); left.Children.Add(ls); g.Children.Add(left); StackPanel right = new() { Margin = new Thickness(7, 0, 0, 0) }; Border ss = Section("YOUR SYSTEM", out StackPanel sb); sb.Children.Add(sysRows); right.Children.Add(ss); Border rs = Section("REPORT A BUG", out StackPanel rb); rb.Children.Add(T("1. Copy the report (your system and the last 300 log lines).\n2. Open the Nexus Bugs page and paste it in, with what you did and what happened.\n\nYour home folder and user name are hidden in the report.", 13, Ui.B(Ui.Dim), false, true)); WrapPanel rw = new(); foreach (Button b in new[] { Btn("Copy Report", async () => { if (Clipboard != null) await Clipboard.SetTextAsync(BuildReport()); Message("Report copied. Paste it on the Nexus Bugs page.", false); }, true), Btn("Save Report...", () => _ = SaveReport()), Btn("Open Nexus Bugs Page", () => Game.Open(Nexus.PageUrl + "?tab=bugs")), }) { b.Margin = new Thickness(0, 4, 6, 0); rw.Children.Add(b); } rb.Children.Add(rw); right.Children.Add(rs); Grid.SetColumn(right, 1); g.Children.Add(right); return Scroll(g); } private void RefreshReport() { if (sys == null || DateTime.UtcNow - sysAt > TimeSpan.FromSeconds(30)) { sysAt = DateTime.UtcNow; string gd = game; Task.Run(() => SysInfo.Collect(gd)).ContinueWith(t => { if (t.IsFaulted) return; Dispatcher.UIThread.Post(() => { sys = t.Result; sysRows.Children.Clear(); foreach (var (label, value) in sys) { Grid r = new() { ColumnDefinitions = new ColumnDefinitions("110,*") }; r.Children.Add(T(label, 12, Ui.B(Ui.Dim))); TextBlock v = T(SysInfo.Scrub(value), 12, Ui.B(Ui.Text), false, true); Grid.SetColumn(v, 1); r.Children.Add(v); sysRows.Children.Add(r); } }); }); } PollLog(); } private void PollLog() { string f = game != null ? Path.Combine(game, "BepInEx", "LogOutput.log") : null; if (f != logFile) { logFile = f; logPos = 0; logPartial = ""; logLines.Clear(); ShowLog(); } if (f == null) { logState.Text = "Set the game folder in Game Setup to see the log."; return; } if (!File.Exists(f)) { logState.Text = "No log yet. It appears once the game has started with BepInEx."; return; } try { using FileStream fs = new(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); if (fs.Length < logPos) { logPos = 0; logPartial = ""; logLines.Clear(); } if (logPos == 0 && fs.Length > 2 * 1024 * 1024) logPos = fs.Length - 2 * 1024 * 1024; if (fs.Length == logPos) { logState.Text = (Game.GameRunning() ? "Live. " : "The game isn't running; this is its last log. ") + logLines.Count + " lines."; return; } fs.Position = logPos; byte[] buf = new byte[Math.Min(fs.Length - logPos, 4 * 1024 * 1024)]; int n = fs.Read(buf, 0, buf.Length); logPos += n; string text = logPartial + Encoding.UTF8.GetString(buf, 0, n); int last = text.LastIndexOf('\n'); if (last < 0) { logPartial = text; return; } logPartial = text[(last + 1)..]; foreach (string line in text[..last].Split('\n')) logLines.Add(line.TrimEnd('\r')); if (logLines.Count > 3000) logLines.RemoveRange(0, logLines.Count - 3000); logState.Text = (Game.GameRunning() ? "Live. " : "The game isn't running; this is its last log. ") + logLines.Count + " lines."; ShowLog(); } catch (Exception e) { logState.Text = "Couldn't read the log: " + e.Message; } } private static bool IsProblem(string l) => l.StartsWith("[Warning", StringComparison.Ordinal) || l.StartsWith("[Error", StringComparison.Ordinal) || l.StartsWith("[Fatal", StringComparison.Ordinal) || l.Contains("Exception", StringComparison.Ordinal); private void ShowLog() { IEnumerable lines = logErrorsOnly ? logLines.Where(IsProblem) : logLines; logBox.Text = SysInfo.Scrub(string.Join("\n", lines.TakeLast(1500))); if (logFollow) logBox.CaretIndex = logBox.Text?.Length ?? 0; } private string BuildReport() { StringBuilder sb = new(); sb.AppendLine("=== Ultra Paradise CU:Trainer bug report ==="); sb.AppendLine("Made " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture) + " UTC"); sb.AppendLine("Attached to game: " + (link.IsConnected ? "yes" + (pluginVersion != null ? " (plugin v" + pluginVersion + ")" : "") : "no")); foreach (var (label, value) in sys ?? SysInfo.Collect(game)) sb.AppendLine(label + ": " + value); sb.AppendLine(); sb.AppendLine("=== Last log lines (BepInEx/LogOutput.log) ==="); foreach (string l in logLines.TakeLast(300)) sb.AppendLine(l); return SysInfo.Scrub(sb.ToString()); } private async Task SaveReport() { IStorageFile f = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { Title = "Save the bug report", SuggestedFileName = "UltraParadise-bug-report.txt", DefaultExtension = "txt", }); string p = f?.TryGetLocalPath(); if (p == null) return; try { File.WriteAllText(p, BuildReport()); Message("Report saved.", false); } catch (Exception e) { Message("Couldn't save it: " + e.Message, true); } } } }