From 7af50608962c4628dafdbe85fcaf07dd9b01f7a6 Mon Sep 17 00:00:00 2001 From: chuanweizhang2013 Date: Tue, 18 Mar 2014 18:24:58 +0800 Subject: [PATCH] add pc args --- .../runtime-src/Classes/Runtime.cpp | 44 ++++++++++++------- .../proj.ios_mac/mac/SimulatorApp.mm | 7 +++ .../runtime-src/proj.win32/main.cpp | 15 +++++++ 3 files changed, 49 insertions(+), 17 deletions(-) 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 9b93e39c96..0f954e2218 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.cpp +++ b/templates/lua-template-runtime/frameworks/runtime-src/Classes/Runtime.cpp @@ -39,6 +39,7 @@ THE SOFTWARE. using namespace std; using namespace cocos2d; +std::string g_resourcePath; extern string getIPAddress(); string getRuntimeVersion() { @@ -216,7 +217,6 @@ public: _listenfd = -1; _running = false; _endThread = false; - _writepath = FileUtils::getInstance()->getWritablePath(); } bool listenOnTCP(int port); @@ -235,7 +235,6 @@ private: fd_set _read_set; bool _running; bool _endThread; - std::string _writepath; }; bool FileServer::listenOnTCP(int port) @@ -315,7 +314,7 @@ void FileServer::stop() } } -string& replace_all(string& str,const string& old_value,const string& new_value) +string& replaceAll(string& str,const string& old_value,const string& new_value) { while(true) { @@ -373,9 +372,8 @@ bool FileServer::recv_file(int fd) } char fullfilename[1024]={0}; - sprintf(fullfilename,"%s%s",_writepath.c_str(),buffer); + sprintf(fullfilename,"%s%s",g_resourcePath.c_str(),buffer); string file(fullfilename); - file=replace_all(file,"\\","/"); sprintf(fullfilename, "%s", file.c_str()); cocos2d::log("recv fullfilename = %s",fullfilename); CreateDir(file.substr(0,file.find_last_of("/")).c_str()); @@ -494,8 +492,6 @@ class ConsoleCustomCommand public: ConsoleCustomCommand():_fileserver(nullptr) { - _writepath = FileUtils::getInstance()->getWritablePath(); - _writepath=replace_all(_writepath,"\\","/"); cocos2d::Console *_console = Director::getInstance()->getConsole(); static struct Console::Command commands[] = { {"shutdownapp","exit runtime app",std::bind(&ConsoleCustomCommand::onShutDownApp, this, std::placeholders::_1, std::placeholders::_2)}, @@ -513,6 +509,7 @@ public: } ~ConsoleCustomCommand() { + Director::getInstance()->getConsole()->stop(); _fileserver->stop(); if (_fileserver) { delete _fileserver; @@ -563,19 +560,32 @@ private: void startRuntime() { + vector oldSearchPathArray; + oldSearchPathArray=FileUtils::getInstance()->getSearchPaths(); + vector newSearchPathArray; +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) + if (g_resourcePath.empty()) + { + g_resourcePath = FileUtils::getInstance()->getWritablePath(); + } + +#else + g_resourcePath = FileUtils::getInstance()->getWritablePath(); + +#endif + + g_resourcePath=replaceAll(g_resourcePath,"\\","/"); + newSearchPathArray.push_back(g_resourcePath); + FileUtils::getInstance()->setSearchPaths(newSearchPathArray); + for (unsigned i = 0; i < oldSearchPathArray.size(); i++) + { + FileUtils::getInstance()->addSearchPath(oldSearchPathArray[i]); + } + static ConsoleCustomCommand s_customCommand; auto engine = LuaEngine::getInstance(); ScriptEngineManager::getInstance()->setScriptEngine(engine); luaopen_debugger(engine->getLuaStack()->getLuaState()); - static ConsoleCustomCommand s_customCommand; - vector searchPathArray; - searchPathArray=FileUtils::getInstance()->getSearchPaths(); - vector writePathArray; - writePathArray.push_back(FileUtils::getInstance()->getWritablePath()); - FileUtils::getInstance()->setSearchPaths(writePathArray); - for (unsigned i = 0; i < searchPathArray.size(); i++) - { - FileUtils::getInstance()->addSearchPath(searchPathArray[i]); - } + auto scene = Scene::create(); auto layer = new ConnectWaitLayer(); layer->autorelease(); diff --git a/templates/lua-template-runtime/frameworks/runtime-src/proj.ios_mac/mac/SimulatorApp.mm b/templates/lua-template-runtime/frameworks/runtime-src/proj.ios_mac/mac/SimulatorApp.mm index 7ef45ef4e9..af5df54d00 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/proj.ios_mac/mac/SimulatorApp.mm +++ b/templates/lua-template-runtime/frameworks/runtime-src/proj.ios_mac/mac/SimulatorApp.mm @@ -63,6 +63,13 @@ using namespace cocos2d; - (void) applicationDidFinishLaunching:(NSNotification *)aNotification { + NSArray *args = [[NSProcessInfo processInfo] arguments]; + + if (args!=nullptr && [args count]>=2) { + extern std::string g_resourcePath; + g_resourcePath = [[args objectAtIndex:1]UTF8String]; + } + AppDelegate app; [self createSimulator:[NSString stringWithUTF8String:"HelloLua"] viewWidth:960 viewHeight:640 factor:1.0]; Application::getInstance()->run(); diff --git a/templates/lua-template-runtime/frameworks/runtime-src/proj.win32/main.cpp b/templates/lua-template-runtime/frameworks/runtime-src/proj.win32/main.cpp index 14085d53d7..80f98b6e56 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/proj.win32/main.cpp +++ b/templates/lua-template-runtime/frameworks/runtime-src/proj.win32/main.cpp @@ -16,6 +16,21 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); + LPWSTR *szArgList=nullptr; + int argCount=0; + + szArgList = CommandLineToArgvW(GetCommandLine(),&argCount); + if (argCount >=2 ) + { + int iLen = 2*wcslen(szArgList[1]); + char* chRtn = new char[iLen+1]; + wcstombs(chRtn,szArgList[1],iLen+1); + extern std::string g_resourcePath; + g_resourcePath = chRtn; + delete [] chRtn; + } + LocalFree(szArgList); + #ifdef USE_WIN32_CONSOLE AllocConsole(); freopen("CONIN$", "r", stdin);