From c973dba4f14c4dabc2b99eaa473c4c941ff64787 Mon Sep 17 00:00:00 2001 From: chuanweizhang2013 Date: Fri, 9 May 2014 14:06:20 +0800 Subject: [PATCH] add entry file support --- .../cocos-project-template.json | 6 ++++++ .../runtime-src/Classes/AppDelegate.cpp | 6 +++--- .../runtime-src/Classes/ConfigParser.cpp | 19 ++++++++++++++----- .../runtime-src/Classes/ConfigParser.h | 2 ++ .../runtime-src/Classes/Runtime.cpp | 17 ++++++++++++----- .../frameworks/runtime-src/Classes/Runtime.h | 2 +- .../lua-template-runtime/res/config.json | 5 +++-- 7 files changed, 41 insertions(+), 16 deletions(-) diff --git a/templates/lua-template-runtime/cocos-project-template.json b/templates/lua-template-runtime/cocos-project-template.json index 3e4a833639..286e7d8b73 100644 --- a/templates/lua-template-runtime/cocos-project-template.json +++ b/templates/lua-template-runtime/cocos-project-template.json @@ -3,6 +3,12 @@ "exclude_from_template": [ "frameworks/runtime-src" ], + "project_replace_project_name": { + "src_project_name": "HelloLua", + "files": [ + "res/config.json", + ".project"] + }, "append_dir": [ { "from": "cocos/scripting/lua-bindings/script", diff --git a/templates/lua-template-runtime/frameworks/runtime-src/Classes/AppDelegate.cpp b/templates/lua-template-runtime/frameworks/runtime-src/Classes/AppDelegate.cpp index 7f3bb2064b..8b2af4aab4 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/templates/lua-template-runtime/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -21,9 +21,9 @@ AppDelegate::~AppDelegate() bool AppDelegate::applicationDidFinishLaunching() { - string entryfile ="src/main.lua"; + #if (COCOS2D_DEBUG>0) - initRuntime(entryfile); + initRuntime(); #endif // initialize director @@ -65,7 +65,7 @@ bool AppDelegate::applicationDidFinishLaunching() return true; #endif - engine->executeScriptFile(entryfile.c_str()); + engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str()); return true; } diff --git a/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.cpp b/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.cpp index f017337415..e1e5dc4ee8 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.cpp +++ b/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.cpp @@ -23,8 +23,6 @@ bool ConfigParser::isInit() void ConfigParser::readConfig() { - _initViewSize.setSize(960,640); - _viewName="HelloLua"; _isInit = true; string filecfg = "res/config.json"; string fullPathFile = FileUtils::getInstance()->fullPathForFilename(filecfg); @@ -34,9 +32,9 @@ void ConfigParser::readConfig() rapidjson::FileStream inputStream(pFile); _docRootjson.ParseStream<0>(inputStream); fclose(pFile); - if (_docRootjson.HasMember("init_view") && _docRootjson["init_view"].IsObject()) + if (_docRootjson.HasMember("init_cfg") && _docRootjson["init_cfg"].IsObject()) { - const rapidjson::Value& objectInitView = _docRootjson["init_view"]; + const rapidjson::Value& objectInitView = _docRootjson["init_cfg"]; if (objectInitView.HasMember("width") && objectInitView.HasMember("height")) { _initViewSize.width = objectInitView["width"].GetUint(); @@ -49,6 +47,9 @@ void ConfigParser::readConfig() if (objectInitView.HasMember("isLandscape") && objectInitView["isLandscape"].IsBool()) { _isLandscape = objectInitView["isLandscape"].GetBool(); } + if (objectInitView.HasMember("entry") && objectInitView["entry"].IsString()) { + _entryfile = objectInitView["entry"].GetString(); + } } if (_docRootjson.HasMember("simulator_screen_size")) { @@ -72,18 +73,26 @@ void ConfigParser::readConfig() ConfigParser::ConfigParser(void):_isInit(false),_isLandscape(true) { - + _initViewSize.setSize(960,640); + _viewName = "HelloLua"; + _entryfile = "src/main.lua"; } rapidjson::Document& ConfigParser::getConfigJsonRoot() { return _docRootjson; } + string ConfigParser::getInitViewName() { return _viewName; } +string ConfigParser::getEntryFile() +{ + return _entryfile; +} + Size ConfigParser::getInitViewSize() { return _initViewSize; diff --git a/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.h b/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.h index f76c681f75..7b2f536bb6 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.h +++ b/templates/lua-template-runtime/frameworks/runtime-src/Classes/ConfigParser.h @@ -33,6 +33,7 @@ public: int getScreenSizeCount(void); cocos2d::Size getInitViewSize(); string getInitViewName(); + string getEntryFile(); rapidjson::Document& getConfigJsonRoot(); const SimulatorScreenSize getScreenSize(int index); bool isLanscape(); @@ -44,6 +45,7 @@ private: ScreenSizeArray _screenSizeArray; cocos2d::Size _initViewSize; string _viewName; + string _entryfile; bool _isLandscape; bool _isInit; diff --git a/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.cpp b/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.cpp index ba9e1a5aa6..e412f80d62 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.cpp +++ b/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.cpp @@ -32,6 +32,7 @@ THE SOFTWARE. #include "json/writer.h" #include "LuaBasicConversions.h" #include "VisibleRect.h" +#include "ConfigParser.h" #ifdef _WIN32 #include @@ -47,7 +48,6 @@ using namespace cocos2d; std::string g_resourcePath; static rapidjson::Document g_filecfgjson; -static string g_entryfile; extern string getIPAddress(); const char* getRuntimeVersion() { @@ -63,7 +63,7 @@ void startScript(string strDebugArg) engine->executeString(strDebugArg.c_str()); } cocos2d::log("debug args = %s",strDebugArg.c_str()); - engine->executeScriptFile(g_entryfile.c_str()); + engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str()); } bool reloadScript(const string& modulefile) @@ -71,7 +71,7 @@ bool reloadScript(const string& modulefile) string strfile = modulefile; if (strfile.empty()) { - strfile = g_entryfile; + strfile = ConfigParser::getInstance()->getEntryFile().c_str(); } auto director = Director::getInstance(); @@ -583,6 +583,14 @@ public: dReplyParse.AddMember("body",bodyvalue,dReplyParse.GetAllocator()); dReplyParse.AddMember("code",0,dReplyParse.GetAllocator()); + }else if (strcmp(strcmd.c_str(),"getEntryfile")==0) + { + rapidjson::Value bodyvalue(rapidjson::kObjectType); + rapidjson::Value entryFileValue(rapidjson::kStringType); + entryFileValue.SetString(ConfigParser::getInstance()->getEntryFile().c_str(),dReplyParse.GetAllocator()); + bodyvalue.AddMember("entryfile",entryFileValue,dReplyParse.GetAllocator()); + dReplyParse.AddMember("body",bodyvalue,dReplyParse.GetAllocator()); + dReplyParse.AddMember("code",0,dReplyParse.GetAllocator()); }else if(strcmp(strcmd.c_str(),"getIP")==0) { rapidjson::Value bodyvalue(rapidjson::kObjectType); @@ -724,7 +732,7 @@ static void register_runtime_override_function(lua_State* tolua_S) lua_pop(tolua_S, 1); } -bool initRuntime(string& entryfile) +bool initRuntime() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #ifndef _DEBUG @@ -740,7 +748,6 @@ bool initRuntime(string& entryfile) #endif #endif - g_entryfile = entryfile; vector searchPathArray; searchPathArray=FileUtils::getInstance()->getSearchPaths(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) diff --git a/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.h b/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.h index f1708557be..284c34942f 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.h +++ b/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.h @@ -28,7 +28,7 @@ THE SOFTWARE. #include using namespace std; -bool initRuntime(string& entryfile); +bool initRuntime(); bool startRuntime(); bool reloadScript(const string& modulefile); diff --git a/templates/lua-template-runtime/res/config.json b/templates/lua-template-runtime/res/config.json index 30bd68e431..09215954df 100644 --- a/templates/lua-template-runtime/res/config.json +++ b/templates/lua-template-runtime/res/config.json @@ -1,9 +1,10 @@ { - "init_view":{ + "init_cfg":{ "isLandscape": true, "name": "HelloLua", "width": 960, - "height": 640 + "height": 640, + "entry": "src/main.lua" }, "simulator_screen_size": [ {