cu-trainer/Ultra Paradise Linux/app/Debug.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Collections.ObjectModel; |
| 4 | using System.IO; |
| 5 | using System.Linq; |
| 6 | using System.Text; |
| 7 | using System.Threading.Tasks; |
| 8 | using Avalonia; |
| 9 | using Avalonia.Controls; |
| 10 | using Avalonia.Controls.Documents; |
| 11 | using Avalonia.Controls.Primitives; |
| 12 | using Avalonia.Controls.Templates; |
| 13 | using Avalonia.Input; |
| 14 | using Avalonia.Layout; |
| 15 | using Avalonia.Media; |
| 16 | using Avalonia.Platform; |
| 17 | using Avalonia.Platform.Storage; |
| 18 | using Avalonia.Styling; |
| 19 | using Path = System.IO.Path; |
| 20 | |
| 21 | namespace UltraParadise.Linux |
| 22 | { |
| 23 | internal static class Monitors |
| 24 | { |
| 25 | public static int Count(TopLevel t) |
| 26 | { |
| 27 | try { return t?.Screens?.All.Count ?? 1; } catch (Exception) { return 1; } |
| 28 | } |
| 29 | |
| 30 | public static string Name(TopLevel t, Screen s) |
| 31 | { |
| 32 | int i = 0; |
| 33 | try { i = t.Screens.All.ToList().IndexOf(s); } catch (Exception) { } |
| 34 | return "Screen " + (i + 1) + " (" + s.Bounds.Width + "x" + s.Bounds.Height + (s.IsPrimary ? ", main" : "") + ")"; |
| 35 | } |
| 36 | |
| 37 | public static Screen Spare(Window w) |
| 38 | { |
| 39 | IReadOnlyList<Screen> all = w.Screens.All; |
| 40 | if (all.Count < 2) return null; |
| 41 | Screen game = all.FirstOrDefault(s => s.IsPrimary) ?? all[0]; |
| 42 | Screen mine = w.Screens.ScreenFromWindow(w); |
| 43 | Screen best = null; |
| 44 | foreach (Screen s in all) |
| 45 | { |
| 46 | if (s.Equals(game)) continue; |
| 47 | if (best == null || (long)s.Bounds.Width * s.Bounds.Height > (long)best.Bounds.Width * best.Bounds.Height) best = s; |
| 48 | } |
| 49 | if (best != null && mine != null && best.Equals(mine)) |
| 50 | { |
| 51 | int i = all.ToList().IndexOf(mine); |
| 52 | Screen next = all[(i + 1) % all.Count]; |
| 53 | if (!next.Equals(game)) best = next; |
| 54 | } |
| 55 | return best; |
| 56 | } |
| 57 | |
| 58 | public static void MoveTo(Window w, Screen s, bool fill) |
| 59 | { |
| 60 | if (w == null || s == null) return; |
| 61 | bool max = w.WindowState == WindowState.Maximized; |
| 62 | if (w.WindowState != WindowState.Normal) w.WindowState = WindowState.Normal; |
| 63 | PixelRect wa = s.WorkingArea; |
| 64 | double sc = s.Scaling <= 0 ? 1 : s.Scaling; |
| 65 | double aw = wa.Width / sc, ah = wa.Height / sc; |
| 66 | double ww = Math.Min(double.IsNaN(w.Width) ? w.Bounds.Width : w.Width, aw), hh = Math.Min(double.IsNaN(w.Height) ? w.Bounds.Height : w.Height, ah); |
| 67 | if (fill) { ww = Math.Min(aw, Math.Max(ww, aw * 0.8)); hh = Math.Min(ah, Math.Max(hh, ah * 0.8)); } |
| 68 | w.Width = ww; |
| 69 | w.Height = hh; |
| 70 | w.Position = new PixelPoint(wa.X + (int)((wa.Width - ww * sc) / 2), wa.Y + (int)((wa.Height - hh * sc) / 2)); |
| 71 | if (max) w.WindowState = WindowState.Maximized; |
| 72 | } |
| 73 | |
| 74 | public static string Advice(TopLevel t) |
| 75 | { |
| 76 | int n = Count(t); |
| 77 | if (n < 2) return "Only one screen found. A second screen is recommended, see below."; |
| 78 | return n + " screens found. The trainer treats your main screen as the game's screen."; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | internal sealed class DLine |
| 83 | { |
| 84 | public string Time, Kind, Text; |
| 85 | public bool First; |
| 86 | } |
| 87 | |
| 88 | internal sealed class DebugWindow : Window |
| 89 | { |
| 90 | public const char NL = '\u001e', REC = '\u001d', SEP = '\u001c'; |
| 91 | |
| 92 | public Action<bool> Live; |
| 93 | public Action<string, bool> Say; |
| 94 | public bool Quitting; |
| 95 | |
| 96 | private readonly ObservableCollection<DLine> shown = new(); |
| 97 | private readonly List<DLine> all = new(); |
| 98 | private readonly ListBox log = new(); |
| 99 | private readonly StackPanel statusPanel = new(); |
| 100 | private readonly List<SelectableTextBlock> statusRows = new(); |
| 101 | private readonly TextBlock state = new() { FontSize = 13, FontWeight = FontWeight.SemiBold, VerticalAlignment = VerticalAlignment.Center, TextTrimming = TextTrimming.CharacterEllipsis }; |
| 102 | private readonly TextBox find = new() { Width = 220, Watermark = "Find", VerticalAlignment = VerticalAlignment.Center }; |
| 103 | private readonly Dictionary<string, CheckBox> filters = new(); |
| 104 | private Button pauseBtn, pinBtn, otherBtn; |
| 105 | private string snap = "", sys = ""; |
| 106 | private bool paused, linked, dirty; |
| 107 | private DateTime lastSnap = DateTime.MinValue; |
| 108 | private StreamWriter file; |
| 109 | private readonly FontFamily mono = new("Cascadia Mono, DejaVu Sans Mono, Liberation Mono, Noto Sans Mono, monospace"); |
| 110 | private readonly IBrush bText = Ui.B(Ui.Text), bDim = Ui.B(Ui.Dim), bOrange = Ui.B(Ui.Orange), bGood = Ui.B(Ui.Good), bWarn = Ui.B(Ui.Warn), bBad = Ui.B(Ui.Bad), bBright = Ui.B(Ui.Bright), bCard = Ui.B(Ui.Card), bSunk = Ui.B(Ui.Sunk), bLine = Ui.B(Ui.Line); |
| 111 | |
| 112 | private static readonly string[] FilterNames = { "Game log", "Warnings", "Errors", "Events", "Trainer", "Commands" }; |
| 113 | private static readonly string[][] FilterKinds = |
| 114 | { |
| 115 | new[] { "GAME", "SCENE", "SYSTEM", "BEPINEX" }, new[] { "WARNING" }, new[] { "ERROR" }, new[] { "EVENT", "LIMB", "GOOD", "ALERT" }, new[] { "TRAINER" }, new[] { "COMMAND" } |
| 116 | }; |
| 117 | |
| 118 | public static string LogPath => Path.Combine(Dirs.Cache, "debug.log"); |
| 119 | |
| 120 | public DebugWindow() |
| 121 | { |
| 122 | Title = "Ultra Paradise - Debug"; |
| 123 | Width = 1100; |
| 124 | Height = 680; |
| 125 | MinWidth = 640; |
| 126 | MinHeight = 380; |
| 127 | Background = Ui.B(Ui.Back); |
| 128 | FontFamily = Ui.Font; |
| 129 | WindowStartupLocation = WindowStartupLocation.CenterScreen; |
| 130 | Avalonia.Media.Imaging.Bitmap icon = Program.Image("icon.png"); |
| 131 | if (icon != null) Icon = new WindowIcon(icon); |
| 132 | |
| 133 | Grid root = new() { RowDefinitions = new RowDefinitions("Auto,Auto,*") }; |
| 134 | |
| 135 | Grid top = new() { ColumnDefinitions = new ColumnDefinitions("*,Auto"), Background = Ui.B(Ui.Side), Margin = new Thickness(0) }; |
| 136 | state.Margin = new Thickness(14, 0, 10, 0); |
| 137 | top.Children.Add(state); |
| 138 | StackPanel btns = new() { Orientation = Orientation.Horizontal, Spacing = 6, Margin = new Thickness(0, 8, 10, 8) }; |
| 139 | otherBtn = Btn("Other Screen", MoveToOther); |
| 140 | pinBtn = Btn("Pin on Top", () => { Topmost = !Topmost; Paint(pinBtn, Topmost); }); |
| 141 | pauseBtn = Btn("Pause", () => SetPaused(!paused)); |
| 142 | btns.Children.Add(otherBtn); |
| 143 | btns.Children.Add(pinBtn); |
| 144 | btns.Children.Add(pauseBtn); |
| 145 | btns.Children.Add(Btn("Clear", () => { all.Clear(); shown.Clear(); })); |
| 146 | btns.Children.Add(Btn("Copy", () => _ = CopyAll())); |
| 147 | btns.Children.Add(Btn("Save...", () => _ = SaveLog())); |
| 148 | btns.Children.Add(Btn("Log Folder", () => Game.Open(Path.GetDirectoryName(LogPath)))); |
| 149 | Grid.SetColumn(btns, 1); |
| 150 | top.Children.Add(btns); |
| 151 | root.Children.Add(top); |
| 152 | |
| 153 | Grid bar = new() { ColumnDefinitions = new ColumnDefinitions("*,Auto"), Background = Ui.B(Ui.Card) }; |
| 154 | WrapPanel fl = new() { Margin = new Thickness(12, 4, 0, 4) }; |
| 155 | foreach (string n in FilterNames) |
| 156 | { |
| 157 | CheckBox c = new() { Content = n, IsChecked = true, Margin = new Thickness(0, 0, 14, 0) }; |
| 158 | c.IsCheckedChanged += (_, _) => Refilter(); |
| 159 | filters[n] = c; |
| 160 | fl.Children.Add(c); |
| 161 | } |
| 162 | bar.Children.Add(fl); |
| 163 | find.Margin = new Thickness(0, 4, 12, 4); |
| 164 | find.TextChanged += (_, _) => Refilter(); |
| 165 | Grid.SetColumn(find, 1); |
| 166 | bar.Children.Add(find); |
| 167 | Grid.SetRow(bar, 1); |
| 168 | root.Children.Add(bar); |
| 169 | |
| 170 | Grid body = new() { ColumnDefinitions = new ColumnDefinitions("46*,6,54*") }; |
| 171 | body.Children.Add(Pane("LIVE STATUS", new ScrollViewer { Content = statusPanel, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, Padding = new Thickness(10, 4) })); |
| 172 | GridSplitter sp = new() { Background = Ui.B(Ui.Line), ResizeDirection = GridResizeDirection.Columns }; |
| 173 | Grid.SetColumn(sp, 1); |
| 174 | body.Children.Add(sp); |
| 175 | log.ItemsSource = shown; |
| 176 | log.SelectionMode = SelectionMode.Multiple; |
| 177 | log.Background = bCard; |
| 178 | log.ItemTemplate = new FuncDataTemplate<DLine>((l, _) => Row(l)); |
| 179 | log.Styles.Add(new Style(x => x.OfType<ListBoxItem>()) |
| 180 | { |
| 181 | Setters = { new Setter(ListBoxItem.PaddingProperty, new Thickness(8, 1)), new Setter(ListBoxItem.MinHeightProperty, 0d) }, |
| 182 | }); |
| 183 | ScrollViewer.SetHorizontalScrollBarVisibility(log, ScrollBarVisibility.Auto); |
| 184 | Control lp = Pane("LIVE FEED", log); |
| 185 | Grid.SetColumn(lp, 2); |
| 186 | body.Children.Add(lp); |
| 187 | Grid.SetRow(body, 2); |
| 188 | root.Children.Add(body); |
| 189 | Content = root; |
| 190 | |
| 191 | KeyDown += (_, e) => |
| 192 | { |
| 193 | if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.C && log.SelectedItems != null && log.SelectedItems.Count > 0) { _ = CopySelected(); e.Handled = true; } |
| 194 | else if (e.KeyModifiers == KeyModifiers.Control && e.Key == Key.F) { find.Focus(); e.Handled = true; } |
| 195 | else if (e.Key == Key.F5) { SetPaused(!paused); e.Handled = true; } |
| 196 | }; |
| 197 | Opened += (_, _) => { otherBtn.IsVisible = Monitors.Count(this) > 1; OpenFile(); }; |
| 198 | Activated += (_, _) => otherBtn.IsVisible = Monitors.Count(this) > 1; |
| 199 | Closing += (_, e) => |
| 200 | { |
| 201 | if (Quitting) return; |
| 202 | e.Cancel = true; |
| 203 | Hide(); |
| 204 | }; |
| 205 | Closed += (_, _) => { try { file?.Dispose(); } catch (Exception) { } file = null; }; |
| 206 | PropertyChanged += (_, e) => |
| 207 | { |
| 208 | if (e.Property == IsVisibleProperty) |
| 209 | { |
| 210 | bool v = IsVisible; |
| 211 | Live?.Invoke(v); |
| 212 | if (v) { OpenFile(); if (dirty) { RebuildStatus(); Refilter(); } } |
| 213 | else try { file?.Flush(); } catch (Exception) { } |
| 214 | } |
| 215 | }; |
| 216 | Avalonia.Threading.DispatcherTimer t = new() { Interval = TimeSpan.FromSeconds(1) }; |
| 217 | t.Tick += (_, _) => { try { file?.Flush(); } catch (Exception) { } UpdateState(); }; |
| 218 | t.Start(); |
| 219 | SetLinked(false); |
| 220 | } |
| 221 | |
| 222 | private Control Pane(string title, Control content) |
| 223 | { |
| 224 | Grid g = new() { RowDefinitions = new RowDefinitions("Auto,*") }; |
| 225 | StackPanel head = new() { Orientation = Orientation.Horizontal, Spacing = 8, Margin = new Thickness(12, 10, 0, 6) }; |
| 226 | head.Children.Add(new Border { Width = 3, Height = 13, CornerRadius = new CornerRadius(2), Background = bOrange, VerticalAlignment = VerticalAlignment.Center }); |
| 227 | head.Children.Add(new TextBlock { Text = string.Join(" ", title.ToCharArray()), FontSize = 11, FontWeight = FontWeight.Bold, Foreground = bOrange }); |
| 228 | g.Children.Add(head); |
| 229 | Border b = new() { Background = bCard, Child = content }; |
| 230 | Grid.SetRow(b, 1); |
| 231 | g.Children.Add(b); |
| 232 | return g; |
| 233 | } |
| 234 | |
| 235 | private static Button Btn(string text, Action click) |
| 236 | { |
| 237 | Button b = new() { Content = text, Padding = new Thickness(12, 5), MinHeight = 30, CornerRadius = new CornerRadius(6) }; |
| 238 | b.Click += (_, _) => click(); |
| 239 | return b; |
| 240 | } |
| 241 | |
| 242 | private static void Paint(Button b, bool on) |
| 243 | { |
| 244 | if (on) { b.Background = Ui.B(Ui.Orange); b.Foreground = Ui.B(Ui.OnAccent); } |
| 245 | else { b.ClearValue(BackgroundProperty); b.ClearValue(ForegroundProperty); } |
| 246 | } |
| 247 | |
| 248 | private IBrush KindBrush(string k) => k switch |
| 249 | { |
| 250 | "ERROR" or "ALERT" => bBad, |
| 251 | "WARNING" or "LIMB" => bWarn, |
| 252 | "EVENT" or "SCENE" => bOrange, |
| 253 | "GOOD" => bGood, |
| 254 | "TRAINER" or "COMMAND" => bBright, |
| 255 | _ => bText, |
| 256 | }; |
| 257 | |
| 258 | private Control Row(DLine l) |
| 259 | { |
| 260 | if (l == null) return new TextBlock(); |
| 261 | IBrush kc = KindBrush(l.Kind); |
| 262 | IBrush tc = l.Kind is "GAME" or "BEPINEX" or "SYSTEM" ? bText : (!l.First && l.Kind == "ERROR" ? bDim : kc); |
| 263 | TextBlock tb = new() { FontFamily = mono, FontSize = 12, TextWrapping = TextWrapping.NoWrap }; |
| 264 | tb.Inlines = new InlineCollection |
| 265 | { |
| 266 | new Run((l.First ? l.Time : new string(' ', l.Time.Length)) + " ") { Foreground = bDim }, |
| 267 | new Run((l.First ? l.Kind : "").PadRight(8) + " ") { Foreground = kc, FontWeight = FontWeight.Bold }, |
| 268 | new Run(l.Text) { Foreground = tc }, |
| 269 | }; |
| 270 | if (l.First && (l.Kind == "ERROR" || l.Kind == "ALERT")) return new Border { Background = bSunk, Child = tb }; |
| 271 | return tb; |
| 272 | } |
| 273 | |
| 274 | private void OpenFile() |
| 275 | { |
| 276 | if (file != null) return; |
| 277 | try |
| 278 | { |
| 279 | Private.Dir(Path.GetDirectoryName(LogPath)); |
| 280 | file = new StreamWriter(LogPath, false, new UTF8Encoding(false)); |
| 281 | file.WriteLine("Ultra Paradise CU:Trainer v" + Program.Version + " (" + Program.Platform + ") debug log, started " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); |
| 282 | } |
| 283 | catch (Exception) { file = null; } |
| 284 | } |
| 285 | |
| 286 | public void MoveToOther() |
| 287 | { |
| 288 | Screen s = Monitors.Spare(this); |
| 289 | if (s == null) { Say?.Invoke("Only one screen is plugged in.", false); return; } |
| 290 | Monitors.MoveTo(this, s, true); |
| 291 | } |
| 292 | |
| 293 | public void SetLinked(bool on) |
| 294 | { |
| 295 | linked = on; |
| 296 | if (!on) { snap = ""; RebuildStatus(); } |
| 297 | UpdateState(); |
| 298 | } |
| 299 | |
| 300 | private void SetPaused(bool p) |
| 301 | { |
| 302 | paused = p; |
| 303 | pauseBtn.Content = p ? "Resume" : "Pause"; |
| 304 | Paint(pauseBtn, p); |
| 305 | if (!p) { Refilter(); if (dirty) RebuildStatus(); } |
| 306 | UpdateState(); |
| 307 | } |
| 308 | |
| 309 | private void UpdateState() |
| 310 | { |
| 311 | string t; |
| 312 | IBrush c; |
| 313 | if (!linked) { t = "Not attached. Start the game and let the trainer attach, then everything shows up here."; c = bDim; } |
| 314 | else if (paused) { t = "Paused. New lines are still saved. Press Resume (F5) to catch up."; c = bWarn; } |
| 315 | else if ((DateTime.UtcNow - lastSnap).TotalSeconds > 4) { t = "Attached. Waiting for the game (it sends nothing while it's loading)."; c = bWarn; } |
| 316 | else { int n = Monitors.Count(this); t = "Live · " + all.Count + " lines" + (n > 1 ? " · " + n + " screens" : ""); c = bGood; } |
| 317 | state.Text = t; |
| 318 | state.Foreground = c; |
| 319 | } |
| 320 | |
| 321 | public void OnSnapshot(string text) |
| 322 | { |
| 323 | snap = text; |
| 324 | lastSnap = DateTime.UtcNow; |
| 325 | dirty = true; |
| 326 | if (!paused && IsVisible) RebuildStatus(); |
| 327 | UpdateState(); |
| 328 | } |
| 329 | |
| 330 | public void OnSystem(string text) |
| 331 | { |
| 332 | sys = text; |
| 333 | dirty = true; |
| 334 | if (!paused && IsVisible) RebuildStatus(); |
| 335 | } |
| 336 | |
| 337 | private List<string> StatusLines() |
| 338 | { |
| 339 | List<string> lines = new(); |
| 340 | if (snap.Length == 0) lines.Add(linked ? "DWaiting for the first update from the game..." : "DNothing yet. Attach to the game to see its live status."); |
| 341 | else lines.AddRange(snap.Split(NL)); |
| 342 | if (sys.Length > 0) { lines.Add("N"); lines.AddRange(sys.Split(NL)); } |
| 343 | return lines; |
| 344 | } |
| 345 | |
| 346 | private void RebuildStatus() |
| 347 | { |
| 348 | dirty = false; |
| 349 | List<string> lines = StatusLines(); |
| 350 | while (statusRows.Count < lines.Count) |
| 351 | { |
| 352 | SelectableTextBlock tb = new() { FontFamily = mono, FontSize = 12, TextWrapping = TextWrapping.NoWrap }; |
| 353 | statusRows.Add(tb); |
| 354 | statusPanel.Children.Add(tb); |
| 355 | } |
| 356 | for (int i = 0; i < statusRows.Count; i++) |
| 357 | { |
| 358 | SelectableTextBlock tb = statusRows[i]; |
| 359 | if (i >= lines.Count) { tb.IsVisible = false; continue; } |
| 360 | tb.IsVisible = true; |
| 361 | string s = lines[i]; |
| 362 | char code = s.Length > 0 ? s[0] : 'N'; |
| 363 | string text = s.Length > 1 ? s[1..] : ""; |
| 364 | if (tb.Text != text) tb.Text = text; |
| 365 | tb.Foreground = code switch { 'H' => bOrange, 'D' => bDim, 'G' => bGood, 'Y' => bWarn, 'R' => bBad, _ => bText }; |
| 366 | tb.FontWeight = code == 'H' || code == 'R' ? FontWeight.Bold : FontWeight.Normal; |
| 367 | tb.Margin = new Thickness(0, code == 'H' && i > 0 ? 8 : 0, 0, 0); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | public void OnLog(string batch) |
| 372 | { |
| 373 | string now = DateTime.Now.ToString("HH:mm:ss.fff"); |
| 374 | List<DLine> fresh = new(); |
| 375 | foreach (string rec in batch.Split(REC)) |
| 376 | { |
| 377 | int s = rec.IndexOf(SEP); |
| 378 | if (s <= 0) continue; |
| 379 | string kind = rec[..s]; |
| 380 | string[] parts = rec[(s + 1)..].Split(NL); |
| 381 | for (int i = 0; i < parts.Length; i++) |
| 382 | { |
| 383 | if (i > 0 && i == parts.Length - 1 && parts[i].Length == 0) break; |
| 384 | fresh.Add(new DLine { Time = now, Kind = kind, Text = parts[i], First = i == 0 }); |
| 385 | try { file?.WriteLine((i == 0 ? now + " [" + kind + "] " : new string(' ', now.Length + kind.Length + 4)) + SysInfo.Scrub(parts[i])); } catch (Exception) { } |
| 386 | } |
| 387 | } |
| 388 | if (fresh.Count == 0) return; |
| 389 | all.AddRange(fresh); |
| 390 | if (all.Count > 30000) all.RemoveRange(0, all.Count - 25000); |
| 391 | if (paused || !IsVisible) { dirty = true; return; } |
| 392 | bool follow = AtBottom(); |
| 393 | foreach (DLine l in fresh) if (Passes(l)) shown.Add(l); |
| 394 | if (shown.Count > 8000) { for (int i = 0; i < 1000; i++) shown.RemoveAt(0); } |
| 395 | if (follow && shown.Count > 0 && (log.SelectedItems == null || log.SelectedItems.Count == 0)) log.ScrollIntoView(shown.Count - 1); |
| 396 | } |
| 397 | |
| 398 | private bool AtBottom() |
| 399 | { |
| 400 | IScrollable sv = log.Scroll; |
| 401 | if (sv == null) return true; |
| 402 | return sv.Offset.Y + sv.Viewport.Height >= sv.Extent.Height - 30; |
| 403 | } |
| 404 | |
| 405 | private bool Passes(DLine l) |
| 406 | { |
| 407 | bool known = false, ok = true; |
| 408 | for (int i = 0; i < FilterNames.Length; i++) |
| 409 | { |
| 410 | if (Array.IndexOf(FilterKinds[i], l.Kind) < 0) continue; |
| 411 | known = true; |
| 412 | ok = filters[FilterNames[i]].IsChecked == true; |
| 413 | break; |
| 414 | } |
| 415 | if (known && !ok) return false; |
| 416 | string q = (find.Text ?? "").Trim(); |
| 417 | return q.Length == 0 || l.Text.Contains(q, StringComparison.OrdinalIgnoreCase) || l.Kind.Contains(q, StringComparison.OrdinalIgnoreCase); |
| 418 | } |
| 419 | |
| 420 | private void Refilter() |
| 421 | { |
| 422 | if (paused) return; |
| 423 | List<DLine> keep = new(); |
| 424 | for (int i = all.Count - 1; i >= 0 && keep.Count < 7000; i--) if (Passes(all[i])) keep.Add(all[i]); |
| 425 | keep.Reverse(); |
| 426 | shown.Clear(); |
| 427 | foreach (DLine l in keep) shown.Add(l); |
| 428 | if (shown.Count > 0) log.ScrollIntoView(shown.Count - 1); |
| 429 | } |
| 430 | |
| 431 | private static string Prefix(DLine l) => l.Time + " " + (l.First ? l.Kind.PadRight(8) : new string(' ', 8)) + " "; |
| 432 | |
| 433 | private string StatusText() |
| 434 | { |
| 435 | StringBuilder sb = new(); |
| 436 | foreach (string s in StatusLines()) |
| 437 | { |
| 438 | if (s.Length == 0) continue; |
| 439 | sb.Append(s[0] == 'H' ? "== " + s[1..] + " ==" : s[1..]).Append('\n'); |
| 440 | } |
| 441 | return sb.ToString(); |
| 442 | } |
| 443 | |
| 444 | private async Task CopySelected() |
| 445 | { |
| 446 | StringBuilder sb = new(); |
| 447 | foreach (object o in log.SelectedItems) if (o is DLine l) sb.Append(Prefix(l)).Append(l.Text).Append('\n'); |
| 448 | if (Clipboard != null && sb.Length > 0) await Clipboard.SetTextAsync(SysInfo.Scrub(sb.ToString())); |
| 449 | } |
| 450 | |
| 451 | private async Task CopyAll() |
| 452 | { |
| 453 | StringBuilder sb = new(); |
| 454 | sb.Append("=== Ultra Paradise CU:Trainer v").Append(Program.Version).Append(" (").Append(Program.Platform).Append(") debug ===\n\n"); |
| 455 | sb.Append(StatusText()); |
| 456 | sb.Append("\n=== Live feed ===\n"); |
| 457 | bool sel = log.SelectedItems != null && log.SelectedItems.Count > 0; |
| 458 | if (sel) foreach (object o in log.SelectedItems) { if (o is DLine l) sb.Append(Prefix(l)).Append(l.Text).Append('\n'); } |
| 459 | else for (int i = Math.Max(0, shown.Count - 500); i < shown.Count; i++) sb.Append(Prefix(shown[i])).Append(shown[i].Text).Append('\n'); |
| 460 | if (Clipboard == null) { Say?.Invoke("Couldn't reach the clipboard. Try Save.", true); return; } |
| 461 | await Clipboard.SetTextAsync(SysInfo.Scrub(sb.ToString())); |
| 462 | Say?.Invoke("Copied the status and " + (sel ? "the selected lines" : "the last " + Math.Min(500, shown.Count) + " lines") + ".", false); |
| 463 | } |
| 464 | |
| 465 | private async Task SaveLog() |
| 466 | { |
| 467 | IStorageFile f = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions |
| 468 | { |
| 469 | Title = "Save the debug log", |
| 470 | SuggestedFileName = "UltraParadise-debug-" + DateTime.Now.ToString("yyyyMMdd-HHmm") + ".txt", |
| 471 | DefaultExtension = "txt", |
| 472 | }); |
| 473 | string p = f?.TryGetLocalPath(); |
| 474 | if (p == null) return; |
| 475 | try |
| 476 | { |
| 477 | StringBuilder sb = new(); |
| 478 | sb.Append("=== Ultra Paradise CU:Trainer v").Append(Program.Version).Append(" (").Append(Program.Platform).Append(") debug, saved ").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Append(" ===\n\n"); |
| 479 | sb.Append(StatusText()); |
| 480 | sb.Append("\n=== Live feed (").Append(all.Count).Append(" lines) ===\n"); |
| 481 | foreach (DLine l in all) sb.Append(Prefix(l)).Append(l.Text).Append('\n'); |
| 482 | File.WriteAllText(p, SysInfo.Scrub(sb.ToString()), new UTF8Encoding(false)); |
| 483 | Say?.Invoke("Saved " + Path.GetFileName(p) + ".", false); |
| 484 | } |
| 485 | catch (Exception e) { Say?.Invoke("Couldn't save it: " + e.Message, true); } |
| 486 | } |
| 487 | } |
| 488 | } |