casualexpiesource

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

241 lines · 11.7 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using Avalonia;
7using Avalonia.Controls;
8using Avalonia.Input;
9using Avalonia.Layout;
10
11namespace UltraParadise.Linux
12{
13 internal sealed class LModEntry
14 {
15 public string Section, Key, Kind, Value, Desc, Min, Max;
16 public string[] Opts;
17 }
18
19 internal sealed class LModInfo
20 {
21 public string Guid, Name, Version, Stat = "";
22 public bool CanTake, Taken;
23 public readonly List<(string id, string label)> Actions = new();
24 public readonly List<LModEntry> Entries = new();
25 }
26
27 internal sealed partial class MainWindow
28 {
29 private const char MREC = '\u001d', MSEP = '\u001c';
30 private static readonly string[] TakeGuids = { "getstinky", "nodurability" };
31 private readonly List<LModInfo> myMods = new();
32 private readonly StackPanel myModsList = new() { Spacing = 6 };
33 private readonly StackPanel myModsDetail = new() { Spacing = 0 };
34 private readonly TextBlock myModsStat = T("", 13.5, Ui.B(Ui.Good), true, true);
35 private string myModsSel;
36 private bool myModsLoaded;
37
38 private Control MyModsPage()
39 {
40 Grid g = new() { ColumnDefinitions = new ColumnDefinitions("300,*") };
41 StackPanel left = new() { Margin = new Thickness(0, 0, 7, 0) };
42 Border l = Section("MY MODS IN THE GAME", out StackPanel lb);
43 lb.Children.Add(myModsList);
44 left.Children.Add(l);
45 g.Children.Add(left);
46 Grid.SetColumn(myModsDetail, 1);
47 myModsDetail.Margin = new Thickness(7, 0, 0, 0);
48 g.Children.Add(myModsDetail);
49 RebuildMyMods();
50 return Scroll(g);
51 }
52
53 private void MyModsConnected()
54 {
55 foreach (string g in TakeGuids) if (Settings.On("take." + g, false)) link.Send("MODTAKE " + g + " 1");
56 link.Send("MODS");
57 }
58
59 private void MyModsDisconnected()
60 {
61 myMods.Clear();
62 myModsLoaded = false;
63 RebuildMyMods();
64 }
65
66 private void MyModsParse(string body)
67 {
68 myMods.Clear();
69 myModsLoaded = true;
70 LModInfo cur = null;
71 foreach (string rec in body.Split(MREC))
72 {
73 string[] f = rec.Split(MSEP);
74 if (f.Length >= 7 && f[0] == "M")
75 {
76 cur = new LModInfo { Guid = f[1], Name = f[2], Version = f[3], CanTake = f[4] == "1", Taken = f[5] == "1" };
77 foreach (string a in f[6].Split(';', StringSplitOptions.RemoveEmptyEntries))
78 {
79 int eq = a.IndexOf('=');
80 if (eq > 0) cur.Actions.Add((a[..eq], a[(eq + 1)..]));
81 }
82 myMods.Add(cur);
83 }
84 else if (f.Length >= 10 && f[0] == "E" && cur != null && cur.Guid == f[1])
85 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<string>() });
86 }
87 if (myModsSel == null || myMods.All(m => m.Guid != myModsSel)) myModsSel = myMods.FirstOrDefault()?.Guid;
88 RebuildMyMods();
89 }
90
91 private void MyModsStats(string body)
92 {
93 foreach (string rec in body.Split(MREC))
94 {
95 string[] f = rec.Split(MSEP);
96 if (f.Length < 2) continue;
97 foreach (LModInfo m in myMods) if (m.Guid == f[0]) m.Stat = f[1];
98 }
99 LModInfo s = myMods.FirstOrDefault(m => m.Guid == myModsSel);
100 myModsStat.Text = s?.Stat ?? "";
101 myModsStat.IsVisible = myModsStat.Text.Length > 0;
102 }
103
104 private static string NiceKey(string key)
105 {
106 StringBuilder sb = new();
107 for (int i = 0; i < key.Length; i++)
108 {
109 if (i > 0 && char.IsUpper(key[i]) && !char.IsUpper(key[i - 1])) sb.Append(' ');
110 sb.Append(key[i]);
111 }
112 return sb.ToString();
113 }
114
115 private void SendModValue(LModInfo m, LModEntry e, string v)
116 {
117 e.Value = v;
118 link.Send("MODCFG " + m.Guid + " " + e.Section + MSEP + e.Key + MSEP + v);
119 }
120
121 private void RebuildMyMods()
122 {
123 myModsList.Children.Clear();
124 myModsDetail.Children.Clear();
125 if (!link.IsConnected)
126 {
127 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));
128 return;
129 }
130 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; }
131 if (myMods.Count == 0)
132 {
133 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));
134 return;
135 }
136 foreach (LModInfo m in myMods)
137 {
138 StackPanel c = new() { Spacing = 2 };
139 c.Children.Add(T(m.Name, 13.5, Ui.B(Ui.Text), true));
140 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)));
141 Button b = new() { Content = c, HorizontalAlignment = HorizontalAlignment.Stretch, HorizontalContentAlignment = HorizontalAlignment.Left, Padding = new Thickness(12, 8), CornerRadius = new CornerRadius(8) };
142 if (m.Guid == myModsSel) { b.BorderBrush = Ui.B(Ui.Orange); b.BorderThickness = new Thickness(1.5); }
143 string id = m.Guid;
144 b.Click += (_, _) => { myModsSel = id; RebuildMyMods(); };
145 myModsList.Children.Add(b);
146 }
147
148 LModInfo s = myMods.FirstOrDefault(m => m.Guid == myModsSel);
149 if (s == null) return;
150 Border top = Section(s.Name.ToUpperInvariant() + " v" + s.Version, out StackPanel tb);
151 if (s.CanTake)
152 {
153 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 =>
154 {
155 Settings.Set("take." + s.Guid, on ? "1" : "0");
156 link.Send("MODTAKE " + s.Guid + " " + (on ? "1" : "0"));
157 }));
158 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));
159 }
160 myModsStat.Text = s.Stat;
161 myModsStat.IsVisible = s.Stat.Length > 0;
162 if (myModsStat.Parent is Panel old) old.Children.Remove(myModsStat);
163 tb.Children.Add(myModsStat);
164 if (s.Actions.Count > 0)
165 {
166 WrapPanel w = new();
167 bool first = true;
168 foreach ((string id, string label) in s.Actions)
169 {
170 string cmd = "MODACT " + s.Guid + " " + id;
171 Button b = Btn(label, () => link.Send(cmd), first);
172 b.Margin = new Thickness(0, 4, 6, 0);
173 w.Children.Add(b);
174 first = false;
175 }
176 tb.Children.Add(w);
177 }
178 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));
179 myModsDetail.Children.Add(top);
180
181 Border set = Section("SETTINGS", out StackPanel sb);
182 if (s.Entries.Count == 0) sb.Children.Add(T("This mod has no settings.", 13, Ui.B(Ui.Dim), false, true));
183 string lastSection = null;
184 foreach (LModEntry e in s.Entries)
185 {
186 if (e.Section != lastSection)
187 {
188 lastSection = e.Section;
189 TextBlock h = T(e.Section, 12, Ui.B(Ui.Soft), true);
190 h.Margin = new Thickness(0, 8, 0, 0);
191 sb.Children.Add(h);
192 }
193 LModEntry en = e;
194 Grid row = new() { ColumnDefinitions = new ColumnDefinitions("*,200") };
195 StackPanel txt = new() { Spacing = 2, Margin = new Thickness(0, 0, 12, 0) };
196 txt.Children.Add(T(NiceKey(e.Key), 13, Ui.B(Ui.Text), true));
197 string desc = e.Desc + (e.Min.Length > 0 ? " (" + e.Min + " to " + e.Max + ")" : "");
198 if (desc.Trim().Length > 0) txt.Children.Add(T(desc, 12, Ui.B(Ui.Dim), false, true));
199 row.Children.Add(txt);
200 Control input;
201 if (e.Kind == "bool")
202 {
203 ToggleSwitch ts = new() { IsChecked = e.Value.Equals("true", StringComparison.OrdinalIgnoreCase), OnContent = "", OffContent = "", HorizontalAlignment = HorizontalAlignment.Right };
204 ts.Click += (_, _) => SendModValue(s, en, ts.IsChecked == true ? "true" : "false");
205 input = ts;
206 }
207 else if (e.Opts.Length > 0)
208 {
209 ComboBox cb = new() { ItemsSource = e.Opts, SelectedItem = e.Opts.Contains(e.Value) ? e.Value : null, HorizontalAlignment = HorizontalAlignment.Stretch, MaxDropDownHeight = 320 };
210 cb.SelectionChanged += (_, _) => { if (cb.SelectedItem is string v && v != en.Value) SendModValue(s, en, v); };
211 input = cb;
212 }
213 else
214 {
215 TextBox t = new() { Text = e.Value, HorizontalAlignment = HorizontalAlignment.Stretch };
216 void Apply()
217 {
218 string v = (t.Text ?? "").Trim();
219 if (en.Kind is "int" or "float")
220 {
221 if (!double.TryParse(v.Replace(',', '.'), NumberStyles.Float, CultureInfo.InvariantCulture, out double d)) { Message("That isn't a number.", true); t.Text = en.Value; return; }
222 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);
223 v = en.Kind == "int" ? ((int)Math.Round(d)).ToString(CultureInfo.InvariantCulture) : d.ToString("0.###", CultureInfo.InvariantCulture);
224 t.Text = v;
225 }
226 if (v != en.Value) SendModValue(s, en, v);
227 }
228 t.LostFocus += (_, _) => Apply();
229 t.KeyDown += (_, k) => { if (k.Key == Key.Enter) Apply(); };
230 input = t;
231 }
232 input.VerticalAlignment = VerticalAlignment.Center;
233 Grid.SetColumn(input, 1);
234 row.Children.Add(input);
235 row.Margin = new Thickness(0, 4, 0, 4);
236 sb.Children.Add(row);
237 }
238 myModsDetail.Children.Add(set);
239 }
240 }
241}