ConsoleCommand.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "Runtime.h"
  21. #include "ConfigParser.h"
  22. #include "ConsoleCommand.h"
  23. #include "json/document-wrapper.h"
  24. #include "json/filereadstream.h"
  25. #include "json/stringbuffer.h"
  26. #include "RuntimeProtocol.h"
  27. #include "cocos2d.h"
  28. using namespace cocos2d;
  29. ConsoleCommand* ConsoleCommand::s_sharedConsoleCommand = nullptr;
  30. ConsoleCommand* ConsoleCommand::getShareInstance()
  31. {
  32. if (s_sharedConsoleCommand == nullptr)
  33. {
  34. s_sharedConsoleCommand = new ConsoleCommand();
  35. }
  36. return s_sharedConsoleCommand;
  37. }
  38. void ConsoleCommand::purge()
  39. {
  40. if (s_sharedConsoleCommand != nullptr)
  41. {
  42. delete s_sharedConsoleCommand;
  43. }
  44. }
  45. void ConsoleCommand::init()
  46. {
  47. cocos2d::Console *_console = Director::getInstance()->getConsole();
  48. static struct Console::Command commands[] =
  49. {
  50. {"sendrequest","send command to runtime.Args[json format]",std::bind(&ConsoleCommand::onSendCommand, this, std::placeholders::_1, std::placeholders::_2)},
  51. };
  52. for (int i = 0;i< sizeof(commands) / sizeof(Console::Command);i++)
  53. {
  54. _console->addCommand(commands[i]);
  55. }
  56. // set bind address
  57. _console->setBindAddress(ConfigParser::getInstance()->getBindAddress());
  58. _console->listenOnTCP(ConfigParser::getInstance()->getConsolePort()); // 6010,6050
  59. _fileserver = FileServer::getShareInstance();
  60. _fileserver->listenOnTCP(ConfigParser::getInstance()->getUploadPort()); // 6020,6060
  61. _fileserver->readResFileFinfo();
  62. }
  63. void ConsoleCommand::onSendCommand(int fd, const std::string &args)
  64. {
  65. Director::getInstance()->getScheduler()->performFunctionInCocosThread([=](){
  66. rapidjson::Document dArgParse;
  67. dArgParse.Parse<0>(args.c_str());
  68. if (dArgParse.HasMember("cmd"))
  69. {
  70. string strcmd = dArgParse["cmd"].GetString();
  71. rapidjson::Document dReplyParse;
  72. dReplyParse.SetObject();
  73. rapidjson::Document::AllocatorType& allocator = dReplyParse.GetAllocator();
  74. dReplyParse.AddMember("cmd", rapidjson::Value(strcmd.c_str(), allocator)
  75. , allocator);
  76. if (dArgParse.HasMember("seq"))
  77. {
  78. dReplyParse.AddMember("seq",dArgParse["seq"],allocator);
  79. }
  80. if(strcmp(strcmd.c_str(), "start-logic") == 0)
  81. {
  82. auto runtime = RuntimeEngine::getInstance()->getRuntime();
  83. if (!runtime)
  84. {
  85. RuntimeEngine::getInstance()->setupRuntime();
  86. runtime = RuntimeEngine::getInstance()->getRuntime();
  87. }
  88. if (runtime) runtime->onStartDebuger(dArgParse, dReplyParse);
  89. } else if (strcmp(strcmd.c_str(),"clearcompile")==0)
  90. {
  91. auto runtime = RuntimeEngine::getInstance()->getRuntime();
  92. if (!runtime)
  93. {
  94. RuntimeEngine::getInstance()->setupRuntime();
  95. runtime = RuntimeEngine::getInstance()->getRuntime();
  96. }
  97. if (runtime) runtime->onClearCompile(dArgParse, dReplyParse);
  98. } else if(strcmp(strcmd.c_str(),"precompile")==0)
  99. {
  100. auto runtime = RuntimeEngine::getInstance()->getRuntime();
  101. if (!runtime)
  102. {
  103. RuntimeEngine::getInstance()->setupRuntime();
  104. runtime = RuntimeEngine::getInstance()->getRuntime();
  105. }
  106. if (runtime) runtime->onPrecompile(dArgParse, dReplyParse);
  107. } else if(strcmp(strcmd.c_str(), "reload") == 0)
  108. {
  109. auto runtime = RuntimeEngine::getInstance()->getRuntime();
  110. if (!runtime)
  111. {
  112. RuntimeEngine::getInstance()->setupRuntime();
  113. runtime = RuntimeEngine::getInstance()->getRuntime();
  114. }
  115. if (runtime) runtime->onReload(dArgParse, dReplyParse);
  116. } else if(strcmp(strcmd.c_str(), "getversion") == 0)
  117. {
  118. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  119. bodyvalue.AddMember("version", rapidjson::Value(getRuntimeVersion(), allocator)
  120. , allocator);
  121. dReplyParse.AddMember("body", bodyvalue, allocator);
  122. dReplyParse.AddMember("code", 0, allocator);
  123. } else if(strcmp(strcmd.c_str(), "getfileinfo") == 0)
  124. {
  125. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  126. rapidjson::Document* filecfgjson = _fileserver->getFileCfgJson();
  127. for (rapidjson::Value::MemberIterator itr = filecfgjson->MemberBegin()
  128. ; itr != filecfgjson->MemberEnd()
  129. ; ++itr)
  130. {
  131. bodyvalue.AddMember(itr->name, itr->value, allocator);
  132. }
  133. dReplyParse.AddMember("body", bodyvalue, allocator);
  134. dReplyParse.AddMember("code", 0, allocator);
  135. } else if (strcmp(strcmd.c_str(), "getEntryfile") == 0)
  136. {
  137. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  138. rapidjson::Value entryFileValue(rapidjson::kStringType);
  139. entryFileValue.SetString(ConfigParser::getInstance()->getEntryFile().c_str(), allocator);
  140. bodyvalue.AddMember("entryfile", entryFileValue, allocator);
  141. dReplyParse.AddMember("body", bodyvalue,allocator);
  142. dReplyParse.AddMember("code", 0, allocator);
  143. } else if(strcmp(strcmd.c_str(), "getIP") == 0)
  144. {
  145. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  146. rapidjson::Value IPValue(rapidjson::kStringType);
  147. IPValue.SetString(getIPAddress().c_str(), allocator);
  148. bodyvalue.AddMember("IP", IPValue,allocator);
  149. dReplyParse.AddMember("body", bodyvalue,allocator);
  150. dReplyParse.AddMember("code", 0, allocator);
  151. } else if(strcmp(strcmd.c_str(), "remove") == 0)
  152. {
  153. if (dArgParse.HasMember("files"))
  154. {
  155. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  156. const rapidjson::Value& objectfiles = dArgParse["files"];
  157. const char* filename = NULL;
  158. for (rapidjson::SizeType i = 0; i < objectfiles.Size(); i++)
  159. {
  160. filename = objectfiles[i].GetString();
  161. // remove js compiled script
  162. auto runtime = RuntimeEngine::getInstance()->getRuntime();
  163. if (runtime) runtime->onRemove(filename);
  164. // remove file from disk
  165. string filepath(_fileserver->getWritePath() + "/" + filename);
  166. if (FileUtils::getInstance()->isFileExist(filepath))
  167. {
  168. if(remove(filepath.c_str()) != 0)
  169. {
  170. // remove failed
  171. bodyvalue.AddMember(rapidjson::Value(filename, allocator)
  172. , rapidjson::Value(2)
  173. , allocator);
  174. }
  175. } else
  176. {
  177. // file not exist
  178. bodyvalue.AddMember(rapidjson::Value(filename, allocator)
  179. , rapidjson::Value(1)
  180. , allocator);
  181. }
  182. // file remove success, remove it from record
  183. if (! FileUtils::getInstance()->isFileExist(filepath))
  184. _fileserver->removeResFileInfo(filename);
  185. }
  186. dReplyParse.AddMember("body", bodyvalue, allocator);
  187. }
  188. dReplyParse.AddMember("code",0,allocator);
  189. } else if(strcmp(strcmd.c_str(), "shutdownapp") == 0)
  190. {
  191. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  192. #include <windows.h>
  193. auto glview = dynamic_cast<GLViewImpl*> (Director::getInstance()->getOpenGLView());
  194. HWND hWnd = glview->getWin32Window();
  195. ::SendMessage(hWnd, WM_CLOSE, NULL, NULL);
  196. #else
  197. exit(0);
  198. #endif
  199. } else if(strcmp(strcmd.c_str(), "getplatform") == 0)
  200. {
  201. string platform="UNKNOWN";
  202. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  203. platform = "WIN32";
  204. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  205. platform = "MAC";
  206. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  207. platform = "IOS";
  208. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  209. platform = "ANDROID";
  210. #endif
  211. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  212. rapidjson::Value platformValue(rapidjson::kStringType);
  213. platformValue.SetString(platform.c_str(), allocator);
  214. bodyvalue.AddMember("platform", platformValue, allocator);
  215. dReplyParse.AddMember("body", bodyvalue, allocator);
  216. dReplyParse.AddMember("code", 0, allocator);
  217. } else if(strcmp(strcmd.c_str(), "usewritablepath") == 0)
  218. {
  219. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  220. // only iOS and Android need to open using write path by Code IDE
  221. FileServer::getShareInstance()->setIsUsingWritePath(true);
  222. std::vector<std::string> searchPathArray = FileUtils::getInstance()->getSearchPaths();
  223. searchPathArray.insert(searchPathArray.begin(), FileServer::getShareInstance()->getWritePath());
  224. FileUtils::getInstance()->setSearchPaths(searchPathArray);
  225. #endif
  226. dReplyParse.AddMember("code", 0, allocator);
  227. }
  228. else if (strcmp(strcmd.c_str(), "workdir") == 0)
  229. {
  230. if (dArgParse.HasMember("path"))
  231. {
  232. const rapidjson::Value& objectPath = dArgParse["path"];
  233. FileUtils::getInstance()->setDefaultResourceRootPath(objectPath.GetString());
  234. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  235. bodyvalue.AddMember("path", rapidjson::Value(objectPath.GetString(), allocator)
  236. , allocator);
  237. dReplyParse.AddMember("body", bodyvalue, allocator);
  238. }
  239. dReplyParse.AddMember("code", 0, allocator);
  240. }
  241. else if (strcmp(strcmd.c_str(), "writablePath") == 0)
  242. {
  243. if (dArgParse.HasMember("path"))
  244. {
  245. const rapidjson::Value& objectPath = dArgParse["path"];
  246. FileUtils::getInstance()->setWritablePath(objectPath.GetString());
  247. rapidjson::Value bodyvalue(rapidjson::kObjectType);
  248. bodyvalue.AddMember("path", rapidjson::Value(objectPath.GetString(), allocator)
  249. , allocator);
  250. dReplyParse.AddMember("body", bodyvalue, allocator);
  251. }
  252. dReplyParse.AddMember("code", 0, allocator);
  253. }
  254. rapidjson::StringBuffer buffer;
  255. rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
  256. dReplyParse.Accept(writer);
  257. string msgContent = buffer.GetString();
  258. char msgLength[64] = {0x1, 0};
  259. sprintf(msgLength + 1, "%ld:", msgContent.size());
  260. string msg(msgLength + msgContent);
  261. sendBuf(fd, msg.c_str(), msg.size());
  262. }
  263. });
  264. }
  265. ConsoleCommand::~ConsoleCommand()
  266. {
  267. Director::getInstance()->getConsole()->stop();
  268. }