| 37 | } |
| 38 | |
| 39 | static bool LoadAndroid() |
| 40 | { |
| 41 | // Due to a long-standing bug, Mono can only load libraries from |
| 42 | // the main or UI threads, so we need to grab one of those threads |
| 43 | // and ensure the loading happens there. |
| 44 | // For reference: https://github.com/xamarin/xamarin-android/issues/7616 |
| 45 | |
| 46 | // Grab Java.Lang.JavaSystem.LoadLibrary(string) via reflection. |
| 47 | Type javaSystem = Type.GetType("Java.Lang.JavaSystem, Mono.Android"); |
| 48 | if (javaSystem == null) return false; |
| 49 | MethodInfo loadLibrary = javaSystem.GetMethod("LoadLibrary", BindingFlags.Static | BindingFlags.Public); |
| 50 | if (loadLibrary == null) return false; |
| 51 | |
| 52 | try { |
| 53 | // If we're on the right thread, this will just work. But |
| 54 | // there's a good chance we're not, and that's okay. |
| 55 | loadLibrary.Invoke(null, new object[] { "StereoKitC" }); |
| 56 | return true; |
| 57 | } catch { |
| 58 | // We can't invoke on the main thread without the Activity! |
| 59 | |
| 60 | Type activity = Type.GetType("Android.App.Activity, Mono.Android"); |
| 61 | if (activity == null) return false; |
| 62 | |
| 63 | if (SK.AndroidActivity == null) |
| 64 | { |
| 65 | // If we have Xamarin.Essentials or Maui.Essentials, we can |
| 66 | // grab the Activity from their Platform.CurrentActivity |
| 67 | // property. |
| 68 | Type platform = null; |
| 69 | if (IsAssemblyLoaded("Xamarin.Essentials" )) platform = Type.GetType("Xamarin.Essentials.Platform, Xamarin.Essentials"); |
| 70 | else if (IsAssemblyLoaded("Microsoft.Maui.Essentials")) platform = Type.GetType("Microsoft.Maui.ApplicationModel.Platform, Microsoft.Maui.Essentials"); |
| 71 | PropertyInfo currentActivity = platform?.GetProperty("CurrentActivity", BindingFlags.Static | BindingFlags.Public); |
| 72 | if (currentActivity != null) |
| 73 | SK.AndroidActivity = currentActivity.GetValue(null); |
| 74 | } |
| 75 | if (SK.AndroidActivity == null) |
| 76 | { |
| 77 | Debug .WriteLine("[SK error] SK.AndroidActivity must be set before this point!"); |
| 78 | Console.WriteLine("[SK error] SK.AndroidActivity must be set before this point!"); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // We're likely not on the main/UI thread, so lets get a way to |
| 83 | // insert our code onto the main/UI thread. |
| 84 | MethodInfo runOnUiThread = activity.GetMethod("RunOnUiThread", new Type[] { typeof(Action) }); |
| 85 | if (runOnUiThread == null) { Debug.WriteLine("[SK error] Could not find Android.App.Activity.RunOnUiThread"); return false; } |
| 86 | |
| 87 | // Ask the UI thread to load the libraries, then sleep this |
| 88 | // thread until it has succeeded or failed. |
| 89 | int loaded = 0; |
| 90 | Action loadLibs = () => { |
| 91 | try { loadLibrary.Invoke(null, new object[] { "StereoKitC" }); loaded = 1; } |
| 92 | catch (Exception e) { loaded = -1; Debug.WriteLine("[SK error] Exception loading StereoKitC:\n"+e.ToString()); } |
| 93 | }; |
| 94 | try { runOnUiThread.Invoke(SK.AndroidActivity, new object[] { loadLibs }); } |
| 95 | catch { loaded = -1; } |
| 96 | |