cu-trainer/Ultra Paradise Linux/app/Launcher.cs
| 1 | using System; |
| 2 | using System.Diagnostics; |
| 3 | using System.Globalization; |
| 4 | using System.IO; |
| 5 | using System.Linq; |
| 6 | using System.Reflection; |
| 7 | using System.Threading.Tasks; |
| 8 | using Avalonia; |
| 9 | using Avalonia.Controls; |
| 10 | using Avalonia.Input; |
| 11 | using Avalonia.Layout; |
| 12 | using Avalonia.Media; |
| 13 | using Avalonia.Media.Imaging; |
| 14 | using Avalonia.Threading; |
| 15 | |
| 16 | namespace UltraParadise.Linux |
| 17 | { |
| 18 | internal static class Draw |
| 19 | { |
| 20 | public static readonly FontFamily Font = new("fonts:Inter#Inter"); |
| 21 | |
| 22 | public static FormattedText Text(string s, double size, IBrush b, bool bold, double maxWidth, TextAlignment align) |
| 23 | { |
| 24 | FormattedText ft = new(s ?? "", CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(Font, FontStyle.Normal, bold ? FontWeight.SemiBold : FontWeight.Normal), size, b); |
| 25 | if (maxWidth > 0) ft.MaxTextWidth = maxWidth; |
| 26 | ft.TextAlignment = align; |
| 27 | ft.Trimming = TextTrimming.WordEllipsis; |
| 28 | return ft; |
| 29 | } |
| 30 | |
| 31 | public static void Cover(DrawingContext ctx, Bitmap img, Rect dest, double focusY) |
| 32 | { |
| 33 | if (img == null) return; |
| 34 | double iw = img.PixelSize.Width, ih = img.PixelSize.Height; |
| 35 | double sc = Math.Max(dest.Width / iw, dest.Height / ih); |
| 36 | double sw = dest.Width / sc, sh = dest.Height / sc; |
| 37 | double sy = Math.Clamp(ih * focusY - sh / 2, 0, ih - sh); |
| 38 | ctx.DrawImage(img, new Rect((iw - sw) / 2, sy, sw, sh), dest); |
| 39 | } |
| 40 | |
| 41 | public static IBrush Fade(Color top, Color bottom) => new LinearGradientBrush |
| 42 | { |
| 43 | StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), |
| 44 | EndPoint = new RelativePoint(0, 1, RelativeUnit.Relative), |
| 45 | GradientStops = { new GradientStop(top, 0), new GradientStop(bottom, 1) }, |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | internal sealed class LauncherWindow : Window |
| 50 | { |
| 51 | public static string Result; |
| 52 | public static bool Checked; |
| 53 | public bool Restarting; |
| 54 | |
| 55 | private readonly Bitmap art = Program.Image("splash.jpg"), logo = Program.Image("tile.png"); |
| 56 | private readonly Stopwatch clock = Stopwatch.StartNew(); |
| 57 | private readonly DispatcherTimer timer = new() { Interval = TimeSpan.FromMilliseconds(16) }; |
| 58 | private readonly LauncherView view; |
| 59 | private string status = "Starting up", detail = ""; |
| 60 | private int progress = -1; |
| 61 | private bool busy = true, asking, closing; |
| 62 | private long closeAt = long.MaxValue, closingFrom; |
| 63 | private UpdateInfo found; |
| 64 | private Rect updRect, laterRect; |
| 65 | private int hover; |
| 66 | |
| 67 | public LauncherWindow() |
| 68 | { |
| 69 | Title = Program.Name; |
| 70 | Width = 300; |
| 71 | Height = 350; |
| 72 | CanResize = false; |
| 73 | SystemDecorations = SystemDecorations.None; |
| 74 | WindowStartupLocation = WindowStartupLocation.CenterScreen; |
| 75 | Background = new SolidColorBrush(Ui.Back); |
| 76 | Opacity = 0; |
| 77 | Bitmap icon = Program.Image("icon.png"); |
| 78 | if (icon != null) Icon = new WindowIcon(icon); |
| 79 | view = new LauncherView(this); |
| 80 | Content = view; |
| 81 | timer.Tick += (_, _) => Step(); |
| 82 | Opened += (_, _) => { clock.Restart(); timer.Start(); _ = Work(); }; |
| 83 | PointerPressed += OnDown; |
| 84 | PointerMoved += (_, e) => |
| 85 | { |
| 86 | Point p = e.GetPosition(this); |
| 87 | int h = asking ? (updRect.Contains(p) ? 1 : laterRect.Contains(p) ? 2 : 0) : 0; |
| 88 | if (h != hover) { hover = h; Cursor = new Cursor(h != 0 ? StandardCursorType.Hand : StandardCursorType.Arrow); } |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | private void Finish(string s, string d, int ms) |
| 93 | { |
| 94 | status = s; detail = d ?? ""; progress = -1; busy = false; asking = false; |
| 95 | closeAt = clock.ElapsedMilliseconds + ms; |
| 96 | } |
| 97 | |
| 98 | private async Task Work() |
| 99 | { |
| 100 | await Task.Delay(450); |
| 101 | if (!Settings.On("autoupdate", true)) { Finish("Starting", null, 250); return; } |
| 102 | status = "Checking for updates"; |
| 103 | UpdateInfo u; |
| 104 | try { u = await Updater.Check(); } |
| 105 | catch (Exception) |
| 106 | { |
| 107 | Checked = true; |
| 108 | Result = "Couldn't reach the update server. You have v" + Program.Version + "."; |
| 109 | Finish("Couldn't check for updates", "Starting anyway", 1100); |
| 110 | return; |
| 111 | } |
| 112 | Checked = true; |
| 113 | Updater.Ready = u; |
| 114 | if (u == null) |
| 115 | { |
| 116 | Result = "You're up to date (v" + Program.Version + "). Checked " + DateTime.Now.ToString("HH:mm", CultureInfo.InvariantCulture) + "."; |
| 117 | Finish("You're up to date", "Starting", 500); |
| 118 | return; |
| 119 | } |
| 120 | Result = "v" + u.Version + " is out (you have v" + Program.Version + "). Click Update Now."; |
| 121 | if (Settings.On("silentupdate", false) && !Updater.Manual && !Game.GameRunning() && u.VtClean) { await Install(u); return; } |
| 122 | found = u; |
| 123 | busy = false; |
| 124 | asking = true; |
| 125 | status = "Update available"; |
| 126 | detail = "v" + u.Version + (Updater.Manual ? " is out. Get it from Nexus Mods" : " is ready to install") + (u.Notes.Length > 0 ? "\n\nWhat's new:\n" + u.Notes : "") + (Updater.Manual ? "" : "\n\n" + u.VtLine + (u.VtFlagged ? " Look at the report on virustotal.com before you install it." : "")); |
| 127 | FitNotes(); |
| 128 | } |
| 129 | |
| 130 | private double detailH; |
| 131 | |
| 132 | private void FitNotes() |
| 133 | { |
| 134 | double W = 440; |
| 135 | FormattedText d = Draw.Text(detail, 12, new SolidColorBrush(Ui.Soft), false, W - 48, TextAlignment.Left); |
| 136 | double maxText = 420; |
| 137 | try { if (Screens.ScreenFromWindow(this) is Avalonia.Platform.Screen sc) maxText = Math.Max(60, sc.WorkingArea.Height / sc.Scaling - (186 + 28 + 14 + 34 + 60)); } catch (Exception) { } |
| 138 | detailH = Math.Min(maxText, Math.Ceiling(d.Height) + 4); |
| 139 | PixelPoint old = Position; |
| 140 | double ow = Width, oh = Height; |
| 141 | Width = W; |
| 142 | Height = Math.Max(350, 186 + 28 + detailH + 14 + 34 + 46); |
| 143 | Position = new PixelPoint(old.X + (int)((ow - Width) / 2), Math.Max(0, old.Y + (int)((oh - Height) / 2))); |
| 144 | } |
| 145 | |
| 146 | private async Task Install(UpdateInfo u) |
| 147 | { |
| 148 | busy = true; |
| 149 | asking = false; |
| 150 | status = "Downloading update"; |
| 151 | detail = "v" + u.Version + " · 0%"; |
| 152 | progress = 0; |
| 153 | string err; |
| 154 | try |
| 155 | { |
| 156 | err = await Updater.Install(u, pc => Dispatcher.UIThread.Post(() => |
| 157 | { |
| 158 | progress = pc; |
| 159 | status = pc >= 100 ? "Installing update" : "Downloading update"; |
| 160 | detail = "v" + u.Version + " · " + pc + "%"; |
| 161 | })); |
| 162 | } |
| 163 | catch (Exception e) { err = e.Message; } |
| 164 | if (err == null) |
| 165 | { |
| 166 | Restarting = true; |
| 167 | Finish("Restarting", "Opening v" + u.Version, 350); |
| 168 | return; |
| 169 | } |
| 170 | Result = "Update failed: " + err; |
| 171 | Finish("Update failed", err, 4000); |
| 172 | } |
| 173 | |
| 174 | private void OnDown(object sender, PointerPressedEventArgs e) |
| 175 | { |
| 176 | if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return; |
| 177 | Point p = e.GetPosition(this); |
| 178 | if (asking && updRect.Contains(p)) |
| 179 | { |
| 180 | if (Updater.Manual) { Game.Open(Updater.ManualPage); Finish("Starting", "Download v" + found.Version + " from Nexus Mods", 1200); return; } |
| 181 | _ = Install(found); |
| 182 | return; |
| 183 | } |
| 184 | if (asking && laterRect.Contains(p)) { Finish("Starting", null, 200); return; } |
| 185 | try { BeginMoveDrag(e); } catch (Exception) { } |
| 186 | } |
| 187 | |
| 188 | private void Step() |
| 189 | { |
| 190 | long t = clock.ElapsedMilliseconds; |
| 191 | if (closing) |
| 192 | { |
| 193 | double o = 1.0 - (t - closingFrom) / 180.0; |
| 194 | if (o <= 0) { timer.Stop(); Close(); return; } |
| 195 | Opacity = o; |
| 196 | return; |
| 197 | } |
| 198 | if (Opacity < 1) Opacity = Math.Min(1.0, t / 220.0); |
| 199 | if (t >= closeAt) { closing = true; closingFrom = t; } |
| 200 | view.InvalidateVisual(); |
| 201 | } |
| 202 | |
| 203 | private sealed class LauncherView : Control |
| 204 | { |
| 205 | private readonly LauncherWindow w; |
| 206 | public LauncherView(LauncherWindow owner) { w = owner; } |
| 207 | |
| 208 | public override void Render(DrawingContext ctx) |
| 209 | { |
| 210 | double W = Bounds.Width, H = Bounds.Height; |
| 211 | double t = w.clock.ElapsedMilliseconds / 1000.0; |
| 212 | Color back = Ui.Back; |
| 213 | ctx.FillRectangle(new SolidColorBrush(back), new Rect(0, 0, W, H)); |
| 214 | double band = 150; |
| 215 | Draw.Cover(ctx, w.art, new Rect(0, 0, W, band), 0.45); |
| 216 | ctx.FillRectangle(Draw.Fade(Ui.Alpha(back, 70), back), new Rect(0, 0, W, band + 1)); |
| 217 | |
| 218 | double ls = 92; |
| 219 | double bob = w.busy ? Math.Sin(t * 3.2) * 4 : 0; |
| 220 | double pulse = w.busy ? 1 + Math.Sin(t * 3.2 + 1.2) * 0.03 : 1; |
| 221 | double lw = ls * pulse; |
| 222 | double lx = (W - lw) / 2, ly = 70 + bob - (lw - ls) / 2; |
| 223 | byte ga = (byte)(w.busy ? 40 + 25 * Math.Sin(t * 3.2) : 30); |
| 224 | ctx.DrawEllipse(new SolidColorBrush(Ui.Alpha(Ui.Orange, (byte)ga)), null, new Point(W / 2, ly + lw / 2), lw / 2 + 12, lw / 2 + 12); |
| 225 | if (w.logo != null) |
| 226 | { |
| 227 | using (ctx.PushRenderOptions(new RenderOptions { BitmapInterpolationMode = BitmapInterpolationMode.None })) |
| 228 | ctx.DrawImage(w.logo, new Rect(lx, ly, lw, lw)); |
| 229 | } |
| 230 | |
| 231 | double ty = 186; |
| 232 | ctx.DrawText(Draw.Text(w.status, 15, new SolidColorBrush(Ui.Bright), true, W - 24, TextAlignment.Center), new Point(12, ty)); |
| 233 | double by = 262; |
| 234 | if (w.asking && w.detailH > 0) |
| 235 | { |
| 236 | FormattedText d = Draw.Text(w.detail, 12, new SolidColorBrush(Ui.Soft), false, W - 48, TextAlignment.Left); |
| 237 | d.MaxTextHeight = w.detailH; |
| 238 | ctx.DrawText(d, new Point(24, ty + 28)); |
| 239 | by = ty + 28 + w.detailH + 14; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | FormattedText d = Draw.Text(w.detail, 12, new SolidColorBrush(Ui.Soft), false, W - 40, TextAlignment.Center); |
| 244 | d.MaxTextHeight = 40; |
| 245 | ctx.DrawText(d, new Point(20, ty + 28)); |
| 246 | } |
| 247 | if (w.progress >= 0) |
| 248 | { |
| 249 | Rect bar = new(40, by + 10, W - 80, 6); |
| 250 | ctx.DrawRectangle(new SolidColorBrush(Ui.Line), null, bar, 3, 3); |
| 251 | ctx.DrawRectangle(new SolidColorBrush(Ui.Orange), null, new Rect(bar.X, bar.Y, Math.Max(bar.Height, bar.Width * Math.Min(100, w.progress) / 100.0), bar.Height), 3, 3); |
| 252 | } |
| 253 | else if (w.asking) |
| 254 | { |
| 255 | double bw = 112, bh = 34, gap = 10, bx = (W - bw * 2 - gap) / 2; |
| 256 | w.updRect = new Rect(bx, by, bw, bh); |
| 257 | w.laterRect = new Rect(bx + bw + gap, by, bw, bh); |
| 258 | ctx.DrawRectangle(new SolidColorBrush(w.hover == 1 ? Ui.AccentHover : Ui.Orange), null, w.updRect, 5, 5); |
| 259 | ctx.DrawRectangle(new SolidColorBrush(w.hover == 2 ? Ui.Mix(Ui.Line, Ui.Text, 0.18) : Ui.Line), null, w.laterRect, 5, 5); |
| 260 | FormattedText u = Draw.Text("Update", 13, new SolidColorBrush(Ui.OnAccent), true, bw, TextAlignment.Center); |
| 261 | ctx.DrawText(u, new Point(w.updRect.X, w.updRect.Y + (bh - u.Height) / 2)); |
| 262 | FormattedText l = Draw.Text("Later", 13, new SolidColorBrush(Ui.Text), true, bw, TextAlignment.Center); |
| 263 | ctx.DrawText(l, new Point(w.laterRect.X, w.laterRect.Y + (bh - l.Height) / 2)); |
| 264 | } |
| 265 | else if (w.busy) |
| 266 | { |
| 267 | double sp = 14, dx = (W - 2 * sp) / 2; |
| 268 | for (int i = 0; i < 3; i++) |
| 269 | { |
| 270 | double ph = Math.Max(0, Math.Sin(t * 6 - i * 0.7)); |
| 271 | byte a = (byte)(90 + 165 * ph); |
| 272 | ctx.DrawEllipse(new SolidColorBrush(Ui.Alpha(Ui.Orange, (byte)a)), null, new Point(dx + i * sp, by + 12 - ph * 3), 4, 4); |
| 273 | } |
| 274 | } |
| 275 | ctx.DrawText(Draw.Text("v" + Program.Version + " · official copies only on Nexus Mods and GameBanana", 10, new SolidColorBrush(Ui.Dim), false, W, TextAlignment.Center), new Point(0, H - 26)); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | internal sealed class SplashWindow : Window |
| 281 | { |
| 282 | private const int FadeIn = 350, Hold = 1700, FadeOut = 350; |
| 283 | private readonly Bitmap art = Program.Image("splash.jpg"); |
| 284 | private readonly Stopwatch clock = Stopwatch.StartNew(); |
| 285 | private readonly DispatcherTimer timer = new() { Interval = TimeSpan.FromMilliseconds(16) }; |
| 286 | private bool skipping; |
| 287 | private double skipFrom; |
| 288 | |
| 289 | public SplashWindow() |
| 290 | { |
| 291 | Title = Program.Name; |
| 292 | double w = 760, h = art != null ? w * art.PixelSize.Height / art.PixelSize.Width : 500; |
| 293 | Width = w; |
| 294 | Height = h; |
| 295 | CanResize = false; |
| 296 | SystemDecorations = SystemDecorations.None; |
| 297 | WindowStartupLocation = WindowStartupLocation.CenterScreen; |
| 298 | Background = Brushes.Black; |
| 299 | Opacity = 0; |
| 300 | Bitmap icon = Program.Image("icon.png"); |
| 301 | if (icon != null) Icon = new WindowIcon(icon); |
| 302 | Content = new SplashView(art); |
| 303 | timer.Tick += (_, _) => Step(); |
| 304 | Opened += (_, _) => { clock.Restart(); timer.Start(); }; |
| 305 | PointerPressed += (_, _) => Skip(); |
| 306 | KeyDown += (_, _) => Skip(); |
| 307 | } |
| 308 | |
| 309 | private void Skip() |
| 310 | { |
| 311 | if (skipping || clock.ElapsedMilliseconds >= FadeIn + Hold) return; |
| 312 | skipFrom = Opacity; |
| 313 | skipping = true; |
| 314 | clock.Restart(); |
| 315 | } |
| 316 | |
| 317 | private void Step() |
| 318 | { |
| 319 | long t = clock.ElapsedMilliseconds; |
| 320 | if (skipping) |
| 321 | { |
| 322 | double o = skipFrom * (1.0 - t / (double)FadeOut); |
| 323 | if (o <= 0) { timer.Stop(); Close(); return; } |
| 324 | Opacity = o; |
| 325 | return; |
| 326 | } |
| 327 | if (t < FadeIn) Opacity = t / (double)FadeIn; |
| 328 | else if (t < FadeIn + Hold) Opacity = 1; |
| 329 | else if (t < FadeIn + Hold + FadeOut) Opacity = 1 - (t - FadeIn - Hold) / (double)FadeOut; |
| 330 | else { timer.Stop(); Close(); } |
| 331 | } |
| 332 | |
| 333 | private sealed class SplashView : Control |
| 334 | { |
| 335 | private readonly Bitmap art; |
| 336 | public SplashView(Bitmap a) { art = a; } |
| 337 | |
| 338 | public override void Render(DrawingContext ctx) |
| 339 | { |
| 340 | double W = Bounds.Width, H = Bounds.Height; |
| 341 | if (art != null) ctx.DrawImage(art, new Rect(0, 0, W, H)); |
| 342 | ctx.FillRectangle(Draw.Fade(Color.FromArgb(0, 10, 10, 11), Color.FromArgb(215, 10, 10, 11)), new Rect(0, H - 76, W, 76)); |
| 343 | ctx.DrawText(Draw.Text("The only official trainer app by casualexpie", 13, Brushes.White, true, 0, TextAlignment.Left), new Point(12, H - 42)); |
| 344 | ctx.DrawText(Draw.Text("Only download it from Nexus Mods or GameBanana", 11.5, new SolidColorBrush(Ui.Orange), false, 0, TextAlignment.Left), new Point(13, H - 22)); |
| 345 | FormattedText v = Draw.Text("CU:Trainer v" + Program.Version + " for " + Program.Platform + " · open source, (c) casualexpie", 11.5, new SolidColorBrush(Color.FromRgb(170, 170, 170)), false, 0, TextAlignment.Left); |
| 346 | ctx.DrawText(v, new Point(W - v.Width - 10, H - v.Height - 8)); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | internal static class Setup |
| 352 | { |
| 353 | private static string Data |
| 354 | { |
| 355 | get |
| 356 | { |
| 357 | string x = Environment.GetEnvironmentVariable("XDG_DATA_HOME"); |
| 358 | return string.IsNullOrEmpty(x) ? Path.Combine(Dirs.Home, ".local", "share") : x; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | public static string Dest => Path.Combine(Data, "ultraparadise-cutrainer"); |
| 363 | private static string Bin => Path.Combine(Dirs.Home, ".local", "bin"); |
| 364 | private static string Apps => Path.Combine(Data, "applications"); |
| 365 | private const string Exe = "UltraParadiseCUTrainer"; |
| 366 | private const string DesktopName = "ultraparadise-cutrainer.desktop"; |
| 367 | |
| 368 | private static string DesktopDir |
| 369 | { |
| 370 | get |
| 371 | { |
| 372 | try |
| 373 | { |
| 374 | string f = Path.Combine(Dirs.Home, ".config", "user-dirs.dirs"); |
| 375 | if (File.Exists(f)) |
| 376 | foreach (string line in File.ReadAllLines(f)) |
| 377 | if (line.StartsWith("XDG_DESKTOP_DIR=", StringComparison.Ordinal)) |
| 378 | { |
| 379 | string d = line[16..].Trim('"').Replace("$HOME", Dirs.Home); |
| 380 | if (Directory.Exists(d)) return d; |
| 381 | } |
| 382 | } |
| 383 | catch (Exception) { } |
| 384 | string def = Path.Combine(Dirs.Home, "Desktop"); |
| 385 | return Directory.Exists(def) ? def : null; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | public static bool Installed => File.Exists(Path.Combine(Dest, Exe)); |
| 390 | |
| 391 | public static bool RunningInstalled |
| 392 | { |
| 393 | get |
| 394 | { |
| 395 | string p = Environment.ProcessPath; |
| 396 | return p != null && Path.GetFullPath(p).StartsWith(Path.GetFullPath(Dest) + Path.DirectorySeparatorChar, StringComparison.Ordinal); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | public static bool HasShortcut => DesktopDir != null && File.Exists(Path.Combine(DesktopDir, DesktopName)); |
| 401 | |
| 402 | public static string Status() |
| 403 | { |
| 404 | if (OperatingSystem.IsWindows()) return "Installing works on Linux only."; |
| 405 | if (Mac.On) return "On a Mac, keep the app in your Applications folder. Uninstall removes the app and, if you choose, UltraPara.dll and your settings."; |
| 406 | if (RunningInstalled) return "Installed in your home folder, with an app menu entry" + (HasShortcut ? " and a desktop shortcut" : "") + "."; |
| 407 | if (Installed) return "Another copy is installed in your home folder. You're running this one from " + Path.GetDirectoryName(Environment.ProcessPath) + "."; |
| 408 | return "Not installed. You're running it from " + Path.GetDirectoryName(Environment.ProcessPath) + "."; |
| 409 | } |
| 410 | |
| 411 | private static string Entry() => |
| 412 | "[Desktop Entry]\nType=Application\nName=" + Program.Name + "\nComment=Trainer for Casualties: Unknown, by casualexpie\nExec=\"" + Path.Combine(Dest, Exe) + "\"\nIcon=" + Path.Combine(Dest, "icon.png") + "\nTerminal=false\nCategories=Game;Utility;\nStartupWMClass=UltraParadiseCUTrainer\n"; |
| 413 | |
| 414 | public static string Install(bool shortcut) |
| 415 | { |
| 416 | if (OperatingSystem.IsWindows()) return "installing works on Linux only"; |
| 417 | if (Mac.On) return "on a Mac, drag the app into your Applications folder instead"; |
| 418 | try |
| 419 | { |
| 420 | string cur = Environment.ProcessPath; |
| 421 | string exe = Path.Combine(Dest, Exe); |
| 422 | Directory.CreateDirectory(Dest); |
| 423 | if (!RunningInstalled) |
| 424 | { |
| 425 | string tmp = exe + ".new"; |
| 426 | File.Copy(cur, tmp, true); |
| 427 | File.Move(tmp, exe, true); |
| 428 | } |
| 429 | File.SetUnixFileMode(exe, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | UnixFileMode.GroupRead | UnixFileMode.GroupExecute | UnixFileMode.OtherRead | UnixFileMode.OtherExecute); |
| 430 | using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("icon.png")) |
| 431 | using (FileStream fs = File.Create(Path.Combine(Dest, "icon.png"))) |
| 432 | s?.CopyTo(fs); |
| 433 | Directory.CreateDirectory(Bin); |
| 434 | string link = Path.Combine(Bin, "ultraparadise"); |
| 435 | if (File.Exists(link) || new FileInfo(link).LinkTarget != null) File.Delete(link); |
| 436 | File.CreateSymbolicLink(link, exe); |
| 437 | Directory.CreateDirectory(Apps); |
| 438 | File.WriteAllText(Path.Combine(Apps, DesktopName), Entry()); |
| 439 | if (shortcut) AddShortcut(); |
| 440 | Quiet("update-desktop-database", Apps); |
| 441 | return null; |
| 442 | } |
| 443 | catch (Exception e) { return e.Message; } |
| 444 | } |
| 445 | |
| 446 | public static string AddShortcut() |
| 447 | { |
| 448 | string d = DesktopDir; |
| 449 | if (d == null) return "couldn't find your desktop folder"; |
| 450 | try |
| 451 | { |
| 452 | string f = Path.Combine(d, DesktopName); |
| 453 | File.WriteAllText(f, Entry()); |
| 454 | File.SetUnixFileMode(f, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | UnixFileMode.GroupRead | UnixFileMode.OtherRead); |
| 455 | Quiet("gio", "set", f, "metadata::trusted", "true"); |
| 456 | return null; |
| 457 | } |
| 458 | catch (Exception e) { return e.Message; } |
| 459 | } |
| 460 | |
| 461 | public static void OpenInstalled() |
| 462 | { |
| 463 | try { Process.Start(new ProcessStartInfo(Path.Combine(Dest, Exe), "--installed") { UseShellExecute = false }); } catch (Exception) { } |
| 464 | } |
| 465 | |
| 466 | public static string Uninstall(string game, bool plugin, bool settings) |
| 467 | { |
| 468 | if (plugin && game != null && Game.IsGameDir(game)) |
| 469 | { |
| 470 | if (Game.GameRunning()) return "close the game first, so UltraPara.dll can be removed"; |
| 471 | string e = Game.RemovePlugin(game); |
| 472 | if (e != null) return "couldn't remove UltraPara.dll: " + e; |
| 473 | } |
| 474 | if (Mac.On) |
| 475 | { |
| 476 | try |
| 477 | { |
| 478 | if (settings) |
| 479 | { |
| 480 | if (Directory.Exists(Dirs.Config)) Directory.Delete(Dirs.Config, true); |
| 481 | if (Directory.Exists(Dirs.Cache)) Directory.Delete(Dirs.Cache, true); |
| 482 | } |
| 483 | string bundle = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), "..", "..")); |
| 484 | if (bundle.EndsWith(".app", StringComparison.OrdinalIgnoreCase) && Directory.Exists(bundle)) Directory.Delete(bundle, true); |
| 485 | return null; |
| 486 | } |
| 487 | catch (Exception e) { return e.Message; } |
| 488 | } |
| 489 | try |
| 490 | { |
| 491 | foreach (string f in new[] { Path.Combine(Bin, "ultraparadise"), Path.Combine(Apps, DesktopName), DesktopDir != null ? Path.Combine(DesktopDir, DesktopName) : null }) |
| 492 | if (f != null && (File.Exists(f) || new FileInfo(f).LinkTarget != null)) File.Delete(f); |
| 493 | if (Directory.Exists(Dest)) Directory.Delete(Dest, true); |
| 494 | if (settings) |
| 495 | { |
| 496 | if (Directory.Exists(Dirs.Config)) Directory.Delete(Dirs.Config, true); |
| 497 | if (Directory.Exists(Dirs.Cache)) Directory.Delete(Dirs.Cache, true); |
| 498 | } |
| 499 | Quiet("update-desktop-database", Apps); |
| 500 | return null; |
| 501 | } |
| 502 | catch (Exception e) { return e.Message; } |
| 503 | } |
| 504 | |
| 505 | private static void Quiet(string exe, params string[] args) |
| 506 | { |
| 507 | string p = Game.Which(exe); |
| 508 | if (p == null) return; |
| 509 | try |
| 510 | { |
| 511 | ProcessStartInfo si = new(p) { UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; |
| 512 | foreach (string a in args) si.ArgumentList.Add(a); |
| 513 | using Process pr = Process.Start(si); |
| 514 | pr?.WaitForExit(4000); |
| 515 | } |
| 516 | catch (Exception) { } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | internal static class Piracy |
| 521 | { |
| 522 | public const string SteamPage = "https://store.steampowered.com/app/4576490/"; |
| 523 | private static readonly string[] Files = { "steam_emu.ini", "smartsteamemu.ini", "cream_api.ini", "codex.ini", "steam_api64.cdx", "onlinefix.ini", "onlinefix64.dll", "ali213.ini", "3dmgame.ini", "tenoke.ini", "unsteam.ini", "steam_interfaces.txt", "local_save.txt", "account_name.txt", "user_steam_id.txt", "coldclientloader.ini", "steamclient_loader_x64.exe", "empress.ini", "rune.ini", "skidrow.ini" }; |
| 524 | private static readonly string[] Dirs = { "steam_settings", "smartsteamemu", "goldberg steamemu saves", "onlinefix" }; |
| 525 | |
| 526 | public static bool Flagged(string game) |
| 527 | { |
| 528 | if (string.IsNullOrEmpty(game)) return false; |
| 529 | try |
| 530 | { |
| 531 | foreach (string w in new[] { game, Path.Combine(game, "CasualtiesUnknown_Data", "Plugins", "x86_64"), Path.Combine(game, "CasualtiesUnknown_Data", "Plugins") }) |
| 532 | { |
| 533 | if (!Directory.Exists(w)) continue; |
| 534 | foreach (string e in Directory.EnumerateFileSystemEntries(w)) |
| 535 | { |
| 536 | string n = Path.GetFileName(e).ToLowerInvariant(); |
| 537 | if (Directory.Exists(e) ? Dirs.Contains(n) : Files.Contains(n)) return true; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | catch (Exception) { } |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | public static bool Ok(string game) => !Flagged(game) || Settings.Get("piracy.ok", "") == game; |
| 546 | } |
| 547 | |
| 548 | internal sealed class PiracyDialog : Window |
| 549 | { |
| 550 | public bool Proceed; |
| 551 | |
| 552 | public PiracyDialog() |
| 553 | { |
| 554 | Title = Program.Name; |
| 555 | Width = 460; |
| 556 | SizeToContent = SizeToContent.Height; |
| 557 | CanResize = false; |
| 558 | WindowStartupLocation = WindowStartupLocation.CenterOwner; |
| 559 | Background = Ui.B(Ui.Back); |
| 560 | StackPanel sp = new() { Margin = new Thickness(20), Spacing = 12 }; |
| 561 | sp.Children.Add(new TextBlock { Text = "This doesn't look like an official copy", FontSize = 18, FontWeight = FontWeight.Bold, Foreground = Ui.B(Ui.Orange) }); |
| 562 | sp.Children.Add(new TextBlock { Text = "The trainer found files in your game folder that come from cracked copies of the game.", TextWrapping = TextWrapping.Wrap, Foreground = Ui.B(Ui.Text) }); |
| 563 | sp.Children.Add(new TextBlock { Text = "Ultra Paradise doesn't condone piracy. Casualties: Unknown is made by a small team, so please support Orsoniks and Oro Interactive by buying it on Steam.", TextWrapping = TextWrapping.Wrap, Foreground = Ui.B(Ui.Text) }); |
| 564 | sp.Children.Add(new TextBlock { Text = "If you think this is a mistake, you can pick the game's .exe yourself.", TextWrapping = TextWrapping.Wrap, Foreground = Ui.B(Ui.Dim) }); |
| 565 | StackPanel btns = new() { Orientation = Orientation.Horizontal, Spacing = 8, HorizontalAlignment = HorizontalAlignment.Right }; |
| 566 | Button steam = new() { Content = "Open the Steam page", Padding = new Thickness(16, 6), Background = Ui.B(Ui.Orange), Foreground = Ui.B(Ui.OnAccent) }; |
| 567 | steam.Click += (_, _) => { Game.Open(Piracy.SteamPage); Close(); }; |
| 568 | Button go = new() { Content = "Proceed and pick the .exe", Padding = new Thickness(16, 6) }; |
| 569 | go.Click += (_, _) => { Proceed = true; Close(); }; |
| 570 | btns.Children.Add(go); |
| 571 | btns.Children.Add(steam); |
| 572 | sp.Children.Add(btns); |
| 573 | Content = sp; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | internal sealed class UninstallDialog : Window |
| 578 | { |
| 579 | public bool Confirmed, Plugin = true, Settings_; |
| 580 | |
| 581 | public UninstallDialog() |
| 582 | { |
| 583 | Title = "Uninstall " + Program.Name; |
| 584 | Width = 440; |
| 585 | SizeToContent = SizeToContent.Height; |
| 586 | CanResize = false; |
| 587 | WindowStartupLocation = WindowStartupLocation.CenterOwner; |
| 588 | Background = Ui.B(Ui.Back); |
| 589 | StackPanel sp = new() { Margin = new Thickness(20), Spacing = 12 }; |
| 590 | sp.Children.Add(new TextBlock { Text = "Uninstall the trainer?", FontSize = 18, FontWeight = FontWeight.Bold, Foreground = Ui.B(Ui.Text) }); |
| 591 | sp.Children.Add(new TextBlock { Text = Mac.On ? "This removes the app from your Mac." : "This removes the app, its app menu entry, desktop shortcut and the 'ultraparadise' command.", TextWrapping = TextWrapping.Wrap, Foreground = Ui.B(Ui.Dim) }); |
| 592 | CheckBox pl = new() { Content = "Also remove UltraPara.dll from the game folder (the cheats inside the game)", IsChecked = true }; |
| 593 | CheckBox st = new() { Content = "Also remove my settings and cached sounds", IsChecked = false }; |
| 594 | sp.Children.Add(pl); |
| 595 | sp.Children.Add(st); |
| 596 | StackPanel btns = new() { Orientation = Orientation.Horizontal, Spacing = 8, HorizontalAlignment = HorizontalAlignment.Right }; |
| 597 | Button cancel = new() { Content = "Cancel", Padding = new Thickness(16, 6) }; |
| 598 | cancel.Click += (_, _) => Close(); |
| 599 | Button ok = new() { Content = "Uninstall", Padding = new Thickness(16, 6), Background = Ui.B(Ui.Bad), Foreground = Brushes.White }; |
| 600 | ok.Click += (_, _) => { Confirmed = true; Plugin = pl.IsChecked == true; Settings_ = st.IsChecked == true; Close(); }; |
| 601 | btns.Children.Add(cancel); |
| 602 | btns.Children.Add(ok); |
| 603 | sp.Children.Add(btns); |
| 604 | Content = sp; |
| 605 | } |
| 606 | } |
| 607 | } |