Skip to content

Commit

Permalink
add apple AppLovin semaphore AppLovinSdkInitialized
Browse files Browse the repository at this point in the history
improve SDL dispatchMainThreadEvent
  • Loading branch information
irov committed Jan 7, 2024
1 parent c08224e commit 16eff96
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/Environment/iOS/UIPluginApplicationDelegateInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

@optional
- (BOOL)beginLoop;
- (void)endLoop;

- (void)event:(NSString *)name args:(NSArray *)args;

Expand Down
11 changes: 11 additions & 0 deletions src/Platforms/SDLPlatform/SDLPlatformService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Interface/EnumeratorServiceInterface.h"
#include "Interface/PluginServiceInterface.h"
#include "Interface/DateTimeSystemInterface.h"
#include "Interface/ThreadServiceInterface.h"

#if defined(MENGINE_PLATFORM_ANDROID)
# include "Interface/AndroidEnvironmentServiceInterface.h"
Expand Down Expand Up @@ -1423,6 +1424,16 @@ namespace Mengine
//////////////////////////////////////////////////////////////////////////
void SDLPlatformService::dispatchMainThreadEvent( const LambdaEvent & _event )
{
bool isMainThread = THREAD_SERVICE()
->isMainThread();

if( isMainThread == true )
{
_event();

return;
}

m_dispatchEventMutex->lock();

m_dispatchEvents.emplace_back( _event );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "Environment/Apple/AppleDetail.h"

#include "Kernel/PlatformHelper.h"
#include "Kernel/SemaphoreHelper.h"
#include "Kernel/ConstStringHelper.h"

@implementation AppleAppLovinApplicationDelegate

static ALSdk * AppLovinSdk;
Expand Down Expand Up @@ -124,10 +128,14 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
BOOL testModeEnabled = configuration.testModeEnabled;

NSLog(@"AppLovin plugin initialize complete");
NSLog(@"consent flow user geography: %ld", consentFlowUserGeography);
NSLog(@"country code: %@", countryCode);
NSLog(@"app tracking transparency status: %ld", appTrackingTransparencyStatus);
NSLog(@"test mode enabled: %d", testModeEnabled);
NSLog(@"[AppLovin] consent flow user geography: %ld", consentFlowUserGeography);
NSLog(@"[AppLovin] country code: %@", countryCode);
NSLog(@"[AppLovin] app tracking transparency status: %ld", appTrackingTransparencyStatus);
NSLog(@"[AppLovin] test mode enabled: %d", testModeEnabled);

Mengine::Helper::dispatchMainThreadEvent([]() {
Mengine::Helper::activateSemaphore(STRINGIZE_STRING_LOCAL("AppLovinSdkInitialized"));
});
}];

AppleAppLovinApplicationDelegate.AppLovinSdk = appLovinSdk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ - (instancetype _Nullable) initWithAdUnitIdentifier:(NSString * _Nonnull) adUnit
MARewardedAd * rewardedAd;

