-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
261 lines (225 loc) · 9.99 KB
/
App.xaml.cs
File metadata and controls
261 lines (225 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using BabySmash.Properties;
using Updatum;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using WinForms = System.Windows.Forms;
namespace BabySmash
{
public partial class App : Application
{
private static readonly InterceptKeys.LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static Mutex _singleInstanceMutex;
internal static readonly UpdatumManager AppUpdater = new("shanselman", "babysmash")
{
FetchOnlyLatestRelease = true,
InstallUpdateSingleFileExecutableName = "BabySmash",
};
private async void Application_Startup(object sender, StartupEventArgs e)
{
// Parse command line arguments
var args = e.Args;
Controller.ShowFps = args.Contains("--fps") || args.Contains("-fps");
// Auto-detect GPU capability on first run
AutoDetectVisualEffects();
// Check for updates BEFORE launching the game
// This way the parent can handle updates before baby takes over
var shouldLaunch = await CheckForUpdatesBeforeLaunchAsync();
if (shouldLaunch)
{
Controller.Instance.Launch();
}
}
private void AutoDetectVisualEffects()
{
// Only auto-detect once (on first run)
if (Settings.Default.EffectsAutoDetected)
return;
// WPF Rendering Tier: 0=software, 1=partial HW, 2=full GPU acceleration
int renderingTier = RenderCapability.Tier >> 16;
// Enable effects only on Tier 2 (full GPU acceleration)
Settings.Default.BitmapEffects = renderingTier >= 2;
Settings.Default.EffectsAutoDetected = true;
Settings.Default.Save();
Debug.WriteLine($"Auto-detected rendering tier {renderingTier}, visual effects: {Settings.Default.BitmapEffects}");
}
private async Task<bool> CheckForUpdatesBeforeLaunchAsync()
{
try
{
var updateFound = await AppUpdater.CheckForUpdatesAsync();
if (!updateFound) return true;
var release = AppUpdater.LatestRelease!;
var changelog = AppUpdater.GetChangelog(true) ?? "No release notes available.";
var dialog = new UpdateDialog(release.TagName, changelog);
dialog.ShowDialog();
if (dialog.Result == UpdateDialog.UpdateDialogResult.Download)
{
var installed = await DownloadAndInstallUpdateAsync();
// If install succeeded, app will restart - don't launch
// If install failed, let user continue to the app
return !installed;
}
// RemindLater or Skip - continue to launch
return true;
}
catch (Exception ex)
{
// Update check failed - don't block the app, just launch
Debug.WriteLine($"Update check failed: {ex.Message}");
return true;
}
}
private async Task<bool> DownloadAndInstallUpdateAsync()
{
DownloadProgressDialog progressDialog = null;
try
{
progressDialog = new DownloadProgressDialog(AppUpdater);
progressDialog.Show();
var downloadedAsset = await AppUpdater.DownloadUpdateAsync();
progressDialog?.Close();
progressDialog = null;
if (downloadedAsset == null)
{
MessageBox.Show("Failed to download the update. Please try again later.",
"Download Failed", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (!System.IO.File.Exists(downloadedAsset.FilePath))
{
MessageBox.Show($"Update file was deleted or is inaccessible:\n{downloadedAsset.FilePath}\n\nThis may be caused by antivirus software.",
"Update File Missing", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
var confirmResult = MessageBox.Show(
"The update has been downloaded. BabySmash! will now restart to install the update.\n\nContinue?",
"Install Update",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (confirmResult == MessageBoxResult.Yes)
{
await AppUpdater.InstallUpdateAsync(downloadedAsset);
return true; // App will restart
}
return false; // User cancelled, continue to app
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show($"Access denied when accessing update file.\n\n1. Antivirus may be blocking the update\n2. Windows SmartScreen may need approval\n\nError: {ex.Message}",
"Access Denied", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
catch (Exception ex)
{
MessageBox.Show($"Failed to download or install update: {ex.Message}",
"Update Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
finally
{
progressDialog?.Close();
}
}
public App()
{
// Prevent multiple instances from running at the same time
bool createdNew;
_singleInstanceMutex = new Mutex(true, "BabySmashSingleInstance", out createdNew);
if (!createdNew)
{
// Dispose mutex before shutting down to prevent abandonment
_singleInstanceMutex?.Dispose();
Shutdown();
return;
}
ShutdownMode = ShutdownMode.OnExplicitShutdown;
Application.Current.Exit += new ExitEventHandler(Current_Exit);
try
{
_hookID = InterceptKeys.SetHook(_proc);
}
catch
{
DetachKeyboardHook();
}
}
void Current_Exit(object sender, ExitEventArgs e)
{
DetachKeyboardHook();
Controller.Instance.Shutdown();
_singleInstanceMutex?.Dispose();
}
/// <summary>
/// Detach the keyboard hook; call during shutdown to prevent calls as we unload
/// </summary>
private static void DetachKeyboardHook()
{
if (_hookID != IntPtr.Zero)
InterceptKeys.UnhookWindowsHookEx(_hookID);
}
public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
bool alt = (WinForms.Control.ModifierKeys & Keys.Alt) != 0;
bool control = (WinForms.Control.ModifierKeys & Keys.Control) != 0;
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
if (alt && key == Keys.F4)
{
Application.Current.Shutdown();
return (IntPtr)1; // Handled.
}
if (!AllowKeyboardInput(alt, control, key))
{
return (IntPtr)1; // Handled.
}
}
return InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
/// <summary>Determines whether the specified keyboard input should be allowed to be processed by the system.</summary>
/// <remarks>Helps block unwanted keys and key combinations that could exit the app, make system changes, etc.</remarks>
public static bool AllowKeyboardInput(bool alt, bool control, Keys key)
{
// Disallow various special keys.
// F4 alone triggers Properties command in Windows - kids hit it accidentally
if (key <= Keys.Back || key == Keys.None ||
key == Keys.Menu || key == Keys.Pause ||
key == Keys.Help || key == Keys.F4)
{
return false;
}
// Disallow ranges of special keys.
// Currently leaves volume controls enabled; consider if this makes sense.
// Disables non-existing Keys up to 65534, to err on the side of caution for future keyboard expansion.
if ((key >= Keys.LWin && key <= Keys.Sleep) ||
(key >= Keys.KanaMode && key <= Keys.HanjaMode) ||
(key >= Keys.IMEConvert && key <= Keys.IMEModeChange) ||
(key >= Keys.BrowserBack && key <= Keys.BrowserHome) ||
(key >= Keys.MediaNextTrack && key <= Keys.LaunchApplication2) ||
(key >= Keys.ProcessKey && key <= (Keys)65534))
{
return false;
}
// Disallow specific key combinations. (These component keys would be OK on their own.)
if ((alt && key == Keys.Tab) ||
(alt && key == Keys.Space) ||
(control && key == Keys.Escape))
{
return false;
}
// Allow anything else (like letters, numbers, spacebar, braces, and so on).
return true;
}
}
}