Skip to content

Commit

Permalink
Fix problems with view controllers on visionOS and elsewhere. (apple#34)
Browse files Browse the repository at this point in the history
* Simplify view controller presentation.
* Fix visionOS availability annotations.
* Use default access point location and don't automatically activate it.
* Fix tvOS compile error.
* Update GameKit changelog.
  • Loading branch information
AdamSzApple authored and GitHub Enterprise committed Apr 1, 2024
1 parent c4e145e commit 3caa4bd
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.3.18f1
m_EditorVersionWithRevision: 2022.3.18f1 (d29bea25151d)
m_EditorVersion: 2022.3.22f1
m_EditorVersionWithRevision: 2022.3.22f1 (887be4894c44)
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# CHANGELOG
All notable changes to this project will be documented in this file.

## [2.2.1] - 2023-04-01
- Fix some issues with how view controllers are handled on visionOS.

## [2.2.0] - 2023-02-23
### Added
- Support for visionOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ public class AccessPointPanel : MonoBehaviour
{
[SerializeField] Dropdown _stateDropdown = default;

// Start is called before the first frame update
void Start()
{
GKAccessPoint.Shared.Location = GKAccessPoint.GKAccessPointLocation.TopLeading;
GKAccessPoint.Shared.ShowHighlights = false;
GKAccessPoint.Shared.IsActive = true;
}

public void OnToggleAccessPoint()
{
GKAccessPoint.Shared.IsActive = !GKAccessPoint.Shared.IsActive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ protected override void OnDispose(bool isDisposing)
/// <summary>
/// The local player's properties that matchmaking rules used to find the players with some additions.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public GKMatchProperties Properties => PointerCast<GKMatchProperties>(Interop.GKMatch_GetProperties(Pointer));

/// <summary>
/// The properties for other players that matchmaking rules uses to find players, with some additions.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public NSDictionary<GKPlayer, GKMatchProperties> PlayerProperties => PointerCast<NSDictionary<GKPlayer, GKMatchProperties>>(Interop.GKMatch_GetPlayerProperties(Pointer));

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public NSArray<GKPlayer> Recipients
/// <summary>
/// The name of the queue that Game Center places the match request in.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public string QueueName
{
get => Interop.GKMatchRequest_GetQueueName(Pointer);
Expand All @@ -144,7 +144,7 @@ public string QueueName
/// <summary>
/// The criteria for the local player that Game Center uses to find other players when using matchmaking rules.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public GKMatchProperties Properties
{
get => PointerCast<GKMatchProperties>(Interop.GKMatchRequest_GetProperties(Pointer));
Expand All @@ -154,7 +154,7 @@ public GKMatchProperties Properties
/// <summary>
/// The criteria for recipients of the match request that Game Center uses to find other players when using matchmaking rules.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public NSDictionary<GKPlayer, GKMatchProperties> RecipientProperties
{
get => PointerCast<NSDictionary<GKPlayer, GKMatchProperties>>(Interop.GKMatchRequest_GetRecipientProperties(Pointer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Apple.GameKit.Multiplayer
/// <summary>
/// An object that represents matchmaking results, including the players that join the match and their properties that matchmaking rules uses.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public class GKMatchedPlayers : NSObject
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static void OnFindPlayersError(long taskId, IntPtr errorPointer)
/// </summary>
/// <param name="matchRequest">The configuration for the match.</param>
/// <returns>The players that join the match, including their properties that matchmaking rules uses. If unsuccessful, this parameter is null.</returns>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public Task<GKMatchedPlayers> FindMatchedPlayers(GKMatchRequest matchRequest)
{
var tcs = InteropTasks.Create<GKMatchedPlayers>(out var taskId);
Expand Down Expand Up @@ -216,7 +216,7 @@ private static void OnQueryActivityError(long taskId, IntPtr errorPointer)
/// The string should be in reverse-DNS format and queue names are case sensitive.
/// </param>
/// <returns>The number of match requests in the queue during the previous 60 seconds.</returns>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public Task<long> QueryQueueActivity(string queueName)
{
var tcs = InteropTasks.Create<long>(out var taskId);
Expand Down Expand Up @@ -387,8 +387,10 @@ private static class Interop
public static extern void GKMatchmaker_StartBrowsingForNearbyPlayers(IntPtr gkMatchmakerPtr, InternalNearbyPlayerReachableHandler nearbyPlayerReachableHandler);
[DllImport(InteropUtility.DLLName)]
public static extern void GKMatchmaker_StopBrowsingForNearbyPlayers(IntPtr gkMatchmakerPtr);
#if !UNITY_TVOS
[DllImport(InteropUtility.DLLName)]
public static extern void GKMatchmaker_StartGroupActivity(IntPtr gkMatchmakerPtr, InternalPlayerJoiningGroupActivityHandler playerJoiningGroupActivityHandler);
#endif
[DllImport(InteropUtility.DLLName)]
public static extern void GKMatchmaker_StopGroupActivity(IntPtr gkMatchmakerPtr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ private static void OnHostedPlayerDidAccept(IntPtr pointer, IntPtr matchmakerVie
/// <param name="matchmakerViewController">The view controller that finds players for the match.</param>
/// <param name="invitedPlayer">A player to invite to the match.</param>
/// <returns>The properties for recipient that the local player invites to the match.</returns>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public delegate Task<GKMatchProperties> GetMatchPropertiesForRecipientHandler(GKMatchmakerViewController matchmakerViewController, GKPlayer invitedPlayer);
private delegate void InteropGetMatchPropertiesForRecipientHandler(IntPtr gkMatchmakerViewControllerDelegatePtr, IntPtr gkMatchmakerViewControllerPtr, IntPtr gkPlayerPtr, IntPtr completionHandlerPtr);

/// <summary>
/// Dispatches GetMatchPropertiesForRecipient events.
/// </summary>
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2")]
[Introduced(iOS: "17.2", macOS: "14.2", tvOS: "17.2", visionOS: "1.1")]
public event GetMatchPropertiesForRecipientHandler GetMatchPropertiesForRecipient;

[MonoPInvokeCallback(typeof(InteropGetMatchPropertiesForRecipientHandler))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.3.18f1
m_EditorVersionWithRevision: 2022.3.18f1 (d29bea25151d)
m_EditorVersion: 2022.3.22f1
m_EditorVersionWithRevision: 2022.3.22f1 (887be4894c44)
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@
FB8694492AD73AE5009B8676 /* GKMatchedPlayers.m in Sources */ = {isa = PBXBuildFile; fileRef = FB8694482AD73AE5009B8676 /* GKMatchedPlayers.m */; };
FB86944A2AD73AE5009B8676 /* GKMatchedPlayers.m in Sources */ = {isa = PBXBuildFile; fileRef = FB8694482AD73AE5009B8676 /* GKMatchedPlayers.m */; };
FB86944B2AD73AE5009B8676 /* GKMatchedPlayers.m in Sources */ = {isa = PBXBuildFile; fileRef = FB8694482AD73AE5009B8676 /* GKMatchedPlayers.m */; };
FB89C4EB2BAE49320078B971 /* UiUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB89C4EA2BAE49320078B971 /* UiUtilities.swift */; };
FB89C4EC2BAE4BA20078B971 /* UiUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB89C4EA2BAE49320078B971 /* UiUtilities.swift */; };
FB89C4ED2BAE4BA30078B971 /* UiUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB89C4EA2BAE49320078B971 /* UiUtilities.swift */; };
FB89C4EE2BAE4BA40078B971 /* UiUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB89C4EA2BAE49320078B971 /* UiUtilities.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -213,6 +217,7 @@
7AE5BBA22502C0DD00BDBEDA /* GameKitWrapper.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GameKitWrapper.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FB7384C92ADE05A200616362 /* GKErrorCodeExtension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GKErrorCodeExtension.h; sourceTree = "<group>"; };
FB8694482AD73AE5009B8676 /* GKMatchedPlayers.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKMatchedPlayers.m; sourceTree = "<group>"; };
FB89C4EA2BAE49320078B971 /* UiUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UiUtilities.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -271,6 +276,7 @@
7A9B815524A4FC3800FB2FE8 /* GameKitWrapper */ = {
isa = PBXGroup;
children = (
FB89C4EA2BAE49320078B971 /* UiUtilities.swift */,
7A7D6B7F25D6E7650072DB37 /* AccessPoint_BridgingHeader.h */,
7A4ADFE924DDF23400664BD3 /* AccessPoint.swift */,
7A486BA926A09D7A00255BD5 /* AppleCoreRuntimeShared.h */,
Expand Down Expand Up @@ -552,6 +558,7 @@
24B0647A2B7F29E1007ABBB8 /* GKAchievement.swift in Sources */,
24B0647B2B7F29E1007ABBB8 /* GKVoiceChat.swift in Sources */,
24B0647C2B7F29E1007ABBB8 /* GKTurnBasedMatchDelegate.swift in Sources */,
FB89C4ED2BAE4BA30078B971 /* UiUtilities.swift in Sources */,
24B0647D2B7F29E1007ABBB8 /* GKTurnBasedMatch.swift in Sources */,
24B0647E2B7F29E1007ABBB8 /* GKMatchmakerViewController.swift in Sources */,
24B0647F2B7F29E1007ABBB8 /* GameKitUIDelegateHandler.swift in Sources */,
Expand Down Expand Up @@ -594,6 +601,7 @@
7AA8AD5E2729EF5500D3F909 /* GKAchievement.swift in Sources */,
7A7E43A124B509130086FB7A /* GameKitUIDelegateHandler.swift in Sources */,
7AA8AD9F272A46AF00D3F909 /* GKVoiceChat.swift in Sources */,
FB89C4EE2BAE4BA40078B971 /* UiUtilities.swift in Sources */,
7AA8ADB3272B02AF00D3F909 /* GKTurnBasedMatchDelegate.swift in Sources */,
7AA8ADAE272AF7A000D3F909 /* GKTurnBasedMatch.swift in Sources */,
7AA8AD90272A3EE600D3F909 /* GKMatchmakerViewController.swift in Sources */,
Expand Down Expand Up @@ -636,6 +644,7 @@
7AA8AD5C2729EF5500D3F909 /* GKAchievement.swift in Sources */,
7AA8AD9D272A46AF00D3F909 /* GKVoiceChat.swift in Sources */,
7AA8ADB1272B02AF00D3F909 /* GKTurnBasedMatchDelegate.swift in Sources */,
FB89C4EB2BAE49320078B971 /* UiUtilities.swift in Sources */,
7AA8ADAC272AF7A000D3F909 /* GKTurnBasedMatch.swift in Sources */,
7AA8AD8E272A3EE600D3F909 /* GKMatchmakerViewController.swift in Sources */,
7A50579324ABD01000477507 /* GameKitUIDelegateHandler.swift in Sources */,
Expand Down Expand Up @@ -678,6 +687,7 @@
7AA8AD5D2729EF5500D3F909 /* GKAchievement.swift in Sources */,
7AA8AD9E272A46AF00D3F909 /* GKVoiceChat.swift in Sources */,
7AA8ADB2272B02AF00D3F909 /* GKTurnBasedMatchDelegate.swift in Sources */,
FB89C4EC2BAE4BA20078B971 /* UiUtilities.swift in Sources */,
7AA8ADAD272AF7A000D3F909 /* GKTurnBasedMatch.swift in Sources */,
7AA8AD8F272A3EE600D3F909 /* GKMatchmakerViewController.swift in Sources */,
7AE5BB9C2502C0DD00BDBEDA /* GameKitUIDelegateHandler.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public func GKAccessPoint_SetIsActive
)
{
let target = Unmanaged<GKAccessPoint>.fromOpaque(pointer).takeUnretainedValue();

if value {
target.parentWindow = UiUtilities.defaultWindow()
}

target.isActive = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,8 @@ public func GKGameCenterViewController_Present
let target = Unmanaged<GKGameCenterViewController>.fromOpaque(pointer).takeUnretainedValue();
_currentPresentingGameCenterDelegate = GameKitUIDelegateHandler(taskId: taskId, onSuccess: onSuccess);
target.gameCenterDelegate = _currentPresentingGameCenterDelegate;

// TODO: (123075676)
#if os(iOS) || os(tvOS)
let viewController = UIApplication.shared.windows.first!.rootViewController;
viewController?.present(target, animated: true);
#elseif os(macOS)
GKDialogController.shared().parentWindow = NSApplication.shared.keyWindow;
GKDialogController.shared().present(target);
#endif

if let defaultWindow = UiUtilities.defaultWindow() {
UiUtilities.presentViewController(viewController: target)
}
}
30 changes: 11 additions & 19 deletions plug-ins/Apple.GameKit/Native/GameKitWrapper/GKLocalPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,20 @@ public func GKLocalPlayer_Authenticate
onSuccess(taskId, GKLocalPlayer_GetLocal());
return;
}

GKLocalPlayer.local.authenticateHandler = { gcAuthVC, error in
if (error != nil) {
onError(taskId, Unmanaged.passRetained(error! as NSError).toOpaque());
return;
}
// TODO: (123075676)
#if !os(visionOS)
// Always show the viewController if provided...
if gcAuthVC != nil {
#if os(iOS) || os(tvOS)
let viewController = UIApplication.shared.windows.first!.rootViewController;
viewController?.present(gcAuthVC!, animated: true)
#else
let viewController = NSApplication.shared.keyWindow?.contentViewController;
viewController?.presentAsModalWindow(gcAuthVC!)
#endif
} else {
GKLocalPlayer.local.register(_localPlayerListener);
onSuccess(taskId, GKLocalPlayer_GetLocal());
if let gcAuthVC = gcAuthVC {
UiUtilities.presentViewController(viewController: gcAuthVC);
}

if let error = error {
onError(taskId, Unmanaged.passRetained(error as NSError).toOpaque());
return;
}
#endif

GKLocalPlayer.local.register(_localPlayerListener);
onSuccess(taskId, GKLocalPlayer_GetLocal());
};
}

Expand Down
2 changes: 1 addition & 1 deletion plug-ins/Apple.GameKit/Native/GameKitWrapper/GKMatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public func GKMatch_GetPlayerProperties
gkMatchPtr: UnsafeMutableRawPointer
) -> UnsafeMutableRawPointer?
{
if #available(iOS 17.2, tvOS 17.2, macOS 14.2, visionOS 1.0, *) {
if #available(iOS 17.2, tvOS 17.2, macOS 14.2, visionOS 1.1, *) {
let gkMatch = Unmanaged<GKMatch>.fromOpaque(gkMatchPtr).takeUnretainedValue()
if let dictionary = gkMatch.playerProperties as NSDictionary? {
return Unmanaged.passRetained(dictionary).toOpaque();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ public func GKMatchRequest_SetProperties
nsDictionaryPtr: UnsafeMutableRawPointer?
)
{
// TODO: (123075676)
#if !os(visionOS)
if #available(iOS 17.2, tvOS 17.2, macOS 14.2, *) {
if #available(iOS 17.2, tvOS 17.2, macOS 14.2, visionOS 1.1, *) {
let gkMatchRequest = Unmanaged<GKMatchRequest>.fromOpaque(gkMatchRequestPtr).takeUnretainedValue();
if let nonNullDictionaryPtr = nsDictionaryPtr {
let nsDictionary = Unmanaged<NSDictionary>.fromOpaque(nonNullDictionaryPtr).takeUnretainedValue();
Expand All @@ -206,7 +204,6 @@ public func GKMatchRequest_SetProperties
gkMatchRequest.properties = nil;
}
}
#endif
}

@_cdecl("GKMatchRequest_GetRecipientProperties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public func GKMatchMaker_FindMatchedPlayers
onError: @escaping NSErrorCallback
)
{
if #available(iOS 17.2, tvOS 17.2, macOS 14.2, visionOS 1.1, *) {
if #available(iOS 17.2, tvOS 17.2, macOS 14.2, visionOS 1.1, *) {
let gkMatchMaker = Unmanaged<GKMatchmaker>.fromOpaque(gkMatchmakerPtr).takeUnretainedValue()
let gkMatchRequest = Unmanaged<GKMatchRequest>.fromOpaque(gkMatchRequestPtr).takeUnretainedValue()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,15 @@ public func GKMatchmakerViewController_Present
)
{
let target = Unmanaged<GKMatchmakerViewController>.fromOpaque(pointer).takeUnretainedValue();

// TODO: (123075676)
#if os(iOS) || os(tvOS)
let viewController = UIApplication.shared.windows.first!.rootViewController;
viewController?.present(target, animated: true);
#elseif os(macOS)
GKDialogController.shared().parentWindow = NSApplication.shared.keyWindow;
GKDialogController.shared().present(target);
#endif
UiUtilities.presentViewController(viewController: target)
}

public func GKMatchmakerViewController_Dismiss
(
viewController: GKMatchmakerViewController
)
{
// TODO: (123075676)
#if os(iOS) || os(tvOS)
viewController.dismiss(animated: true);
#elseif os(macOS)
GKDialogController.shared().dismiss(viewController);
#endif

UiUtilities.dismissViewController(viewController: viewController)
_activeRealtimeMatchmakerDelegate = nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,7 @@ public func GKTurnBasedMatchmakerViewController_Present
{
let target = Unmanaged<GKTurnBasedMatchmakerViewController>.fromOpaque(pointer).takeUnretainedValue();
_presentingTurnBasedMatchmakerViewController = target;

// TODO: (123075676)
#if os(iOS) || os(tvOS)
let viewController = UIApplication.shared.windows.first!.rootViewController;
viewController?.present(target, animated: true);
#elseif os(macOS)
GKDialogController.shared().parentWindow = NSApplication.shared.keyWindow;
GKDialogController.shared().present(target);
#endif
UiUtilities.presentViewController(viewController: target)
}

@_cdecl("GKTurnBasedMatchmakerViewController_Dismiss")
Expand All @@ -116,13 +108,7 @@ public func GKTurnBasedMatchmakerViewController_Dismiss
viewController: GKTurnBasedMatchmakerViewController
)
{
// TODO: (123075676)
#if os(iOS) || os(tvOS)
viewController.dismiss(animated: true);
#elseif os(macOS)
GKDialogController.shared().dismiss(viewController);
#endif

UiUtilities.dismissViewController(viewController: viewController)
_presentingTurnBasedMatchmakerViewController = nil;
_activeTurnBasedMatchmakerDelegate = nil;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public class GameKitUIDelegateHandler : NSObject {

extension GameKitUIDelegateHandler : GKGameCenterControllerDelegate {
public func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
// TODO: (123075676)
#if os(iOS) || os(tvOS)
gameCenterViewController.dismiss(animated: true, completion: {
self._onSuccess?(self._taskId!);
});
#elseif os(macOS)
GKDialogController.shared().dismiss(gameCenterViewController);
#if os(macOS)
GKDialogController.shared().dismiss(gameCenterViewController);
self._onSuccess?(self._taskId!);
#else
gameCenterViewController.dismiss(animated: true, completion: {
self._onSuccess?(self._taskId!);
#endif
});
#endif
}
}
Loading

0 comments on commit 3caa4bd

Please sign in to comment.