@try {
rewardedAd = [MARewardedAd sharedWithAdUnitIdentifier: adUnitId sdk:AppleAppLovinApplicationDelegate.AppLovinSdk];
rewardedAd = [MARewardedAd sharedWithAdUnitIdentifier:adUnitId sdk:AppleAppLovinApplicationDelegate.AppLovinSdk];
} @catch (NSException * ex) {
LOGGER_ERROR( "[Error] AppleAppLovinRewardedDelegate invalid create MARewardedAd adUnitId: %s exception: %s [%s]"
, [adUnitId UTF8String]
Expand Down
51 changes: 37 additions & 14 deletions src/Plugins/AppleGameCenterPlugin/AppleGameCenterService.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Environment/Apple/AppleErrorHelper.h"

#include "Kernel/Logger.h"
#include "Kernel/PlatformHelper.h"

#include "Config/Algorithm.h"

Expand Down Expand Up @@ -50,6 +51,8 @@
bool AppleGameCenterService::connect()
{
[m_gameCenterDelegate login:^(NSError * _Nullable _error) {
AppleGameCenterProviderInterfacePtr provider = m_provider;

if( _error != nil )
{
m_gameCenterAuthenticate = false;
Expand All @@ -60,9 +63,11 @@
, Helper::AppleGetMessageFromNSError(_error).c_str()
);

if( m_provider != nullptr )
if( provider != nullptr )
{
m_provider->onAppleGameCenterAuthenticate( false );
Mengine::Helper::dispatchMainThreadEvent([provider]() {
provider->onAppleGameCenterAuthenticate( false );
});
}

return;
Expand All @@ -74,21 +79,27 @@
{
m_gameCenterAuthenticate = true;

if( m_provider != nullptr )
if( provider != nullptr )
{
m_provider->onAppleGameCenterAuthenticate( true );
Mengine::Helper::dispatchMainThreadEvent([provider]() {
provider->onAppleGameCenterAuthenticate( true );
});
}
}

m_achievementsSynchronization = false;
m_achievementsComplete.clear();

if( m_provider != nullptr )
if( provider != nullptr )
{
m_provider->onAppleGameCenterSynchronizate( false );
Mengine::Helper::dispatchMainThreadEvent([provider]() {
provider->onAppleGameCenterSynchronizate( false );
});
}

[m_gameCenterDelegate loadCompletedAchievements:^(NSError * _Nullable _error, NSArray * _Nullable _completedAchievements) {
AppleGameCenterProviderInterfacePtr provider = m_provider;

if( _error != nil )
{
m_achievementsSynchronization = false;
Expand All @@ -97,9 +108,11 @@
, Helper::AppleGetMessageFromNSError(_error).c_str()
);

if( m_provider != nullptr )
if( provider != nullptr )
{
m_provider->onAppleGameCenterSynchronizate( false );
Mengine::Helper::dispatchMainThreadEvent([provider]() {
provider->onAppleGameCenterSynchronizate( false );
});
}

return;
Expand All @@ -121,9 +134,11 @@

m_achievementsSynchronization = true;

if( m_provider != nullptr )
if( provider != nullptr )
{
m_provider->onAppleGameCenterSynchronizate( true );
Mengine::Helper::dispatchMainThreadEvent([provider]() {
provider->onAppleGameCenterSynchronizate( true );
});
}
}] ;
}];
Expand Down Expand Up @@ -162,7 +177,9 @@
, Helper::AppleGetMessageFromNSError(_error).c_str()
);

copy_response( false );
Mengine::Helper::dispatchMainThreadEvent([copy_response]() {
copy_response( false );
});

return;
}
Expand All @@ -177,7 +194,9 @@
m_achievementsComplete.push_back( copy_achievementName );
}

copy_response( true );
Mengine::Helper::dispatchMainThreadEvent([copy_response]() {
copy_response( true );
});
}];

if( result == FALSE )
Expand Down Expand Up @@ -251,7 +270,9 @@
, Helper::AppleGetMessageFromNSError(_error).c_str()
);

copy_response( false );
Mengine::Helper::dispatchMainThreadEvent([copy_response]() {
copy_response( false );
});

return;
}
Expand All @@ -261,7 +282,9 @@
, _score
);

copy_response( true );
Mengine::Helper::dispatchMainThreadEvent([copy_response]() {
copy_response( true );
});
}];

if( result == FALSE )
Expand Down
82 changes: 60 additions & 22 deletions src/SDLApplication/SDLUIApplicationDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

