casualexpiesource

cu-trainer/Ultra Paradise Linux/app/DebugPage.cs

162 lines · 9.0 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using Avalonia;
4using Avalonia.Controls;
5using Avalonia.Platform;
6using UltraParadise.Shared;
7
8namespace UltraParadise.Linux
9{
10 internal sealed partial class MainWindow
11 {
12 private DebugWindow debugWin;
13 private readonly List<TextBlock> screenInfos = new();
14 private int lastScreens = -1;
15 private bool screenTipShown;
16
17 private Border ScreensSection(string title, bool welcome)
18 {
19 Border s = Section(title, out StackPanel b);
20 TextBlock info = T(Monitors.Advice(this), 13, Ui.B(Ui.Text), true, true);
21 screenInfos.Add(info);
22 b.Children.Add(info);
23 b.Children.Add(T(welcome
24 ? "In my opinion the best setup is two screens: the game on one, and the trainer and the Debug window on the other, so nothing ever covers the game.\n\nIf you have a second screen plugged in, the trainer spots it. The buttons below move the trainer or the Debug window to the screen that isn't your main one."
25 : "Recommended (in my opinion): a dual monitor setup. Put the game on one screen and the trainer and Debug window on the other one, the screen that isn't showing the game.\n\nWhen the trainer finds more than one screen, the Debug window opens on the one that isn't your main screen (where games usually go fullscreen), and Other Screen buttons work.\n\nOne screen only? Play the game in windowed mode and use Pin on Top in the Debug window.", 13, Ui.B(Ui.Dim), false, true));
26 WrapPanel w = new();
27 Button mt = Btn("Move Trainer", MoveTrainerToOther);
28 mt.Margin = new Thickness(0, 0, 6, 6);
29 w.Children.Add(mt);
30 Button od = Btn(welcome ? "Open Debug Window" : "Move Debug", () => OpenDebug(!welcome));
31 od.Margin = new Thickness(0, 0, 6, 6);
32 w.Children.Add(od);
33 b.Children.Add(w);
34 return s;
35 }
36
37 private static string SaveCheckLine(Dictionary<string, string> d)
38 {
39 if (d == null || !d.TryGetValue("savecheat", out string v)) return "Save file marked as cheated: unknown (start the game, or update UltraPara.dll)";
40 return v switch
41 {
42 "cheated" => "Save file marked as cheated: TRUE",
43 "clean" => "Save file marked as cheated: FALSE",
44 "none" => "Save file marked as cheated: no save file right now",
45 "error" => "Save file marked as cheated: couldn't read the save",
46 _ => "Save file marked as cheated: unknown",
47 };
48 }
49
50 private static string SaveCheckAch(Dictionary<string, string> d) =>
51 d == null || !d.TryGetValue("achoff", out string v) ? "" : v == "1" ? "Achievements right now: OFF" : "Achievements right now: on";
52
53 private Control DebugPage()
54 {
55 Grid g = new() { ColumnDefinitions = new ColumnDefinitions("*,*,*") };
56 StackPanel c0 = new() { Margin = new Thickness(0, 0, 7, 0) };
57 Border d = Section("DEBUG WINDOW", out StackPanel db);
58 db.Children.Add(T("A separate window that shows everything, live: the game's own log (with errors and warnings), BepInEx and plugin messages, what happens to your character, what the cheats block, and a status panel with FPS, the world, every enemy type, your vitals and every limb.\n\nIt's the same kind of debug window as in my CU Cheats mod, but outside the game, so it never gets in your way.", 13, Ui.B(Ui.Dim), false, true));
59 WrapPanel w = new();
60 Button open = Btn("Open Debug Window", () => OpenDebug(false), true);
61 open.Margin = new Thickness(0, 0, 6, 6);
62 w.Children.Add(open);
63 Button other = Btn("Other Screen", () => OpenDebug(true));
64 other.Margin = new Thickness(0, 0, 6, 6);
65 w.Children.Add(other);
66 db.Children.Add(w);
67 db.Children.Add(Switch("Open It With the Game", "Opens the Debug window by itself every time the trainer attaches to the game.", Settings.On("debugwithgame", false), on => Settings.Set("debugwithgame", on ? "1" : "0")));
68 c0.Children.Add(d);
69 Border sc = Section("SAVE CHECK", out StackPanel scb);
70 foreach (Func<Dictionary<string, string>, string> f in new Func<Dictionary<string, string>, string>[] { SaveCheckLine, SaveCheckAch })
71 {
72 TextBlock tb = T(f(info) ?? "", 13, Ui.B(Ui.Text), false, true);
73 infoRows.Add((tb, new COpt { Kind = CKind.Info, Text = f }));
74 scb.Children.Add(tb);
75 }
76 scb.Children.Add(T("Reads the game's own save file (save.sv) to see if the game marked it as cheated. TRUE means the game would turn achievements off when it loads this save. The game deletes the file when it loads a save and writes it again when it saves, so \"no save file\" during a run is normal.", 12, Ui.B(Ui.Dim), false, true));
77 c0.Children.Add(sc);
78 g.Children.Add(c0);
79
80 StackPanel c1 = new() { Margin = new Thickness(7, 0, 7, 0) };
81 Border h = Section("HOW TO USE IT", out StackPanel hb);
82 hb.Children.Add(T("Left: LIVE STATUS, updated every second.\nRight: LIVE FEED, every line the game, BepInEx and the trainer log.\n\nTick the boxes to show or hide kinds of lines, or type in Find to search. Pause (F5) freezes the feed so you can read it; nothing is lost. Select lines and press Ctrl+C to copy them, or Copy for the status and the last 500 lines.\n\nEverything is also saved to debug.log (Log Folder button) while the window is open, so you can attach it to a bug report.\n\nIt only collects data while the window is open, so it costs nothing when it's closed.", 13, Ui.B(Ui.Dim), false, true));
83 c1.Children.Add(h);
84 Grid.SetColumn(c1, 1);
85 g.Children.Add(c1);
86
87 StackPanel c2 = new() { Margin = new Thickness(7, 0, 0, 0) };
88 c2.Children.Add(ScreensSection("SCREENS", false));
89 Grid.SetColumn(c2, 2);
90 g.Children.Add(c2);
91 return Scroll(g);
92 }
93
94 private void MoveTrainerToOther()
95 {
96 Screen s = Monitors.Spare(this);
97 if (s == null) { Message("Only one screen is plugged in. A second screen is recommended so the trainer never covers the game.", false); return; }
98 Monitors.MoveTo(this, s, false);
99 Message("Moved to " + Monitors.Name(this, s) + ".", false);
100 }
101
102 private void OpenDebug(bool other)
103 {
104 if (Mac.On) return;
105 bool fresh = debugWin == null;
106 if (fresh)
107 {
108 debugWin = new DebugWindow();
109 debugWin.Say = Message;
110 debugWin.Live = on => { if (link.IsConnected) link.Send("DEBUG " + (on ? "1" : "0")); };
111 debugWin.SetLinked(link.IsConnected);
112 }
113 if (!debugWin.IsVisible) debugWin.Show();
114 if (debugWin.WindowState == WindowState.Minimized) debugWin.WindowState = WindowState.Normal;
115 if (fresh || other)
116 {
117 Screen s = Monitors.Spare(debugWin);
118 if (s != null) Monitors.MoveTo(debugWin, s, true);
119 else if (other) Message("Only one screen is plugged in.", false);
120 }
121 debugWin.Activate();
122 if (fresh && Monitors.Count(this) < 2) Message("Debug window open. Tip: a second screen is recommended, so the game and the Debug window can sit side by side.", false);
123 }
124
125 private void DebugConnected()
126 {
127 if (Mac.On) return;
128 if (Settings.On("debugwithgame", false) && (debugWin == null || !debugWin.IsVisible)) OpenDebug(false);
129 if (debugWin != null)
130 {
131 debugWin.SetLinked(true);
132 if (debugWin.IsVisible) link.Send("DEBUG 1");
133 }
134 if (!screenTipShown && Monitors.Count(this) > 1)
135 {
136 Screen mine = Screens.ScreenFromWindow(this);
137 if (mine != null && mine.IsPrimary)
138 {
139 screenTipShown = true;
140 Message("Tip: you have " + Monitors.Count(this) + " screens and the trainer is on your main one. Move Trainer (Debug tab) puts it on the other.", false);
141 }
142 }
143 }
144
145 private void DebugTick()
146 {
147 int n = Monitors.Count(this);
148 if (n == lastScreens) return;
149 if (lastScreens > 0) Message(n > lastScreens ? "New screen found: you have " + n + " now. The Debug tab can move the trainer or the Debug window onto it." : "A screen was unplugged. You have " + n + " now.", false);
150 lastScreens = n;
151 string a = Monitors.Advice(this);
152 foreach (TextBlock tb in screenInfos) tb.Text = a;
153 }
154
155 private void CloseDebug()
156 {
157 if (debugWin == null) return;
158 debugWin.Quitting = true;
159 try { debugWin.Close(); } catch (Exception) { }
160 }
161 }
162}