CCConsole.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCConsole.h"
  22. #include <thread>
  23. #include <algorithm>
  24. #include <functional>
  25. #include <cctype>
  26. #include <locale>
  27. #include <sstream>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <time.h>
  31. #include <fcntl.h>
  32. #if defined(_MSC_VER) || defined(__MINGW32__)
  33. #include <io.h>
  34. #include <WS2tcpip.h>
  35. #include <Winsock2.h>
  36. #if defined(__MINGW32__)
  37. #include "platform/win32/inet_pton_mingw.h"
  38. #endif
  39. #define bzero(a, b) memset(a, 0, b);
  40. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  41. #include "platform/winrt/inet_ntop_winrt.h"
  42. #include "platform/winrt/inet_pton_winrt.h"
  43. #include "platform/winrt/CCWinRTUtils.h"
  44. #endif
  45. #else
  46. #include <netdb.h>
  47. #include <unistd.h>
  48. #include <arpa/inet.h>
  49. #include <netinet/in.h>
  50. #include <sys/socket.h>
  51. #include <sys/un.h>
  52. #include <sys/ioctl.h>
  53. #endif
  54. #include "base/CCDirector.h"
  55. #include "base/CCScheduler.h"
  56. #include "platform/CCPlatformConfig.h"
  57. #include "base/CCConfiguration.h"
  58. #include "2d/CCScene.h"
  59. #include "platform/CCFileUtils.h"
  60. #include "renderer/CCTextureCache.h"
  61. #include "base/base64.h"
  62. #include "base/ccUtils.h"
  63. #include "base/allocator/CCAllocatorDiagnostics.h"
  64. NS_CC_BEGIN
  65. extern const char* cocos2dVersion(void);
  66. #define PROMPT "> "
  67. #define DEFAULT_COMMAND_SEPARATOR '|'
  68. static const size_t SEND_BUFSIZ = 512;
  69. /** private functions */
  70. namespace {
  71. #if defined(__MINGW32__)
  72. // inet
  73. const char* inet_ntop(int af, const void* src, char* dst, int cnt)
  74. {
  75. struct sockaddr_in srcaddr;
  76. memset(&srcaddr, 0, sizeof(struct sockaddr_in));
  77. memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
  78. srcaddr.sin_family = af;
  79. if (WSAAddressToStringA((struct sockaddr*) &srcaddr, sizeof(struct sockaddr_in), 0, dst, (LPDWORD) &cnt) != 0)
  80. {
  81. return nullptr;
  82. }
  83. return dst;
  84. }
  85. #endif
  86. //
  87. // Free functions to log
  88. //
  89. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  90. void SendLogToWindow(const char *log)
  91. {
  92. static const int CCLOG_STRING_TAG = 1;
  93. // Send data as a message
  94. COPYDATASTRUCT myCDS;
  95. myCDS.dwData = CCLOG_STRING_TAG;
  96. myCDS.cbData = (DWORD)strlen(log) + 1;
  97. myCDS.lpData = (PVOID)log;
  98. if (Director::getInstance()->getOpenGLView())
  99. {
  100. HWND hwnd = Director::getInstance()->getOpenGLView()->getWin32Window();
  101. SendMessage(hwnd,
  102. WM_COPYDATA,
  103. (WPARAM)(HWND)hwnd,
  104. (LPARAM)(LPVOID)&myCDS);
  105. }
  106. }
  107. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  108. void SendLogToWindow(const char *log)
  109. {
  110. }
  111. #endif
  112. }
  113. void log(const char * format, ...)
  114. {
  115. int bufferSize = MAX_LOG_LENGTH;
  116. char* buf = nullptr;
  117. int nret = 0;
  118. va_list args;
  119. do
  120. {
  121. buf = new (std::nothrow) char[bufferSize];
  122. if (buf == nullptr)
  123. return;
  124. /*
  125. pitfall: The behavior of vsnprintf between VS2013 and VS2015/2017 is different
  126. VS2013 or Unix-Like System will return -1 when buffer not enough, but VS2015/2017 will return the actural needed length for buffer at this station
  127. The _vsnprintf behavior is compatible API which always return -1 when buffer isn't enough at VS2013/2015/2017
  128. Yes, The vsnprintf is more efficient implemented by MSVC 19.0 or later, AND it's also standard-compliant, see reference: http://www.cplusplus.com/reference/cstdio/vsnprintf/
  129. */
  130. va_start(args, format);
  131. nret = vsnprintf(buf, bufferSize - 3, format, args);
  132. va_end(args);
  133. if (nret >= 0)
  134. { // VS2015/2017
  135. if (nret <= bufferSize - 3)
  136. {// success, so don't need to realloc
  137. break;
  138. }
  139. else
  140. {
  141. bufferSize = nret + 3;
  142. delete[] buf;
  143. }
  144. }
  145. else // < 0
  146. { // VS2013 or Unix-like System(GCC)
  147. bufferSize *= 2;
  148. delete[] buf;
  149. }
  150. } while (true);
  151. buf[nret] = '\n';
  152. buf[++nret] = '\0';
  153. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  154. __android_log_print(ANDROID_LOG_DEBUG, "cocos2d-x debug info", "%s", buf);
  155. #elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  156. int pos = 0;
  157. int len = nret;
  158. char tempBuf[MAX_LOG_LENGTH + 1] = { 0 };
  159. WCHAR wszBuf[MAX_LOG_LENGTH + 1] = { 0 };
  160. do
  161. {
  162. std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
  163. tempBuf[MAX_LOG_LENGTH] = 0;
  164. MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
  165. OutputDebugStringW(wszBuf);
  166. WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, tempBuf, sizeof(tempBuf), nullptr, FALSE);
  167. printf("%s", tempBuf);
  168. pos += MAX_LOG_LENGTH;
  169. } while (pos < len);
  170. SendLogToWindow(buf);
  171. fflush(stdout);
  172. #else
  173. // Linux, Mac, iOS, etc
  174. fprintf(stdout, "%s", buf);
  175. fflush(stdout);
  176. #endif
  177. Director::getInstance()->getConsole()->log(buf);
  178. delete[] buf;
  179. }
  180. // FIXME: Deprecated
  181. // void CCLog(const char * format, ...);
  182. //
  183. // Utility code
  184. //
  185. std::string Console::Utility::_prompt(PROMPT);
  186. //TODO: these general utils should be in a separate class
  187. //
  188. // Trimming functions were taken from: http://stackoverflow.com/a/217605
  189. // Since c++17, some parts of the standard library were removed, include "ptr_fun".
  190. // trim from start
  191. std::string& Console::Utility::ltrim(std::string& s) {
  192. s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
  193. return !std::isspace(ch);
  194. }));
  195. return s;
  196. }
  197. // trim from end
  198. std::string& Console::Utility::rtrim(std::string& s) {
  199. s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
  200. return !std::isspace(ch);
  201. }).base(), s.end());
  202. return s;
  203. }
  204. // trim from both ends
  205. std::string& Console::Utility::trim(std::string& s) {
  206. return Console::Utility::ltrim(Console::Utility::rtrim(s));
  207. }
  208. std::vector<std::string>& Console::Utility::split(const std::string& s, char delim, std::vector<std::string>& elems) {
  209. std::stringstream ss(s);
  210. std::string item;
  211. while (std::getline(ss, item, delim)) {
  212. elems.push_back(item);
  213. }
  214. return elems;
  215. }
  216. std::vector<std::string> Console::Utility::split(const std::string& s, char delim) {
  217. std::vector<std::string> elems;
  218. Console::Utility::split(s, delim, elems);
  219. return elems;
  220. }
  221. //isFloat taken from http://stackoverflow.com/questions/447206/c-isfloat-function
  222. bool Console::Utility::isFloat(const std::string& myString) {
  223. std::istringstream iss(myString);
  224. float f;
  225. iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid
  226. // Check the entire string was consumed and if either failbit or badbit is set
  227. return iss.eof() && !iss.fail();
  228. }
  229. ssize_t Console::Utility::sendToConsole(int fd, const void* buffer, size_t length, int flags)
  230. {
  231. if (_prompt.length() == length) {
  232. if (strncmp(_prompt.c_str(), static_cast<const char*>(buffer), length) == 0) {
  233. fprintf(stderr,"bad parameter error: a buffer is the prompt string.\n");
  234. return 0;
  235. }
  236. }
  237. const char* buf = static_cast<const char*>(buffer);
  238. ssize_t retLen = 0;
  239. for (size_t i = 0; i < length; ) {
  240. size_t len = length - i;
  241. if (SEND_BUFSIZ < len) len = SEND_BUFSIZ;
  242. retLen += send(fd, buf + i, len, flags);
  243. i += len;
  244. }
  245. return retLen;
  246. }
  247. // dprintf() is not defined in Android
  248. // so we add our own 'dpritnf'
  249. ssize_t Console::Utility::mydprintf(int sock, const char *format, ...)
  250. {
  251. va_list args;
  252. char buf[16386];
  253. va_start(args, format);
  254. vsnprintf(buf, sizeof(buf), format, args);
  255. va_end(args);
  256. return sendToConsole(sock, buf, strlen(buf));
  257. }
  258. void Console::Utility::sendPrompt(int fd)
  259. {
  260. const char* prompt = _prompt.c_str();
  261. send(fd, prompt, strlen(prompt), 0);
  262. }
  263. void Console::Utility::setPrompt(const std::string &prompt)
  264. {
  265. _prompt = prompt;
  266. }
  267. const std::string& Console::Utility::getPrompt()
  268. {
  269. return _prompt;
  270. }
  271. //
  272. // Command code
  273. //
  274. Console::Command::Command()
  275. : _callback(nullptr)
  276. {
  277. }
  278. Console::Command::Command(const std::string& name, const std::string& help)
  279. : _name(name)
  280. , _help(help)
  281. , _callback(nullptr)
  282. {
  283. }
  284. Console::Command::Command(const std::string& name, const std::string& help, const Callback& callback)
  285. : _name(name)
  286. , _help(help)
  287. , _callback(callback)
  288. {
  289. }
  290. Console::Command::Command(const Command& o)
  291. {
  292. *this = o;
  293. }
  294. Console::Command::Command(Command&& o)
  295. {
  296. *this = std::move(o);
  297. }
  298. Console::Command::~Command()
  299. {
  300. for (const auto& e : _subCommands)
  301. {
  302. delete e.second;
  303. }
  304. }
  305. Console::Command& Console::Command::operator=(const Command& o)
  306. {
  307. if (this != &o)
  308. {
  309. _name = o._name;
  310. _help = o._help;
  311. _callback = o._callback;
  312. for (const auto& e : _subCommands)
  313. delete e.second;
  314. _subCommands.clear();
  315. for (const auto& e : o._subCommands)
  316. {
  317. Command* subCommand = e.second;
  318. auto newCommand = new (std::nothrow) Command(*subCommand);
  319. _subCommands[e.first] = newCommand;
  320. }
  321. }
  322. return *this;
  323. }
  324. Console::Command& Console::Command::operator=(Command&& o)
  325. {
  326. if (this != &o)
  327. {
  328. _name = std::move(o._name);
  329. _help = std::move(o._help);
  330. _callback = std::move(o._callback);
  331. o._callback = nullptr;
  332. for (const auto& e : _subCommands)
  333. delete e.second;
  334. _subCommands.clear();
  335. _subCommands = std::move(o._subCommands);
  336. }
  337. return *this;
  338. }
  339. void Console::Command::addCallback(const Callback& callback)
  340. {
  341. _callback = callback;
  342. }
  343. void Console::Command::addSubCommand(const Command& subCmd)
  344. {
  345. auto iter = _subCommands.find(subCmd._name);
  346. if (iter != _subCommands.end())
  347. {
  348. delete iter->second;
  349. _subCommands.erase(iter);
  350. }
  351. Command* cmd = new (std::nothrow) Command();
  352. *cmd = subCmd;
  353. _subCommands[subCmd._name] = cmd;
  354. }
  355. const Console::Command* Console::Command::getSubCommand(const std::string& subCmdName) const
  356. {
  357. auto it = _subCommands.find(subCmdName);
  358. if(it != _subCommands.end()) {
  359. auto& subCmd = it->second;
  360. return subCmd;
  361. }
  362. return nullptr;
  363. }
  364. void Console::Command::delSubCommand(const std::string& subCmdName)
  365. {
  366. auto iter = _subCommands.find(subCmdName);
  367. if (iter != _subCommands.end()) {
  368. delete iter->second;
  369. _subCommands.erase(iter);
  370. }
  371. }
  372. void Console::Command::commandHelp(int fd, const std::string& /*args*/)
  373. {
  374. if (! _help.empty()) {
  375. Console::Utility::mydprintf(fd, "%s\n", _help.c_str());
  376. }
  377. if (! _subCommands.empty()) {
  378. sendHelp(fd, _subCommands, "");
  379. }
  380. }
  381. void Console::Command::commandGeneric(int fd, const std::string& args)
  382. {
  383. // The first argument (including the empty)
  384. std::string key(args);
  385. auto pos = args.find(" ");
  386. if ((pos != std::string::npos) && (0 < pos)) {
  387. key = args.substr(0, pos);
  388. }
  389. // help
  390. if (key == "help" || key == "-h") {
  391. commandHelp(fd, args);
  392. return;
  393. }
  394. // find sub command
  395. auto it = _subCommands.find(key);
  396. if (it != _subCommands.end()) {
  397. auto subCmd = it->second;
  398. if (subCmd->_callback) {
  399. subCmd->_callback(fd, args);
  400. }
  401. return;
  402. }
  403. // can not find
  404. if (_callback) {
  405. _callback(fd, args);
  406. }
  407. }
  408. //
  409. // Console code
  410. //
  411. Console::Console()
  412. : _listenfd(-1)
  413. , _running(false)
  414. , _endThread(false)
  415. , _isIpv6Server(false)
  416. , _sendDebugStrings(false)
  417. , _bindAddress("")
  418. , _commandSeparator(DEFAULT_COMMAND_SEPARATOR)
  419. {
  420. createCommandAllocator();
  421. createCommandConfig();
  422. createCommandDebugMsg();
  423. createCommandDirector();
  424. createCommandExit();
  425. createCommandFileUtils();
  426. createCommandFps();
  427. createCommandHelp();
  428. createCommandProjection();
  429. createCommandResolution();
  430. createCommandSceneGraph();
  431. createCommandTexture();
  432. createCommandTouch();
  433. createCommandUpload();
  434. createCommandVersion();
  435. }
  436. Console::~Console()
  437. {
  438. stop();
  439. for (auto& e : _commands)
  440. delete e.second;
  441. }
  442. bool Console::listenOnTCP(int port)
  443. {
  444. int listenfd = -1, n;
  445. const int on = 1;
  446. struct addrinfo hints, *res, *ressave;
  447. char serv[30];
  448. snprintf(serv, sizeof(serv)-1, "%d", port );
  449. bzero(&hints, sizeof(struct addrinfo));
  450. hints.ai_flags = AI_PASSIVE;
  451. hints.ai_family = AF_UNSPEC;
  452. hints.ai_socktype = SOCK_STREAM;
  453. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  454. WSADATA wsaData;
  455. n = WSAStartup(MAKEWORD(2, 2),&wsaData);
  456. #endif
  457. if ( (n = getaddrinfo(nullptr, serv, &hints, &res)) != 0) {
  458. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  459. fprintf(stderr, "net_listen error for %s: %s", serv, gai_strerrorA(n));
  460. #else
  461. fprintf(stderr,"net_listen error for %s: %s", serv, gai_strerror(n));
  462. #endif
  463. return false;
  464. }
  465. ressave = res;
  466. do {
  467. listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  468. if (listenfd < 0)
  469. continue; /* error, try next one */
  470. setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
  471. // bind address
  472. if (!_bindAddress.empty())
  473. {
  474. if (res->ai_family == AF_INET)
  475. {
  476. struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
  477. inet_pton(res->ai_family, _bindAddress.c_str(), (void*)&sin->sin_addr);
  478. }
  479. else if (res->ai_family == AF_INET6)
  480. {
  481. struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
  482. inet_pton(res->ai_family, _bindAddress.c_str(), (void*)&sin->sin6_addr);
  483. }
  484. }
  485. if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
  486. break; /* success */
  487. /* bind error, close and try next one */
  488. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  489. closesocket(listenfd);
  490. #else
  491. close(listenfd);
  492. #endif
  493. } while ( (res = res->ai_next) != nullptr);
  494. if (res == nullptr) {
  495. perror("net_listen:");
  496. freeaddrinfo(ressave);
  497. return false;
  498. }
  499. listen(listenfd, 50);
  500. if (res->ai_family == AF_INET) {
  501. _isIpv6Server = false;
  502. char buf[INET_ADDRSTRLEN] = {0};
  503. struct sockaddr_in *sin = (struct sockaddr_in*) res->ai_addr;
  504. if( inet_ntop(res->ai_family, &sin->sin_addr, buf, sizeof(buf)) != nullptr )
  505. cocos2d::log("Console: IPV4 server is listening on %s:%d", buf, ntohs(sin->sin_port));
  506. else
  507. perror("inet_ntop");
  508. } else if (res->ai_family == AF_INET6) {
  509. _isIpv6Server = true;
  510. char buf[INET6_ADDRSTRLEN] = {0};
  511. struct sockaddr_in6 *sin = (struct sockaddr_in6*) res->ai_addr;
  512. if( inet_ntop(res->ai_family, &sin->sin6_addr, buf, sizeof(buf)) != nullptr )
  513. cocos2d::log("Console: IPV6 server is listening on [%s]:%d", buf, ntohs(sin->sin6_port));
  514. else
  515. perror("inet_ntop");
  516. }
  517. freeaddrinfo(ressave);
  518. return listenOnFileDescriptor(listenfd);
  519. }
  520. bool Console::listenOnFileDescriptor(int fd)
  521. {
  522. if(_running) {
  523. cocos2d::log("Console already started. 'stop' it before calling 'listen' again");
  524. return false;
  525. }
  526. _listenfd = fd;
  527. _thread = std::thread( std::bind( &Console::loop, this) );
  528. return true;
  529. }
  530. void Console::stop()
  531. {
  532. if( _running ) {
  533. _endThread = true;
  534. if (_thread.joinable())
  535. {
  536. _thread.join();
  537. }
  538. }
  539. }
  540. void Console::addCommand(const Command& cmd)
  541. {
  542. Command* newCommand = new (std::nothrow) Command(cmd);
  543. auto iter = _commands.find(cmd.getName());
  544. if (iter != _commands.end())
  545. {
  546. delete iter->second;
  547. _commands.erase(iter);
  548. }
  549. _commands[cmd.getName()] = newCommand;
  550. }
  551. void Console::addSubCommand(const std::string& cmdName, const Command& subCmd)
  552. {
  553. auto it = _commands.find(cmdName);
  554. if (it != _commands.end())
  555. {
  556. auto& cmd = it->second;
  557. addSubCommand(*cmd, subCmd);
  558. }
  559. }
  560. void Console::addSubCommand(Command& cmd, const Command& subCmd)
  561. {
  562. cmd.addSubCommand(subCmd);
  563. }
  564. const Console::Command* Console::getCommand(const std::string& cmdName)
  565. {
  566. auto it = _commands.find(cmdName);
  567. if(it != _commands.end()) {
  568. auto& cmd = it->second;
  569. return cmd;
  570. }
  571. return nullptr;
  572. }
  573. const Console::Command* Console::getSubCommand(const std::string& cmdName, const std::string& subCmdName)
  574. {
  575. auto it = _commands.find(cmdName);
  576. if(it != _commands.end()) {
  577. auto& cmd = it->second;
  578. return getSubCommand(*cmd, subCmdName);
  579. }
  580. return nullptr;
  581. }
  582. const Console::Command* Console::getSubCommand(const Command& cmd, const std::string& subCmdName)
  583. {
  584. return cmd.getSubCommand(subCmdName);
  585. }
  586. void Console::delCommand(const std::string& cmdName)
  587. {
  588. auto it = _commands.find(cmdName);
  589. if(it != _commands.end()) {
  590. delete it->second;
  591. _commands.erase(it);
  592. }
  593. }
  594. void Console::delSubCommand(const std::string& cmdName, const std::string& subCmdName)
  595. {
  596. auto it = _commands.find(cmdName);
  597. if(it != _commands.end()) {
  598. auto& cmd = it->second;
  599. delSubCommand(*cmd, subCmdName);
  600. }
  601. }
  602. void Console::delSubCommand(Command& cmd, const std::string& subCmdName)
  603. {
  604. cmd.delSubCommand(subCmdName);
  605. }
  606. void Console::log(const char* buf)
  607. {
  608. if( _sendDebugStrings ) {
  609. _DebugStringsMutex.lock();
  610. _DebugStrings.push_back(buf);
  611. _DebugStringsMutex.unlock();
  612. }
  613. }
  614. void Console::setBindAddress(const std::string &address)
  615. {
  616. _bindAddress = address;
  617. }
  618. bool Console::isIpv6Server() const
  619. {
  620. return _isIpv6Server;
  621. }
  622. //
  623. // Main Loop
  624. //
  625. void Console::loop()
  626. {
  627. fd_set copy_set;
  628. struct timeval timeout, timeout_copy;
  629. _running = true;
  630. FD_ZERO(&_read_set);
  631. FD_SET(_listenfd, &_read_set);
  632. _maxfd = _listenfd;
  633. timeout.tv_sec = 0;
  634. /* 0.016 seconds. Wake up once per frame at 60PFS */
  635. timeout.tv_usec = 16000;
  636. while(!_endThread) {
  637. copy_set = _read_set;
  638. timeout_copy = timeout;
  639. int nready = select(_maxfd+1, &copy_set, nullptr, nullptr, &timeout_copy);
  640. if( nready == -1 )
  641. {
  642. /* error */
  643. if(errno != EINTR)
  644. cocos2d::log("Abnormal error in select()\n");
  645. continue;
  646. }
  647. else if( nready == 0 )
  648. {
  649. /* timeout. do something ? */
  650. }
  651. else
  652. {
  653. /* new client */
  654. if(FD_ISSET(_listenfd, &copy_set)) {
  655. addClient();
  656. if(--nready <= 0)
  657. continue;
  658. }
  659. /* data from client */
  660. std::vector<int> to_remove;
  661. for(const auto &fd: _fds) {
  662. if(FD_ISSET(fd,&copy_set))
  663. {
  664. //fix Bug #4302 Test case ConsoleTest--ConsoleUploadFile crashed on Linux
  665. //On linux, if you send data to a closed socket, the sending process will
  666. //receive a SIGPIPE, which will cause linux system shutdown the sending process.
  667. //Add this ioctl code to check if the socket has been closed by peer.
  668. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  669. u_long n = 0;
  670. ioctlsocket(fd, FIONREAD, &n);
  671. #else
  672. int n = 0;
  673. ioctl(fd, FIONREAD, &n);
  674. #endif
  675. if(n == 0)
  676. {
  677. //no data received, or fd is closed
  678. continue;
  679. }
  680. if( ! parseCommand(fd) )
  681. {
  682. to_remove.push_back(fd);
  683. }
  684. if(--nready <= 0)
  685. break;
  686. }
  687. }
  688. /* remove closed connections */
  689. for(int fd: to_remove) {
  690. FD_CLR(fd, &_read_set);
  691. _fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
  692. }
  693. }
  694. /* Any message for the remote console ? send it! */
  695. if( !_DebugStrings.empty() ) {
  696. if (_DebugStringsMutex.try_lock())
  697. {
  698. for (const auto &str : _DebugStrings) {
  699. for (auto fd : _fds) {
  700. Console::Utility::sendToConsole(fd, str.c_str(), str.length());
  701. }
  702. }
  703. _DebugStrings.clear();
  704. _DebugStringsMutex.unlock();
  705. }
  706. }
  707. }
  708. // clean up: ignore stdin, stdout and stderr
  709. for(const auto &fd: _fds )
  710. {
  711. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  712. closesocket(fd);
  713. #else
  714. close(fd);
  715. #endif
  716. }
  717. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  718. closesocket(_listenfd);
  719. WSACleanup();
  720. #else
  721. close(_listenfd);
  722. #endif
  723. _running = false;
  724. }
  725. //
  726. // Helpers
  727. //
  728. ssize_t Console::readline(int fd, char* ptr, size_t maxlen)
  729. {
  730. size_t n, rc;
  731. char c;
  732. for( n = 0; n < maxlen - 1; n++ ) {
  733. if( (rc = recv(fd, &c, 1, 0)) ==1 ) {
  734. *ptr++ = c;
  735. if(c == '\n') {
  736. break;
  737. }
  738. } else if( rc == 0 ) {
  739. return 0;
  740. } else if( errno == EINTR ) {
  741. continue;
  742. } else {
  743. return -1;
  744. }
  745. }
  746. *ptr = 0;
  747. return n;
  748. }
  749. ssize_t Console::readBytes(int fd, char* buffer, size_t maxlen, bool* more)
  750. {
  751. size_t n, rc;
  752. char c, *ptr = buffer;
  753. *more = false;
  754. for( n = 0; n < maxlen; n++ ) {
  755. if( (rc = recv(fd, &c, 1, 0)) ==1 ) {
  756. *ptr++ = c;
  757. if(c == '\n') {
  758. return n;
  759. }
  760. } else if( rc == 0 ) {
  761. return 0;
  762. } else if( errno == EINTR ) {
  763. continue;
  764. } else {
  765. return -1;
  766. }
  767. }
  768. *more = true;
  769. return n;
  770. }
  771. bool Console::parseCommand(int fd)
  772. {
  773. char buf[512];
  774. bool more_data;
  775. auto h = readBytes(fd, buf, 6, &more_data);
  776. if( h < 0)
  777. {
  778. return false;
  779. }
  780. if(strncmp(buf, "upload", 6) == 0)
  781. {
  782. char c = '\0';
  783. recv(fd, &c, 1, 0);
  784. if(c == ' ')
  785. {
  786. commandUpload(fd);
  787. Console::Utility::sendPrompt(fd);
  788. return true;
  789. }
  790. else
  791. {
  792. const char err[] = "upload: invalid args! Type 'help' for options\n";
  793. Console::Utility::sendToConsole(fd, err, strlen(err));
  794. Console::Utility::sendPrompt(fd);
  795. return true;
  796. }
  797. }
  798. if(!more_data)
  799. {
  800. buf[h] = 0;
  801. }
  802. else
  803. {
  804. char *pb = buf + 6;
  805. auto r = readline(fd, pb, sizeof(buf)-6);
  806. if(r < 0)
  807. {
  808. const char err[] = "Unknown error!\n";
  809. Console::Utility::sendPrompt(fd);
  810. Console::Utility::sendToConsole(fd, err, strlen(err));
  811. return false;
  812. }
  813. }
  814. std::string cmdLine;
  815. cmdLine = std::string(buf);
  816. auto commands = Console::Utility::split(cmdLine, _commandSeparator);
  817. try {
  818. for(auto command : commands) {
  819. performCommand(fd, Console::Utility::trim(command));
  820. }
  821. } catch (const std::runtime_error& e) {
  822. Console::Utility::sendToConsole(fd, e.what(), strlen(e.what()));
  823. }
  824. Console::Utility::sendPrompt(fd);
  825. return true;
  826. }
  827. void Console::performCommand(int fd, const std::string& command) {
  828. std::vector<std::string> args = Console::Utility::split(command, ' ');
  829. if(args.empty()) {
  830. throw std::runtime_error("Unknown command. Type 'help' for options\n");
  831. }
  832. auto it = _commands.find(Console::Utility::trim(args[0]));
  833. if(it != _commands.end())
  834. {
  835. std::string args2;
  836. for(size_t i = 1; i < args.size(); ++i)
  837. {
  838. if(i > 1)
  839. {
  840. args2 += ' ';
  841. }
  842. args2 += Console::Utility::trim(args[i]);
  843. }
  844. auto cmd = it->second;
  845. cmd->commandGeneric(fd, args2);
  846. } else {
  847. throw std::runtime_error("Unknown command " + command + ". Type 'help' for options\n");
  848. }
  849. }
  850. void Console::addClient()
  851. {
  852. struct sockaddr_in6 ipv6Addr;
  853. struct sockaddr_in ipv4Addr;
  854. struct sockaddr* addr = _isIpv6Server ? (struct sockaddr*)&ipv6Addr : (struct sockaddr*)&ipv4Addr;
  855. socklen_t addrLen = _isIpv6Server ? sizeof(ipv6Addr) : sizeof(ipv4Addr);
  856. /* new client */
  857. int fd = accept(_listenfd, addr, &addrLen);
  858. // add fd to list of FD
  859. if( fd != -1 ) {
  860. FD_SET(fd, &_read_set);
  861. _fds.push_back(fd);
  862. _maxfd = std::max(_maxfd,fd);
  863. Console::Utility::sendPrompt(fd);
  864. /**
  865. * A SIGPIPE is sent to a process if it tried to write to socket that had been shutdown for
  866. * writing or isn't connected (anymore) on iOS.
  867. *
  868. * The default behaviour for this signal is to end the process.So we make the process ignore SIGPIPE.
  869. */
  870. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  871. int set = 1;
  872. setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
  873. #endif
  874. }
  875. }
  876. //
  877. // create commands
  878. //
  879. void Console::createCommandAllocator()
  880. {
  881. addCommand({"allocator", "Display allocator diagnostics for all allocators. Args: [-h | help | ]",
  882. CC_CALLBACK_2(Console::commandAllocator, this)});
  883. }
  884. void Console::createCommandConfig()
  885. {
  886. addCommand({"config", "Print the Configuration object. Args: [-h | help | ]",
  887. CC_CALLBACK_2(Console::commandConfig, this)});
  888. }
  889. void Console::createCommandDebugMsg()
  890. {
  891. addCommand({"debugmsg", "Whether or not to forward the debug messages on the console. Args: [-h | help | on | off | ]",
  892. CC_CALLBACK_2(Console::commandDebugMsg, this)});
  893. addSubCommand("debugmsg", {"on", "enable debug logging", CC_CALLBACK_2(Console::commandDebugMsgSubCommandOnOff, this)});
  894. addSubCommand("debugmsg", {"off", "disable debug logging", CC_CALLBACK_2(Console::commandDebugMsgSubCommandOnOff, this)});
  895. }
  896. void Console::createCommandDirector()
  897. {
  898. addCommand({"director", "director commands, type -h or [director help] to list supported directives"});
  899. addSubCommand("director", {"pause", "pause all scheduled timers, the draw rate will be 4 FPS to reduce CPU consumption",
  900. CC_CALLBACK_2(Console::commandDirectorSubCommandPause, this)});
  901. addSubCommand("director", {"resume", "resume all scheduled timers",
  902. CC_CALLBACK_2(Console::commandDirectorSubCommandResume, this)});
  903. addSubCommand("director", {"stop", "Stops the animation. Nothing will be drawn.",
  904. CC_CALLBACK_2(Console::commandDirectorSubCommandStop, this)});
  905. addSubCommand("director", {"start", "Restart the animation again, Call this function only if [director stop] was called earlier",
  906. CC_CALLBACK_2(Console::commandDirectorSubCommandStart, this)});
  907. addSubCommand("director", {"end", "exit this app.",
  908. CC_CALLBACK_2(Console::commandDirectorSubCommandEnd, this)});
  909. }
  910. void Console::createCommandExit()
  911. {
  912. addCommand({"exit", "Close connection to the console. Args: [-h | help | ]", CC_CALLBACK_2(Console::commandExit, this)});
  913. }
  914. void Console::createCommandFileUtils()
  915. {
  916. addCommand({"fileutils", "Flush or print the FileUtils info. Args: [-h | help | flush | ]",
  917. CC_CALLBACK_2(Console::commandFileUtils, this)});
  918. addSubCommand("fileutils", {"flush", "Purges the file searching cache.",
  919. CC_CALLBACK_2(Console::commandFileUtilsSubCommandFlush, this)});
  920. }
  921. void Console::createCommandFps()
  922. {
  923. addCommand({"fps", "Turn on / off the FPS. Args: [-h | help | on | off | ]", CC_CALLBACK_2(Console::commandFps, this)});
  924. addSubCommand("fps", {"on", "Display the FPS on the bottom-left corner.", CC_CALLBACK_2(Console::commandFpsSubCommandOnOff, this)});
  925. addSubCommand("fps", {"off", "Hide the FPS on the bottom-left corner.", CC_CALLBACK_2(Console::commandFpsSubCommandOnOff, this)});
  926. }
  927. void Console::createCommandHelp()
  928. {
  929. addCommand({"help", "Print this message. Args: [ ]", CC_CALLBACK_2(Console::commandHelp, this)});
  930. }
  931. void Console::createCommandProjection()
  932. {
  933. addCommand({"projection", "Change or print the current projection. Args: [-h | help | 2d | 3d | ]",
  934. CC_CALLBACK_2(Console::commandProjection, this)});
  935. addSubCommand("projection", {"2d", "sets a 2D projection (orthogonal projection).",
  936. CC_CALLBACK_2(Console::commandProjectionSubCommand2d, this)});
  937. addSubCommand("projection", {"3d", "sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500.",
  938. CC_CALLBACK_2(Console::commandProjectionSubCommand3d, this)});
  939. }
  940. void Console::createCommandResolution()
  941. {
  942. addCommand({"resolution", "Change or print the window resolution. Args: [-h | help | width height resolution_policy | ]",
  943. CC_CALLBACK_2(Console::commandResolution, this)});
  944. addSubCommand("resolution", {"", "", CC_CALLBACK_2(Console::commandResolutionSubCommandEmpty, this)});
  945. }
  946. void Console::createCommandSceneGraph()
  947. {
  948. addCommand({"scenegraph", "Print the scene graph", CC_CALLBACK_2(Console::commandSceneGraph, this)});
  949. }
  950. void Console::createCommandTexture()
  951. {
  952. addCommand({"texture", "Flush or print the TextureCache info. Args: [-h | help | flush | ] ",
  953. CC_CALLBACK_2(Console::commandTextures, this)});
  954. addSubCommand("texture", {"flush", "Purges the dictionary of loaded textures.",
  955. CC_CALLBACK_2(Console::commandTexturesSubCommandFlush, this)});
  956. }
  957. void Console::createCommandTouch()
  958. {
  959. addCommand({"touch", "simulate touch event via console, type -h or [touch help] to list supported directives"});
  960. addSubCommand("touch", {"tap", "touch tap x y: simulate touch tap at (x,y).",
  961. CC_CALLBACK_2(Console::commandTouchSubCommandTap, this)});
  962. addSubCommand("touch", {"swipe", "touch swipe x1 y1 x2 y2: simulate touch swipe from (x1,y1) to (x2,y2).",
  963. CC_CALLBACK_2(Console::commandTouchSubCommandSwipe, this)});
  964. }
  965. void Console::createCommandUpload()
  966. {
  967. addCommand({"upload", "upload file. Args: [filename base64_encoded_data]", CC_CALLBACK_1(Console::commandUpload, this)});
  968. }
  969. void Console::createCommandVersion()
  970. {
  971. addCommand({"version", "print version string ", CC_CALLBACK_2(Console::commandVersion, this)});
  972. }
  973. //
  974. // commands
  975. //
  976. void Console::commandAllocator(int fd, const std::string& /*args*/)
  977. {
  978. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  979. auto info = allocator::AllocatorDiagnostics::instance()->diagnostics();
  980. Console::Utility::mydprintf(fd, info.c_str());
  981. #else
  982. Console::Utility::mydprintf(fd, "allocator diagnostics not available. CC_ENABLE_ALLOCATOR_DIAGNOSTICS must be set to 1 in ccConfig.h\n");
  983. #endif
  984. }
  985. void Console::commandConfig(int fd, const std::string& /*args*/)
  986. {
  987. Scheduler *sched = Director::getInstance()->getScheduler();
  988. sched->performFunctionInCocosThread( [=](){
  989. Console::Utility::mydprintf(fd, "%s", Configuration::getInstance()->getInfo().c_str());
  990. Console::Utility::sendPrompt(fd);
  991. });
  992. }
  993. void Console::commandDebugMsg(int fd, const std::string& /*args*/)
  994. {
  995. Console::Utility::mydprintf(fd, "Debug message is: %s\n", _sendDebugStrings ? "on" : "off");
  996. }
  997. void Console::commandDebugMsgSubCommandOnOff(int /*fd*/, const std::string& args)
  998. {
  999. _sendDebugStrings = (args.compare("on") == 0);
  1000. }
  1001. void Console::commandDirectorSubCommandPause(int /*fd*/, const std::string& /*args*/)
  1002. {
  1003. auto director = Director::getInstance();
  1004. Scheduler *sched = director->getScheduler();
  1005. sched->performFunctionInCocosThread( [](){
  1006. Director::getInstance()->pause();
  1007. });
  1008. }
  1009. void Console::commandDirectorSubCommandResume(int /*fd*/, const std::string& /*args*/)
  1010. {
  1011. auto director = Director::getInstance();
  1012. director->resume();
  1013. }
  1014. void Console::commandDirectorSubCommandStop(int /*fd*/, const std::string& /*args*/)
  1015. {
  1016. auto director = Director::getInstance();
  1017. Scheduler *sched = director->getScheduler();
  1018. sched->performFunctionInCocosThread( [](){
  1019. Director::getInstance()->stopAnimation();
  1020. });
  1021. }
  1022. void Console::commandDirectorSubCommandStart(int /*fd*/, const std::string& /*args*/)
  1023. {
  1024. auto director = Director::getInstance();
  1025. director->startAnimation();
  1026. }
  1027. void Console::commandDirectorSubCommandEnd(int /*fd*/, const std::string& /*args*/)
  1028. {
  1029. auto director = Director::getInstance();
  1030. director->end();
  1031. }
  1032. void Console::commandExit(int fd, const std::string& /*args*/)
  1033. {
  1034. FD_CLR(fd, &_read_set);
  1035. _fds.erase(std::remove(_fds.begin(), _fds.end(), fd), _fds.end());
  1036. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  1037. closesocket(fd);
  1038. #else
  1039. close(fd);
  1040. #endif
  1041. }
  1042. void Console::commandFileUtils(int fd, const std::string& /*args*/)
  1043. {
  1044. Scheduler *sched = Director::getInstance()->getScheduler();
  1045. sched->performFunctionInCocosThread( std::bind(&Console::printFileUtils, this, fd) );
  1046. }
  1047. void Console::commandFileUtilsSubCommandFlush(int /*fd*/, const std::string& /*args*/)
  1048. {
  1049. FileUtils::getInstance()->purgeCachedEntries();
  1050. }
  1051. void Console::commandFps(int fd, const std::string& /*args*/)
  1052. {
  1053. Console::Utility::mydprintf(fd, "FPS is: %s\n", Director::getInstance()->isDisplayStats() ? "on" : "off");
  1054. }
  1055. void Console::commandFpsSubCommandOnOff(int /*fd*/, const std::string& args)
  1056. {
  1057. bool state = (args.compare("on") == 0);
  1058. Director *dir = Director::getInstance();
  1059. Scheduler *sched = dir->getScheduler();
  1060. sched->performFunctionInCocosThread( std::bind(&Director::setDisplayStats, dir, state));
  1061. }
  1062. void Console::commandHelp(int fd, const std::string& /*args*/)
  1063. {
  1064. sendHelp(fd, _commands, "\nAvailable commands:\n");
  1065. }
  1066. void Console::commandProjection(int fd, const std::string& /*args*/)
  1067. {
  1068. auto director = Director::getInstance();
  1069. char buf[20];
  1070. auto proj = director->getProjection();
  1071. switch (proj) {
  1072. case cocos2d::Director::Projection::_2D:
  1073. sprintf(buf,"2d");
  1074. break;
  1075. case cocos2d::Director::Projection::_3D:
  1076. sprintf(buf,"3d");
  1077. break;
  1078. case cocos2d::Director::Projection::CUSTOM:
  1079. sprintf(buf,"custom");
  1080. break;
  1081. default:
  1082. sprintf(buf,"unknown");
  1083. break;
  1084. }
  1085. Console::Utility::mydprintf(fd, "Current projection: %s\n", buf);
  1086. }
  1087. void Console::commandProjectionSubCommand2d(int /*fd*/, const std::string& /*args*/)
  1088. {
  1089. auto director = Director::getInstance();
  1090. Scheduler *sched = director->getScheduler();
  1091. sched->performFunctionInCocosThread( [=](){
  1092. director->setProjection(Director::Projection::_2D);
  1093. } );
  1094. }
  1095. void Console::commandProjectionSubCommand3d(int /*fd*/, const std::string& /*args*/)
  1096. {
  1097. auto director = Director::getInstance();
  1098. Scheduler *sched = director->getScheduler();
  1099. sched->performFunctionInCocosThread( [=](){
  1100. director->setProjection(Director::Projection::_3D);
  1101. } );
  1102. }
  1103. void Console::commandResolution(int /*fd*/, const std::string& args)
  1104. {
  1105. int width, height, policy;
  1106. std::istringstream stream( args );
  1107. stream >> width >> height>> policy;
  1108. Scheduler *sched = Director::getInstance()->getScheduler();
  1109. sched->performFunctionInCocosThread( [=](){
  1110. Director::getInstance()->getOpenGLView()->setDesignResolutionSize(width, height, static_cast<ResolutionPolicy>(policy));
  1111. } );
  1112. }
  1113. void Console::commandResolutionSubCommandEmpty(int fd, const std::string& /*args*/)
  1114. {
  1115. auto director = Director::getInstance();
  1116. Size points = director->getWinSize();
  1117. Size pixels = director->getWinSizeInPixels();
  1118. auto glview = director->getOpenGLView();
  1119. Size design = glview->getDesignResolutionSize();
  1120. ResolutionPolicy res = glview->getResolutionPolicy();
  1121. Rect visibleRect = glview->getVisibleRect();
  1122. Console::Utility::mydprintf(fd, "Window Size:\n"
  1123. "\t%d x %d (points)\n"
  1124. "\t%d x %d (pixels)\n"
  1125. "\t%d x %d (design resolution)\n"
  1126. "Resolution Policy: %d\n"
  1127. "Visible Rect:\n"
  1128. "\torigin: %d x %d\n"
  1129. "\tsize: %d x %d\n",
  1130. (int)points.width, (int)points.height,
  1131. (int)pixels.width, (int)pixels.height,
  1132. (int)design.width, (int)design.height,
  1133. (int)res,
  1134. (int)visibleRect.origin.x, (int)visibleRect.origin.y,
  1135. (int)visibleRect.size.width, (int)visibleRect.size.height
  1136. );
  1137. }
  1138. void Console::commandSceneGraph(int fd, const std::string& /*args*/)
  1139. {
  1140. Scheduler *sched = Director::getInstance()->getScheduler();
  1141. sched->performFunctionInCocosThread( std::bind(&Console::printSceneGraphBoot, this, fd) );
  1142. }
  1143. void Console::commandTextures(int fd, const std::string& /*args*/)
  1144. {
  1145. Scheduler *sched = Director::getInstance()->getScheduler();
  1146. sched->performFunctionInCocosThread( [=](){
  1147. Console::Utility::mydprintf(fd, "%s", Director::getInstance()->getTextureCache()->getCachedTextureInfo().c_str());
  1148. Console::Utility::sendPrompt(fd);
  1149. });
  1150. }
  1151. void Console::commandTexturesSubCommandFlush(int /*fd*/, const std::string& /*args*/)
  1152. {
  1153. Scheduler *sched = Director::getInstance()->getScheduler();
  1154. sched->performFunctionInCocosThread( [](){
  1155. Director::getInstance()->getTextureCache()->removeAllTextures();
  1156. });
  1157. }
  1158. void Console::commandTouchSubCommandTap(int fd, const std::string& args)
  1159. {
  1160. auto argv = Console::Utility::split(args,' ');
  1161. if((argv.size() == 3 ) && (Console::Utility::isFloat(argv[1]) && Console::Utility::isFloat(argv[2])))
  1162. {
  1163. float x = utils::atof(argv[1].c_str());
  1164. float y = utils::atof(argv[2].c_str());
  1165. std::srand ((unsigned)time(nullptr));
  1166. _touchId = rand();
  1167. Scheduler *sched = Director::getInstance()->getScheduler();
  1168. sched->performFunctionInCocosThread( [&](){
  1169. Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x, &y);
  1170. Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x, &y);
  1171. });
  1172. }
  1173. else
  1174. {
  1175. const char msg[] = "touch: invalid arguments.\n";
  1176. Console::Utility::sendToConsole(fd, msg, strlen(msg));
  1177. }
  1178. }
  1179. void Console::commandTouchSubCommandSwipe(int fd, const std::string& args)
  1180. {
  1181. auto argv = Console::Utility::split(args,' ');
  1182. if((argv.size() == 5)
  1183. && (Console::Utility::isFloat(argv[1])) && (Console::Utility::isFloat(argv[2]))
  1184. && (Console::Utility::isFloat(argv[3])) && (Console::Utility::isFloat(argv[4])))
  1185. {
  1186. float x1 = utils::atof(argv[1].c_str());
  1187. float y1 = utils::atof(argv[2].c_str());
  1188. float x2 = utils::atof(argv[3].c_str());
  1189. float y2 = utils::atof(argv[4].c_str());
  1190. std::srand ((unsigned)time(nullptr));
  1191. _touchId = rand();
  1192. Scheduler *sched = Director::getInstance()->getScheduler();
  1193. sched->performFunctionInCocosThread( [=](){
  1194. float tempx = x1, tempy = y1;
  1195. Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &tempx, &tempy);
  1196. });
  1197. float dx = std::abs(x1 - x2);
  1198. float dy = std::abs(y1 - y2);
  1199. float _x_ = x1, _y_ = y1;
  1200. if(dx > dy)
  1201. {
  1202. while(dx > 1)
  1203. {
  1204. if(x1 < x2)
  1205. {
  1206. _x_ += 1;
  1207. }
  1208. if(x1 > x2)
  1209. {
  1210. _x_ -= 1;
  1211. }
  1212. if(y1 < y2)
  1213. {
  1214. _y_ += dy/dx;
  1215. }
  1216. if(y1 > y2)
  1217. {
  1218. _y_ -= dy/dx;
  1219. }
  1220. sched->performFunctionInCocosThread( [=](){
  1221. float tempx = _x_, tempy = _y_;
  1222. Director::getInstance()->getOpenGLView()->handleTouchesMove(1, &_touchId, &tempx, &tempy);
  1223. });
  1224. dx -= 1;
  1225. }
  1226. }
  1227. else
  1228. {
  1229. while(dy > 1)
  1230. {
  1231. if(x1 < x2)
  1232. {
  1233. _x_ += dx/dy;
  1234. }
  1235. if(x1 > x2)
  1236. {
  1237. _x_ -= dx/dy;
  1238. }
  1239. if(y1 < y2)
  1240. {
  1241. _y_ += 1;
  1242. }
  1243. if(y1 > y2)
  1244. {
  1245. _y_ -= 1;
  1246. }
  1247. sched->performFunctionInCocosThread( [=](){
  1248. float tempx = _x_, tempy = _y_;
  1249. Director::getInstance()->getOpenGLView()->handleTouchesMove(1, &_touchId, &tempx, &tempy);
  1250. });
  1251. dy -= 1;
  1252. }
  1253. }
  1254. sched->performFunctionInCocosThread( [=](){
  1255. float tempx = x2, tempy = y2;
  1256. Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &tempx, &tempy);
  1257. });
  1258. }
  1259. else
  1260. {
  1261. const char msg[] = "touch: invalid arguments.\n";
  1262. Console::Utility::sendToConsole(fd, msg, strlen(msg));
  1263. }
  1264. }
  1265. static char invalid_filename_char[] = {':', '/', '\\', '?', '%', '*', '<', '>', '"', '|', '\r', '\n', '\t'};
  1266. void Console::commandUpload(int fd)
  1267. {
  1268. ssize_t n, rc;
  1269. char buf[512] = {0};
  1270. char c = 0;
  1271. char *ptr = buf;
  1272. //read file name
  1273. for( n = 0; n < sizeof(buf) - 1; n++ )
  1274. {
  1275. if( (rc = recv(fd, &c, 1, 0)) == 1 )
  1276. {
  1277. for(char x : invalid_filename_char)
  1278. {
  1279. if (c == x)
  1280. {
  1281. const char err[] = "upload: invalid file name!\n";
  1282. Console::Utility::sendToConsole(fd, err, strlen(err));
  1283. return;
  1284. }
  1285. }
  1286. if (c == ' ')
  1287. {
  1288. break;
  1289. }
  1290. *ptr++ = c;
  1291. }
  1292. else if( rc == 0 )
  1293. {
  1294. break;
  1295. }
  1296. else if( errno == EINTR )
  1297. {
  1298. continue;
  1299. }
  1300. else
  1301. {
  1302. break;
  1303. }
  1304. }
  1305. *ptr = 0;
  1306. static std::string writablePath = FileUtils::getInstance()->getWritablePath();
  1307. std::string filepath = writablePath + std::string(buf);
  1308. FILE* fp = fopen(FileUtils::getInstance()->getSuitableFOpen(filepath).c_str(), "wb");
  1309. if(!fp)
  1310. {
  1311. const char err[] = "can't create file!\n";
  1312. Console::Utility::sendToConsole(fd, err, strlen(err));
  1313. return;
  1314. }
  1315. while (true)
  1316. {
  1317. char data[4];
  1318. for(int i = 0; i < 4; i++)
  1319. {
  1320. data[i] = '=';
  1321. }
  1322. bool more_data;
  1323. readBytes(fd, data, 4, &more_data);
  1324. if(!more_data)
  1325. {
  1326. break;
  1327. }
  1328. unsigned char *decode;
  1329. unsigned char *in = (unsigned char *)data;
  1330. int dt = base64Decode(in, 4, &decode);
  1331. if (dt > 0)
  1332. {
  1333. fwrite(decode, dt, 1, fp);
  1334. }
  1335. free(decode);
  1336. }
  1337. fclose(fp);
  1338. }
  1339. void Console::commandVersion(int fd, const std::string& /*args*/)
  1340. {
  1341. Console::Utility::mydprintf(fd, "%s\n", cocos2dVersion());
  1342. }
  1343. // helper free functions
  1344. int Console::printSceneGraph(int fd, Node* node, int level)
  1345. {
  1346. int total = 1;
  1347. for(int i=0; i<level; ++i)
  1348. Console::Utility::sendToConsole(fd, "-", 1);
  1349. Console::Utility::mydprintf(fd, " %s\n", node->getDescription().c_str());
  1350. for(const auto& child: node->getChildren())
  1351. total += printSceneGraph(fd, child, level+1);
  1352. return total;
  1353. }
  1354. void Console::printSceneGraphBoot(int fd)
  1355. {
  1356. Console::Utility::sendToConsole(fd,"\n",1);
  1357. auto scene = Director::getInstance()->getRunningScene();
  1358. int total = printSceneGraph(fd, scene, 0);
  1359. Console::Utility::mydprintf(fd, "Total Nodes: %d\n", total);
  1360. Console::Utility::sendPrompt(fd);
  1361. }
  1362. void Console::printFileUtils(int fd)
  1363. {
  1364. FileUtils* fu = FileUtils::getInstance();
  1365. Console::Utility::mydprintf(fd, "\nSearch Paths:\n");
  1366. auto& list = fu->getSearchPaths();
  1367. for( const auto &item : list) {
  1368. Console::Utility::mydprintf(fd, "%s\n", item.c_str());
  1369. }
  1370. Console::Utility::mydprintf(fd, "\nResolution Order:\n");
  1371. auto& list1 = fu->getSearchResolutionsOrder();
  1372. for( const auto &item : list1) {
  1373. Console::Utility::mydprintf(fd, "%s\n", item.c_str());
  1374. }
  1375. Console::Utility::mydprintf(fd, "\nWritable Path:\n");
  1376. Console::Utility::mydprintf(fd, "%s\n", fu->getWritablePath().c_str());
  1377. Console::Utility::mydprintf(fd, "\nFull Path Cache:\n");
  1378. auto& cache = fu->getFullPathCache();
  1379. for( const auto &item : cache) {
  1380. Console::Utility::mydprintf(fd, "%s -> %s\n", item.first.c_str(), item.second.c_str());
  1381. }
  1382. Console::Utility::sendPrompt(fd);
  1383. }
  1384. void Console::sendHelp(int fd, const std::unordered_map<std::string, Command*>& commands, const char* msg)
  1385. {
  1386. Console::Utility::sendToConsole(fd, msg, strlen(msg));
  1387. for(auto& it : commands)
  1388. {
  1389. auto command = it.second;
  1390. if (command->getHelp().empty()) continue;
  1391. Console::Utility::mydprintf(fd, "\t%s", command->getName().c_str());
  1392. ssize_t tabs = strlen(command->getName().c_str()) / 8;
  1393. tabs = 3 - tabs;
  1394. for(int j=0;j<tabs;j++){
  1395. Console::Utility::mydprintf(fd, "\t");
  1396. }
  1397. Console::Utility::mydprintf(fd,"%s\n", command->getHelp().c_str());
  1398. }
  1399. }
  1400. NS_CC_END