protected void OnLoad(object sender, EventArgs e) { try { flagsChangedHandler = flagsChanged; windowWillUseFullScreenHandler = windowWillUseFullScreen; windowDidEnterFullScreenHandler = windowDidEnterFullScreen; windowDidExitFullScreenHandler = windowDidExitFullScreen; windowShouldZoomToFrameHandler = windowShouldZoomToFrame; const BindingFlags instance_member = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var fieldImplementation = typeof(NativeWindow).GetField("implementation", instance_member); Debug.Assert(fieldImplementation != null, "Reflection is broken!"); var nativeWindow = fieldImplementation.GetValue(Implementation); Debug.Assert(nativeWindow != null, "Reflection is broken!"); var typeCocoaNativeWindow = nativeWindow.GetType(); Debug.Assert(typeCocoaNativeWindow.Name == "CocoaNativeWindow", "Reflection is broken!"); var fieldWindowClass = typeCocoaNativeWindow.GetField("windowClass", instance_member); Debug.Assert(fieldWindowClass != null, "Reflection is broken!"); var windowClassValue = fieldWindowClass.GetValue(nativeWindow); Debug.Assert(windowClassValue != null); var windowClass = (IntPtr)windowClassValue; // register new methods Class.RegisterMethod(windowClass, flagsChangedHandler, "flagsChanged:", "v@:@"); Class.RegisterMethod(windowClass, windowWillUseFullScreenHandler, "window:willUseFullScreenPresentationOptions:", "I@:@I"); Class.RegisterMethod(windowClass, windowDidEnterFullScreenHandler, "windowDidEnterFullScreen:", "v@:@"); Class.RegisterMethod(windowClass, windowDidExitFullScreenHandler, "windowDidExitFullScreen:", "v@:@"); // replace methods that currently break Class.RegisterMethod(windowClass, windowShouldZoomToFrameHandler, "windowShouldZoom:toFrame:", "b@:@{NSRect={NSPoint=ff}{NSSize=ff}}"); NSNotificationCenter.AddObserver(WindowInfo.Handle, Selector.Get("windowDidEnterFullScreen:"), NSNotificationCenter.WINDOW_DID_ENTER_FULL_SCREEN, IntPtr.Zero); NSNotificationCenter.AddObserver(WindowInfo.Handle, Selector.Get("windowDidExitFullScreen:"), NSNotificationCenter.WINDOW_DID_EXIT_FULL_SCREEN, IntPtr.Zero); var methodKeyDown = typeCocoaNativeWindow.GetMethod("OnKeyDown", instance_member); Debug.Assert(methodKeyDown != null, "Reflection is broken!"); actionKeyDown = (Action <osuTK.Input.Key, bool>)methodKeyDown.CreateDelegate(typeof(Action <osuTK.Input.Key, bool>), nativeWindow); var methodKeyUp = typeCocoaNativeWindow.GetMethod("OnKeyUp", instance_member); Debug.Assert(methodKeyUp != null, "Reflection is broken!"); actionKeyUp = (Action <osuTK.Input.Key>)methodKeyUp.CreateDelegate(typeof(Action <osuTK.Input.Key>), nativeWindow); var methodInvalidateCursorRects = typeCocoaNativeWindow.GetMethod("InvalidateCursorRects", instance_member); Debug.Assert(methodInvalidateCursorRects != null, "Reflection is broken!"); actionInvalidateCursorRects = (Action)methodInvalidateCursorRects.CreateDelegate(typeof(Action), nativeWindow); } catch { Logger.Log("Window initialisation couldn't complete, likely due to the SDL backend being enabled.", LoggingTarget.Runtime, LogLevel.Important); Logger.Log("Execution will continue but keyboard functionality may be limited.", LoggingTarget.Runtime, LogLevel.Important); } }
protected void OnLoad(object sender, EventArgs e) { try { flagsChangedHandler = flagsChanged; windowWillUseFullScreenHandler = windowWillUseFullScreen; windowDidEnterFullScreenHandler = windowDidEnterFullScreen; windowDidExitFullScreenHandler = windowDidExitFullScreen; windowShouldZoomToFrameHandler = windowShouldZoomToFrame; var fieldImplementation = typeof(NativeWindow).GetRuntimeFields().Single(x => x.Name == "implementation"); var typeCocoaNativeWindow = typeof(NativeWindow).Assembly.GetTypes().Single(x => x.Name == "CocoaNativeWindow"); var fieldWindowClass = typeCocoaNativeWindow.GetRuntimeFields().Single(x => x.Name == "windowClass"); nativeWindow = fieldImplementation.GetValue(Implementation); var windowClass = (IntPtr)fieldWindowClass.GetValue(nativeWindow); // register new methods Class.RegisterMethod(windowClass, flagsChangedHandler, "flagsChanged:", "v@:@"); Class.RegisterMethod(windowClass, windowWillUseFullScreenHandler, "window:willUseFullScreenPresentationOptions:", "I@:@I"); Class.RegisterMethod(windowClass, windowDidEnterFullScreenHandler, "windowDidEnterFullScreen:", "v@:@"); Class.RegisterMethod(windowClass, windowDidExitFullScreenHandler, "windowDidExitFullScreen:", "v@:@"); // replace methods that currently break Class.RegisterMethod(windowClass, windowShouldZoomToFrameHandler, "windowShouldZoom:toFrame:", "b@:@{NSRect={NSPoint=ff}{NSSize=ff}}"); NSNotificationCenter.AddObserver(WindowInfo.Handle, Selector.Get("windowDidEnterFullScreen:"), NSNotificationCenter.WINDOW_DID_ENTER_FULL_SCREEN, IntPtr.Zero); NSNotificationCenter.AddObserver(WindowInfo.Handle, Selector.Get("windowDidExitFullScreen:"), NSNotificationCenter.WINDOW_DID_EXIT_FULL_SCREEN, IntPtr.Zero); methodKeyDown = nativeWindow.GetType().GetRuntimeMethods().Single(x => x.Name == "OnKeyDown"); methodKeyUp = nativeWindow.GetType().GetRuntimeMethods().Single(x => x.Name == "OnKeyUp"); methodInvalidateCursorRects = nativeWindow.GetType().GetRuntimeMethods().Single(x => x.Name == "InvalidateCursorRects"); } catch { Logger.Log("Window initialisation couldn't complete, likely due to the SDL backend being enabled.", LoggingTarget.Runtime, LogLevel.Important); Logger.Log("Execution will continue but keyboard functionality may be limited.", LoggingTarget.Runtime, LogLevel.Important); } }