Skip to content

Commit bf100fb

Browse files
authored
Merge pull request #100 from winnerspiros/copilot/fix-android-startup-crash
Fix Android startup crash: null safety, API level guards, safe defaults
2 parents 863a566 + 3c4af10 commit bf100fb

5 files changed

Lines changed: 74 additions & 24 deletions

File tree

osu.Android/Native/OboeAudioBridge.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ public sealed class OboeAudioBridge : IDisposable
2323

2424
static OboeAudioBridge()
2525
{
26-
native_loaded = NativeLibrary.TryLoad(lib_name, typeof(OboeAudioBridge).Assembly, null, out _);
26+
try
27+
{
28+
native_loaded = NativeLibrary.TryLoad(lib_name, typeof(OboeAudioBridge).Assembly, null, out _);
29+
}
30+
catch (Exception e)
31+
{
32+
Debug.WriteLine($"[osu!] Failed to probe native library for Oboe: {e.Message}");
33+
native_loaded = false;
34+
}
2735

2836
if (!native_loaded)
2937
Debug.WriteLine("[osu!] Native library not found, Oboe unavailable");

osu.Android/Native/VulkanProbe.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ public sealed class VulkanProbe : IDisposable
2323

2424
static VulkanProbe()
2525
{
26-
native_loaded = NativeLibrary.TryLoad(lib_name, typeof(VulkanProbe).Assembly, null, out _);
26+
try
27+
{
28+
native_loaded = NativeLibrary.TryLoad(lib_name, typeof(VulkanProbe).Assembly, null, out _);
29+
}
30+
catch (Exception e)
31+
{
32+
Debug.WriteLine($"[osu!] Failed to probe native library for Vulkan: {e.Message}");
33+
native_loaded = false;
34+
}
2735

2836
if (!native_loaded)
2937
Debug.WriteLine("[osu!] Native library not found, Vulkan probe unavailable");

osu.Android/OsuGameActivity.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ protected override void OnStart()
7575

7676
try
7777
{
78-
// Request unbuffered touch dispatch for lower input latency during gameplay.
79-
Window?.DecorView?.RequestUnbufferedDispatch((int)InputSourceType.Touchscreen);
78+
// RequestUnbufferedDispatch(int sourceClass) requires API 31+.
79+
if (OperatingSystem.IsAndroidVersionAtLeast(31))
80+
Window?.DecorView?.RequestUnbufferedDispatch((int)InputSourceType.Touchscreen);
8081
}
8182
catch (Exception e)
8283
{

osu.Android/OsuGameAndroid.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using osu.Framework.Allocation;
1111
using osu.Framework.Bindables;
1212
using osu.Framework.Development;
13-
using osu.Framework.Extensions.ObjectExtensions;
1413
using osu.Framework.Platform;
1514
using osu.Game;
1615
using osu.Game.Configuration;
@@ -27,7 +26,7 @@ public partial class OsuGameAndroid : OsuGame
2726
[Cached]
2827
private readonly OsuGameActivity gameActivity;
2928

30-
private readonly PackageInfo packageInfo;
29+
private readonly PackageInfo? packageInfo;
3130

3231
public override Vector2 ScalingContainerTargetDrawSize => new Vector2(1024, 1024 * DrawHeight / DrawWidth);
3332

@@ -43,7 +42,16 @@ public OsuGameAndroid(OsuGameActivity activity)
4342
: base(null)
4443
{
4544
gameActivity = activity;
46-
packageInfo = Application.Context.ApplicationContext!.PackageManager!.GetPackageInfo(Application.Context.ApplicationContext.PackageName!, 0).AsNonNull();
45+
46+
try
47+
{
48+
packageInfo = Application.Context.ApplicationContext!.PackageManager!.GetPackageInfo(Application.Context.ApplicationContext.PackageName!, 0);
49+
}
50+
catch (Exception e)
51+
{
52+
Debug.WriteLine($"[osu!] Failed to retrieve package info: {e.Message}");
53+
packageInfo = null;
54+
}
4755
}
4856

4957
public override string Version
@@ -53,11 +61,29 @@ public override string Version
5361
if (!IsDeployedBuild)
5462
return @"local " + (DebugUtils.IsDebugBuild ? @"debug" : @"release");
5563

56-
return packageInfo.VersionName.AsNonNull();
64+
return packageInfo?.VersionName ?? @"unknown";
5765
}
5866
}
5967

60-
public override Version AssemblyVersion => new Version(packageInfo.VersionName.AsNonNull().Split('-').First());
68+
public override Version AssemblyVersion
69+
{
70+
get
71+
{
72+
try
73+
{
74+
string? versionName = packageInfo?.VersionName;
75+
76+
if (!string.IsNullOrEmpty(versionName))
77+
return new Version(versionName.Split('-').First());
78+
}
79+
catch (Exception e)
80+
{
81+
Debug.WriteLine($"[osu!] Failed to parse assembly version: {e.Message}");
82+
}
83+
84+
return new Version(@"0.0.0");
85+
}
86+
}
6187

6288
[BackgroundDependencyLoader]
6389
private void load(OsuConfigManager config)
@@ -247,24 +273,31 @@ private void updateOrientation()
247273
{
248274
gameActivity.RunOnUiThread(() =>
249275
{
250-
if (ScreenStack.CurrentScreen is not IOsuScreen currentScreen)
251-
return;
276+
try
277+
{
278+
if (ScreenStack.CurrentScreen is not IOsuScreen currentScreen)
279+
return;
252280

253-
var orientation = MobileUtils.GetOrientation(this, currentScreen, gameActivity.IsTablet);
281+
var orientation = MobileUtils.GetOrientation(this, currentScreen, gameActivity.IsTablet);
254282

255-
switch (orientation)
256-
{
257-
case MobileUtils.Orientation.Locked:
258-
gameActivity.RequestedOrientation = ScreenOrientation.Locked;
259-
break;
283+
switch (orientation)
284+
{
285+
case MobileUtils.Orientation.Locked:
286+
gameActivity.RequestedOrientation = ScreenOrientation.Locked;
287+
break;
260288

261-
case MobileUtils.Orientation.Portrait:
262-
gameActivity.RequestedOrientation = ScreenOrientation.Portrait;
263-
break;
289+
case MobileUtils.Orientation.Portrait:
290+
gameActivity.RequestedOrientation = ScreenOrientation.Portrait;
291+
break;
264292

265-
case MobileUtils.Orientation.Default:
266-
gameActivity.RequestedOrientation = gameActivity.DefaultOrientation;
267-
break;
293+
case MobileUtils.Orientation.Default:
294+
gameActivity.RequestedOrientation = gameActivity.DefaultOrientation;
295+
break;
296+
}
297+
}
298+
catch (Exception e)
299+
{
300+
Debug.WriteLine($"[osu!] Failed to update orientation: {e.Message}");
268301
}
269302
});
270303
}

osu.Game/Configuration/OsuConfigManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protected override void InitialiseDefaults()
238238

239239
SetDefault(OsuSetting.DashboardSortMode, UserSortCriteria.LastVisit);
240240
SetDefault(OsuSetting.DashboardDisplayStyle, OverlayPanelDisplayStyle.Card);
241-
SetDefault(OsuSetting.AndroidPerformanceMode, true);
241+
SetDefault(OsuSetting.AndroidPerformanceMode, false);
242242
SetDefault(OsuSetting.AndroidLowLatencyAudio, false);
243243
SetDefault(OsuSetting.AndroidVulkanProbe, false);
244244
}

0 commit comments

Comments
 (0)