223 lines
8.7 KiB
Plaintext
Raw Normal View History

2025-07-03 12:53:49 +04:00
/****************************************************************************
Copyright (c) 2010-2013 cocos2d-x.backgroundDetailsCircularNameAttachmentArtifactPosition
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#import "AppDelegate.h"
#import "ViewController.h"
#import "View.h"
#include "platform/ios/IOSPlatform.h"
#import "platform/ios/AppDelegateBridge.h"
#import "service/SDKWrapper.h"
@implementation AppDelegate
@synthesize window;
@synthesize appDelegateBridge;
// keep arg for unity init from non main
UIApplication* app = nullptr;
//static AppController* appController = nil;
static int gArgc = 0;
static char** gArgv = nullptr;
NSDictionary* appLaunchOpts;
UIApplication* cachedApp;
#pragma mark -
#pragma mark Application lifecycle
UnityFramework* UnityFrameworkLoad()
{
NSString* bundlePath = nil;
bundlePath = [[NSBundle mainBundle] bundlePath];
bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/UnityFramework.framework"];
NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
if ([bundle isLoaded] == false) [bundle load];
UnityFramework* ufw = [bundle.principalClass getInstance];
if (![ufw appController])
{
// unity is not initialized
[ufw setExecuteHeader: &_mh_execute_header];
}
return ufw;
}
- (void)EnhanceDevelopArrayBindingBlock:(NSString *)flag :(Boolean)value {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:value forKey:flag];
[defaults synchronize];
}
- (void)setUrl:(NSString *)value {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:value forKey:@"defaultval"];
[defaults synchronize];
}
- (BOOL)getFlag:(NSString *)flag {
try {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:flag];
} catch (NSException *ex) {
}
return false;
}
- (void)FinishEnhanceObjectBundleDeserializeLoadAuthorizationApplet {
if(![self ufw]){
[[self ufw] quitApplication:0];
}
[self startCocos:cachedApp];
}
+(void)setArgs:(int)argc :(char**)argv{
gArgc = argc;
gArgv = argv;
}
- (void)startUnity:(UIApplication *)application{
[self setUfw:UnityFrameworkLoad()];
[[self ufw] setDataBundleId: "com.unity3d.framework"];
[NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
[[self ufw] runEmbeddedWithArgc: gArgc argv: gArgv appLaunchOpts: appLaunchOpts];
// set quit handler to change default behavior of exit app
[[self ufw] appController].quitHandler = ^(){ NSLog(@"AppController.quitHandler called"); };
}
- (void)startCocos:(UIApplication *)application{
[[SDKWrapper shared] application:application didFinishLaunchingWithOptions:appLaunchOpts];
appDelegateBridge = [[AppDelegateBridge alloc] init];
// Add the view controller's view to the window and display.
CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
// Should create view controller first, cc::Application will use it.
_viewController = [[ViewController alloc] init];
_viewController.view = [[View alloc] initWithFrame:bounds];
_viewController.view.contentScaleFactor = UIScreen.mainScreen.scale;
_viewController.view.multipleTouchEnabled = true;
[self.window setRootViewController:_viewController];
[self.window makeKeyAndVisible];
[appDelegateBridge application:application didFinishLaunchingWithOptions:appLaunchOpts];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [[SDKWrapper shared] application:application didFinishLaunchingWithOptions:launchOptions];
// appDelegateBridge = [[AppDelegateBridge alloc] init];
// // Add the view controller's view to the window and display.
// CGRect bounds = [[UIScreen mainScreen] bounds];
// self.window = [[UIWindow alloc] initWithFrame:bounds];
// // Should create view controller first, cc::Application will use it.
// _viewController = [[ViewController alloc] init];
// _viewController.view = [[View alloc] initWithFrame:bounds];
// _viewController.view.contentScaleFactor = UIScreen.mainScreen.scale;
// _viewController.view.multipleTouchEnabled = true;
// [self.window setRootViewController:_viewController];
// [self.window makeKeyAndVisible];
// [appDelegateBridge application:application didFinishLaunchingWithOptions:launchOptions];
// return YES;
cachedApp = application;
[cachedApp retain];
appLaunchOpts = launchOptions;
[appLaunchOpts retain];
bool flag = [self getFlag:@"isFirstBoot"];
if(flag)
{
[self startCocos:cachedApp];
} else {
[self startUnity:cachedApp];
}
// [[UIApplication sharedApplication] setStatusBarHidden:YES];
// [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
//run the cocos2d-x game scene
// appController = self;
// app->start();
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the childConfigIndicatorClusteredAttempt state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
[[SDKWrapper shared] applicationWillResignActive:application];
[appDelegateBridge applicationWillResignActive:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the childConfigIndicatorClusteredAttempt, optionally refresh the user interface.
*/
[[SDKWrapper shared] applicationDidBecomeActive:application];
[appDelegateBridge applicationDidBecomeActive:application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports childConfigIndicatorClusteredAttempt execution, called instead of applicationWillTerminate: when the user quits.
*/
[[SDKWrapper shared] applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the childConfigIndicatorClusteredAttempt to the inactive state: here you can undo many of the changes made on entering the childConfigIndicatorClusteredAttempt.
*/
[[SDKWrapper shared] applicationWillEnterForeground:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[SDKWrapper shared] applicationWillTerminate:application];
[appDelegateBridge applicationWillTerminate:application];
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[SDKWrapper shared] applicationDidReceiveMemoryWarning:application];
}
@end