Skip to content

Commit 98701d4

Browse files
author
Bin Zhang
committed
Merge pull request #6 from yinjimmy/jsb
new runtime for jsb
2 parents 8da5cdf + 0c708dc commit 98701d4

File tree

89 files changed

+9164
-1672
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+9164
-1672
lines changed

templates/js-template-runtime/frameworks/runtime-src/Classes/AppDelegate.cpp

Lines changed: 231 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,63 @@
2929
#include "Runtime.h"
3030
#include "ConfigParser.h"
3131

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+
3239
USING_NS_CC;
3340
using namespace CocosDenshion;
3441

3542
AppDelegate::AppDelegate()
3643
{
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
3748
}
3849

3950
AppDelegate::~AppDelegate()
4051
{
52+
SimpleAudioEngine::end();
4153
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+
}
4660

4761
ConfigParser::purge();
4862
}
4963

64+
//if you want a different context,just modify the value of glContextAttrs
65+
//it will takes effect on all platforms
5066
void AppDelegate::initGLContextAttrs()
5167
{
68+
//set OpenGL context attributions,now can only set six attributions:
69+
//red,green,blue,alpha,depth,stencil
5270
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
53-
71+
5472
GLView::setGLContextAttrs(glContextAttrs);
5573
}
5674

5775
bool AppDelegate::applicationDidFinishLaunching()
5876
{
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+
6281
#endif
6382
// initialize director
6483
auto director = Director::getInstance();
6584
auto glview = director->getOpenGLView();
6685
if(!glview) {
6786
Size viewSize = ConfigParser::getInstance()->getInitViewSize();
6887
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)
7389
#else
7490
glview = cocos2d::GLViewImpl::createWithRect(title.c_str(), Rect(0, 0, viewSize.width, viewSize.height));
7591
director->setOpenGLView(glview);
@@ -123,17 +139,10 @@ bool AppDelegate::applicationDidFinishLaunching()
123139
sc->addRegisterCallback(JavaScriptObjCBridge::_js_register);
124140
#endif
125141

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();
136144

145+
cocos2d::log("iShow!");
137146
return true;
138147
}
139148

@@ -156,3 +165,203 @@ void AppDelegate::applicationWillEnterForeground()
156165
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
157166
SimpleAudioEngine::getInstance()->resumeAllEffects();
158167
}
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+

templates/js-template-runtime/frameworks/runtime-src/Classes/AppDelegate.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#ifndef _APP_DELEGATE_H_
1010
#define _APP_DELEGATE_H_
1111

12+
13+
#include "ProjectConfig/ProjectConfig.h"
14+
#include "ProjectConfig/SimulatorConfig.h"
1215
#include "platform/CCApplication.h"
1316
/**
1417
@brief The cocos2d Application.
@@ -41,7 +44,40 @@ class AppDelegate : private cocos2d::Application
4144
@param the pointer of the application
4245
*/
4346
virtual void applicationWillEnterForeground();
47+
48+
void setProjectConfig(const ProjectConfig& project);
49+
50+
void reopenProject();
51+
52+
private:
53+
ProjectConfig _project;
54+
55+
friend class StartupCall;
56+
};
57+
58+
59+
class StartupCall : public cocos2d::Ref
60+
{
61+
public:
62+
static StartupCall *create(AppDelegate *app);
63+
void startup();
64+
65+
private:
66+
StartupCall();
67+
68+
void trackEvent(const char *eventName);
69+
void trackLaunchEvent();
70+
71+
void onPreviewJs(const std::string &path);
72+
73+
void updateConfigParser(const ProjectConfig& project);
74+
void updatePreviewFuncForPath(const std::string &path);
75+
76+
private:
77+
AppDelegate *_app;
78+
std::function<void(const std::string &)> _previewFunc;
79+
std::string _launchEvent;
4480
};
4581

46-
#endif // _APP_DELEGATE_H_
82+
#endif // __APP_DELEGATE_H__
4783

0 commit comments

Comments
 (0)