forked from KrishAmal/Fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContext.mm
178 lines (149 loc) · 5.26 KB
/
Context.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <Uno-iOS/Context.h>
#include <Uno-iOS/Uno-iOS.h>
#include <uObjC.Foreign.h>
#include <Uno.Platform.CoreApp.h>
#include <Uno.Platform.iOS.Application.h>
#include <Uno.Platform.EventSources.InterAppInvoke.h>
#import <BuddyBuildSDK/BuddyBuildSDK.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
@interface uContext()
{
@private
uRuntime* _uno;
EAGLContext* _glContext;
UIWindow* (^_windowGetter)();
}
@end
@implementation uContext
@synthesize window = _window;
@synthesize glContext = _glContext;
struct NotificationSelector
{
NSString* name;
SEL selector;
};
const static NotificationSelector _notifications[] = {
{ UIApplicationDidFinishLaunchingNotification, @selector(applicationDidFinishLaunching:) },
{ UIApplicationWillEnterForegroundNotification, @selector(applicationWillEnterForeground:) },
{ UIApplicationDidBecomeActiveNotification, @selector(applicationDidBecomeActive:) },
{ UIApplicationWillResignActiveNotification, @selector(applicationWillResignActive:) },
{ UIApplicationDidEnterBackgroundNotification, @selector(applicationDidEnterBackground:) },
{ UIApplicationWillTerminateNotification, @selector(applicationWillTerminate:) },
{ UIApplicationDidReceiveMemoryWarningNotification, @selector(applicationDidReceiveMemoryWarning:) },
};
static uContext* instance = nil;
+ (instancetype)sharedContext
{
if (instance == nil)
{
NSException* notInitializedException = [NSException
exceptionWithName:@"Failed to get sharedContext"
reason:@"uContext has not been initialized"
userInfo: nil];
@throw notInitializedException;
}
return instance;
}
+ (instancetype)initSharedContext
{
if (instance == nil)
{
instance = [[uContext alloc] init];
}
return instance;
}
+ (instancetype)initSharedContextWithWindow:(UIWindow*(^)())windowGetter
{
uContext* context = [uContext initSharedContext];
context->_windowGetter = windowGetter;
return context;
}
- (UIWindow*)window
{
if (self->_windowGetter != nil)
{
return self->_windowGetter();
}
else
{
return nil;
}
}
- (instancetype)init
{
if (self = [super init])
{
_uno = new uRuntime();
_glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:_glContext];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
int num_notifications = sizeof(_notifications) / sizeof(_notifications[0]);
for (int i = 0; i < num_notifications; ++i)
{
[nc addObserver:self
selector:_notifications[i].selector
name:_notifications[i].name
object:nil];
}
}
return self;
}
- (void)dealloc
{
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
int num_notifications = sizeof(_notifications) / sizeof(_notifications[0]);
for (int i = 0; i < num_notifications; ++i)
{
[nc removeObserver:self
name:_notifications[i].name
object:nil];
}
delete _uno;
}
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::Start(); }();
::g::Uno::Platform::iOS::Application::LaunchOptions(::g::ObjC::Object::Create(launchOptions));
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::EventSources::InterAppInvoke::OnReceivedURI(::uObjC::UnoString([url absoluteString])); }();
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::EventSources::InterAppInvoke::OnReceivedURI(::uObjC::UnoString([url absoluteString])); }();
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
[BuddyBuildSDK setup];
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::EnterForeground(); }();
}
- (void)applicationWillEnterForeground:(NSNotification*)notification
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::EnterForeground(); }();
}
- (void)applicationDidBecomeActive:(NSNotification*)notification
{
[FBSDKAppEvents activateApp];
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::EnterInteractive(); }();
}
- (void)applicationWillResignActive:(NSNotification*)notification
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::ExitInteractive(); }();
}
- (void)applicationDidEnterBackground:(NSNotification*)notification
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::EnterBackground(); }();
}
- (void)applicationWillTerminate:(NSNotification*)notification
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::Terminate(); }();
}
- (void)applicationDidReceiveMemoryWarning:(NSNotification*)notification
{
[&]() -> void { ::uForeignPool __foreignPool; ::g::Uno::Platform::CoreApp::OnReceivedLowMemoryWarning(); }();
}
@end