cu-trainer/Ultra Paradise Application/src/DebugWindow.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.Drawing; |
| 5 | using System.IO; |
| 6 | using System.Text; |
| 7 | using System.Windows.Forms; |
| 8 | |
| 9 | namespace UltraParadise |
| 10 | { |
| 11 | internal static class Monitors |
| 12 | { |
| 13 | public static Process GameProc; |
| 14 | |
| 15 | public static int Count { get { try { return Screen.AllScreens.Length; } catch (Exception) { return 1; } } } |
| 16 | |
| 17 | public static string Advice() |
| 18 | { |
| 19 | int n = Count; |
| 20 | if (n < 2) return "Only one screen found. A second screen is recommended, see below."; |
| 21 | Screen gs = GameScreen(GameProc); |
| 22 | return n + " screens found" + (gs != null ? ". The game is on " + Name(gs) : ". Start the game and I'll tell you which one it's on") + "."; |
| 23 | } |
| 24 | |
| 25 | public static int IndexOf(Screen s) |
| 26 | { |
| 27 | Screen[] all = Screen.AllScreens; |
| 28 | for (int i = 0; i < all.Length; i++) if (all[i].DeviceName == s.DeviceName) return i; |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | public static string Name(Screen s) |
| 33 | { |
| 34 | return "Screen " + (IndexOf(s) + 1) + " (" + s.Bounds.Width + "x" + s.Bounds.Height + (s.Primary ? ", main" : "") + ")"; |
| 35 | } |
| 36 | |
| 37 | public static Screen GameScreen(Process game) |
| 38 | { |
| 39 | if (game == null) return null; |
| 40 | try |
| 41 | { |
| 42 | game.Refresh(); |
| 43 | IntPtr h = game.MainWindowHandle; |
| 44 | if (h == IntPtr.Zero) return null; |
| 45 | return Screen.FromHandle(h); |
| 46 | } |
| 47 | catch (Exception) { return null; } |
| 48 | } |
| 49 | |
| 50 | public static Screen Spare(Screen game, Form near) |
| 51 | { |
| 52 | Screen[] all = Screen.AllScreens; |
| 53 | if (all.Length < 2) return null; |
| 54 | Screen avoid = game ?? Screen.PrimaryScreen; |
| 55 | Screen best = null; |
| 56 | foreach (Screen s in all) |
| 57 | { |
| 58 | if (s.DeviceName == avoid.DeviceName) continue; |
| 59 | if (best == null || (long)s.Bounds.Width * s.Bounds.Height > (long)best.Bounds.Width * best.Bounds.Height) best = s; |
| 60 | } |
| 61 | return best; |
| 62 | } |
| 63 | |
| 64 | public static Screen Next(Form f) |
| 65 | { |
| 66 | Screen[] all = Screen.AllScreens; |
| 67 | if (all.Length < 2) return null; |
| 68 | int i = IndexOf(Screen.FromControl(f)); |
| 69 | return all[(i + 1) % all.Length]; |
| 70 | } |
| 71 | |
| 72 | public static void MoveTo(Form f, Screen s, bool fill) |
| 73 | { |
| 74 | if (f == null || s == null) return; |
| 75 | bool max = f.WindowState == FormWindowState.Maximized; |
| 76 | if (f.WindowState != FormWindowState.Normal) f.WindowState = FormWindowState.Normal; |
| 77 | Rectangle wa = s.WorkingArea; |
| 78 | int w = Math.Min(f.Width, wa.Width), h = Math.Min(f.Height, wa.Height); |
| 79 | if (fill) { w = Math.Max(w, (int)(wa.Width * 0.8)); h = Math.Max(h, (int)(wa.Height * 0.8)); w = Math.Min(w, wa.Width); h = Math.Min(h, wa.Height); } |
| 80 | f.StartPosition = FormStartPosition.Manual; |
| 81 | f.Bounds = new Rectangle(wa.X + (wa.Width - w) / 2, wa.Y + (wa.Height - h) / 2, w, h); |
| 82 | if (max) f.WindowState = FormWindowState.Maximized; |
| 83 | } |
| 84 | |
| 85 | public static string Describe(Process game) |
| 86 | { |
| 87 | Screen[] all; |
| 88 | try { all = Screen.AllScreens; } catch (Exception) { return "Couldn't read your screens."; } |
| 89 | Screen gs = GameScreen(game); |
| 90 | StringBuilder sb = new StringBuilder(); |
| 91 | sb.Append(all.Length == 1 ? "1 screen found" : all.Length + " screens found"); |
| 92 | for (int i = 0; i < all.Length; i++) |
| 93 | { |
| 94 | Screen s = all[i]; |
| 95 | sb.Append("\n").Append(Name(s)); |
| 96 | if (gs != null && gs.DeviceName == s.DeviceName) sb.Append(" - the game is here"); |
| 97 | } |
| 98 | return sb.ToString(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | internal class DebugLine |
| 103 | { |
| 104 | public string Time, Kind, Text; |
| 105 | public bool First; |
| 106 | } |
| 107 | |
| 108 | internal class DebugForm : Form |
| 109 | { |
| 110 | public Action<bool> Live; |
| 111 | public Action<string, bool> Say; |
| 112 | public Func<Process> Game; |
| 113 | public bool Quitting; |
| 114 | |
| 115 | private readonly Panel top = new Panel(), bar = new Panel(); |
| 116 | private readonly Label state = new Label(), filterLabel = new Label(); |
| 117 | private readonly Button other = new Button(), pin = new Button(), pause = new Button(), clear = new Button(), copy = new Button(), save = new Button(), folder = new Button(); |
| 118 | private readonly TextBox search = new TextBox(); |
| 119 | private readonly SplitContainer split = new SplitContainer(); |
| 120 | private readonly ListBox status = new ListBox(), log = new ListBox(); |
| 121 | private readonly Label statusHead = new Label(), logHead = new Label(); |
| 122 | private readonly Dictionary<string, CheckBox> filters = new Dictionary<string, CheckBox>(); |
| 123 | private readonly List<DebugLine> all = new List<DebugLine>(); |
| 124 | private readonly Timer flushTimer = new Timer(); |
| 125 | private string snap = "", sys = ""; |
| 126 | private bool paused, linked, dirtyStatus; |
| 127 | private DateTime lastSnap = DateTime.MinValue; |
| 128 | private StreamWriter file; |
| 129 | private Font mono, monoBold, ui, uiBold; |
| 130 | private int lineH; |
| 131 | |
| 132 | private static readonly string[] Kinds = { "GAME", "WARNING", "ERROR", "EVENT", "TRAINER", "COMMAND", "SCENE", "SYSTEM", "BEPINEX" }; |
| 133 | private static readonly string[] FilterNames = { "Game log", "Warnings", "Errors", "Events", "Trainer", "Commands" }; |
| 134 | private static readonly string[][] FilterKinds = |
| 135 | { |
| 136 | new[] { "GAME", "SCENE", "SYSTEM", "BEPINEX" }, new[] { "WARNING" }, new[] { "ERROR" }, new[] { "EVENT", "LIMB", "GOOD", "ALERT" }, new[] { "TRAINER" }, new[] { "COMMAND" } |
| 137 | }; |
| 138 | |
| 139 | public static string LogPath { get { return Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "UltraParadise"), "debug.log"); } } |
| 140 | |
| 141 | public DebugForm() |
| 142 | { |
| 143 | AutoScaleMode = AutoScaleMode.None; |
| 144 | Text = "Ultra Paradise - Debug"; |
| 145 | try { Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); } catch (Exception) { } |
| 146 | BackColor = Theme.Back; |
| 147 | ForeColor = Theme.Text; |
| 148 | ui = Theme.F("Segoe UI", 9f); |
| 149 | uiBold = Theme.F("Segoe UI", 9f, FontStyle.Bold); |
| 150 | mono = Theme.F("Consolas", 9f); |
| 151 | monoBold = Theme.F("Consolas", 9f, FontStyle.Bold); |
| 152 | Font = ui; |
| 153 | using (Graphics g = CreateGraphics()) lineH = (int)Math.Ceiling(mono.GetHeight(g)) + Theme.S(3); |
| 154 | ClientSize = new Size(Theme.S(1100), Theme.S(680)); |
| 155 | MinimumSize = SizeFromClientSize(new Size(Theme.S(640), Theme.S(380))); |
| 156 | StartPosition = FormStartPosition.CenterScreen; |
| 157 | KeyPreview = true; |
| 158 | |
| 159 | top.Dock = DockStyle.Top; |
| 160 | top.Height = Theme.S(46); |
| 161 | top.BackColor = Theme.Side; |
| 162 | state.Font = uiBold; |
| 163 | state.ForeColor = Theme.Dim; |
| 164 | state.BackColor = Theme.Side; |
| 165 | state.AutoEllipsis = true; |
| 166 | state.TextAlign = ContentAlignment.MiddleLeft; |
| 167 | top.Controls.Add(state); |
| 168 | Btn(top, other, "Other Screen", delegate { MoveToOther(); }); |
| 169 | Btn(top, pin, "Pin on Top", delegate { TopMost = !TopMost; pin.BackColor = TopMost ? Theme.Orange : Theme.Panel; pin.ForeColor = TopMost ? Theme.OnAccent : Theme.Text; }); |
| 170 | Btn(top, pause, "Pause", delegate { SetPaused(!paused); }); |
| 171 | Btn(top, clear, "Clear", delegate { all.Clear(); log.Items.Clear(); }); |
| 172 | Btn(top, copy, "Copy", delegate { CopyLines(); }); |
| 173 | Btn(top, save, "Save...", delegate { SaveLog(); }); |
| 174 | Btn(top, folder, "Log Folder", delegate { try { Process.Start("explorer.exe", "/select,\"" + LogPath + "\""); } catch (Exception) { } }); |
| 175 | top.Resize += delegate { LayoutTop(); }; |
| 176 | |
| 177 | bar.Dock = DockStyle.Top; |
| 178 | bar.Height = Theme.S(34); |
| 179 | bar.BackColor = Theme.Panel; |
| 180 | for (int i = 0; i < FilterNames.Length; i++) |
| 181 | { |
| 182 | CheckBox c = new CheckBox(); |
| 183 | c.Text = FilterNames[i]; |
| 184 | c.Checked = true; |
| 185 | c.AutoSize = true; |
| 186 | c.Font = ui; |
| 187 | c.ForeColor = Theme.Text; |
| 188 | c.BackColor = Theme.Panel; |
| 189 | c.FlatStyle = FlatStyle.Flat; |
| 190 | c.Cursor = Cursors.Hand; |
| 191 | c.CheckedChanged += delegate { Refilter(); }; |
| 192 | filters[FilterNames[i]] = c; |
| 193 | bar.Controls.Add(c); |
| 194 | } |
| 195 | filterLabel.Text = "Find:"; |
| 196 | filterLabel.Font = ui; |
| 197 | filterLabel.ForeColor = Theme.Dim; |
| 198 | filterLabel.BackColor = Theme.Panel; |
| 199 | filterLabel.AutoSize = true; |
| 200 | bar.Controls.Add(filterLabel); |
| 201 | search.BorderStyle = BorderStyle.FixedSingle; |
| 202 | search.BackColor = Theme.Back; |
| 203 | search.ForeColor = Theme.Text; |
| 204 | search.Font = ui; |
| 205 | search.TextChanged += delegate { Refilter(); }; |
| 206 | bar.Controls.Add(search); |
| 207 | bar.Resize += delegate { LayoutBar(); }; |
| 208 | |
| 209 | split.Dock = DockStyle.Fill; |
| 210 | split.BackColor = Theme.Line; |
| 211 | split.SplitterWidth = Theme.S(5); |
| 212 | split.Panel1.BackColor = Theme.Back; |
| 213 | split.Panel2.BackColor = Theme.Back; |
| 214 | Head(split.Panel1, statusHead, "LIVE STATUS"); |
| 215 | Head(split.Panel2, logHead, "LIVE FEED"); |
| 216 | List(split.Panel1, status); |
| 217 | List(split.Panel2, log); |
| 218 | status.SelectionMode = SelectionMode.MultiExtended; |
| 219 | log.SelectionMode = SelectionMode.MultiExtended; |
| 220 | status.DrawItem += DrawStatus; |
| 221 | log.DrawItem += DrawLog; |
| 222 | |
| 223 | Controls.Add(split); |
| 224 | Controls.Add(bar); |
| 225 | Controls.Add(top); |
| 226 | |
| 227 | flushTimer.Interval = 1000; |
| 228 | flushTimer.Tick += delegate |
| 229 | { |
| 230 | try { if (file != null) file.Flush(); } catch (Exception) { } |
| 231 | UpdateState(); |
| 232 | }; |
| 233 | Theme.Changed += OnThemeChanged; |
| 234 | SetLinked(false); |
| 235 | } |
| 236 | |
| 237 | private void OnThemeChanged(Color[] before) |
| 238 | { |
| 239 | if (IsDisposed) return; |
| 240 | Theme.Recolor(this, before, Theme.Tokens()); |
| 241 | status.Invalidate(); |
| 242 | log.Invalidate(); |
| 243 | } |
| 244 | |
| 245 | private void Btn(Panel p, Button b, string text, EventHandler click) |
| 246 | { |
| 247 | b.Text = text; |
| 248 | b.FlatStyle = FlatStyle.Flat; |
| 249 | b.FlatAppearance.BorderColor = Theme.Edge; |
| 250 | b.FlatAppearance.MouseOverBackColor = Theme.Hover; |
| 251 | b.FlatAppearance.MouseDownBackColor = Theme.Line; |
| 252 | b.BackColor = Theme.Panel; |
| 253 | b.ForeColor = Theme.Text; |
| 254 | b.Font = ui; |
| 255 | b.Cursor = Cursors.Hand; |
| 256 | b.Click += click; |
| 257 | p.Controls.Add(b); |
| 258 | } |
| 259 | |
| 260 | private void Head(Control parent, Label l, string text) |
| 261 | { |
| 262 | l.Text = text; |
| 263 | l.Dock = DockStyle.Top; |
| 264 | l.Height = Theme.S(26); |
| 265 | l.Font = uiBold; |
| 266 | l.ForeColor = Theme.Orange; |
| 267 | l.BackColor = Theme.Back; |
| 268 | l.Padding = new Padding(Theme.S(8), Theme.S(6), 0, 0); |
| 269 | parent.Controls.Add(l); |
| 270 | } |
| 271 | |
| 272 | private void List(Control parent, ListBox l) |
| 273 | { |
| 274 | l.Dock = DockStyle.Fill; |
| 275 | l.BorderStyle = BorderStyle.None; |
| 276 | l.BackColor = Theme.Panel; |
| 277 | l.ForeColor = Theme.Text; |
| 278 | l.Font = mono; |
| 279 | l.DrawMode = DrawMode.OwnerDrawFixed; |
| 280 | l.ItemHeight = lineH; |
| 281 | l.IntegralHeight = false; |
| 282 | l.HorizontalScrollbar = true; |
| 283 | l.KeyDown += delegate (object s, KeyEventArgs e) |
| 284 | { |
| 285 | if (e.Control && e.KeyCode == Keys.C) { CopyFrom((ListBox)s, true); e.Handled = true; } |
| 286 | if (e.Control && e.KeyCode == Keys.A) { ListBox b = (ListBox)s; b.BeginUpdate(); for (int i = 0; i < b.Items.Count; i++) b.SetSelected(i, true); b.EndUpdate(); e.Handled = true; } |
| 287 | }; |
| 288 | parent.Controls.Add(l); |
| 289 | l.BringToFront(); |
| 290 | } |
| 291 | |
| 292 | private void LayoutTop() |
| 293 | { |
| 294 | int g = Theme.S(8), h = Theme.S(28), y = (top.Height - h) / 2, x = top.Width - g; |
| 295 | Button[] row = { folder, save, copy, clear, pause, pin, other }; |
| 296 | foreach (Button b in row) |
| 297 | { |
| 298 | int w = TextRenderer.MeasureText(b.Text, ui).Width + Theme.S(22); |
| 299 | x -= w; |
| 300 | b.Bounds = new Rectangle(x, y, w, h); |
| 301 | x -= Theme.S(6); |
| 302 | } |
| 303 | other.Visible = Monitors.Count > 1; |
| 304 | int left = other.Visible ? other.Left : pin.Left; |
| 305 | state.Bounds = new Rectangle(Theme.S(12), 0, Math.Max(Theme.S(60), left - Theme.S(20)), top.Height); |
| 306 | } |
| 307 | |
| 308 | private void LayoutBar() |
| 309 | { |
| 310 | int x = Theme.S(10), g = Theme.S(14); |
| 311 | foreach (string n in FilterNames) |
| 312 | { |
| 313 | CheckBox c = filters[n]; |
| 314 | c.Location = new Point(x, (bar.Height - c.Height) / 2); |
| 315 | x += c.Width + g; |
| 316 | } |
| 317 | int sw = Math.Max(Theme.S(120), Math.Min(Theme.S(260), bar.Width - x - Theme.S(60))); |
| 318 | search.Bounds = new Rectangle(bar.Width - sw - Theme.S(10), (bar.Height - search.Height) / 2, sw, search.Height); |
| 319 | filterLabel.Location = new Point(search.Left - filterLabel.Width - Theme.S(4), (bar.Height - filterLabel.Height) / 2); |
| 320 | } |
| 321 | |
| 322 | protected override void OnShown(EventArgs e) |
| 323 | { |
| 324 | base.OnShown(e); |
| 325 | LayoutTop(); |
| 326 | LayoutBar(); |
| 327 | try { split.SplitterDistance = (int)(split.Width * 0.46); } catch (Exception) { } |
| 328 | OpenFile(); |
| 329 | flushTimer.Start(); |
| 330 | } |
| 331 | |
| 332 | protected override void OnVisibleChanged(EventArgs e) |
| 333 | { |
| 334 | base.OnVisibleChanged(e); |
| 335 | if (Live != null) Live(Visible); |
| 336 | if (Visible) { OpenFile(); flushTimer.Start(); LayoutTop(); } |
| 337 | else flushTimer.Stop(); |
| 338 | } |
| 339 | |
| 340 | protected override void OnFormClosing(FormClosingEventArgs e) |
| 341 | { |
| 342 | if (!Quitting && e.CloseReason == CloseReason.UserClosing) |
| 343 | { |
| 344 | e.Cancel = true; |
| 345 | Hide(); |
| 346 | return; |
| 347 | } |
| 348 | Theme.Changed -= OnThemeChanged; |
| 349 | try { if (file != null) file.Close(); } catch (Exception) { } |
| 350 | file = null; |
| 351 | base.OnFormClosing(e); |
| 352 | } |
| 353 | |
| 354 | protected override bool ProcessCmdKey(ref Message msg, Keys keyData) |
| 355 | { |
| 356 | if (keyData == (Keys.Control | Keys.F)) { search.Focus(); search.SelectAll(); return true; } |
| 357 | if (keyData == Keys.F5) { SetPaused(!paused); return true; } |
| 358 | return base.ProcessCmdKey(ref msg, keyData); |
| 359 | } |
| 360 | |
| 361 | private void OpenFile() |
| 362 | { |
| 363 | if (file != null) return; |
| 364 | try |
| 365 | { |
| 366 | Directory.CreateDirectory(Path.GetDirectoryName(LogPath)); |
| 367 | file = new StreamWriter(LogPath, false, new UTF8Encoding(false)); |
| 368 | file.WriteLine("Ultra Paradise CU:Trainer v" + Program.Version + " debug log, started " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); |
| 369 | } |
| 370 | catch (Exception) { file = null; } |
| 371 | } |
| 372 | |
| 373 | public void MoveToOther() |
| 374 | { |
| 375 | Screen s = Monitors.Spare(Monitors.GameScreen(Game != null ? Game() : null), this); |
| 376 | if (s != null && s.DeviceName == Screen.FromControl(this).DeviceName) s = Monitors.Next(this); |
| 377 | if (s == null) { if (Say != null) Say("Only one screen is plugged in.", false); return; } |
| 378 | Monitors.MoveTo(this, s, true); |
| 379 | } |
| 380 | |
| 381 | public void SetLinked(bool on) |
| 382 | { |
| 383 | linked = on; |
| 384 | if (!on) { snap = ""; dirtyStatus = true; RebuildStatus(); } |
| 385 | UpdateState(); |
| 386 | } |
| 387 | |
| 388 | private void SetPaused(bool p) |
| 389 | { |
| 390 | paused = p; |
| 391 | pause.Text = p ? "Resume" : "Pause"; |
| 392 | pause.BackColor = p ? Theme.Orange : Theme.Panel; |
| 393 | pause.ForeColor = p ? Theme.OnAccent : Theme.Text; |
| 394 | LayoutTop(); |
| 395 | if (!p) { Refilter(); if (dirtyStatus) RebuildStatus(); } |
| 396 | UpdateState(); |
| 397 | } |
| 398 | |
| 399 | private void UpdateState() |
| 400 | { |
| 401 | string t; |
| 402 | Color c; |
| 403 | if (!linked) { t = "Not attached. Start the game and let the trainer attach, then everything shows up here."; c = Theme.Dim; } |
| 404 | else if (paused) { t = "Paused. New lines are still saved. Press Resume (F5) to catch up."; c = Theme.Warn; } |
| 405 | else if ((DateTime.UtcNow - lastSnap).TotalSeconds > 4) { t = "Attached. Waiting for the game (it sends nothing while it's loading)."; c = Theme.Warn; } |
| 406 | else { t = "Live - " + all.Count + " lines" + (Monitors.Count > 1 ? " - " + Monitors.Count + " screens" : ""); c = Theme.On; } |
| 407 | if (state.Text != t) state.Text = t; |
| 408 | state.ForeColor = c; |
| 409 | } |
| 410 | |
| 411 | public void OnSnapshot(string text) |
| 412 | { |
| 413 | snap = text; |
| 414 | lastSnap = DateTime.UtcNow; |
| 415 | dirtyStatus = true; |
| 416 | if (!paused && Visible) RebuildStatus(); |
| 417 | UpdateState(); |
| 418 | } |
| 419 | |
| 420 | public void OnSystem(string text) |
| 421 | { |
| 422 | sys = text; |
| 423 | dirtyStatus = true; |
| 424 | if (!paused && Visible) RebuildStatus(); |
| 425 | } |
| 426 | |
| 427 | private void RebuildStatus() |
| 428 | { |
| 429 | dirtyStatus = false; |
| 430 | List<string> lines = new List<string>(); |
| 431 | 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."); |
| 432 | else lines.AddRange(snap.Split(DebugCodes.NL)); |
| 433 | if (sys.Length > 0) { lines.Add("N"); lines.AddRange(sys.Split(DebugCodes.NL)); } |
| 434 | if (status.SelectedIndices.Count > 1) { dirtyStatus = true; return; } |
| 435 | int top = status.TopIndex; |
| 436 | status.BeginUpdate(); |
| 437 | int i = 0; |
| 438 | for (; i < lines.Count; i++) |
| 439 | { |
| 440 | if (i < status.Items.Count) { if ((string)status.Items[i] != lines[i]) status.Items[i] = lines[i]; } |
| 441 | else status.Items.Add(lines[i]); |
| 442 | } |
| 443 | while (status.Items.Count > lines.Count) status.Items.RemoveAt(status.Items.Count - 1); |
| 444 | if (top < status.Items.Count) status.TopIndex = top; |
| 445 | int widest = 0; |
| 446 | using (Graphics g = status.CreateGraphics()) |
| 447 | foreach (string l in lines) widest = Math.Max(widest, TextRenderer.MeasureText(g, l.Length > 1 ? l.Substring(1) : "", mono).Width); |
| 448 | status.HorizontalExtent = widest + Theme.S(24); |
| 449 | status.EndUpdate(); |
| 450 | } |
| 451 | |
| 452 | public void OnLog(string batch) |
| 453 | { |
| 454 | string now = DateTime.Now.ToString("HH:mm:ss.fff"); |
| 455 | List<DebugLine> fresh = new List<DebugLine>(); |
| 456 | foreach (string rec in batch.Split(DebugCodes.REC)) |
| 457 | { |
| 458 | int s = rec.IndexOf(DebugCodes.SEP); |
| 459 | if (s <= 0) continue; |
| 460 | string kind = rec.Substring(0, s); |
| 461 | string[] parts = rec.Substring(s + 1).Split(DebugCodes.NL); |
| 462 | for (int i = 0; i < parts.Length; i++) |
| 463 | { |
| 464 | if (i > 0 && i == parts.Length - 1 && parts[i].Length == 0) break; |
| 465 | DebugLine l = new DebugLine(); |
| 466 | l.Time = now; |
| 467 | l.Kind = kind; |
| 468 | l.Text = parts[i]; |
| 469 | l.First = i == 0; |
| 470 | fresh.Add(l); |
| 471 | if (file != null) |
| 472 | { |
| 473 | try { file.WriteLine((i == 0 ? now + " [" + kind + "] " : new string(' ', now.Length + kind.Length + 4)) + SysInfo.Scrub(parts[i])); } catch (Exception) { } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | if (fresh.Count == 0) return; |
| 478 | all.AddRange(fresh); |
| 479 | if (all.Count > 30000) all.RemoveRange(0, all.Count - 25000); |
| 480 | if (paused || !Visible) return; |
| 481 | bool follow = AtBottom(); |
| 482 | log.BeginUpdate(); |
| 483 | foreach (DebugLine l in fresh) if (Passes(l)) log.Items.Add(l); |
| 484 | if (log.Items.Count > 8000) { for (int i = 0; i < 1000; i++) log.Items.RemoveAt(0); } |
| 485 | if (follow && log.Items.Count > 0 && log.SelectedIndices.Count == 0) log.TopIndex = log.Items.Count - 1; |
| 486 | GrowExtent(fresh); |
| 487 | log.EndUpdate(); |
| 488 | } |
| 489 | |
| 490 | private void GrowExtent(List<DebugLine> lines) |
| 491 | { |
| 492 | int widest = log.HorizontalExtent; |
| 493 | using (Graphics g = log.CreateGraphics()) |
| 494 | foreach (DebugLine l in lines) |
| 495 | { |
| 496 | if (l.Text.Length * 9 < widest) continue; |
| 497 | widest = Math.Max(widest, TextRenderer.MeasureText(g, Prefix(l) + l.Text, mono).Width + Theme.S(24)); |
| 498 | } |
| 499 | log.HorizontalExtent = widest; |
| 500 | } |
| 501 | |
| 502 | private bool AtBottom() |
| 503 | { |
| 504 | int visible = Math.Max(1, log.ClientSize.Height / Math.Max(1, log.ItemHeight)); |
| 505 | return log.TopIndex + visible >= log.Items.Count - 1; |
| 506 | } |
| 507 | |
| 508 | private bool Passes(DebugLine l) |
| 509 | { |
| 510 | bool kindOk = false; |
| 511 | for (int i = 0; i < FilterNames.Length; i++) |
| 512 | { |
| 513 | if (Array.IndexOf(FilterKinds[i], l.Kind) < 0) continue; |
| 514 | kindOk = filters[FilterNames[i]].Checked; |
| 515 | break; |
| 516 | } |
| 517 | if (!kindOk && Array.IndexOf(Kinds, l.Kind) < 0 && l.Kind != "LIMB" && l.Kind != "GOOD" && l.Kind != "ALERT") kindOk = true; |
| 518 | if (!kindOk) return false; |
| 519 | string q = search.Text.Trim(); |
| 520 | return q.Length == 0 || l.Text.IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0 || l.Kind.IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0; |
| 521 | } |
| 522 | |
| 523 | private void Refilter() |
| 524 | { |
| 525 | if (paused) return; |
| 526 | log.BeginUpdate(); |
| 527 | log.Items.Clear(); |
| 528 | int start = 0; |
| 529 | List<DebugLine> shown = new List<DebugLine>(); |
| 530 | for (int i = all.Count - 1; i >= start && shown.Count < 7000; i--) if (Passes(all[i])) shown.Add(all[i]); |
| 531 | shown.Reverse(); |
| 532 | log.Items.AddRange(shown.ToArray()); |
| 533 | log.HorizontalExtent = 0; |
| 534 | GrowExtent(shown); |
| 535 | if (log.Items.Count > 0) log.TopIndex = log.Items.Count - 1; |
| 536 | log.EndUpdate(); |
| 537 | } |
| 538 | |
| 539 | private static string Prefix(DebugLine l) |
| 540 | { |
| 541 | return l.Time + " " + (l.First ? l.Kind.PadRight(8) : new string(' ', 8)) + " "; |
| 542 | } |
| 543 | |
| 544 | private Color KindColor(string k) |
| 545 | { |
| 546 | switch (k) |
| 547 | { |
| 548 | case "ERROR": case "ALERT": return Theme.Bad; |
| 549 | case "WARNING": case "LIMB": return Theme.Warn; |
| 550 | case "EVENT": case "SCENE": return Theme.Orange; |
| 551 | case "GOOD": return Theme.On; |
| 552 | case "TRAINER": case "COMMAND": return Theme.Bright; |
| 553 | default: return Theme.Text; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | private void DrawLog(object sender, DrawItemEventArgs e) |
| 558 | { |
| 559 | if (e.Index < 0 || e.Index >= log.Items.Count) return; |
| 560 | DebugLine l = log.Items[e.Index] as DebugLine; |
| 561 | bool sel = (e.State & DrawItemState.Selected) != 0; |
| 562 | using (SolidBrush b = new SolidBrush(sel ? Theme.Hover : (l != null && (l.Kind == "ERROR" || l.Kind == "ALERT") ? Theme.Sunk : Theme.Panel))) e.Graphics.FillRectangle(b, e.Bounds); |
| 563 | if (l == null) return; |
| 564 | int x = e.Bounds.X + Theme.S(6); |
| 565 | TextFormatFlags tf = TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding; |
| 566 | Rectangle r = new Rectangle(x, e.Bounds.Y, e.Bounds.Width + log.HorizontalExtent, e.Bounds.Height); |
| 567 | string time = l.First ? l.Time + " " : new string(' ', l.Time.Length + 2); |
| 568 | TextRenderer.DrawText(e.Graphics, time, mono, r, Theme.Dim, tf); |
| 569 | int tw = TextRenderer.MeasureText(e.Graphics, time, mono, Size.Empty, tf).Width; |
| 570 | r.X += tw; |
| 571 | Color kc = KindColor(l.Kind); |
| 572 | string kind = (l.First ? l.Kind : "").PadRight(8) + " "; |
| 573 | TextRenderer.DrawText(e.Graphics, kind, monoBold, r, kc, tf); |
| 574 | r.X += TextRenderer.MeasureText(e.Graphics, kind, monoBold, Size.Empty, tf).Width; |
| 575 | Color tc = l.Kind == "GAME" || l.Kind == "BEPINEX" || l.Kind == "SYSTEM" ? Theme.Text : kc; |
| 576 | if (!l.First && (l.Kind == "ERROR")) tc = Theme.Dim; |
| 577 | TextRenderer.DrawText(e.Graphics, l.Text, mono, r, tc, tf); |
| 578 | } |
| 579 | |
| 580 | private void DrawStatus(object sender, DrawItemEventArgs e) |
| 581 | { |
| 582 | if (e.Index < 0 || e.Index >= status.Items.Count) return; |
| 583 | string s = status.Items[e.Index] as string ?? ""; |
| 584 | bool sel = (e.State & DrawItemState.Selected) != 0; |
| 585 | char code = s.Length > 0 ? s[0] : 'N'; |
| 586 | string text = s.Length > 1 ? s.Substring(1) : ""; |
| 587 | using (SolidBrush b = new SolidBrush(sel ? Theme.Hover : Theme.Panel)) e.Graphics.FillRectangle(b, e.Bounds); |
| 588 | Color c = Theme.Text; |
| 589 | Font f = mono; |
| 590 | switch (code) |
| 591 | { |
| 592 | case 'H': c = Theme.Orange; f = monoBold; break; |
| 593 | case 'D': c = Theme.Dim; break; |
| 594 | case 'G': c = Theme.On; break; |
| 595 | case 'Y': c = Theme.Warn; break; |
| 596 | case 'R': c = Theme.Bad; f = monoBold; break; |
| 597 | } |
| 598 | Rectangle r = new Rectangle(e.Bounds.X + Theme.S(8), e.Bounds.Y, e.Bounds.Width + status.HorizontalExtent, e.Bounds.Height); |
| 599 | if (code == 'H' && e.Index > 0) |
| 600 | { |
| 601 | using (Pen p = new Pen(Theme.Line)) e.Graphics.DrawLine(p, e.Bounds.X + Theme.S(8), e.Bounds.Y + 1, e.Bounds.Right - Theme.S(8), e.Bounds.Y + 1); |
| 602 | } |
| 603 | TextRenderer.DrawText(e.Graphics, text, f, r, c, TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding); |
| 604 | } |
| 605 | |
| 606 | private string LineText(object o) |
| 607 | { |
| 608 | DebugLine l = o as DebugLine; |
| 609 | if (l != null) return Prefix(l) + l.Text; |
| 610 | string s = o as string ?? ""; |
| 611 | if (s.Length == 0) return ""; |
| 612 | return s[0] == 'H' ? "== " + s.Substring(1) + " ==" : s.Substring(1); |
| 613 | } |
| 614 | |
| 615 | private void CopyFrom(ListBox l, bool selectedOnly) |
| 616 | { |
| 617 | StringBuilder sb = new StringBuilder(); |
| 618 | if (selectedOnly && l.SelectedItems.Count > 0) foreach (object o in l.SelectedItems) sb.Append(LineText(o)).Append("\r\n"); |
| 619 | else foreach (object o in l.Items) sb.Append(LineText(o)).Append("\r\n"); |
| 620 | if (sb.Length == 0) return; |
| 621 | try { Clipboard.SetText(SysInfo.Scrub(sb.ToString())); } catch (Exception) { } |
| 622 | } |
| 623 | |
| 624 | private void CopyLines() |
| 625 | { |
| 626 | StringBuilder sb = new StringBuilder(); |
| 627 | sb.Append("=== Ultra Paradise CU:Trainer v").Append(Program.Version).Append(" debug ===\r\n\r\n"); |
| 628 | foreach (object o in status.Items) sb.Append(LineText(o)).Append("\r\n"); |
| 629 | sb.Append("\r\n=== Live feed ===\r\n"); |
| 630 | ListBox l = log; |
| 631 | if (l.SelectedItems.Count > 0) foreach (object o in l.SelectedItems) sb.Append(LineText(o)).Append("\r\n"); |
| 632 | else for (int i = Math.Max(0, l.Items.Count - 500); i < l.Items.Count; i++) sb.Append(LineText(l.Items[i])).Append("\r\n"); |
| 633 | try |
| 634 | { |
| 635 | Clipboard.SetText(SysInfo.Scrub(sb.ToString())); |
| 636 | if (Say != null) Say("Copied the status and " + (l.SelectedItems.Count > 0 ? "the selected lines" : "the last " + Math.Min(500, l.Items.Count) + " lines") + ".", false); |
| 637 | } |
| 638 | catch (Exception) { if (Say != null) Say("Couldn't copy to the clipboard. Try Save.", true); } |
| 639 | } |
| 640 | |
| 641 | private void SaveLog() |
| 642 | { |
| 643 | using (SaveFileDialog d = new SaveFileDialog()) |
| 644 | { |
| 645 | d.Filter = "Text file (*.txt)|*.txt"; |
| 646 | d.FileName = "UltraParadise-debug-" + DateTime.Now.ToString("yyyyMMdd-HHmm") + ".txt"; |
| 647 | if (d.ShowDialog(this) != DialogResult.OK) return; |
| 648 | try |
| 649 | { |
| 650 | StringBuilder sb = new StringBuilder(); |
| 651 | sb.Append("=== Ultra Paradise CU:Trainer v").Append(Program.Version).Append(" debug, saved ").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Append(" ===\r\n\r\n"); |
| 652 | foreach (object o in status.Items) sb.Append(LineText(o)).Append("\r\n"); |
| 653 | sb.Append("\r\n=== Live feed (").Append(all.Count).Append(" lines) ===\r\n"); |
| 654 | foreach (DebugLine l in all) sb.Append(Prefix(l)).Append(l.Text).Append("\r\n"); |
| 655 | File.WriteAllText(d.FileName, SysInfo.Scrub(sb.ToString()), new UTF8Encoding(false)); |
| 656 | if (Say != null) Say("Saved " + Path.GetFileName(d.FileName) + ".", false); |
| 657 | } |
| 658 | catch (Exception ex) { if (Say != null) Say("Couldn't save it: " + ex.Message, true); } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | internal static class DebugCodes |
| 664 | { |
| 665 | public const char NL = '\u001e'; |
| 666 | public const char REC = '\u001d'; |
| 667 | public const char SEP = '\u001c'; |
| 668 | } |
| 669 | } |