using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; namespace UltraParadise { internal static class Monitors { public static Process GameProc; public static int Count { get { try { return Screen.AllScreens.Length; } catch (Exception) { return 1; } } } public static string Advice() { int n = Count; if (n < 2) return "Only one screen found. A second screen is recommended, see below."; Screen gs = GameScreen(GameProc); 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") + "."; } public static int IndexOf(Screen s) { Screen[] all = Screen.AllScreens; for (int i = 0; i < all.Length; i++) if (all[i].DeviceName == s.DeviceName) return i; return 0; } public static string Name(Screen s) { return "Screen " + (IndexOf(s) + 1) + " (" + s.Bounds.Width + "x" + s.Bounds.Height + (s.Primary ? ", main" : "") + ")"; } public static Screen GameScreen(Process game) { if (game == null) return null; try { game.Refresh(); IntPtr h = game.MainWindowHandle; if (h == IntPtr.Zero) return null; return Screen.FromHandle(h); } catch (Exception) { return null; } } public static Screen Spare(Screen game, Form near) { Screen[] all = Screen.AllScreens; if (all.Length < 2) return null; Screen avoid = game ?? Screen.PrimaryScreen; Screen best = null; foreach (Screen s in all) { if (s.DeviceName == avoid.DeviceName) continue; if (best == null || (long)s.Bounds.Width * s.Bounds.Height > (long)best.Bounds.Width * best.Bounds.Height) best = s; } return best; } public static Screen Next(Form f) { Screen[] all = Screen.AllScreens; if (all.Length < 2) return null; int i = IndexOf(Screen.FromControl(f)); return all[(i + 1) % all.Length]; } public static void MoveTo(Form f, Screen s, bool fill) { if (f == null || s == null) return; bool max = f.WindowState == FormWindowState.Maximized; if (f.WindowState != FormWindowState.Normal) f.WindowState = FormWindowState.Normal; Rectangle wa = s.WorkingArea; int w = Math.Min(f.Width, wa.Width), h = Math.Min(f.Height, wa.Height); 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); } f.StartPosition = FormStartPosition.Manual; f.Bounds = new Rectangle(wa.X + (wa.Width - w) / 2, wa.Y + (wa.Height - h) / 2, w, h); if (max) f.WindowState = FormWindowState.Maximized; } public static string Describe(Process game) { Screen[] all; try { all = Screen.AllScreens; } catch (Exception) { return "Couldn't read your screens."; } Screen gs = GameScreen(game); StringBuilder sb = new StringBuilder(); sb.Append(all.Length == 1 ? "1 screen found" : all.Length + " screens found"); for (int i = 0; i < all.Length; i++) { Screen s = all[i]; sb.Append("\n").Append(Name(s)); if (gs != null && gs.DeviceName == s.DeviceName) sb.Append(" - the game is here"); } return sb.ToString(); } } internal class DebugLine { public string Time, Kind, Text; public bool First; } internal class DebugForm : Form { public Action Live; public Action Say; public Func Game; public bool Quitting; private readonly Panel top = new Panel(), bar = new Panel(); private readonly Label state = new Label(), filterLabel = new Label(); private readonly Button other = new Button(), pin = new Button(), pause = new Button(), clear = new Button(), copy = new Button(), save = new Button(), folder = new Button(); private readonly TextBox search = new TextBox(); private readonly SplitContainer split = new SplitContainer(); private readonly ListBox status = new ListBox(), log = new ListBox(); private readonly Label statusHead = new Label(), logHead = new Label(); private readonly Dictionary filters = new Dictionary(); private readonly List all = new List(); private readonly Timer flushTimer = new Timer(); private string snap = "", sys = ""; private bool paused, linked, dirtyStatus; private DateTime lastSnap = DateTime.MinValue; private StreamWriter file; private Font mono, monoBold, ui, uiBold; private int lineH; private static readonly string[] Kinds = { "GAME", "WARNING", "ERROR", "EVENT", "TRAINER", "COMMAND", "SCENE", "SYSTEM", "BEPINEX" }; private static readonly string[] FilterNames = { "Game log", "Warnings", "Errors", "Events", "Trainer", "Commands" }; private static readonly string[][] FilterKinds = { new[] { "GAME", "SCENE", "SYSTEM", "BEPINEX" }, new[] { "WARNING" }, new[] { "ERROR" }, new[] { "EVENT", "LIMB", "GOOD", "ALERT" }, new[] { "TRAINER" }, new[] { "COMMAND" } }; public static string LogPath { get { return Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "UltraParadise"), "debug.log"); } } public DebugForm() { AutoScaleMode = AutoScaleMode.None; Text = "Ultra Paradise - Debug"; try { Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); } catch (Exception) { } BackColor = Theme.Back; ForeColor = Theme.Text; ui = Theme.F("Segoe UI", 9f); uiBold = Theme.F("Segoe UI", 9f, FontStyle.Bold); mono = Theme.F("Consolas", 9f); monoBold = Theme.F("Consolas", 9f, FontStyle.Bold); Font = ui; using (Graphics g = CreateGraphics()) lineH = (int)Math.Ceiling(mono.GetHeight(g)) + Theme.S(3); ClientSize = new Size(Theme.S(1100), Theme.S(680)); MinimumSize = SizeFromClientSize(new Size(Theme.S(640), Theme.S(380))); StartPosition = FormStartPosition.CenterScreen; KeyPreview = true; top.Dock = DockStyle.Top; top.Height = Theme.S(46); top.BackColor = Theme.Side; state.Font = uiBold; state.ForeColor = Theme.Dim; state.BackColor = Theme.Side; state.AutoEllipsis = true; state.TextAlign = ContentAlignment.MiddleLeft; top.Controls.Add(state); Btn(top, other, "Other Screen", delegate { MoveToOther(); }); Btn(top, pin, "Pin on Top", delegate { TopMost = !TopMost; pin.BackColor = TopMost ? Theme.Orange : Theme.Panel; pin.ForeColor = TopMost ? Theme.OnAccent : Theme.Text; }); Btn(top, pause, "Pause", delegate { SetPaused(!paused); }); Btn(top, clear, "Clear", delegate { all.Clear(); log.Items.Clear(); }); Btn(top, copy, "Copy", delegate { CopyLines(); }); Btn(top, save, "Save...", delegate { SaveLog(); }); Btn(top, folder, "Log Folder", delegate { try { Process.Start("explorer.exe", "/select,\"" + LogPath + "\""); } catch (Exception) { } }); top.Resize += delegate { LayoutTop(); }; bar.Dock = DockStyle.Top; bar.Height = Theme.S(34); bar.BackColor = Theme.Panel; for (int i = 0; i < FilterNames.Length; i++) { CheckBox c = new CheckBox(); c.Text = FilterNames[i]; c.Checked = true; c.AutoSize = true; c.Font = ui; c.ForeColor = Theme.Text; c.BackColor = Theme.Panel; c.FlatStyle = FlatStyle.Flat; c.Cursor = Cursors.Hand; c.CheckedChanged += delegate { Refilter(); }; filters[FilterNames[i]] = c; bar.Controls.Add(c); } filterLabel.Text = "Find:"; filterLabel.Font = ui; filterLabel.ForeColor = Theme.Dim; filterLabel.BackColor = Theme.Panel; filterLabel.AutoSize = true; bar.Controls.Add(filterLabel); search.BorderStyle = BorderStyle.FixedSingle; search.BackColor = Theme.Back; search.ForeColor = Theme.Text; search.Font = ui; search.TextChanged += delegate { Refilter(); }; bar.Controls.Add(search); bar.Resize += delegate { LayoutBar(); }; split.Dock = DockStyle.Fill; split.BackColor = Theme.Line; split.SplitterWidth = Theme.S(5); split.Panel1.BackColor = Theme.Back; split.Panel2.BackColor = Theme.Back; Head(split.Panel1, statusHead, "LIVE STATUS"); Head(split.Panel2, logHead, "LIVE FEED"); List(split.Panel1, status); List(split.Panel2, log); status.SelectionMode = SelectionMode.MultiExtended; log.SelectionMode = SelectionMode.MultiExtended; status.DrawItem += DrawStatus; log.DrawItem += DrawLog; Controls.Add(split); Controls.Add(bar); Controls.Add(top); flushTimer.Interval = 1000; flushTimer.Tick += delegate { try { if (file != null) file.Flush(); } catch (Exception) { } UpdateState(); }; Theme.Changed += OnThemeChanged; SetLinked(false); } private void OnThemeChanged(Color[] before) { if (IsDisposed) return; Theme.Recolor(this, before, Theme.Tokens()); status.Invalidate(); log.Invalidate(); } private void Btn(Panel p, Button b, string text, EventHandler click) { b.Text = text; b.FlatStyle = FlatStyle.Flat; b.FlatAppearance.BorderColor = Theme.Edge; b.FlatAppearance.MouseOverBackColor = Theme.Hover; b.FlatAppearance.MouseDownBackColor = Theme.Line; b.BackColor = Theme.Panel; b.ForeColor = Theme.Text; b.Font = ui; b.Cursor = Cursors.Hand; b.Click += click; p.Controls.Add(b); } private void Head(Control parent, Label l, string text) { l.Text = text; l.Dock = DockStyle.Top; l.Height = Theme.S(26); l.Font = uiBold; l.ForeColor = Theme.Orange; l.BackColor = Theme.Back; l.Padding = new Padding(Theme.S(8), Theme.S(6), 0, 0); parent.Controls.Add(l); } private void List(Control parent, ListBox l) { l.Dock = DockStyle.Fill; l.BorderStyle = BorderStyle.None; l.BackColor = Theme.Panel; l.ForeColor = Theme.Text; l.Font = mono; l.DrawMode = DrawMode.OwnerDrawFixed; l.ItemHeight = lineH; l.IntegralHeight = false; l.HorizontalScrollbar = true; l.KeyDown += delegate (object s, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.C) { CopyFrom((ListBox)s, true); e.Handled = true; } 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; } }; parent.Controls.Add(l); l.BringToFront(); } private void LayoutTop() { int g = Theme.S(8), h = Theme.S(28), y = (top.Height - h) / 2, x = top.Width - g; Button[] row = { folder, save, copy, clear, pause, pin, other }; foreach (Button b in row) { int w = TextRenderer.MeasureText(b.Text, ui).Width + Theme.S(22); x -= w; b.Bounds = new Rectangle(x, y, w, h); x -= Theme.S(6); } other.Visible = Monitors.Count > 1; int left = other.Visible ? other.Left : pin.Left; state.Bounds = new Rectangle(Theme.S(12), 0, Math.Max(Theme.S(60), left - Theme.S(20)), top.Height); } private void LayoutBar() { int x = Theme.S(10), g = Theme.S(14); foreach (string n in FilterNames) { CheckBox c = filters[n]; c.Location = new Point(x, (bar.Height - c.Height) / 2); x += c.Width + g; } int sw = Math.Max(Theme.S(120), Math.Min(Theme.S(260), bar.Width - x - Theme.S(60))); search.Bounds = new Rectangle(bar.Width - sw - Theme.S(10), (bar.Height - search.Height) / 2, sw, search.Height); filterLabel.Location = new Point(search.Left - filterLabel.Width - Theme.S(4), (bar.Height - filterLabel.Height) / 2); } protected override void OnShown(EventArgs e) { base.OnShown(e); LayoutTop(); LayoutBar(); try { split.SplitterDistance = (int)(split.Width * 0.46); } catch (Exception) { } OpenFile(); flushTimer.Start(); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (Live != null) Live(Visible); if (Visible) { OpenFile(); flushTimer.Start(); LayoutTop(); } else flushTimer.Stop(); } protected override void OnFormClosing(FormClosingEventArgs e) { if (!Quitting && e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; Hide(); return; } Theme.Changed -= OnThemeChanged; try { if (file != null) file.Close(); } catch (Exception) { } file = null; base.OnFormClosing(e); } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Control | Keys.F)) { search.Focus(); search.SelectAll(); return true; } if (keyData == Keys.F5) { SetPaused(!paused); return true; } return base.ProcessCmdKey(ref msg, keyData); } private void OpenFile() { if (file != null) return; try { Directory.CreateDirectory(Path.GetDirectoryName(LogPath)); file = new StreamWriter(LogPath, false, new UTF8Encoding(false)); file.WriteLine("Ultra Paradise CU:Trainer v" + Program.Version + " debug log, started " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } catch (Exception) { file = null; } } public void MoveToOther() { Screen s = Monitors.Spare(Monitors.GameScreen(Game != null ? Game() : null), this); if (s != null && s.DeviceName == Screen.FromControl(this).DeviceName) s = Monitors.Next(this); if (s == null) { if (Say != null) Say("Only one screen is plugged in.", false); return; } Monitors.MoveTo(this, s, true); } public void SetLinked(bool on) { linked = on; if (!on) { snap = ""; dirtyStatus = true; RebuildStatus(); } UpdateState(); } private void SetPaused(bool p) { paused = p; pause.Text = p ? "Resume" : "Pause"; pause.BackColor = p ? Theme.Orange : Theme.Panel; pause.ForeColor = p ? Theme.OnAccent : Theme.Text; LayoutTop(); if (!p) { Refilter(); if (dirtyStatus) RebuildStatus(); } UpdateState(); } private void UpdateState() { string t; Color c; if (!linked) { t = "Not attached. Start the game and let the trainer attach, then everything shows up here."; c = Theme.Dim; } else if (paused) { t = "Paused. New lines are still saved. Press Resume (F5) to catch up."; c = Theme.Warn; } else if ((DateTime.UtcNow - lastSnap).TotalSeconds > 4) { t = "Attached. Waiting for the game (it sends nothing while it's loading)."; c = Theme.Warn; } else { t = "Live - " + all.Count + " lines" + (Monitors.Count > 1 ? " - " + Monitors.Count + " screens" : ""); c = Theme.On; } if (state.Text != t) state.Text = t; state.ForeColor = c; } public void OnSnapshot(string text) { snap = text; lastSnap = DateTime.UtcNow; dirtyStatus = true; if (!paused && Visible) RebuildStatus(); UpdateState(); } public void OnSystem(string text) { sys = text; dirtyStatus = true; if (!paused && Visible) RebuildStatus(); } private void RebuildStatus() { dirtyStatus = false; List lines = new List(); 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."); else lines.AddRange(snap.Split(DebugCodes.NL)); if (sys.Length > 0) { lines.Add("N"); lines.AddRange(sys.Split(DebugCodes.NL)); } if (status.SelectedIndices.Count > 1) { dirtyStatus = true; return; } int top = status.TopIndex; status.BeginUpdate(); int i = 0; for (; i < lines.Count; i++) { if (i < status.Items.Count) { if ((string)status.Items[i] != lines[i]) status.Items[i] = lines[i]; } else status.Items.Add(lines[i]); } while (status.Items.Count > lines.Count) status.Items.RemoveAt(status.Items.Count - 1); if (top < status.Items.Count) status.TopIndex = top; int widest = 0; using (Graphics g = status.CreateGraphics()) foreach (string l in lines) widest = Math.Max(widest, TextRenderer.MeasureText(g, l.Length > 1 ? l.Substring(1) : "", mono).Width); status.HorizontalExtent = widest + Theme.S(24); status.EndUpdate(); } public void OnLog(string batch) { string now = DateTime.Now.ToString("HH:mm:ss.fff"); List fresh = new List(); foreach (string rec in batch.Split(DebugCodes.REC)) { int s = rec.IndexOf(DebugCodes.SEP); if (s <= 0) continue; string kind = rec.Substring(0, s); string[] parts = rec.Substring(s + 1).Split(DebugCodes.NL); for (int i = 0; i < parts.Length; i++) { if (i > 0 && i == parts.Length - 1 && parts[i].Length == 0) break; DebugLine l = new DebugLine(); l.Time = now; l.Kind = kind; l.Text = parts[i]; l.First = i == 0; fresh.Add(l); if (file != null) { try { file.WriteLine((i == 0 ? now + " [" + kind + "] " : new string(' ', now.Length + kind.Length + 4)) + SysInfo.Scrub(parts[i])); } catch (Exception) { } } } } if (fresh.Count == 0) return; all.AddRange(fresh); if (all.Count > 30000) all.RemoveRange(0, all.Count - 25000); if (paused || !Visible) return; bool follow = AtBottom(); log.BeginUpdate(); foreach (DebugLine l in fresh) if (Passes(l)) log.Items.Add(l); if (log.Items.Count > 8000) { for (int i = 0; i < 1000; i++) log.Items.RemoveAt(0); } if (follow && log.Items.Count > 0 && log.SelectedIndices.Count == 0) log.TopIndex = log.Items.Count - 1; GrowExtent(fresh); log.EndUpdate(); } private void GrowExtent(List lines) { int widest = log.HorizontalExtent; using (Graphics g = log.CreateGraphics()) foreach (DebugLine l in lines) { if (l.Text.Length * 9 < widest) continue; widest = Math.Max(widest, TextRenderer.MeasureText(g, Prefix(l) + l.Text, mono).Width + Theme.S(24)); } log.HorizontalExtent = widest; } private bool AtBottom() { int visible = Math.Max(1, log.ClientSize.Height / Math.Max(1, log.ItemHeight)); return log.TopIndex + visible >= log.Items.Count - 1; } private bool Passes(DebugLine l) { bool kindOk = false; for (int i = 0; i < FilterNames.Length; i++) { if (Array.IndexOf(FilterKinds[i], l.Kind) < 0) continue; kindOk = filters[FilterNames[i]].Checked; break; } if (!kindOk && Array.IndexOf(Kinds, l.Kind) < 0 && l.Kind != "LIMB" && l.Kind != "GOOD" && l.Kind != "ALERT") kindOk = true; if (!kindOk) return false; string q = search.Text.Trim(); return q.Length == 0 || l.Text.IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0 || l.Kind.IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0; } private void Refilter() { if (paused) return; log.BeginUpdate(); log.Items.Clear(); int start = 0; List shown = new List(); for (int i = all.Count - 1; i >= start && shown.Count < 7000; i--) if (Passes(all[i])) shown.Add(all[i]); shown.Reverse(); log.Items.AddRange(shown.ToArray()); log.HorizontalExtent = 0; GrowExtent(shown); if (log.Items.Count > 0) log.TopIndex = log.Items.Count - 1; log.EndUpdate(); } private static string Prefix(DebugLine l) { return l.Time + " " + (l.First ? l.Kind.PadRight(8) : new string(' ', 8)) + " "; } private Color KindColor(string k) { switch (k) { case "ERROR": case "ALERT": return Theme.Bad; case "WARNING": case "LIMB": return Theme.Warn; case "EVENT": case "SCENE": return Theme.Orange; case "GOOD": return Theme.On; case "TRAINER": case "COMMAND": return Theme.Bright; default: return Theme.Text; } } private void DrawLog(object sender, DrawItemEventArgs e) { if (e.Index < 0 || e.Index >= log.Items.Count) return; DebugLine l = log.Items[e.Index] as DebugLine; bool sel = (e.State & DrawItemState.Selected) != 0; 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); if (l == null) return; int x = e.Bounds.X + Theme.S(6); TextFormatFlags tf = TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding; Rectangle r = new Rectangle(x, e.Bounds.Y, e.Bounds.Width + log.HorizontalExtent, e.Bounds.Height); string time = l.First ? l.Time + " " : new string(' ', l.Time.Length + 2); TextRenderer.DrawText(e.Graphics, time, mono, r, Theme.Dim, tf); int tw = TextRenderer.MeasureText(e.Graphics, time, mono, Size.Empty, tf).Width; r.X += tw; Color kc = KindColor(l.Kind); string kind = (l.First ? l.Kind : "").PadRight(8) + " "; TextRenderer.DrawText(e.Graphics, kind, monoBold, r, kc, tf); r.X += TextRenderer.MeasureText(e.Graphics, kind, monoBold, Size.Empty, tf).Width; Color tc = l.Kind == "GAME" || l.Kind == "BEPINEX" || l.Kind == "SYSTEM" ? Theme.Text : kc; if (!l.First && (l.Kind == "ERROR")) tc = Theme.Dim; TextRenderer.DrawText(e.Graphics, l.Text, mono, r, tc, tf); } private void DrawStatus(object sender, DrawItemEventArgs e) { if (e.Index < 0 || e.Index >= status.Items.Count) return; string s = status.Items[e.Index] as string ?? ""; bool sel = (e.State & DrawItemState.Selected) != 0; char code = s.Length > 0 ? s[0] : 'N'; string text = s.Length > 1 ? s.Substring(1) : ""; using (SolidBrush b = new SolidBrush(sel ? Theme.Hover : Theme.Panel)) e.Graphics.FillRectangle(b, e.Bounds); Color c = Theme.Text; Font f = mono; switch (code) { case 'H': c = Theme.Orange; f = monoBold; break; case 'D': c = Theme.Dim; break; case 'G': c = Theme.On; break; case 'Y': c = Theme.Warn; break; case 'R': c = Theme.Bad; f = monoBold; break; } Rectangle r = new Rectangle(e.Bounds.X + Theme.S(8), e.Bounds.Y, e.Bounds.Width + status.HorizontalExtent, e.Bounds.Height); if (code == 'H' && e.Index > 0) { 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); } TextRenderer.DrawText(e.Graphics, text, f, r, c, TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding); } private string LineText(object o) { DebugLine l = o as DebugLine; if (l != null) return Prefix(l) + l.Text; string s = o as string ?? ""; if (s.Length == 0) return ""; return s[0] == 'H' ? "== " + s.Substring(1) + " ==" : s.Substring(1); } private void CopyFrom(ListBox l, bool selectedOnly) { StringBuilder sb = new StringBuilder(); if (selectedOnly && l.SelectedItems.Count > 0) foreach (object o in l.SelectedItems) sb.Append(LineText(o)).Append("\r\n"); else foreach (object o in l.Items) sb.Append(LineText(o)).Append("\r\n"); if (sb.Length == 0) return; try { Clipboard.SetText(SysInfo.Scrub(sb.ToString())); } catch (Exception) { } } private void CopyLines() { StringBuilder sb = new StringBuilder(); sb.Append("=== Ultra Paradise CU:Trainer v").Append(Program.Version).Append(" debug ===\r\n\r\n"); foreach (object o in status.Items) sb.Append(LineText(o)).Append("\r\n"); sb.Append("\r\n=== Live feed ===\r\n"); ListBox l = log; if (l.SelectedItems.Count > 0) foreach (object o in l.SelectedItems) sb.Append(LineText(o)).Append("\r\n"); 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"); try { Clipboard.SetText(SysInfo.Scrub(sb.ToString())); 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); } catch (Exception) { if (Say != null) Say("Couldn't copy to the clipboard. Try Save.", true); } } private void SaveLog() { using (SaveFileDialog d = new SaveFileDialog()) { d.Filter = "Text file (*.txt)|*.txt"; d.FileName = "UltraParadise-debug-" + DateTime.Now.ToString("yyyyMMdd-HHmm") + ".txt"; if (d.ShowDialog(this) != DialogResult.OK) return; try { StringBuilder sb = new StringBuilder(); 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"); foreach (object o in status.Items) sb.Append(LineText(o)).Append("\r\n"); sb.Append("\r\n=== Live feed (").Append(all.Count).Append(" lines) ===\r\n"); foreach (DebugLine l in all) sb.Append(Prefix(l)).Append(l.Text).Append("\r\n"); File.WriteAllText(d.FileName, SysInfo.Scrub(sb.ToString()), new UTF8Encoding(false)); if (Say != null) Say("Saved " + Path.GetFileName(d.FileName) + ".", false); } catch (Exception ex) { if (Say != null) Say("Couldn't save it: " + ex.Message, true); } } } } internal static class DebugCodes { public const char NL = '\u001e'; public const char REC = '\u001d'; public const char SEP = '\u001c'; } }