using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Layout; namespace UltraParadise.Linux { internal sealed class LModEntry { public string Section, Key, Kind, Value, Desc, Min, Max; public string[] Opts; } internal sealed class LModInfo { public string Guid, Name, Version, Stat = ""; public bool CanTake, Taken; public readonly List<(string id, string label)> Actions = new(); public readonly List Entries = new(); } internal sealed partial class MainWindow { private const char MREC = '\u001d', MSEP = '\u001c'; private static readonly string[] TakeGuids = { "getstinky", "nodurability" }; private readonly List myMods = new(); private readonly StackPanel myModsList = new() { Spacing = 6 }; private readonly StackPanel myModsDetail = new() { Spacing = 0 }; private readonly TextBlock myModsStat = T("", 13.5, Ui.B(Ui.Good), true, true); private string myModsSel; private bool myModsLoaded; private Control MyModsPage() { Grid g = new() { ColumnDefinitions = new ColumnDefinitions("300,*") }; StackPanel left = new() { Margin = new Thickness(0, 0, 7, 0) }; Border l = Section("MY MODS IN THE GAME", out StackPanel lb); lb.Children.Add(myModsList); left.Children.Add(l); g.Children.Add(left); Grid.SetColumn(myModsDetail, 1); myModsDetail.Margin = new Thickness(7, 0, 0, 0); g.Children.Add(myModsDetail); RebuildMyMods(); return Scroll(g); } private void MyModsConnected() { foreach (string g in TakeGuids) if (Settings.On("take." + g, false)) link.Send("MODTAKE " + g + " 1"); link.Send("MODS"); } private void MyModsDisconnected() { myMods.Clear(); myModsLoaded = false; RebuildMyMods(); } private void MyModsParse(string body) { myMods.Clear(); myModsLoaded = true; LModInfo cur = null; foreach (string rec in body.Split(MREC)) { string[] f = rec.Split(MSEP); if (f.Length >= 7 && f[0] == "M") { cur = new LModInfo { Guid = f[1], Name = f[2], Version = f[3], CanTake = f[4] == "1", Taken = f[5] == "1" }; foreach (string a in f[6].Split(';', StringSplitOptions.RemoveEmptyEntries)) { int eq = a.IndexOf('='); if (eq > 0) cur.Actions.Add((a[..eq], a[(eq + 1)..])); } myMods.Add(cur); } else if (f.Length >= 10 && f[0] == "E" && cur != null && cur.Guid == f[1]) cur.Entries.Add(new LModEntry { Section = f[2], Key = f[3], Kind = f[4], Value = f[5], Desc = f[6], Min = f[7], Max = f[8], Opts = f[9].Length > 0 ? f[9].Split('|') : Array.Empty() }); } if (myModsSel == null || myMods.All(m => m.Guid != myModsSel)) myModsSel = myMods.FirstOrDefault()?.Guid; RebuildMyMods(); } private void MyModsStats(string body) { foreach (string rec in body.Split(MREC)) { string[] f = rec.Split(MSEP); if (f.Length < 2) continue; foreach (LModInfo m in myMods) if (m.Guid == f[0]) m.Stat = f[1]; } LModInfo s = myMods.FirstOrDefault(m => m.Guid == myModsSel); myModsStat.Text = s?.Stat ?? ""; myModsStat.IsVisible = myModsStat.Text.Length > 0; } private static string NiceKey(string key) { StringBuilder sb = new(); for (int i = 0; i < key.Length; i++) { if (i > 0 && char.IsUpper(key[i]) && !char.IsUpper(key[i - 1])) sb.Append(' '); sb.Append(key[i]); } return sb.ToString(); } private void SendModValue(LModInfo m, LModEntry e, string v) { e.Value = v; link.Send("MODCFG " + m.Guid + " " + e.Section + MSEP + e.Key + MSEP + v); } private void RebuildMyMods() { myModsList.Children.Clear(); myModsDetail.Children.Clear(); if (!link.IsConnected) { myModsList.Children.Add(T("Start the game with the trainer attached. Your mods that are installed show up here, and you can change their settings without opening their in-game windows.", 13, Ui.B(Ui.Dim), false, true)); return; } if (!myModsLoaded) { bool stale = game != null && Game.PluginState(game) == "old"; myModsList.Children.Add(T(stale ? "The UltraPara.dll in your game folder is older than this trainer, so it can't list your mods. Close the game: the trainer updates it, then start the game again." : "Reading your mods...", 13, Ui.B(stale ? Ui.Bad : Ui.Dim), false, true)); return; } if (myMods.Count == 0) { myModsList.Children.Add(T("None of my mods are loaded in the game. Install them from the Install My Mods tab (they need BepInEx, which Game Setup can install), then restart the game.", 13, Ui.B(Ui.Dim), false, true)); return; } foreach (LModInfo m in myMods) { StackPanel c = new() { Spacing = 2 }; c.Children.Add(T(m.Name, 13.5, Ui.B(Ui.Text), true)); c.Children.Add(T("v" + m.Version + (m.Taken ? " · used from the trainer" : m.CanTake ? " · in-game window" : ""), 12, Ui.B(m.Taken ? Ui.Good : Ui.Dim))); Button b = new() { Content = c, HorizontalAlignment = HorizontalAlignment.Stretch, HorizontalContentAlignment = HorizontalAlignment.Left, Padding = new Thickness(12, 8), CornerRadius = new CornerRadius(8) }; if (m.Guid == myModsSel) { b.BorderBrush = Ui.B(Ui.Orange); b.BorderThickness = new Thickness(1.5); } string id = m.Guid; b.Click += (_, _) => { myModsSel = id; RebuildMyMods(); }; myModsList.Children.Add(b); } LModInfo s = myMods.FirstOrDefault(m => m.Guid == myModsSel); if (s == null) return; Border top = Section(s.Name.ToUpperInvariant() + " v" + s.Version, out StackPanel tb); if (s.CanTake) { tb.Children.Add(Switch("Use it from the trainer", "Hides the mod's in-game window and turns off its hotkey while the trainer is attached.", s.Taken, on => { Settings.Set("take." + s.Guid, on ? "1" : "0"); link.Send("MODTAKE " + s.Guid + " " + (on ? "1" : "0")); })); tb.Children.Add(T("Hides " + s.Name + "'s in-game window and turns off its hotkey while the trainer is attached. Use the buttons below instead. Off by default.", 12.5, Ui.B(Ui.Dim), false, true)); } myModsStat.Text = s.Stat; myModsStat.IsVisible = s.Stat.Length > 0; if (myModsStat.Parent is Panel old) old.Children.Remove(myModsStat); tb.Children.Add(myModsStat); if (s.Actions.Count > 0) { WrapPanel w = new(); bool first = true; foreach ((string id, string label) in s.Actions) { string cmd = "MODACT " + s.Guid + " " + id; Button b = Btn(label, () => link.Send(cmd), first); b.Margin = new Thickness(0, 4, 6, 0); w.Children.Add(b); first = false; } tb.Children.Add(w); } if (!s.CanTake && s.Actions.Count == 0) tb.Children.Add(T("Change this mod's settings below. They're saved in its own config file, the same as editing it by hand.", 12.5, Ui.B(Ui.Dim), false, true)); myModsDetail.Children.Add(top); Border set = Section("SETTINGS", out StackPanel sb); if (s.Entries.Count == 0) sb.Children.Add(T("This mod has no settings.", 13, Ui.B(Ui.Dim), false, true)); string lastSection = null; foreach (LModEntry e in s.Entries) { if (e.Section != lastSection) { lastSection = e.Section; TextBlock h = T(e.Section, 12, Ui.B(Ui.Soft), true); h.Margin = new Thickness(0, 8, 0, 0); sb.Children.Add(h); } LModEntry en = e; Grid row = new() { ColumnDefinitions = new ColumnDefinitions("*,200") }; StackPanel txt = new() { Spacing = 2, Margin = new Thickness(0, 0, 12, 0) }; txt.Children.Add(T(NiceKey(e.Key), 13, Ui.B(Ui.Text), true)); string desc = e.Desc + (e.Min.Length > 0 ? " (" + e.Min + " to " + e.Max + ")" : ""); if (desc.Trim().Length > 0) txt.Children.Add(T(desc, 12, Ui.B(Ui.Dim), false, true)); row.Children.Add(txt); Control input; if (e.Kind == "bool") { ToggleSwitch ts = new() { IsChecked = e.Value.Equals("true", StringComparison.OrdinalIgnoreCase), OnContent = "", OffContent = "", HorizontalAlignment = HorizontalAlignment.Right }; ts.Click += (_, _) => SendModValue(s, en, ts.IsChecked == true ? "true" : "false"); input = ts; } else if (e.Opts.Length > 0) { ComboBox cb = new() { ItemsSource = e.Opts, SelectedItem = e.Opts.Contains(e.Value) ? e.Value : null, HorizontalAlignment = HorizontalAlignment.Stretch, MaxDropDownHeight = 320 }; cb.SelectionChanged += (_, _) => { if (cb.SelectedItem is string v && v != en.Value) SendModValue(s, en, v); }; input = cb; } else { TextBox t = new() { Text = e.Value, HorizontalAlignment = HorizontalAlignment.Stretch }; void Apply() { string v = (t.Text ?? "").Trim(); if (en.Kind is "int" or "float") { if (!double.TryParse(v.Replace(',', '.'), NumberStyles.Float, CultureInfo.InvariantCulture, out double d)) { Message("That isn't a number.", true); t.Text = en.Value; return; } if (double.TryParse(en.Min, NumberStyles.Float, CultureInfo.InvariantCulture, out double lo) && double.TryParse(en.Max, NumberStyles.Float, CultureInfo.InvariantCulture, out double hi)) d = Math.Clamp(d, lo, hi); v = en.Kind == "int" ? ((int)Math.Round(d)).ToString(CultureInfo.InvariantCulture) : d.ToString("0.###", CultureInfo.InvariantCulture); t.Text = v; } if (v != en.Value) SendModValue(s, en, v); } t.LostFocus += (_, _) => Apply(); t.KeyDown += (_, k) => { if (k.Key == Key.Enter) Apply(); }; input = t; } input.VerticalAlignment = VerticalAlignment.Center; Grid.SetColumn(input, 1); row.Children.Add(input); row.Margin = new Thickness(0, 4, 0, 4); sb.Children.Add(row); } myModsDetail.Children.Add(set); } } }