for (id delegate in self.m_pluginDelegates) {
if ([delegate application:application didFinishLaunchingWithOptions:launchOptions] == NO) {
NSLog(@"Mengine application initialize plugin %@ failed", NSStringFromClass([delegate class]));
NSLog(@"Mengine application didFinishLaunchingWithOptions plugin %@ failed", NSStringFromClass([delegate class]));

return NO;
}
Expand All @@ -90,6 +90,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
if( initialize == false ) {
NSLog(@"Mengine application initialize [Failed]");

NSLog(@"Mengine application begin finalize");

MENGINE_application.finalize();

NSLog(@"Mengine application end finalize");

SDL_iPhoneSetEventPump( SDL_FALSE );

return NO;
Expand All @@ -106,81 +112,97 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio
for (id delegate in self.m_pluginDelegates) {
NSLog(@"Mengine application didRegisterForRemoteNotificationsWithDeviceToken");

if ([delegate respondsToSelector:@selector(application: didRegisterForRemoteNotificationsWithDeviceToken:)] == YES) {
[delegate application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
if ([delegate respondsToSelector:@selector(application: didRegisterForRemoteNotificationsWithDeviceToken:)] == NO) {
continue;
}

[delegate application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler API_AVAILABLE(ios(7.0)) {
NSLog(@"Mengine application didReceiveRemoteNotification");

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(application: didReceiveRemoteNotification: fetchCompletionHandler:)] == YES) {
[delegate application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
if ([delegate respondsToSelector:@selector(application: didReceiveRemoteNotification: fetchCompletionHandler:)] == NO) {
continue;
}

[delegate application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"Mengine application applicationDidBecomeActive");

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(applicationDidBecomeActive:)] == YES) {
[delegate applicationDidBecomeActive:application];
if ([delegate respondsToSelector:@selector(applicationDidBecomeActive:)] == NO) {
continue;
}

[delegate applicationDidBecomeActive:application];
}
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"Mengine application applicationWillEnterForeground");

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(applicationWillEnterForeground:)] == YES) {
[delegate applicationWillEnterForeground:application];
if ([delegate respondsToSelector:@selector(applicationWillEnterForeground:)] == NO) {
continue;
}

[delegate applicationWillEnterForeground:application];
}
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Mengine application applicationDidEnterBackground");

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(applicationDidEnterBackground:)] == YES) {
[delegate applicationDidEnterBackground:application];
if ([delegate respondsToSelector:@selector(applicationDidEnterBackground:)] == NO) {
continue;
}

[delegate applicationDidEnterBackground:application];
}
}

- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"Mengine application applicationWillResignActive");

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(applicationWillResignActive:)] == YES) {
[delegate applicationWillResignActive:application];
if ([delegate respondsToSelector:@selector(applicationWillResignActive:)] == NO) {
continue;
}

[delegate applicationWillResignActive:application];
}
}

- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"Mengine application applicationWillTerminate");

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(applicationWillTerminate:)] == YES) {
[delegate applicationWillTerminate:application];
if ([delegate respondsToSelector:@selector(applicationWillTerminate:)] == NO) {
continue;
}

[delegate applicationWillTerminate:application];
}
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options API_AVAILABLE(ios(9.0)) {
for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(application: openURL: options: handled:)] == YES) {
BOOL handler = NO;
BOOL result = [delegate application:application openURL:url options:options handled:&handler];

if (handler == YES) {
return result;
}
if ([delegate respondsToSelector:@selector(application: openURL: options: handled:)] == NO) {
continue;
}

BOOL handler = NO;
BOOL result = [delegate application:application openURL:url options:options handled:&handler];

if (handler == YES) {
return result;
}
}

Expand Down Expand Up @@ -209,6 +231,14 @@ - (void)setWindow:(UIWindow *)window {
}

- (void)postFinishLaunch {
for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(beginLoop:)] == NO) {
continue;
}

[delegate beginLoop];
}

NSLog(@"Mengine application start loop");

MENGINE_application.loop();
Expand All @@ -222,6 +252,14 @@ - (void)postFinishLaunch {
NSLog(@"Mengine application end finalize");

SDL_iPhoneSetEventPump( SDL_FALSE );

for (id delegate in self.m_pluginDelegates) {
if ([delegate respondsToSelector:@selector(endLoop:)] == NO) {
continue;
}

[delegate endLoop];
}
}

@end

0 comments on commit 16eff96

Please sign in to comment.