Skip to content

#1108: Add restart game feature and test case #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions frameworks/js-bindings/bindings/manual/ScriptingCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ bool JSBCore_os(JSContext *cx, uint32_t argc, jsval *vp)
return true;
};

bool JSB_cleanScript(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc != 1)
{
JS_ReportError(cx, "Invalid number of arguments in JSB_cleanScript");
return false;
}
jsval *argv = JS_ARGV(cx, vp);
JSString *jsPath = JSVAL_TO_STRING(argv[0]);
JSB_PRECONDITION2(jsPath, cx, false, "Error js file in clean script");
JSStringWrapper wrapper(jsPath);
ScriptingCore::getInstance()->cleanScript(wrapper.get());

JS_SET_RVAL(cx, vp, JSVAL_VOID);

return true;
};

bool JSB_core_restartVM(JSContext *cx, uint32_t argc, jsval *vp)
{
JSB_PRECONDITION2(argc==0, cx, false, "Invalid number of arguments in executeScript");
Expand Down Expand Up @@ -394,11 +412,12 @@ void registerDefaultClasses(JSContext* cx, JSObject* global) {
JS_DefineFunction(cx, global, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, global, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, global, "forceGC", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT);

JS_DefineFunction(cx, global, "__getPlatform", JSBCore_platform, 0, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, global, "__getOS", JSBCore_os, 0, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, global, "__getVersion", JSBCore_version, 0, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, global, "__restartVM", JSB_core_restartVM, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
JS_DefineFunction(cx, global, "__cleanScript", JSB_cleanScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
}

static void sc_finalize(JSFreeOp *freeOp, JSObject *obj) {
Expand All @@ -422,6 +441,11 @@ ScriptingCore::ScriptingCore()
// set utf8 strings internally (we don't need utf16)
// XXX: Removed in SpiderMonkey 19.0
//JS_SetCStringsAreUTF8();
initRegister();
}

void ScriptingCore::initRegister()
{
this->addRegisterCallback(registerDefaultClasses);
this->_runLoop = new SimpleRunLoop();
}
Expand Down Expand Up @@ -703,9 +727,15 @@ bool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx)
}

void ScriptingCore::reset()
{
Director::getInstance()->restart();
}

void ScriptingCore::restartVM()
{
cleanup();
start();
initRegister();
CCApplication::getInstance()->applicationDidFinishLaunching();
}

ScriptingCore::~ScriptingCore()
Expand Down Expand Up @@ -741,6 +771,7 @@ void ScriptingCore::cleanup()

_js_global_type_map.clear();
filename_script.clear();
registrationList.clear();
}

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

// special type, can't use this code after JSAutoCompartment
if (evt->type == kRestartGame)
{
restartVM();
return 0;
}

JSAutoCompartment ac(_cx, _global);

switch (evt->type)
Expand Down
4 changes: 3 additions & 1 deletion frameworks/js-bindings/bindings/manual/ScriptingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol

private:
void string_report(jsval val);

void initRegister();
public:
int handleNodeEvent(void* data);
int handleComponentEvent(void* data);
Expand All @@ -257,6 +257,8 @@ class ScriptingCore : public cocos2d::ScriptEngineProtocol
bool handleTouchEvent(void* nativeObj, cocos2d::EventTouch::EventCode eventCode, cocos2d::Touch* touch, cocos2d::Event* event, jsval* jsvalRet = nullptr);
bool handleMouseEvent(void* nativeObj, cocos2d::EventMouse::MouseEventType eventType, cocos2d::Event* event, jsval* jsvalRet = nullptr);
bool handleKeybardEvent(void* nativeObj, cocos2d::EventKeyboard::KeyCode keyCode, bool isPressed, cocos2d::Event* event);

void restartVM();
};

JSObject* NewGlobalObject(JSContext* cx, bool debug = false);
Expand Down
15 changes: 15 additions & 0 deletions frameworks/js-bindings/bindings/script/jsb_boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,12 @@ cc._initSys = function(config, CONFIG_KEY){
__restartVM();
};

// clean a singal js file
locSys.cleanScript = function(jsFile) {
__cleanScript(jsFile);
};


locSys.dump = function(){
var self = this;
var str = "";
Expand Down Expand Up @@ -1506,6 +1512,14 @@ cc.game = {
config[CONFIG_KEY.frameRate] = frameRate;
cc.director.setAnimationInterval(1.0/frameRate);
},

/**
* Restart game.
*/
restart: function () {
__restartVM();
},

/**
* Run game.
*/
Expand All @@ -1519,6 +1533,7 @@ cc.game = {
self.onStart();
}
},

