29
29
#include " Runtime.h"
30
30
#include " ConfigParser.h"
31
31
32
+ #if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
33
+ #include " service/DeviceEx.h"
34
+ #include " network/CCHTTPRequest.h"
35
+ #endif
36
+
37
+ using namespace CocosDenshion ;
38
+
32
39
USING_NS_CC;
33
40
using namespace CocosDenshion ;
34
41
35
42
AppDelegate::AppDelegate ()
36
43
{
44
+ #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
45
+ auto config = ConfigParser::getInstance ();
46
+ _project.setScriptFile (config->getEntryFile ());
47
+ #endif
37
48
}
38
49
39
50
AppDelegate::~AppDelegate ()
40
51
{
52
+ SimpleAudioEngine::end ();
41
53
ScriptEngineManager::destroyInstance ();
42
- #if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
43
- // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
44
- endRuntime ();
45
- #endif
54
+
55
+ if (_project.getDebuggerType () != kCCRuntimeDebuggerNone )
56
+ {
57
+ // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
58
+ endRuntime ();
59
+ }
46
60
47
61
ConfigParser::purge ();
48
62
}
49
63
64
+ // if you want a different context,just modify the value of glContextAttrs
65
+ // it will takes effect on all platforms
50
66
void AppDelegate::initGLContextAttrs ()
51
67
{
68
+ // set OpenGL context attributions,now can only set six attributions:
69
+ // red,green,blue,alpha,depth,stencil
52
70
GLContextAttrs glContextAttrs = {8 , 8 , 8 , 8 , 24 , 8 };
53
-
71
+
54
72
GLView::setGLContextAttrs (glContextAttrs);
55
73
}
56
74
57
75
bool AppDelegate::applicationDidFinishLaunching ()
58
76
{
59
- #if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
60
- // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
61
- initRuntime ();
77
+ //
78
+ #if ((CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) && (CC_CODE_IDE_DEBUG_SUPPORT > 0))
79
+ _project.setDebuggerType (kCCRuntimeDebuggerCodeIDE );
80
+
62
81
#endif
63
82
// initialize director
64
83
auto director = Director::getInstance ();
65
84
auto glview = director->getOpenGLView ();
66
85
if (!glview) {
67
86
Size viewSize = ConfigParser::getInstance ()->getInitViewSize ();
68
87
string title = ConfigParser::getInstance ()->getInitViewName ();
69
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) && (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
70
- extern void createSimulator (const char * viewName, float width, float height,bool isLandscape = true , float frameZoomFactor = 1 .0f );
71
- bool isLanscape = ConfigParser::getInstance ()->isLanscape ();
72
- createSimulator (title.c_str (), viewSize.width ,viewSize.height , isLanscape);
88
+ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) && (CC_CODE_IDE_DEBUG_SUPPORT > 0)
73
89
#else
74
90
glview = cocos2d::GLViewImpl::createWithRect (title.c_str (), Rect (0 , 0 , viewSize.width , viewSize.height ));
75
91
director->setOpenGLView (glview);
@@ -123,17 +139,10 @@ bool AppDelegate::applicationDidFinishLaunching()
123
139
sc->addRegisterCallback (JavaScriptObjCBridge::_js_register);
124
140
#endif
125
141
126
- #if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
127
- // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
128
- startRuntime ();
129
- #else
130
- sc->start ();
131
- sc->runScript (" script/jsb_boot.js" );
132
- auto engine = ScriptingCore::getInstance ();
133
- ScriptEngineManager::getInstance ()->setScriptEngine (engine);
134
- ScriptingCore::getInstance ()->runScript (ConfigParser::getInstance ()->getEntryFile ().c_str ());
135
- #endif
142
+ StartupCall *call = StartupCall::create (this );
143
+ call->startup ();
136
144
145
+ cocos2d::log (" iShow!" );
137
146
return true ;
138
147
}
139
148
@@ -156,3 +165,203 @@ void AppDelegate::applicationWillEnterForeground()
156
165
SimpleAudioEngine::getInstance ()->resumeBackgroundMusic ();
157
166
SimpleAudioEngine::getInstance ()->resumeAllEffects ();
158
167
}
168
+
169
+ void AppDelegate::setProjectConfig (const ProjectConfig& project)
170
+ {
171
+ _project = project;
172
+ }
173
+
174
+ void AppDelegate::reopenProject ()
175
+ {
176
+ auto fileUtils = FileUtils::getInstance ();
177
+
178
+ //
179
+ // set root path
180
+ // set search root **MUST** before set search paths
181
+ //
182
+ fileUtils->setDefaultResourceRootPath (_project.getProjectDir ());
183
+
184
+ // clean
185
+ Director::getInstance ()->getTextureCache ()->removeAllTextures ();
186
+ Director::getInstance ()->purgeCachedData ();
187
+ SimpleAudioEngine::getInstance ()->stopAllEffects ();
188
+ SimpleAudioEngine::getInstance ()->stopBackgroundMusic (true );
189
+ vector<string> searchPaths;
190
+ fileUtils->setSearchPaths (searchPaths);
191
+
192
+ const string writablePath = _project.getWritableRealPath ();
193
+ if (writablePath.length ())
194
+ {
195
+ FileUtils::getInstance ()->setWritablePath (writablePath.c_str ());
196
+ }
197
+
198
+ resetDesignResolution ();
199
+
200
+ StartupCall *call = StartupCall::create (this );
201
+ call->startup ();
202
+ }
203
+
204
+ // ----------------------------------------
205
+
206
+ StartupCall *StartupCall::create (AppDelegate *app)
207
+ {
208
+ StartupCall *call = new StartupCall ();
209
+ call->_app = app;
210
+ call->autorelease ();
211
+ return call;
212
+ }
213
+
214
+ StartupCall::StartupCall ()
215
+ : _launchEvent(" empty" )
216
+ {
217
+ }
218
+
219
+ static bool endWithString (const std::string &buf, const std::string &suffix)
220
+ {
221
+ return ((buf.find (suffix) + suffix.length ()) == buf.length ());
222
+ }
223
+
224
+ void StartupCall::startup ()
225
+ {
226
+ const ProjectConfig &project = _app->_project ;
227
+
228
+ // set search path
229
+ string path = FileUtils::getInstance ()->fullPathForFilename (project.getScriptFileRealPath ().c_str ());
230
+ size_t pos;
231
+ while ((pos = path.find_first_of (" \\ " )) != std::string::npos)
232
+ {
233
+ path.replace (pos, 1 , " /" );
234
+ }
235
+ size_t p = path.find_last_of (" /" );
236
+ string workdir;
237
+ if (p != path.npos )
238
+ {
239
+ workdir = path.substr (0 , p);
240
+ FileUtils::getInstance ()->addSearchPath (workdir);
241
+ }
242
+
243
+ // update search pathes
244
+ FileUtils::getInstance ()->addSearchPath (project.getProjectDir ());
245
+ auto &customizedPathes = project.getSearchPath ();
246
+ for (auto &path : customizedPathes)
247
+ {
248
+ FileUtils::getInstance ()->addSearchPath (path);
249
+ }
250
+
251
+ updateConfigParser (project);
252
+ if (FileUtils::getInstance ()->isFileExist (path))
253
+ {
254
+ updatePreviewFuncForPath (path);
255
+
256
+ // launch
257
+ if (project.getDebuggerType () == kCCRuntimeDebuggerNone )
258
+ {
259
+ _previewFunc (path);
260
+ }
261
+ else
262
+ {
263
+ // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
264
+ initRuntime (project.getProjectDir ());
265
+ startRuntime ();
266
+ }
267
+ }
268
+ else
269
+ {
270
+ CCLOG (" [ERROR]: %s is not exist." , path.c_str ());
271
+ }
272
+
273
+ // track start event
274
+ trackLaunchEvent ();
275
+ }
276
+
277
+ // *NOTE*
278
+ // track event on windows / mac platform
279
+ //
280
+ void StartupCall::trackEvent (const char *eventName)
281
+ {
282
+ #if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
283
+
284
+ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
285
+ const char *platform = " win" ;
286
+ #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
287
+ const char *platform = " mac" ;
288
+ #else
289
+ const char *platform = " UNKNOWN" ;
290
+ #endif
291
+
292
+ auto request = extra::HTTPRequest::createWithUrl (NULL ,
293
+ " http://www.google-analytics.com/collect" ,
294
+ kCCHTTPRequestMethodPOST );
295
+ request->addPOSTValue (" v" , " 1" );
296
+ request->addPOSTValue (" tid" , " UA-58200293-1" );
297
+ request->addPOSTValue (" cid" , player::DeviceEx::getInstance ()->getUserGUID ().c_str ());
298
+ request->addPOSTValue (" t" , " event" );
299
+
300
+ request->addPOSTValue (" an" , " simulator" );
301
+ request->addPOSTValue (" av" , cocos2dVersion ());
302
+
303
+ request->addPOSTValue (" ec" , platform);
304
+ request->addPOSTValue (" ea" , eventName);
305
+
306
+ request->start ();
307
+
308
+ #endif // ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
309
+ }
310
+
311
+ void StartupCall::trackLaunchEvent ()
312
+ {
313
+ trackEvent (_launchEvent.c_str ());
314
+ }
315
+
316
+ void StartupCall::onPreviewJs (const std::string &path)
317
+ {
318
+ std::string filepath = path;
319
+ if (filepath.empty ())
320
+ {
321
+ filepath = ConfigParser::getInstance ()->getEntryFile ();
322
+ }
323
+ CCLOG (" ------------------------------------------------" );
324
+ CCLOG (" LOAD Js FILE: %s" , filepath.c_str ());
325
+ CCLOG (" ------------------------------------------------" );
326
+
327
+ ScriptingCore* sc = ScriptingCore::getInstance ();
328
+ sc->start ();
329
+ sc->runScript (" script/jsb_boot.js" );
330
+ auto engine = ScriptingCore::getInstance ();
331
+ ScriptEngineManager::getInstance ()->setScriptEngine (engine);
332
+ ScriptingCore::getInstance ()->runScript (filepath.c_str ());
333
+ }
334
+
335
+ void StartupCall::updateConfigParser (const ProjectConfig& project)
336
+ {
337
+ // set entry file
338
+ auto parser = ConfigParser::getInstance ();
339
+ string entryFile (project.getScriptFileRealPath ());
340
+ if (entryFile.find (project.getProjectDir ()) != string::npos)
341
+ {
342
+ entryFile.erase (0 , project.getProjectDir ().length ());
343
+ }
344
+ entryFile = replaceAll (entryFile, " \\ " , " /" );
345
+ parser->setEntryFile (entryFile);
346
+
347
+ parser->setBindAddress (project.getBindAddress ());
348
+ }
349
+
350
+ void StartupCall::updatePreviewFuncForPath (const std::string &path)
351
+ {
352
+ // set loader
353
+ _previewFunc = [](const std::string &path) { CCLOG (" [WARNING]: unsupport %s" , path.c_str ()); };
354
+
355
+ if (!FileUtils::getInstance ()->isFileExist (path))
356
+ {
357
+ CCLOG (" [ERROR]: %s is not exist." , path.c_str ());
358
+ return ;
359
+ }
360
+
361
+ if (endWithString (path, " .js" ))
362
+ {
363
+ _launchEvent = " js" ;
364
+ _previewFunc = std::bind (&StartupCall::onPreviewJs, this , std::placeholders::_1);
365
+ }
366
+ }
367
+
0 commit comments