CCConsole.cpp 47 KB

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