Skip to content

Commit d408252

Browse files
committed
Merge pull request #1169 from pandamicro/restartGame
#1108: Add restart game feature and test case
2 parents 6b78ac0 + 93a9f8c commit d408252

File tree

10 files changed

+267
-33
lines changed

10 files changed

+267
-33
lines changed

frameworks/js-bindings/bindings/manual/ScriptingCore.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ bool JSBCore_os(JSContext *cx, uint32_t argc, jsval *vp)
354354
return true;
355355
};
356356

357+
bool JSB_cleanScript(JSContext *cx, uint32_t argc, jsval *vp)
358+
{
359+
if (argc != 1)
360+
{
361+
JS_ReportError(cx, "Invalid number of arguments in JSB_cleanScript");
362+
return false;
363+
}
364+
jsval *argv = JS_ARGV(cx, vp);
365+
JSString *jsPath = JSVAL_TO_STRING(argv[0]);
366+
JSB_PRECONDITION2(jsPath, cx, false, "Error js file in clean script");
367+
JSStringWrapper wrapper(jsPath);
368+
ScriptingCore::getInstance()->cleanScript(wrapper.get());
369+
370+
JS_SET_RVAL(cx, vp, JSVAL_VOID);
371+
372+
return true;
373+
};
374+
357375
bool JSB_core_restartVM(JSContext *cx, uint32_t argc, jsval *vp)
358376
{
359377
JSB_PRECONDITION2(argc==0, cx, false, "Invalid number of arguments in executeScript");
@@ -394,11 +412,12 @@ void registerDefaultClasses(JSContext* cx, JSObject* global) {
394412
JS_DefineFunction(cx, global, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT);
395413
JS_DefineFunction(cx, global, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
396414
JS_DefineFunction(cx, global, "forceGC", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT);
397-
415+
398416
JS_DefineFunction(cx, global, "__getPlatform", JSBCore_platform, 0, JSPROP_READONLY | JSPROP_PERMANENT);
399417
JS_DefineFunction(cx, global, "__getOS", JSBCore_os, 0, JSPROP_READONLY | JSPROP_PERMANENT);
400418
JS_DefineFunction(cx, global, "__getVersion", JSBCore_version, 0, JSPROP_READONLY | JSPROP_PERMANENT);
401419
JS_DefineFunction(cx, global, "__restartVM", JSB_core_restartVM, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
420+
JS_DefineFunction(cx, global, "__cleanScript", JSB_cleanScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
402421
}
403422

404423
static void sc_finalize(JSFreeOp *freeOp, JSObject *obj) {
@@ -422,6 +441,11 @@ ScriptingCore::ScriptingCore()
422441
// set utf8 strings internally (we don't need utf16)
423442
// XXX: Removed in SpiderMonkey 19.0
424443
//JS_SetCStringsAreUTF8();
444+
initRegister();
445+
}
446+
447+
void ScriptingCore::initRegister()
448+
{
425449
this->addRegisterCallback(registerDefaultClasses);
426450
this->_runLoop = new SimpleRunLoop();
427451
}
@@ -703,9 +727,15 @@ bool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx)
703727
}
704728

705729
void ScriptingCore::reset()
730+
{
731+
Director::getInstance()->restart();
732+
}
733+
734+
void ScriptingCore::restartVM()
706735
{
707736
cleanup();
708-
start();
737+
initRegister();
738+
CCApplication::getInstance()->applicationDidFinishLaunching();
709739
}
710740

711741
ScriptingCore::~ScriptingCore()
@@ -741,6 +771,7 @@ void ScriptingCore::cleanup()
741771

742772
_js_global_type_map.clear();
743773
filename_script.clear();
774+
registrationList.clear();
744775
}
745776

746777
void ScriptingCore::reportError(JSContext *cx, const char *message, JSErrorReport *report)
@@ -1344,6 +1375,13 @@ int ScriptingCore::sendEvent(ScriptEvent* evt)
13441375
if (NULL == evt)
13451376
return 0;
13461377

1378+
// special type, can't use this code after JSAutoCompartment
1379+
if (evt->type == kRestartGame)
1380+
{
1381+
restartVM();
1382+
return 0;
1383+
}
1384+
13471385
JSAutoCompartment ac(_cx, _global);
13481386

13491387
switch (evt->type)

frameworks/js-bindings/bindings/manual/ScriptingCore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol
247247

248248
private:
249249
void string_report(jsval val);
250-
250+
void initRegister();
251251
public:
252252
int handleNodeEvent(void* data);
253253
int handleComponentEvent(void* data);
@@ -257,6 +257,8 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol
257257
bool handleTouchEvent(void* nativeObj, cocos2d::EventTouch::EventCode eventCode, cocos2d::Touch* touch, cocos2d::Event* event, jsval* jsvalRet = nullptr);
258258
bool handleMouseEvent(void* nativeObj, cocos2d::EventMouse::MouseEventType eventType, cocos2d::Event* event, jsval* jsvalRet = nullptr);
259259
bool handleKeybardEvent(void* nativeObj, cocos2d::EventKeyboard::KeyCode keyCode, bool isPressed, cocos2d::Event* event);
260+
261+
void restartVM();
260262
};
261263

262264
JSObject* NewGlobalObject(JSContext* cx, bool debug = false);

frameworks/js-bindings/bindings/script/jsb_boot.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,12 @@ cc._initSys = function(config, CONFIG_KEY){
13271327
__restartVM();
13281328
};
13291329

