using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Globalization; using System.IO; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; [assembly: AssemblyMetadata("Notice", "Copyright (c) casualexpie. Source available at https://opensource.smellypawz.com under the casualexpie Source-Available License. Don't redistribute, sell or reuse it without permission.")] [assembly: AssemblyTitle("Ultra Paradise CU:Trainer")] [assembly: AssemblyDescription("Trainer for Casualties: Unknown")] [assembly: AssemblyProduct("Ultra Paradise CU:Trainer")] [assembly: AssemblyCompany("casualexpie")] [assembly: AssemblyCopyright("Copyright (c) casualexpie")] [assembly: AssemblyVersion("2.0.12.0")] [assembly: AssemblyFileVersion("2.0.12.0")] namespace UltraParadise { internal static class Program { public const string Version = "2.0.14"; internal static bool Quiet; public const string Name = "Ultra Paradise CU:Trainer"; public const string GameProcess = "CasualtiesUnknown"; internal static bool JustUpdated, JustInstalled, JustRestyled, Relaunch; [STAThread] private static void Main(string[] args) { JustUpdated = Array.IndexOf(args, "--updated") >= 0; JustInstalled = Array.IndexOf(args, "--installed") >= 0; JustRestyled = Array.IndexOf(args, "--restyled") >= 0; bool uninstall = Array.IndexOf(args, "--uninstall") >= 0; using (Mutex m = new Mutex(false, "UltraParadiseCUTrainer")) { bool have; try { have = m.WaitOne(JustUpdated || JustInstalled || JustRestyled ? 30000 : 0); } catch (AbandonedMutexException) { have = true; } if (!have) { MessageBox.Show(uninstall ? "Close " + Name + " first, then uninstall it." : Name + " is already open.", Name, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { try { Native.SetProcessDPIAware(); } catch (Exception) { } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (uninstall) { Setup.Uninstall(); return; } Theme.Load(); Updater.Cleanup(); Setup.RefreshIfInstalled(); Image splash = Res.Image("splash.jpg"); if (!JustUpdated && !JustInstalled && !JustRestyled) { using (LauncherForm l = new LauncherForm(splash)) { Application.Run(l); if (l.Restarting) return; } } Application.Run(new MainForm(splash)); if (Relaunch) { try { Process.Start(Application.ExecutablePath, "--restyled"); } catch (Exception) { } } } finally { try { m.ReleaseMutex(); } catch (Exception) { } } } } } internal static class Res { public static Stream Open(string name) { return Assembly.GetExecutingAssembly().GetManifestResourceStream("UltraParadise." + name); } public static Image Image(string name) { try { Stream s = Open(name); if (s == null) return null; using (s) { MemoryStream ms = new MemoryStream(); s.CopyTo(ms); ms.Position = 0; return System.Drawing.Image.FromStream(ms); } } catch (Exception) { return null; } } public static byte[] Bytes(string name) { Stream s = Open(name); if (s == null) return null; using (s) { MemoryStream ms = new MemoryStream(); s.CopyTo(ms); return ms.ToArray(); } } } internal static class Native { [DllImport("user32.dll")] public static extern bool FlashWindow(IntPtr hWnd, bool invert); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; } [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X, Y; } [DllImport("user32.dll")] public static extern bool SetProcessDPIAware(); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid); [DllImport("user32.dll")] public static extern bool GetClientRect(IntPtr hWnd, out RECT r); [DllImport("user32.dll")] public static extern bool ClientToScreen(IntPtr hWnd, ref POINT p); [DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint mods, uint vk); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam); } internal class LinkServer { private readonly TcpListener listener; private readonly string token; private readonly object gate = new object(); private TcpClient current; private StreamWriter writer; public event Action Line; public event Action Sent; public event Action Connected; public event Action Disconnected; public int Port { get; private set; } public bool IsConnected { get { lock (gate) return writer != null; } } public LinkServer() { byte[] r = new byte[16]; using (System.Security.Cryptography.RandomNumberGenerator g = System.Security.Cryptography.RandomNumberGenerator.Create()) g.GetBytes(r); token = BitConverter.ToString(r).Replace("-", "").ToLowerInvariant(); listener = new TcpListener(IPAddress.Loopback, 0); listener.Start(); Port = ((IPEndPoint)listener.LocalEndpoint).Port; Thread t = new Thread(AcceptLoop); t.IsBackground = true; t.Start(); } public const string CodeVersion = "4"; public volatile string GameCode = ""; public static string LinkFolder { get { return Path.Combine(Path.GetTempPath(), "UltraParadise"); } } public void WriteLinkFile(int pid) { Directory.CreateDirectory(LinkFolder); File.WriteAllText(Path.Combine(LinkFolder, "link-" + pid + ".txt"), Port + " " + token); } public static void DeleteLinkFile(int pid) { try { File.Delete(Path.Combine(LinkFolder, "link-" + pid + ".txt")); } catch (Exception) { } } private void AcceptLoop() { while (true) { TcpClient c; try { c = listener.AcceptTcpClient(); } catch (Exception) { return; } Thread t = new Thread(delegate () { Serve(c); }); t.IsBackground = true; t.Start(); } } private void Serve(TcpClient c) { bool mine = false; try { c.NoDelay = true; NetworkStream ns = c.GetStream(); ns.ReadTimeout = 5000; StreamReader r = new StreamReader(ns, new UTF8Encoding(false)); StreamWriter w = new StreamWriter(ns, new UTF8Encoding(false)); w.NewLine = "\n"; w.AutoFlush = true; string hello = r.ReadLine(); if (hello == null) return; string[] p = hello.Split(' '); if (p.Length < 2 || p[0] != "HELLO" || p[1] != token) return; ns.ReadTimeout = Timeout.Infinite; w.WriteLine("OK"); GameCode = p.Length >= 3 ? p[2] : ""; lock (gate) { if (current != null) { try { current.Close(); } catch (Exception) { } } current = c; writer = w; mine = true; } if (Connected != null) Connected(); string line; while ((line = r.ReadLine()) != null) { if (line.Length > 2048 && !(line.Length < 12000000 && (line.StartsWith("SFX ", StringComparison.Ordinal) || line.StartsWith("MICON ", StringComparison.Ordinal) || (line.Length < 200000 && (line.StartsWith("MOODLES ", StringComparison.Ordinal) || line.StartsWith("DLOG ", StringComparison.Ordinal) || line.StartsWith("DSNAP ", StringComparison.Ordinal) || line.StartsWith("DSYS ", StringComparison.Ordinal) || line.StartsWith("MODS ", StringComparison.Ordinal) || line.StartsWith("MUSICLIST ", StringComparison.Ordinal)))))) continue; if (Line != null) Line(line); } } catch (Exception) { } finally { bool dropped = false; lock (gate) { if (mine && current == c) { current = null; writer = null; dropped = true; } } try { c.Close(); } catch (Exception) { } if (dropped && Disconnected != null) Disconnected(); } } public void Send(string s) { lock (gate) { if (writer == null) return; try { writer.WriteLine(s); } catch (Exception) { try { current.Close(); } catch (Exception) { } return; } } Action h = Sent; if (h != null) { try { h(s); } catch (Exception) { } } } } internal enum Kind { Toggle, Action, Slider, Buttons, Info, Note } internal class Btn { public string Label, Act, Arg; public Image Icon; public Btn(string label, string act, string arg) { Label = label; Act = act; Arg = arg; } private static Image nexus, banana, discord; private static bool loaded; public static Btn Logo(Btn b) { if (!loaded) { loaded = true; nexus = Res.Image("logo-nexus.png"); banana = Res.Image("logo-gamebanana.png"); discord = Res.Image("logo-discord.png"); } if (b.Act == "@discord") b.Icon = discord; else if (b.Arg != null && b.Arg.StartsWith("https://gamebanana.com/", StringComparison.Ordinal)) b.Icon = banana; else if (b.Arg != null && b.Arg.StartsWith("https://www.nexusmods.com/", StringComparison.Ordinal) && b.Arg.IndexOf("tab=bugs", StringComparison.Ordinal) < 0) b.Icon = nexus; return b; } } internal class Option { public Kind Kind; public string Id, Name, Hint; public Keys Key, DefaultKey; public bool On; public float Value, Min, Max, Step, Default; public string Format = "x{0:0.00}"; public List Buttons; public Func, string> Text; public Func Swatch; public bool Local; public bool Fav; public string Origin; public Func Show; public bool Bindable { get { return Kind == Kind.Toggle || Kind == Kind.Action; } } } internal class Section { public string Title; public readonly List