casualexpiesource

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

301 lines · 19.7 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Threading.Tasks;
6using Avalonia;
7using Avalonia.Controls;
8using Avalonia.Controls.Primitives;
9using Avalonia.Input;
10using Avalonia.Layout;
11using Avalonia.Media;
12using Avalonia.Platform.Storage;
13
14namespace UltraParadise.Linux
15{
16 internal sealed partial class MainWindow
17 {
18 private Control WelcomePage()
19 {
20 bool mac = Mac.On;
21 string runner = mac ? "CrossOver, Whisky or another Wine app" : "Proton";
22 Grid g = new() { ColumnDefinitions = new ColumnDefinitions("*,*,*") };
23
24 StackPanel c0 = new() { Margin = new Thickness(0, 0, 7, 0) };
25 Border w = Section("WELCOME", out StackPanel wb);
26 wb.Children.Add(T("Thanks for trying " + Program.Name + " v" + Program.Version + " for " + Program.Platform + "!", 15, Ui.B(Ui.Text), true, true));
27 wb.Children.Add(T("It's a cheat app for Casualties: Unknown. The game runs through " + runner + ", so the cheats run inside the game through UltraPara.dll, a small loader this app puts in the game folder for you. You don't need BepInEx for it. Then you switch cheats on and off here with a click, or with your own hotkeys.", 13, Ui.B(Ui.Dim), false, true));
28 c0.Children.Add(w);
29 Border pu = Section("PEOPLE USING THE TRAINER", out StackPanel pub);
30 pub.Children.Add(usageBoard);
31 pub.Children.Add(T("Counts the people who said yes to being counted (Settings, Count Me as a User), so the real number is higher. Updated every hour.", 12, Ui.B(Ui.Dim), false, true));
32 c0.Children.Add(pu);
33 Border s = Section("GETTING STARTED", out StackPanel sb);
34 sb.Children.Add(T("1. Open Game Setup and click Set Everything Up (quit Steam first" + (mac ? ", in your bottle" : "") + ").\n2. Start the game.\n3. Wait for the dot in the bottom left to turn green.\n4. Pick a tab on the left and switch cheats on.\n\nIn game, press " + (mac ? "fn + F10" : "F10") + ", or both sticks (L3 + R3) on a controller, for a menu with every cheat. It works without this app open" + (mac ? "." : ", even in Steam Deck Gaming Mode."), 13, Ui.B(Ui.Dim), false, true));
35 WrapPanel sw = new();
36 Button gs = Btn("Game Setup", () => Show("Game Setup"), true);
37 gs.Margin = new Thickness(0, 0, 6, 6);
38 sw.Children.Add(gs);
39 Button st = Btn("Start Game", () => StartGame());
40 st.Margin = new Thickness(0, 0, 6, 6);
41 sw.Children.Add(st);
42 sb.Children.Add(sw);
43 c0.Children.Add(s);
44 Border l = Section("WHAT'S ON THE LEFT", out StackPanel lb);
45 lb.Children.Add(T("CHEATS: Player, Health, Movement, Items, Combat, World, Traders and Challenges. Health shows your vitals live and suggests the cheat that fixes a problem.\n\nTRAINER: Game Setup, Settings (updates, install, in-game options), Appearance (themes, colours, your own background picture, fonts and size)" + (mac ? "" : " and Debug (a separate window that shows everything the game is doing, live)") + ".\n\nEXTRAS: Install My Mods, Report a Bug and About Me.", 13, Ui.B(Ui.Dim), false, true));
46 c0.Children.Add(l);
47 g.Children.Add(c0);
48
49 StackPanel c1 = new() { Margin = new Thickness(7, 0, 7, 0) };
50 Border b = Section("WHAT IT'S BUILT FROM", out StackPanel bb);
51 bb.Children.Add(T("The app is written in C# on .NET 8, with the Avalonia UI toolkit, and ships as one self-contained program, so you don't need to install .NET.\n\nThe cheats run inside the game, loaded by UltraPara.dll (written in C). They use HarmonyX to change the game's code in memory only. The only files added to your game folder are UltraPara.dll and version.dll (plus BepInEx, if you install it for my other mods). Closing the game clears everything.\n\nThe app and the game talk over a private connection on your own computer.\n\n" + (mac ? "Updates are announced by casualexpie's update server (updates.smellypawz.com, run on Cloudflare), and you download them from the official Nexus page." : "Updates come from casualexpie's update server (updates.smellypawz.com, run on Cloudflare). Every download is checked against the fingerprints on the official Nexus page before it's installed.") + "\n\nThere's also a Windows version, built on .NET Framework and Windows Forms.", 13, Ui.B(Ui.Dim), false, true));
52 c1.Children.Add(b);
53 Border p = Section("SAFE AND PRIVATE", out StackPanel pb);
54 pb.Children.Add(T("Single player only: if a multiplayer mod is loaded, the cheats stay off.\n\nIt only goes online to Nexus Mods (to check your copy is official, and for About Me), to Thunderstore (for the newest versions of my mods), to the update server, and to the official BepInEx GitHub when Game Setup installs BepInEx. It sends nothing about you, only which trainer version you have.\n\nOnly download it from casualexpie's pages on Nexus Mods or GameBanana.", 13, Ui.B(Ui.Dim), false, true));
55 c1.Children.Add(p);
56 Grid.SetColumn(c1, 1);
57 g.Children.Add(c1);
58
59 StackPanel c2 = new() { Margin = new Thickness(7, 0, 0, 0) };
60 if (!mac) c2.Children.Add(ScreensSection("TWO SCREENS? (RECOMMENDED)", true));
61 Border gm = Section("THE GAME", out StackPanel gmb);
62 gmb.Children.Add(T("Casualties: Unknown is made by Orsoniks and Oro Interactive. This trainer isn't made by them or connected to them. If you enjoy the game, support them on Steam and leave a review.", 13, Ui.B(Ui.Dim), false, true));
63 gmb.Children.Add(Btn("Steam Page", () => Game.Open("https://store.steampowered.com/app/4576490/")));
64 c2.Children.Add(gm);
65 Border me = Section("FROM CASUALEXPIE", out StackPanel meb);
66 meb.Children.Add(T("Made by casualexpie. I also make other mods for the game: see Install My Mods for the ones I recommend, and About Me for all of them.\n\nFound a bug? The Report a Bug tab collects everything I need. Paste it on the Nexus Bugs page.", 13, Ui.B(Ui.Dim), false, true));
67 WrapPanel mw = new();
68 Button np = Btn("Nexus Page", () => Game.Open(Nexus.PageUrl));
69 np.Margin = new Thickness(0, 0, 6, 6);
70 mw.Children.Add(np);
71 Button gbp = Btn("GameBanana", () => Game.Open(NexusMods.BananaUrl));
72 gbp.Margin = new Thickness(0, 0, 6, 6);
73 mw.Children.Add(gbp);
74 Button rb = Btn("Report a Bug", () => Show("Report a Bug"));
75 rb.Margin = new Thickness(0, 0, 6, 6);
76 mw.Children.Add(rb);
77 Button sc = Btn("Source Code", () => Game.Open("https://opensource.smellypawz.com/cu-trainer/"));
78 sc.Margin = new Thickness(0, 0, 6, 6);
79 mw.Children.Add(sc);
80 meb.Children.Add(mw);
81 c2.Children.Add(me);
82 Border lg = Section("LEGAL", out StackPanel lgb);
83 lgb.Children.Add(T("Copyright (c) casualexpie. Open source: the code is public at opensource.smellypawz.com under a source-available license. You can read it and build it yourself, but don't reupload, sell or reuse it.", 12, Ui.B(Ui.Faint), false, true));
84 c2.Children.Add(lg);
85 Grid.SetColumn(c2, 2);
86 g.Children.Add(c2);
87 return Scroll(g);
88 }
89
90 private static readonly string[] Palette =
91 {
92 "#ff7a18", "#ff5fa2", "#e8650a", "#ffd400", "#ffb03c", "#84d65c", "#50c878", "#2f9e44", "#3ec7c2", "#4aa3ff", "#5a6cff", "#9b6bff",
93 "#c86bff", "#eb5050", "#d63b2f", "#ffffff", "#ececef", "#c8c8cc", "#9a9aa3", "#6b6b70", "#3a3a40", "#2c2c31", "#1c1c1f", "#141416",
94 "#101011", "#000000", "#f3f3f1", "#e6e6e3", "#241a2c", "#1a1320", "#0c2030", "#10241a",
95 };
96
97 private Button Small(string text, Action click)
98 {
99 Button b = new() { Content = text, Padding = new Thickness(10, 3), MinHeight = 28, FontSize = 12, CornerRadius = new CornerRadius(6), Margin = new Thickness(0, 0, 4, 4) };
100 b.Click += (_, _) => click();
101 return b;
102 }
103
104 private Control ColorRow(int i)
105 {
106 string key = Ui.ColorKeys[i];
107 Grid g = new() { ColumnDefinitions = new ColumnDefinitions("Auto,*,Auto,Auto") };
108 Border sw = new() { Width = 22, Height = 22, CornerRadius = new CornerRadius(6), BorderBrush = Ui.B(Ui.Line), BorderThickness = new Thickness(1), Margin = new Thickness(0, 0, 10, 0), VerticalAlignment = VerticalAlignment.Center, Cursor = new Cursor(StandardCursorType.Hand) };
109 g.Children.Add(sw);
110 TextBlock name = T(Ui.ColorNames[i], 13, Ui.B(Ui.Text));
111 name.VerticalAlignment = VerticalAlignment.Center;
112 Grid.SetColumn(name, 1);
113 g.Children.Add(name);
114 TextBox hex = new() { Width = 88, FontSize = 12, MinHeight = 28, Padding = new Thickness(6, 3), Margin = new Thickness(6, 0, 4, 0), VerticalAlignment = VerticalAlignment.Center };
115 Grid.SetColumn(hex, 2);
116 g.Children.Add(hex);
117 Button reset = Small("Reset", () => Ui.Set(key, null));
118 reset.Margin = new Thickness(0);
119 reset.VerticalAlignment = VerticalAlignment.Center;
120 Grid.SetColumn(reset, 3);
121 g.Children.Add(reset);
122 void Fill()
123 {
124 Color c = Ui.ColorOf(key);
125 sw.Background = new SolidColorBrush(c);
126 hex.Text = Ui.Hex(c).ToUpperInvariant();
127 reset.IsEnabled = Ui.Values.ContainsKey(key);
128 }
129 void Commit()
130 {
131 if (Ui.TryHex(hex.Text, out Color c)) { if (c != Ui.ColorOf(key)) Ui.Set(key, Ui.Hex(c)); }
132 else { Message("Colours look like #ff7a18.", true); Fill(); }
133 }
134 hex.LostFocus += (_, _) => Commit();
135 hex.KeyDown += (_, e) => { if (e.Key == Key.Enter) Commit(); };
136 WrapPanel pal = new() { Width = 12 * 26, Margin = new Thickness(4) };
137 foreach (string p in Palette)
138 {
139 Ui.TryHex(p, out Color pc);
140 Button pb = new() { Width = 22, Height = 22, MinHeight = 0, Padding = new Thickness(0), Margin = new Thickness(2), CornerRadius = new CornerRadius(5), Background = new SolidColorBrush(pc), BorderBrush = Ui.B(Ui.Line), BorderThickness = new Thickness(1) };
141 ToolTip.SetTip(pb, p.ToUpperInvariant());
142 pb.Click += (_, _) => { Ui.Set(key, p); FlyoutBase.GetAttachedFlyout(sw)?.Hide(); };
143 pal.Children.Add(pb);
144 }
145 FlyoutBase.SetAttachedFlyout(sw, new Flyout { Content = pal, Placement = PlacementMode.BottomEdgeAlignedLeft });
146 sw.PointerPressed += (_, _) => FlyoutBase.ShowAttachedFlyout(sw);
147 ToolTip.SetTip(sw, "Click for a palette, or type a colour like #ff7a18.");
148 Fill();
149 themeRefresh.Add(Fill);
150 return g;
151 }
152
153 private Control ThemeSlider(string name, string key, int min, int max, Func<int> get)
154 {
155 StackPanel sp = new();
156 Grid top = new() { ColumnDefinitions = new ColumnDefinitions("*,Auto") };
157 top.Children.Add(T(name, 13, Ui.B(Ui.Text)));
158 TextBlock val = T(get() + "%", 13, Ui.B(Ui.Orange), true);
159 Grid.SetColumn(val, 1);
160 top.Children.Add(val);
161 sp.Children.Add(top);
162 Slider s = new() { Minimum = min, Maximum = max, TickFrequency = 5, IsSnapToTickEnabled = true, Value = get() };
163 bool quiet = false;
164 s.PropertyChanged += (_, e) =>
165 {
166 if (e.Property != RangeBase.ValueProperty || quiet) return;
167 int v = (int)Math.Round(s.Value);
168 val.Text = v + "%";
169 Ui.Set(key, v.ToString());
170 };
171 themeRefresh.Add(() => { quiet = true; s.Value = get(); val.Text = get() + "%"; quiet = false; });
172 sp.Children.Add(s);
173 return sp;
174 }
175
176 private WrapPanel Choices(params (string label, Action act)[] items)
177 {
178 WrapPanel w = new();
179 foreach (var (label, act) in items) w.Children.Add(Small(label, act));
180 return w;
181 }
182
183 private Control AppearancePage()
184 {
185 Grid g = new() { ColumnDefinitions = new ColumnDefinitions("*,*,*") };
186
187 StackPanel c0 = new() { Margin = new Thickness(0, 0, 7, 0) };
188 Border th = Section("THEME", out StackPanel thb);
189 TextBlock presetName = T("", 14, Ui.B(Ui.Text), true);
190 thb.Children.Add(presetName);
191 themeRefresh.Add(() => presetName.Text = "Theme: " + Ui.PresetName);
192 WrapPanel pw = new();
193 for (int i = 0; i < Ui.PresetIds.Length; i++)
194 {
195 string id = Ui.PresetIds[i];
196 pw.Children.Add(Small(Ui.PresetNames[i], () => { Ui.UsePreset(id); Message("Theme: " + Ui.PresetName, false); }));
197 }
198 thb.Children.Add(pw);
199 thb.Children.Add(T("Picks a ready-made theme. Your colour changes are cleared, your picture and font stay. Follow System matches your desktop's light or dark setting.", 12, Ui.B(Ui.Dim), false, true));
200 c0.Children.Add(th);
201 Border co = Section("COLOURS", out StackPanel cob);
202 for (int i = 0; i < Ui.ColorKeys.Length; i++) cob.Children.Add(ColorRow(i));
203 cob.Children.Add(T("Click a swatch for a palette, or type any colour like #ff7a18 and press Enter.", 12, Ui.B(Ui.Dim), false, true));
204 c0.Children.Add(co);
205 g.Children.Add(c0);
206
207 StackPanel c1 = new() { Margin = new Thickness(7, 0, 7, 0) };
208 Border bg = Section("BACKGROUND PICTURE", out StackPanel bgb);
209 TextBlock pic = T("", 13, Ui.B(Ui.Text), false, true);
210 themeRefresh.Add(() =>
211 {
212 string p = Ui.Values.TryGetValue("image", out string v) ? v.Trim() : "";
213 pic.Text = p.Length == 0 ? "No picture" : (Ui.BgImage != null ? Path.GetFileName(p) : "Missing: " + Path.GetFileName(p));
214 });
215 bgb.Children.Add(pic);
216 bgb.Children.Add(Choices(("Choose...", () => _ = PickBackground()), ("Remove", () => Ui.Set("image", null))));
217 bgb.Children.Add(ThemeSlider("Visibility", "image.opacity", 0, 100, () => Ui.BgOpacity));
218 bgb.Children.Add(T("Fit", 12, Ui.B(Ui.Dim)));
219 bgb.Children.Add(Choices(("Cover", () => Ui.Set("image.fit", "cover")), ("Fit", () => Ui.Set("image.fit", "fit")), ("Stretch", () => Ui.Set("image.fit", "stretch")), ("Center", () => Ui.Set("image.fit", "center")), ("Tile", () => Ui.Set("image.fit", "tile"))));
220 bgb.Children.Add(T("Header art", 12, Ui.B(Ui.Dim)));
221 bgb.Children.Add(Choices(("Show", () => Ui.Set("banner", "art")), ("Hide", () => Ui.Set("banner", "plain"))));
222 c1.Children.Add(bg);
223 Border tx = Section("TEXT AND SIZE", out StackPanel txb);
224 ComboBox fonts = new() { HorizontalAlignment = HorizontalAlignment.Stretch, MaxDropDownHeight = 360 };
225 List<string> names = new() { "Default (Inter)" };
226 try { names.AddRange(FontManager.Current.SystemFonts.Select(x => x.Name).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x, StringComparer.OrdinalIgnoreCase)); } catch (Exception) { }
227 fonts.ItemsSource = names;
228 bool fq = false;
229 fonts.SelectionChanged += (_, _) =>
230 {
231 if (fq || fonts.SelectedItem is not string n) return;
232 Ui.Set("font", n == names[0] ? null : n);
233 };
234 themeRefresh.Add(() => { fq = true; fonts.SelectedItem = Ui.FontName != null && names.Contains(Ui.FontName) ? Ui.FontName : names[0]; fq = false; });
235 txb.Children.Add(T("Font", 12, Ui.B(Ui.Dim)));
236 txb.Children.Add(fonts);
237 txb.Children.Add(ThemeSlider("Size", "scale", 80, 160, () => (int)Math.Round(Ui.Scale * 100)));
238 c1.Children.Add(tx);
239 Grid.SetColumn(c1, 1);
240 g.Children.Add(c1);
241
242 StackPanel c2 = new() { Margin = new Thickness(7, 0, 0, 0) };
243 Border ov = Section("IN-GAME OVERLAY", out StackPanel ovb);
244 ovb.Children.Add(ThemeSlider("Visibility", "overlay.opacity", 30, 100, () => Ui.OverlayOpacity));
245 ovb.Children.Add(ThemeSlider("Size", "overlay.size", 60, 160, () => Ui.OverlaySize));
246 ovb.Children.Add(T("Corner", 12, Ui.B(Ui.Dim)));
247 ovb.Children.Add(Choices(("Top Left", () => Ui.Set("overlay.corner", "top-left")), ("Top Right", () => Ui.Set("overlay.corner", "top-right")), ("Bottom Left", () => Ui.Set("overlay.corner", "bottom-left")), ("Bottom Right", () => Ui.Set("overlay.corner", "bottom-right"))));
248 ovb.Children.Add(T("The overlay and the in-game menu use your accent colour too. Changes reach the game while the trainer is attached.", 12, Ui.B(Ui.Dim), false, true));
249 c2.Children.Add(ov);
250 Border tf = Section("THEME FILE", out StackPanel tfb);
251 tfb.Children.Add(T("Everything here is saved in theme.ini. Open it in a text editor to change anything by hand (it explains every setting), then click Reload. Export makes a copy to share, Import loads one. The same file works in the Windows trainer.", 12, Ui.B(Ui.Dim), false, true));
252 tfb.Children.Add(Choices(("Open File", () => { Ui.Save(); Game.Open(Ui.FilePath); }), ("Reload", () => { Ui.Reload(); Message("Theme file reloaded.", false); })));
253 tfb.Children.Add(Choices(("Import...", () => _ = ImportTheme()), ("Export...", () => _ = ExportTheme())));
254 tfb.Children.Add(Choices(("Reset Everything", () => { Ui.ResetAll(); Message("Back to the standard dark theme.", false); })));
255 c2.Children.Add(tf);
256 Grid.SetColumn(c2, 2);
257 g.Children.Add(c2);
258
259 foreach (Action a in themeRefresh) a();
260 return Scroll(g);
261 }
262
263 private async Task PickBackground()
264 {
265 IReadOnlyList<IStorageFile> r = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
266 {
267 Title = "Pick a background picture",
268 AllowMultiple = false,
269 FileTypeFilter = new[] { new FilePickerFileType("Pictures") { Patterns = new[] { "*.png", "*.jpg", "*.jpeg", "*.bmp", "*.gif", "*.webp", "*.PNG", "*.JPG", "*.JPEG" } } },
270 });
271 string f = r.Count > 0 ? r[0].TryGetLocalPath() : null;
272 if (f == null) return;
273 Ui.Set("image", f);
274 if (Ui.BgImage == null) { Ui.Set("image", null); Message("That picture couldn't be opened.", true); return; }
275 if (Ui.BgOpacity == 0) Ui.Set("image.opacity", "30");
276 }
277
278 private async Task ImportTheme()
279 {
280 IReadOnlyList<IStorageFile> r = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
281 {
282 Title = "Import a theme",
283 AllowMultiple = false,
284 FileTypeFilter = new[] { new FilePickerFileType("Theme files") { Patterns = new[] { "*.ini", "*.txt", "*.theme" } }, FilePickerFileTypes.All },
285 });
286 string f = r.Count > 0 ? r[0].TryGetLocalPath() : null;
287 if (f == null) return;
288 string e = Ui.Import(f);
289 Message(e ?? "Theme imported.", e != null);
290 }
291
292 private async Task ExportTheme()
293 {
294 IStorageFile f = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { Title = "Export your theme", SuggestedFileName = "my-theme.ini", DefaultExtension = "ini" });
295 string p = f?.TryGetLocalPath();
296 if (p == null) return;
297 string e = Ui.Export(p);
298 Message(e ?? "Saved " + Path.GetFileName(p) + ".", e != null);
299 }
300 }
301}