1330+
// clean a singal js file
1331+
locSys.cleanScript = function(jsFile) {
1332+
__cleanScript(jsFile);
1333+
};
1334+
1335+
13301336
locSys.dump = function(){
13311337
var self = this;
13321338
var str = "";
@@ -1506,6 +1512,14 @@ cc.game = {
15061512
config[CONFIG_KEY.frameRate] = frameRate;
15071513
cc.director.setAnimationInterval(1.0/frameRate);
15081514
},
1515+
1516+
/**
1517+
* Restart game.
1518+
*/
1519+
restart: function () {
1520+
__restartVM();
1521+
},
1522+
15091523
/**
15101524
* Run game.
15111525
*/
@@ -1519,6 +1533,7 @@ cc.game = {
15191533
self.onStart();
15201534
}
15211535
},
1536+
15221537
/**
15231538
* Init config.
15241539
* @param cb

samples/js-moonwarriors/project/Classes/AppDelegate.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,8 @@
44
#include "SimpleAudioEngine.h"
55
#include "ScriptingCore.h"
66
#include "jsb_cocos2dx_auto.hpp"
7-
#include "jsb_cocos2dx_extension_auto.hpp"
8-
#include "jsb_cocos2dx_builder_auto.hpp"
9-
#include "extension/jsb_cocos2dx_extension_manual.h"
107
#include "cocos2d_specifics.hpp"
11-
#include "cocosbuilder/js_bindings_ccbreader.h"
128
#include "localstorage/js_bindings_system_registration.h"
13-
#include "chipmunk/js_bindings_chipmunk_registration.h"
14-
#include "jsb_opengl_registration.h"
15-
#include "jsb_cocos2dx_ui_auto.hpp"
16-
#include "ui/jsb_cocos2dx_ui_manual.h"
17-
#include "cocostudio/jsb_cocos2dx_studio_manual.h"
18-
#include "jsb_cocos2dx_studio_auto.hpp"
199

2010
USING_NS_CC;
2111
using namespace CocosDenshion;
@@ -26,7 +16,7 @@ AppDelegate::AppDelegate()
2616

2717
AppDelegate::~AppDelegate()
2818
{
29-
ScriptEngineManager::destroyInstance();
19+
ScriptEngineManager::destroyInstance();
3020
}
3121

3222
void AppDelegate::initGLContextAttrs()
@@ -54,27 +44,13 @@ bool AppDelegate::applicationDidFinishLaunching()
5444
sc->addRegisterCallback(register_cocos2dx_js_core);
5545
sc->addRegisterCallback(register_cocos2dx_js_extensions);
5646
sc->addRegisterCallback(jsb_register_system);
57-
58-
//sc->addRegisterCallback(register_all_cocos2dx_extension);
59-
//sc->addRegisterCallback(register_all_cocos2dx_extension_manual);
60-
61-
//sc->addRegisterCallback(jsb_register_chipmunk);
6247

63-
//sc->addRegisterCallback(register_all_cocos2dx_builder);
64-
//sc->addRegisterCallback(register_CCBuilderReader);
65-
66-
//sc->addRegisterCallback(JSB_register_opengl);
67-
//sc->addRegisterCallback(register_all_cocos2dx_ui);
68-
//sc->addRegisterCallback(register_all_cocos2dx_ui_manual);
69-
//sc->addRegisterCallback(register_all_cocos2dx_studio);
70-
//sc->addRegisterCallback(register_all_cocos2dx_studio_manual);
71-
7248
sc->start();
7349
sc->runScript("script/jsb_boot.js");
7450
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
7551
sc->enableDebugger();
7652
#endif
77-
53+
7854
auto pEngine = ScriptingCore::getInstance();
7955
ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
8056

@@ -90,7 +66,7 @@ void AppDelegate::applicationDidEnterBackground()
9066
director->stopAnimation();
9167
director->getEventDispatcher()->dispatchCustomEvent("game_on_hide");
9268
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
93-
SimpleAudioEngine::getInstance()->pauseAllEffects();
69+
SimpleAudioEngine::getInstance()->pauseAllEffects();
9470
}
9571

9672
// this function will be called when the app is active again

samples/js-tests/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"src/EventTest/EventTest.js",
7171
"src/UnitTest/UnitTest.js",
7272
"src/SysTest/SysTest.js",
73+
"src/SysTest/ScriptTestTempFile.js",
7374
"src/EffectsTest/EffectsTest.js",
7475
"src/EffectsAdvancedTest/EffectsAdvancedTest.js",
7576
"src/MotionStreakTest/MotionStreakTest.js",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"packageUrl" : "http://tools.itharbors.com/assets_manager/ScriptTest/",
3+
"remoteManifestUrl" : "http://tools.itharbors.com/assets_manager/ScriptTest/project_dev.manifest",
4+
"remoteVersionUrl" : "http://tools.itharbors.com/assets_manager/ScriptTest/version_dev.manifest",
5+
"version" : "1.0.0",
6+
"engineVersion" : "3.0",
7+
8+
"assets" : {
9+
},
10+
11+
"searchPaths" : [
12+
]
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/****************************************************************************
2+
Copyright (c) 2008-2010 Ricardo Quesada
3+
Copyright (c) 2011-2012 cocos2d-x.org
4+
Copyright (c) 2013-2014 Chukong Technologies Inc.
5+
6+
http://www.cocos2d-x.org
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
****************************************************************************/
26+
27+
var ScriptTestTempLayer = cc.Layer.extend({
28+
ctor : function () {
29+
this._super();
30+
31+
var labelTest = new cc.LabelTTF("this is the ScriptTestTempLayer old file", "Verdana", 32, cc.size(winSize.width, 50), cc.TEXT_ALIGNMENT_CENTER);
32+
var size = cc.winSize;
33+
labelTest.setPosition(size.width / 2, size.height / 4);
34+
this.addChild(labelTest);
35+
36+
}
37+
38+
});

0 commit comments

Comments
 (0)