casualexpiesource

cu-trainer/Ultra Paradise Linux/plugin/LinuxHost.cs

768 lines · 34.0 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.IO;
5using System.Text;
6using UltraParadise.Shared;
7using UnityEngine;
8
9namespace UltraParadise
10{
11 internal struct KeyBind
12 {
13 public KeyCode Key;
14 public bool Ctrl, Shift, Alt;
15
16 public static bool TryParse(string s, out KeyBind b)
17 {
18 b = new KeyBind();
19 if (string.IsNullOrEmpty(s) || s == "none") return false;
20 string[] parts = s.Split('+');
21 for (int i = 0; i < parts.Length - 1; i++)
22 {
23 if (parts[i] == "Ctrl") b.Ctrl = true;
24 else if (parts[i] == "Shift") b.Shift = true;
25 else if (parts[i] == "Alt") b.Alt = true;
26 }
27 try { b.Key = (KeyCode)Enum.Parse(typeof(KeyCode), parts[parts.Length - 1], true); }
28 catch (Exception) { return false; }
29 return b.Key != KeyCode.None;
30 }
31 }
32
33 public class LinuxHost : MonoBehaviour
34 {
35 public const string Version = "0.5.0";
36 private static LinuxHost I;
37 private static readonly object gate = new object();
38 private static Dictionary<string, string> info;
39 private static readonly List<string> toastQ = new List<string>();
40 private static string blockedWhy;
41
42 internal static string Home { get { return Path.Combine(Path.GetDirectoryName(Application.dataPath), "UltraParadise"); } }
43 internal static string LinkFile { get { return Path.Combine(Home, "link.txt"); } }
44 private static string CfgFile
45 {
46 get
47 {
48 string f = Path.Combine(Home, "settings.cfg");
49 try
50 {
51 string old = Path.Combine(Path.Combine(Path.Combine(Path.GetDirectoryName(Application.dataPath), "BepInEx"), "config"), "UltraParadise.CUTrainer.cfg");
52 if (!File.Exists(f) && File.Exists(old)) { Directory.CreateDirectory(Home); File.Copy(old, f); }
53 }
54 catch (Exception) { }
55 return f;
56 }
57 }
58
59 private List<CTab> tabs;
60 private readonly Dictionary<string, COpt> byId = new Dictionary<string, COpt>();
61 private readonly Dictionary<string, string> defaults = new Dictionary<string, string>();
62 private readonly Dictionary<string, string> binds = new Dictionary<string, string>();
63 private readonly Dictionary<string, KeyBind> extra = new Dictionary<string, KeyBind>();
64 private readonly List<string> ovExtra = new List<string>();
65 private int ovSize = 100;
66 private readonly Dictionary<string, KeyBind> parsed = new Dictionary<string, KeyBind>();
67 private readonly Dictionary<string, string> last = new Dictionary<string, string>();
68 private readonly Dictionary<string, string> saved = new Dictionary<string, string>();
69 private readonly List<KeyValuePair<string, float>> toasts = new List<KeyValuePair<string, float>>();
70
71 private bool overlayOn = true, remember = true, pauseInMenu = true, menuOpen, restored;
72 private string ovCorner = "top-left", accentHex = "ff7a18";
73 private int ovAlpha = 93;
74 private int tab;
75 private Vector2 scroll;
76 private string binding;
77 private float nextDiff, saveAt = -1f, savedScale = 1f;
78 private bool wePaused, oldCursorVisible;
79 private CursorLockMode oldLock;
80
81 private void Awake()
82 {
83 I = this;
84 tabs = Cheats.Build();
85 foreach (CTab t in tabs)
86 foreach (CSection s in t.Sections)
87 foreach (COpt o in s.Rows)
88 {
89 byId[o.Id] = o;
90 if (o.Kind == CKind.Toggle || o.Kind == CKind.Action) defaults[o.Id] = o.Key;
91 }
92 defaults["menu"] = Cheats.MenuKey;
93 defaults["overlay"] = Cheats.OverlayKey;
94 foreach (KeyValuePair<string, string> kv in defaults) binds[kv.Key] = kv.Value;
95 Load();
96 Reparse();
97 try { Directory.CreateDirectory(Path.GetDirectoryName(LinkFile)); } catch (Exception) { }
98 }
99
100 private void Start()
101 {
102 if (restored) return;
103 restored = true;
104 if (!remember) return;
105 foreach (KeyValuePair<string, string> kv in saved)
106 {
107 COpt o;
108 if (!byId.TryGetValue(kv.Key, out o)) continue;
109 if (o.Kind == CKind.Toggle && kv.Value == "1") Link.Inject("SET " + o.Id + " 1");
110 else if (o.Kind == CKind.Slider) Link.Inject("VAL " + o.Id + " " + kv.Value);
111 }
112 }
113
114 internal static void Sent(string s)
115 {
116 if (s.StartsWith("MSG ", StringComparison.Ordinal)) Toast(s.Substring(4));
117 else if (s.StartsWith("INFO ", StringComparison.Ordinal))
118 {
119 Dictionary<string, string> d = new Dictionary<string, string>();
120 foreach (string part in s.Substring(5).Split('|'))
121 {
122 int eq = part.IndexOf('=');
123 if (eq > 0) d[part.Substring(0, eq)] = part.Substring(eq + 1);
124 }
125 lock (gate) info = d;
126 }
127 else if (s.StartsWith("BLOCKED ", StringComparison.Ordinal)) blockedWhy = s.Substring(8);
128 }
129
130 internal static void Toast(string s)
131 {
132 lock (gate)
133 {
134 if (toastQ.Count < 8) toastQ.Add(s);
135 }
136 }
137
138 internal static void OnConnected()
139 {
140 if (I != null) I.SendAll();
141 }
142
143 internal static bool Handle(string cmd, string[] p)
144 {
145 if (I == null) return false;
146 if (cmd == "BIND" && p.Length >= 3)
147 {
148 if (!I.binds.ContainsKey(p[1])) return true;
149 string k = p[2].Trim();
150 KeyBind b;
151 if (k != "none" && !KeyBind.TryParse(k, out b)) return true;
152 I.binds[p[1]] = k;
153 I.Reparse();
154 I.Dirty();
155 return true;
156 }
157 if (cmd == "OPT" && p.Length >= 3)
158 {
159 bool on = p[2] == "1";
160 if (p[1] == "overlay") I.overlayOn = on;
161 else if (p[1] == "remember") I.remember = on;
162 else if (p[1] == "pause") I.pauseInMenu = on;
163 I.Dirty();
164 I.SendOpts();
165 return true;
166 }
167 if (cmd == "THEME" && p.Length >= 2)
168 {
169 I.ApplyTheme(string.Join(" ", p, 1, p.Length - 1));
170 I.Dirty();
171 return true;
172 }
173 if (cmd == "STATEREQ") { I.SendAll(); return true; }
174 if (cmd == "TOAST" && p.Length >= 2) { Toast(string.Join(" ", p, 1, p.Length - 1)); return true; }
175 if (cmd == "XBINDS")
176 {
177 I.extra.Clear();
178 if (p.Length >= 2)
179 foreach (string part in string.Join(" ", p, 1, p.Length - 1).Split(','))
180 {
181 int eq = part.IndexOf('=');
182 KeyBind b;
183 if (eq > 0 && KeyBind.TryParse(part.Substring(eq + 1), out b)) I.extra[part.Substring(0, eq)] = b;
184 }
185 return true;
186 }
187 if (cmd == "OVEXTRA")
188 {
189 I.ovExtra.Clear();
190 if (p.Length >= 2) foreach (string l in string.Join(" ", p, 1, p.Length - 1).Split('\u001c')) if (l.Length > 0 && I.ovExtra.Count < 12) I.ovExtra.Add(l);
191 return true;
192 }
193 if (cmd == "ACT" && p.Length >= 2)
194 {
195 if (p[1] == "speedcycle") { I.CycleSpeed(); return true; }
196 if (p[1] == "alloff") { Plugin.AllOff(); Toast("Everything is off"); return true; }
197 }
198 return false;
199 }
200
201 private void CycleSpeed()
202 {
203 float[] steps = { 1f, 1.5f, 2f, 0.5f };
204 int at = 0;
205 for (int i = 0; i < steps.Length; i++) if (Mathf.Abs(Plugin.GameSpeed - steps[i]) < 0.01f) { at = i; break; }
206 float v = steps[(at + 1) % steps.Length];
207 Link.Inject("VAL GameSpeed " + v.ToString(CultureInfo.InvariantCulture));
208 Toast("Game speed x" + v.ToString("0.0", CultureInfo.InvariantCulture));
209 }
210
211 private void SendAll()
212 {
213 last.Clear();
214 foreach (COpt o in byId.Values)
215 {
216 if (o.Kind != CKind.Toggle && o.Kind != CKind.Slider) continue;
217 string v = Value(o);
218 last[o.Id] = v;
219 Link.Send("STATE " + o.Id + " " + v);
220 }
221 foreach (KeyValuePair<string, string> kv in binds) Link.Send("BIND " + kv.Key + " " + (string.IsNullOrEmpty(kv.Value) ? "none" : kv.Value));
222 SendOpts();
223 Link.Send("PLUGIN " + Version);
224 }
225
226 private void ApplyTheme(string s)
227 {
228 foreach (string part in s.Split('|'))
229 {
230 int eq = part.IndexOf('=');
231 if (eq <= 0) continue;
232 string k = part.Substring(0, eq).Trim(), v = part.Substring(eq + 1).Trim().ToLowerInvariant();
233 int n;
234 if (k == "corner" && (v == "top-left" || v == "top-right" || v == "bottom-left" || v == "bottom-right")) ovCorner = v;
235 else if (k == "alpha" && int.TryParse(v, NumberStyles.Integer, CultureInfo.InvariantCulture, out n)) ovAlpha = Mathf.Clamp(n, 30, 100);
236 else if (k == "size" && int.TryParse(v, NumberStyles.Integer, CultureInfo.InvariantCulture, out n)) ovSize = Mathf.Clamp(n, 60, 160);
237 else if (k == "accent" && v.Length == 6 && int.TryParse(v, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out n)) { accentHex = v; panel = null; }
238 }
239 }
240
241 private Color Accent(float a)
242 {
243 int n;
244 if (!int.TryParse(accentHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out n)) n = 0xff7a18;
245 return C((n >> 16) & 255, (n >> 8) & 255, n & 255, a);
246 }
247
248 private void SendOpts()
249 {
250 if (!Link.Connected) return;
251 Link.Send("OPTS overlay=" + (overlayOn ? 1 : 0) + "|remember=" + (remember ? 1 : 0) + "|pause=" + (pauseInMenu ? 1 : 0));
252 }
253
254 private static float Num(string id)
255 {
256 if (id == "GameSpeed") return Plugin.GameSpeed;
257 if (id == "CameraZoom") return Plugin.CameraZoom;
258 Num n = Plugin.FindNum(id);
259 return n != null ? n.Value : 0f;
260 }
261
262 private static bool On(string id)
263 {
264 Flag f = Plugin.FindFlag(id);
265 return f != null && f.Value;
266 }
267
268 private static string Value(COpt o)
269 {
270 if (o.Kind == CKind.Toggle) return On(o.Id) ? "1" : "0";
271 return Num(o.Id).ToString("0.###", CultureInfo.InvariantCulture);
272 }
273
274 private void Diff()
275 {
276 bool changed = false;
277 foreach (COpt o in byId.Values)
278 {
279 if (o.Kind != CKind.Toggle && o.Kind != CKind.Slider) continue;
280 string v = Value(o);
281 string was;
282 if (last.TryGetValue(o.Id, out was) && was == v) continue;
283 bool first = was == null;
284 last[o.Id] = v;
285 if (!first) changed = true;
286 if (Link.Connected && !first) Link.Send("STATE " + o.Id + " " + v);
287 }
288 if (changed) Dirty();
289 }
290
291 private void Dirty() { saveAt = Time.unscaledTime + 1f; }
292
293 private void Reparse()
294 {
295 parsed.Clear();
296 foreach (KeyValuePair<string, string> kv in binds)
297 {
298 KeyBind b;
299 if (KeyBind.TryParse(kv.Value, out b)) parsed[kv.Key] = b;
300 }
301 if (Link.Connected)
302 foreach (KeyValuePair<string, string> kv in binds) Link.Send("BIND " + kv.Key + " " + (string.IsNullOrEmpty(kv.Value) ? "none" : kv.Value));
303 }
304
305 private void Load()
306 {
307 try
308 {
309 if (!File.Exists(CfgFile)) return;
310 foreach (string line in File.ReadAllLines(CfgFile))
311 {
312 int eq = line.IndexOf('=');
313 if (eq <= 0) continue;
314 string k = line.Substring(0, eq).Trim(), v = line.Substring(eq + 1).Trim();
315 if (k.StartsWith("bind.", StringComparison.Ordinal))
316 {
317 string id = k.Substring(5);
318 if (binds.ContainsKey(id)) binds[id] = v;
319 }
320 else if (k.StartsWith("cheat.", StringComparison.Ordinal)) saved[k.Substring(6)] = v;
321 else if (k == "overlay") overlayOn = v == "1";
322 else if (k == "remember") remember = v == "1";
323 else if (k == "pause") pauseInMenu = v == "1";
324 else if (k == "theme.corner" || k == "theme.alpha" || k == "theme.accent") ApplyTheme(k.Substring(6) + "=" + v);
325 }
326 }
327 catch (Exception e) { Plugin.Log.LogWarning("Couldn't read settings: " + e.Message); }
328 }
329
330 private void Save()
331 {
332 saveAt = -1f;
333 try
334 {
335 List<string> lines = new List<string>();
336 lines.Add("# Ultra Paradise CU:Trainer (in-game settings). The menu and the Linux app change these for you.");
337 lines.Add("overlay=" + (overlayOn ? 1 : 0));
338 lines.Add("remember=" + (remember ? 1 : 0));
339 lines.Add("pause=" + (pauseInMenu ? 1 : 0));
340 lines.Add("theme.corner=" + ovCorner);
341 lines.Add("theme.alpha=" + ovAlpha);
342 lines.Add("theme.accent=" + accentHex);
343 foreach (KeyValuePair<string, string> kv in binds) lines.Add("bind." + kv.Key + "=" + (string.IsNullOrEmpty(kv.Value) ? "none" : kv.Value));
344 if (remember)
345 foreach (COpt o in byId.Values)
346 {
347 if (o.Kind == CKind.Toggle && On(o.Id)) lines.Add("cheat." + o.Id + "=1");
348 else if (o.Kind == CKind.Slider) lines.Add("cheat." + o.Id + "=" + Value(o));
349 }
350 Directory.CreateDirectory(Path.GetDirectoryName(CfgFile));
351 File.WriteAllLines(CfgFile, lines.ToArray());
352 }
353 catch (Exception e) { Plugin.Log.LogWarning("Couldn't save settings: " + e.Message); }
354 }
355
356 private void OnApplicationQuit()
357 {
358 Save();
359 if (wePaused && Time.timeScale == 0f) Time.timeScale = savedScale;
360 }
361
362 private void Update()
363 {
364 lock (gate)
365 {
366 foreach (string t in toastQ) toasts.Add(new KeyValuePair<string, float>(t, Time.unscaledTime + 3.5f));
367 toastQ.Clear();
368 }
369 toasts.RemoveAll(delegate (KeyValuePair<string, float> t) { return t.Value < Time.unscaledTime; });
370 while (toasts.Count > 4) toasts.RemoveAt(0);
371
372 if (binding == null) Keys();
373 if (Time.unscaledTime >= nextDiff)
374 {
375 nextDiff = Time.unscaledTime + 0.25f;
376 try { Diff(); } catch (Exception) { }
377 }
378 if (saveAt > 0f && Time.unscaledTime >= saveAt) Save();
379 if (menuOpen)
380 {
381 Cursor.visible = true;
382 if (Cursor.lockState != CursorLockMode.None) Cursor.lockState = CursorLockMode.None;
383 if (wePaused && Time.timeScale != 0f) wePaused = false;
384 }
385 }
386
387 private void Keys()
388 {
389 bool sticks = (Input.GetKey(KeyCode.JoystickButton8) && Input.GetKeyDown(KeyCode.JoystickButton9)) || (Input.GetKey(KeyCode.JoystickButton9) && Input.GetKeyDown(KeyCode.JoystickButton8));
390 if (sticks) { ToggleMenu(); return; }
391 bool ctrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
392 bool shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
393 bool alt = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
394 string hit = null;
395 foreach (KeyValuePair<string, KeyBind> kv in parsed)
396 {
397 KeyBind b = kv.Value;
398 if (b.Ctrl != ctrl || b.Shift != shift || b.Alt != alt) continue;
399 if (Input.GetKeyDown(b.Key)) { hit = kv.Key; break; }
400 }
401 if (hit != null) { Trigger(hit); return; }
402 if (menuOpen) return;
403 foreach (KeyValuePair<string, KeyBind> kv in extra)
404 {
405 KeyBind b = kv.Value;
406 if (b.Ctrl != ctrl || b.Shift != shift || b.Alt != alt) continue;
407 if (Input.GetKeyDown(b.Key)) { if (Link.Connected) Link.Send("HOTKEY " + kv.Key); return; }
408 }
409 }
410
411 private void Trigger(string id)
412 {
413 if (id == "menu") { ToggleMenu(); return; }
414 if (id == "overlay")
415 {
416 overlayOn = !overlayOn;
417 Toast("Overlay " + (overlayOn ? "on" : "off"));
418 Dirty();
419 SendOpts();
420 return;
421 }
422 if (menuOpen) return;
423 COpt o;
424 if (!byId.TryGetValue(id, out o)) return;
425 if (o.Kind == CKind.Toggle)
426 {
427 bool on = On(id);
428 Link.Inject("SET " + id + (on ? " 0" : " 1"));
429 if (blockedWhy == null) Toast(o.Name + (on ? " off" : " on"));
430 }
431 else if (o.Kind == CKind.Action) Link.Inject("ACT " + id);
432 }
433
434 private void ToggleMenu()
435 {
436 menuOpen = !menuOpen;
437 binding = null;
438 if (menuOpen)
439 {
440 oldCursorVisible = Cursor.visible;
441 oldLock = Cursor.lockState;
442 if (pauseInMenu && Time.timeScale > 0f)
443 {
444 savedScale = Time.timeScale;
445 Time.timeScale = 0f;
446 wePaused = true;
447 }
448 }
449 else
450 {
451 Cursor.visible = oldCursorVisible;
452 Cursor.lockState = oldLock;
453 if (wePaused && Time.timeScale == 0f) Time.timeScale = savedScale;
454 wePaused = false;
455 }
456 }
457
458 private GUIStyle panel, rowOff, rowOn, btn, key, keyOn, tabOff, tabOn, title, section, label, dim, note, ovTitle, ovLine, toast;
459
460 private static Texture2D Tex(Color c)
461 {
462 Texture2D t = new Texture2D(1, 1, TextureFormat.RGBA32, false);
463 t.hideFlags = HideFlags.HideAndDontSave;
464 t.SetPixel(0, 0, c);
465 t.Apply();
466 return t;
467 }
468
469 private static Color C(int r, int g, int b, float a) { return new Color(r / 255f, g / 255f, b / 255f, a); }
470
471 private static GUIStyle Box(Color bg, Color hover, Color text, int size, TextAnchor align, bool bold)
472 {
473 GUIStyle s = new GUIStyle(GUI.skin.label);
474 s.normal.background = Tex(bg);
475 s.hover.background = Tex(hover);
476 s.active.background = Tex(hover);
477 s.normal.textColor = text;
478 s.hover.textColor = text;
479 s.active.textColor = text;
480 s.border = new RectOffset(0, 0, 0, 0);
481 s.padding = new RectOffset(10, 10, 4, 4);
482 s.margin = new RectOffset(2, 2, 2, 2);
483 s.fontSize = size;
484 s.alignment = align;
485 s.fontStyle = bold ? FontStyle.Bold : FontStyle.Normal;
486 s.wordWrap = false;
487 return s;
488 }
489
490 private static GUIStyle Text(Color c, int size, bool bold, bool wrap)
491 {
492 GUIStyle s = new GUIStyle(GUI.skin.label);
493 s.normal.textColor = c;
494 s.fontSize = size;
495 s.fontStyle = bold ? FontStyle.Bold : FontStyle.Normal;
496 s.wordWrap = wrap;
497 s.richText = false;
498 return s;
499 }
500
501 private void Styles()
502 {
503 Color orange = Accent(1f), light = C(236, 236, 239, 1f), dark = (orange.r * 0.2126f + orange.g * 0.7152f + orange.b * 0.0722f) > 0.45f ? C(20, 20, 22, 1f) : C(250, 250, 250, 1f);
504 Color hover = Color.Lerp(orange, Color.white, 0.15f), keyBg = Color.Lerp(C(26, 26, 30, 1f), orange, 0.3f);
505 panel = Box(C(18, 18, 20, 0.97f), C(18, 18, 20, 0.97f), light, 14, TextAnchor.UpperLeft, false);
506 rowOff = Box(C(34, 34, 38, 1f), C(48, 48, 54, 1f), light, 14, TextAnchor.MiddleLeft, false);
507 rowOn = Box(orange, hover, dark, 14, TextAnchor.MiddleLeft, true);
508 btn = Box(C(48, 48, 54, 1f), C(66, 66, 74, 1f), light, 13, TextAnchor.MiddleCenter, false);
509 key = Box(C(26, 26, 30, 1f), C(40, 40, 46, 1f), C(170, 170, 180, 1f), 12, TextAnchor.MiddleCenter, false);
510 keyOn = Box(keyBg, keyBg, orange, 12, TextAnchor.MiddleCenter, true);
511 tabOff = Box(C(30, 30, 34, 1f), C(46, 46, 52, 1f), light, 14, TextAnchor.MiddleCenter, false);
512 tabOn = Box(orange, orange, dark, 14, TextAnchor.MiddleCenter, true);
513 title = Text(orange, 20, true, false);
514 section = Text(orange, 12, true, false);
515 label = Text(light, 14, false, false);
516 dim = Text(C(150, 150, 160, 1f), 12, false, true);
517 note = Text(C(200, 200, 206, 1f), 13, false, true);
518 ovTitle = Text(orange, 13, true, false);
519 ovLine = Text(light, 12, false, false);
520 toast = Box(C(18, 18, 20, 0.92f), C(18, 18, 20, 0.92f), light, 14, TextAnchor.MiddleCenter, false);
521 }
522
523 private void OnGUI()
524 {
525 if (panel == null || panel.normal.background == null) Styles();
526 float s = Mathf.Clamp(Screen.height / 760f, 1f, 3f);
527 Matrix4x4 old = GUI.matrix;
528 GUI.matrix = Matrix4x4.Scale(new Vector3(s, s, 1f));
529 float w = Screen.width / s, h = Screen.height / s;
530 if (binding != null) CaptureKey(Event.current);
531 if (menuOpen) DrawMenu(w, h);
532 else if (overlayOn)
533 {
534 Matrix4x4 m2 = GUI.matrix;
535 GUI.matrix = Matrix4x4.Scale(new Vector3(s * ovSize / 100f, s * ovSize / 100f, 1f));
536 DrawOverlay();
537 GUI.matrix = m2;
538 }
539 DrawToasts(w, h);
540 GUI.matrix = old;
541 }
542
543 private void CaptureKey(Event e)
544 {
545 if (e == null || e.type != EventType.KeyDown || e.keyCode == KeyCode.None) return;
546 KeyCode k = e.keyCode;
547 if (k == KeyCode.LeftControl || k == KeyCode.RightControl || k == KeyCode.LeftShift || k == KeyCode.RightShift || k == KeyCode.LeftAlt || k == KeyCode.RightAlt) return;
548 e.Use();
549 if (k == KeyCode.Escape) { binding = null; return; }
550 string v;
551 if (k == KeyCode.Delete || k == KeyCode.Backspace) v = "none";
552 else v = (e.control ? "Ctrl+" : "") + (e.shift ? "Shift+" : "") + (e.alt ? "Alt+" : "") + k.ToString();
553 if (v != "none" && binding == "menu" && (e.control || e.shift || e.alt)) v = k.ToString();
554 binds[binding] = v;
555 binding = null;
556 Reparse();
557 Dirty();
558 }
559
560 private string KeyOf(string id)
561 {
562 string k;
563 return binds.TryGetValue(id, out k) ? Cheats.KeyLabel(k) : "-";
564 }
565
566 private void DrawOverlay()
567 {
568 List<string> lines = new List<string>();
569 foreach (CTab t in tabs)
570 foreach (CSection sec in t.Sections)
571 foreach (COpt o in sec.Rows)
572 {
573 if (o.Kind != CKind.Toggle || !On(o.Id)) continue;
574 string k = KeyOf(o.Id);
575 lines.Add(o.Name + (k != "-" ? " " + k : ""));
576 }
577 if (Mathf.Abs(Plugin.GameSpeed - 1f) > 0.01f) lines.Add("Game speed x" + Plugin.GameSpeed.ToString("0.##", CultureInfo.InvariantCulture));
578 int more = 0;
579 if (lines.Count > 14) { more = lines.Count - 13; lines.RemoveRange(13, lines.Count - 13); lines.Add("+" + more + " more"); }
580 string head = blockedWhy != null ? "Cheats are off: multiplayer mod" : KeyOf("menu") + " opens the menu";
581 if (ovExtra.Count > 0) { lines.Add(""); lines.AddRange(ovExtra); }
582 float hgt = 44f + lines.Count * 17f;
583 float sc = Mathf.Clamp(Screen.height / 760f, 1f, 3f) * ovSize / 100f;
584 float x = ovCorner.EndsWith("right", StringComparison.Ordinal) ? Screen.width / sc - 270f : 10f;
585 float y = ovCorner.StartsWith("bottom", StringComparison.Ordinal) ? Screen.height / sc - hgt - 10f : 10f;
586 Color oldC = GUI.color;
587 GUI.color = new Color(1f, 1f, 1f, ovAlpha / 100f);
588 GUI.Box(new Rect(x, y, 260f, hgt), GUIContent.none, panel);
589 GUI.Label(new Rect(x + 10f, y + 4f, 240f, 20f), "ULTRA PARADISE", ovTitle);
590 GUI.Label(new Rect(x + 10f, y + 20f, 240f, 18f), head, dim);
591 for (int i = 0; i < lines.Count; i++) GUI.Label(new Rect(x + 10f, y + 38f + i * 17f, 240f, 18f), lines[i], ovLine);
592 GUI.color = oldC;
593 }
594
595 private void DrawToasts(float w, float h)
596 {
597 for (int i = 0; i < toasts.Count; i++)
598 {
599 Rect r = new Rect((w - 420f) / 2f, h - 70f - (toasts.Count - 1 - i) * 38f, 420f, 32f);
600 GUI.Box(r, toasts[i].Key, toast);
601 }
602 }
603
604 private Dictionary<string, string> Info()
605 {
606 lock (gate) return info;
607 }
608
609 private void DrawMenu(float sw, float sh)
610 {
611 float w = Mathf.Min(1000f, sw - 30f), h = Mathf.Min(620f, sh - 30f);
612 Rect r = new Rect((sw - w) / 2f, (sh - h) / 2f, w, h);
613 GUI.Box(r, GUIContent.none, panel);
614 GUILayout.BeginArea(new Rect(r.x + 16f, r.y + 10f, w - 32f, h - 20f));
615
616 GUILayout.BeginHorizontal();
617 GUILayout.Label("ULTRA PARADISE", title);
618 string status = blockedWhy != null ? "Cheats are off: " + blockedWhy : Link.Connected ? "Linked to the Linux app" : "In-game menu";
619 GUILayout.Label(" " + status, dim, GUILayout.ExpandWidth(true));
620 if (GUILayout.Button("Close", btn, GUILayout.Width(90f), GUILayout.Height(28f))) ToggleMenu();
621 GUILayout.EndHorizontal();
622 GUILayout.Space(6f);
623
624 GUILayout.BeginHorizontal();
625 for (int i = 0; i <= tabs.Count; i++)
626 {
627 string name = i < tabs.Count ? tabs[i].Name : "Settings";
628 if (GUILayout.Button(name, i == tab ? tabOn : tabOff, GUILayout.Height(30f)))
629 {
630 tab = i;
631 scroll = Vector2.zero;
632 binding = null;
633 }
634 }
635 GUILayout.EndHorizontal();
636 GUILayout.Space(8f);
637
638 scroll = GUILayout.BeginScrollView(scroll, false, false);
639 float colW = (w - 32f - 40f) / 2f;
640 GUILayout.BeginHorizontal();
641 if (tab < tabs.Count)
642 {
643 Dictionary<string, string> d = Info();
644 GUILayout.BeginVertical(GUILayout.Width(colW));
645 foreach (CSection sec in tabs[tab].Sections) if (sec.Col == 0) DrawSection(sec, d);
646 GUILayout.EndVertical();
647 GUILayout.Space(16f);
648 GUILayout.BeginVertical(GUILayout.Width(colW));
649 foreach (CSection sec in tabs[tab].Sections) if (sec.Col != 0) DrawSection(sec, d);
650 GUILayout.EndVertical();
651 }
652 else DrawSettings(colW);
653 GUILayout.EndHorizontal();
654 GUILayout.EndScrollView();
655
656 string tip = GUI.tooltip;
657 GUILayout.Label(string.IsNullOrEmpty(tip) ? (binding != null ? "Press a key. Esc cancels, Delete clears it." : "Click a cheat to switch it. Click its key box to pick a new key. " + KeyOf("menu") + " (or both sticks on a controller) closes this menu.") : tip, dim);
658 GUILayout.EndArea();
659 }
660
661 private void DrawSection(CSection sec, Dictionary<string, string> d)
662 {
663 GUILayout.Label(sec.Title, section);
664 foreach (COpt o in sec.Rows) DrawRow(o, d);
665 GUILayout.Space(10f);
666 }
667
668 private void KeyBox(string id)
669 {
670 bool waiting = binding == id;
671 if (GUILayout.Button(waiting ? "press a key" : KeyOf(id), waiting ? keyOn : key, GUILayout.Width(104f), GUILayout.Height(30f)))
672 binding = waiting ? null : id;
673 }
674
675 private void DrawRow(COpt o, Dictionary<string, string> d)
676 {
677 switch (o.Kind)
678 {
679 case CKind.Toggle:
680 {
681 bool on = On(o.Id);
682 GUILayout.BeginHorizontal();
683 if (GUILayout.Button(new GUIContent((on ? "ON " : "OFF ") + o.Name, o.Hint ?? ""), on ? rowOn : rowOff, GUILayout.Height(30f)))
684 Link.Inject("SET " + o.Id + (on ? " 0" : " 1"));
685 KeyBox(o.Id);
686 GUILayout.EndHorizontal();
687 break;
688 }
689 case CKind.Action:
690 GUILayout.BeginHorizontal();
691 if (GUILayout.Button(new GUIContent(o.Name, o.Hint ?? ""), btn, GUILayout.Height(30f))) Link.Inject("ACT " + o.Id);
692 KeyBox(o.Id);
693 GUILayout.EndHorizontal();
694 break;
695 case CKind.Slider:
696 {
697 float v = Num(o.Id);
698 GUILayout.BeginHorizontal();
699 GUILayout.Label(new GUIContent(o.Name + " " + string.Format(CultureInfo.InvariantCulture, o.Format, v), o.Hint ?? ""), label, GUILayout.Width(190f));
700 float nv = GUILayout.HorizontalSlider(v, o.Min, o.Max, GUILayout.ExpandWidth(true), GUILayout.Height(24f));
701 if (GUILayout.Button("Reset", btn, GUILayout.Width(60f), GUILayout.Height(24f))) nv = o.Default;
702 GUILayout.EndHorizontal();
703 nv = Mathf.Clamp(Mathf.Round(nv / o.Step) * o.Step, o.Min, o.Max);
704 if (Mathf.Abs(nv - v) > 0.0001f) Link.Inject("VAL " + o.Id + " " + nv.ToString("0.###", CultureInfo.InvariantCulture));
705 break;
706 }
707 case CKind.Buttons:
708 {
709 GUILayout.BeginHorizontal();
710 string text = o.Text != null ? o.Text(d) : o.Name;
711 GUILayout.Label(new GUIContent(text ?? "", o.Hint ?? ""), label, GUILayout.Width(150f));
712 foreach (CBtn b in o.Buttons)
713 if (GUILayout.Button(b.Label, btn, GUILayout.Height(28f))) Link.Inject("ACT " + b.Act + (b.Arg != null ? " " + b.Arg : ""));
714 GUILayout.EndHorizontal();
715 break;
716 }
717 case CKind.Info:
718 {
719 string text = o.Text != null ? o.Text(d) : "";
720 if (!string.IsNullOrEmpty(text)) GUILayout.Label(text, label);
721 break;
722 }
723 case CKind.Note:
724 GUILayout.Label(o.Name, note);
725 break;
726 }
727 }
728
729 private bool Switch(string name, bool on, string hint)
730 {
731 return GUILayout.Button(new GUIContent((on ? "ON " : "OFF ") + name, hint), on ? rowOn : rowOff, GUILayout.Height(30f));
732 }
733
734 private void DrawSettings(float colW)
735 {
736 GUILayout.BeginVertical(GUILayout.Width(colW));
737 GUILayout.Label("IN GAME", section);
738 GUILayout.BeginHorizontal();
739 if (Switch("In-Game Overlay", overlayOn, "Shows what's on in the top-left corner.")) { overlayOn = !overlayOn; Dirty(); SendOpts(); }
740 KeyBox("overlay");
741 GUILayout.EndHorizontal();
742 GUILayout.BeginHorizontal();
743 GUILayout.Label(new GUIContent("Open This Menu", "Pressing both sticks (L3 + R3) on a controller also opens it."), label, GUILayout.ExpandWidth(true), GUILayout.Height(30f));
744 KeyBox("menu");
745 GUILayout.EndHorizontal();
746 if (Switch("Pause the Game While This Menu Is Open", pauseInMenu, "Stops time while you pick cheats.")) { pauseInMenu = !pauseInMenu; Dirty(); SendOpts(); }
747 if (Switch("Remember My Cheats", remember, "The cheats you had on switch back on the next time you start the game.")) { remember = !remember; Dirty(); SendOpts(); }
748 GUILayout.Space(6f);
749 if (GUILayout.Button("Switch Everything Off", btn, GUILayout.Height(30f))) Link.Inject("ACT alloff");
750 if (GUILayout.Button("Reset All Keys", btn, GUILayout.Height(30f)))
751 {
752 foreach (KeyValuePair<string, string> kv in defaults) binds[kv.Key] = kv.Value;
753 Reparse();
754 Dirty();
755 Toast("Keys reset");
756 }
757 GUILayout.EndVertical();
758 GUILayout.Space(16f);
759 GUILayout.BeginVertical(GUILayout.Width(colW));
760 GUILayout.Label("STEAM DECK", section);
761 GUILayout.Label("Press both sticks (L3 + R3) to open or close this menu. Tap the screen or use the trackpad to click. You can also map " + KeyOf("menu") + " to a back button in Steam Input.", note);
762 GUILayout.Space(10f);
763 GUILayout.Label("ABOUT", section);
764 GUILayout.Label("Ultra Paradise CU:Trainer v" + Version + " (Linux)\nCopyright (c) casualexpie. All rights reserved.\n\nThis is closed-source software. Don't decompile, modify or reupload it.\n\nSingle player only. Cheats stay off while a multiplayer mod is loaded.", note);
765 GUILayout.EndVertical();
766 }
767 }
768}