/**
* Init config.
* @param cb
Expand Down
2 changes: 1 addition & 1 deletion frameworks/js-bindings/cocos2d-x
30 changes: 3 additions & 27 deletions samples/js-moonwarriors/project/Classes/AppDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@
#include "SimpleAudioEngine.h"
#include "ScriptingCore.h"
#include "jsb_cocos2dx_auto.hpp"
#include "jsb_cocos2dx_extension_auto.hpp"
#include "jsb_cocos2dx_builder_auto.hpp"
#include "extension/jsb_cocos2dx_extension_manual.h"
#include "cocos2d_specifics.hpp"
#include "cocosbuilder/js_bindings_ccbreader.h"
#include "localstorage/js_bindings_system_registration.h"
#include "chipmunk/js_bindings_chipmunk_registration.h"
#include "jsb_opengl_registration.h"
#include "jsb_cocos2dx_ui_auto.hpp"
#include "ui/jsb_cocos2dx_ui_manual.h"
#include "cocostudio/jsb_cocos2dx_studio_manual.h"
#include "jsb_cocos2dx_studio_auto.hpp"

USING_NS_CC;
using namespace CocosDenshion;
Expand All @@ -26,7 +16,7 @@ AppDelegate::AppDelegate()

AppDelegate::~AppDelegate()
{
ScriptEngineManager::destroyInstance();
ScriptEngineManager::destroyInstance();
}

void AppDelegate::initGLContextAttrs()
Expand Down Expand Up @@ -54,27 +44,13 @@ bool AppDelegate::applicationDidFinishLaunching()
sc->addRegisterCallback(register_cocos2dx_js_core);
sc->addRegisterCallback(register_cocos2dx_js_extensions);
sc->addRegisterCallback(jsb_register_system);

//sc->addRegisterCallback(register_all_cocos2dx_extension);
//sc->addRegisterCallback(register_all_cocos2dx_extension_manual);

//sc->addRegisterCallback(jsb_register_chipmunk);

//sc->addRegisterCallback(register_all_cocos2dx_builder);
//sc->addRegisterCallback(register_CCBuilderReader);

//sc->addRegisterCallback(JSB_register_opengl);
//sc->addRegisterCallback(register_all_cocos2dx_ui);
//sc->addRegisterCallback(register_all_cocos2dx_ui_manual);
//sc->addRegisterCallback(register_all_cocos2dx_studio);
//sc->addRegisterCallback(register_all_cocos2dx_studio_manual);

sc->start();
sc->runScript("script/jsb_boot.js");
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
sc->enableDebugger();
#endif

auto pEngine = ScriptingCore::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(pEngine);

Expand All @@ -90,7 +66,7 @@ void AppDelegate::applicationDidEnterBackground()
director->stopAnimation();
director->getEventDispatcher()->dispatchCustomEvent("game_on_hide");
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
SimpleAudioEngine::getInstance()->pauseAllEffects();
}

// this function will be called when the app is active again
Expand Down
1 change: 1 addition & 0 deletions samples/js-tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"src/EventTest/EventTest.js",
"src/UnitTest/UnitTest.js",
"src/SysTest/SysTest.js",
"src/SysTest/ScriptTestTempFile.js",
"src/EffectsTest/EffectsTest.js",
"src/EffectsAdvancedTest/EffectsAdvancedTest.js",
"src/MotionStreakTest/MotionStreakTest.js",
Expand Down
13 changes: 13 additions & 0 deletions samples/js-tests/res/Manifests/ScriptTest/project.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"packageUrl" : "http://tools.itharbors.com/assets_manager/ScriptTest/",
"remoteManifestUrl" : "http://tools.itharbors.com/assets_manager/ScriptTest/project_dev.manifest",
"remoteVersionUrl" : "http://tools.itharbors.com/assets_manager/ScriptTest/version_dev.manifest",
"version" : "1.0.0",
"engineVersion" : "3.0",

"assets" : {
},

"searchPaths" : [
]
}
38 changes: 38 additions & 0 deletions samples/js-tests/src/SysTest/ScriptTestTempFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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.
****************************************************************************/

var ScriptTestTempLayer = cc.Layer.extend({
ctor : function () {
this._super();

var labelTest = new cc.LabelTTF("this is the ScriptTestTempLayer old file", "Verdana", 32, cc.size(winSize.width, 50), cc.TEXT_ALIGNMENT_CENTER);
var size = cc.winSize;
labelTest.setPosition(size.width / 2, size.height / 4);
this.addChild(labelTest);

}

});
Loading