EAMain.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /////////////////////////////////////////////////////////////////////////////
  2. // EAMain.cpp
  3. //
  4. // Copyright (c) 2011 Electronic Arts Inc. All Rights Reserved.
  5. // Created by Paul Pedriana
  6. /////////////////////////////////////////////////////////////////////////////
  7. #ifdef _MSC_VER
  8. #pragma warning(disable: 4530 4548 4509)
  9. #pragma warning(disable: 6320) // Exception-filter expression is the constant EXCEPTION_EXECUTE_HANDLER.
  10. #pragma warning(disable: 4472 4355) // additional warnings generated by XDK with VS2015
  11. #endif
  12. #include <EAAssert/eaassert.h>
  13. #include <EAMain/EAMain.h>
  14. #include <eathread/eathread.h>
  15. #include <eathread/eathread_atomic.h>
  16. #ifdef EA_PLATFORM_ANDROID
  17. #include <eathread/eathread_futex.h>
  18. #endif
  19. #include <EAStdC/EAString.h>
  20. #include <EAStdC/EASprintf.h>
  21. #include <EAStdC/EADateTime.h>
  22. #include <EAStdC/EAProcess.h>
  23. #include <EAMain/internal/EAMainStartupShutdown.h>
  24. #include <EAMain/internal/EAMainPrintManager.h>
  25. #include <EABase/eabase.h>
  26. EA_DISABLE_ALL_VC_WARNINGS()
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <time.h>
  31. #include <stdarg.h>
  32. #if defined(EA_PLATFORM_MICROSOFT)
  33. #ifndef WIN32_LEAN_AND_MEAN
  34. #define WIN32_LEAN_AND_MEAN
  35. #endif
  36. #include <Windows.h>
  37. #elif defined(__APPLE__) // OS X, iPhone, iPad, etc.
  38. #include <stdbool.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #include <sys/sysctl.h>
  42. #import <mach/mach.h>
  43. #import <mach/mach_host.h>
  44. #elif defined(EA_PLATFORM_BSD)
  45. #include <sys/types.h>
  46. #include <sys/ptrace.h>
  47. #elif defined(EA_HAVE_SYS_PTRACE_H)
  48. #include <unistd.h>
  49. #include <sys/ptrace.h>
  50. #elif defined(EA_PLATFORM_ANDROID)
  51. #include <unistd.h>
  52. #include <android/log.h>
  53. #endif
  54. EA_RESTORE_ALL_VC_WARNINGS()
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // EA_COMPILER_VA_COPY_REQUIRED
  57. //
  58. // This is already present in EABase version >= 2.00.40a report may not cause a flush
  59. // See EABase for documentation.
  60. //
  61. #ifndef EA_COMPILER_VA_COPY_REQUIRED
  62. #if (EABASE_VERSION_N < 20040) // If not already handled by EABase...
  63. #if ((defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__)) && (!defined(__i386__) || defined(__x86_64__)) && !defined(__ppc__) && !defined(__PPC__) && !defined(__PPC64__)
  64. #define EA_COMPILER_VA_COPY_REQUIRED 1
  65. #endif
  66. #endif
  67. #endif
  68. namespace EA
  69. {
  70. namespace EAMain
  71. {
  72. namespace Internal
  73. {
  74. EAMAIN_API EAMainFunction gEAMainFunction;
  75. /// ReportDefault
  76. ///
  77. /// This is the default report function.
  78. /// It does not append any newlines to the output nor does it require
  79. /// the user to do so. It simply passes on the input to stdout.
  80. /// If the user wants the output to have a newline, the user must supply it.
  81. /// This allows the user to report multiple text items to the same line if desired.
  82. /// It does not send the input to stderr, as the output of a unit test
  83. /// is deemed to be test results (success or failure) and not errors.
  84. ///
  85. #ifdef EA_PLATFORM_ANDROID
  86. static const size_t ANDROID_REPORT_BUFFER_SIZE = 1023;
  87. static char gAndroidReportBuffer[ANDROID_REPORT_BUFFER_SIZE + 1];
  88. static char *gAndroidReportBufferWritePtr = gAndroidReportBuffer;
  89. static EA::Thread::Futex gBufferFutex;
  90. static EA::Thread::ThreadTime gLastThreadTime = EA::Thread::kTimeoutImmediate;
  91. static EA::Thread::ThreadTime gMinTimeBetweenPrints = EA::Thread::ThreadTime(1);
  92. // This function assumes that the buffer futex above is held
  93. // prior to entry. Please ensure that the buffer futex is either
  94. // held by the calling code.
  95. static void FlushAndroidReportBuffer()
  96. {
  97. if (gAndroidReportBufferWritePtr != gAndroidReportBuffer)
  98. {
  99. *gAndroidReportBufferWritePtr = 0;
  100. __android_log_write(ANDROID_LOG_INFO, "EAMain", gAndroidReportBuffer);
  101. // We found that if the OS is spammed too quickly with log info, it stops taking output from the app
  102. // because it thinks it's a DDOS attack. So we sleep to give the OS time in the case the last
  103. // log output was too recent.
  104. EA::Thread::ThreadTime currentTime = EA::Thread::GetThreadTime();
  105. if (gLastThreadTime != EA::Thread::kTimeoutImmediate &&
  106. ((currentTime - gLastThreadTime) < gMinTimeBetweenPrints))
  107. {
  108. EA::Thread::ThreadSleep(gMinTimeBetweenPrints);
  109. }
  110. gLastThreadTime = currentTime;
  111. gAndroidReportBufferWritePtr = gAndroidReportBuffer;
  112. }
  113. }
  114. static void AppendToReportBuffer(char8_t c)
  115. {
  116. char *ptr = gAndroidReportBufferWritePtr;
  117. char *end = &gAndroidReportBuffer[ANDROID_REPORT_BUFFER_SIZE];
  118. if (ptr >= end)
  119. {
  120. FlushAndroidReportBuffer();
  121. }
  122. *gAndroidReportBufferWritePtr++ = c;
  123. }
  124. static void AndroidReport(const char8_t *pMessage)
  125. {
  126. using namespace EA::StdC;
  127. using namespace EA::Thread;
  128. AutoFutex autoFutex(gBufferFutex);
  129. size_t messageLength = Strlen(pMessage);
  130. for (size_t i = 0; i < messageLength; ++i)
  131. {
  132. char8_t c = pMessage[i];
  133. switch (c)
  134. {
  135. case '\n':
  136. FlushAndroidReportBuffer();
  137. break;
  138. default:
  139. AppendToReportBuffer(c);
  140. break;
  141. }
  142. }
  143. }
  144. #endif
  145. static void ReportDefault(const char8_t* pMessage)
  146. {
  147. if (!pMessage)
  148. {
  149. return;
  150. }
  151. // It's possible that the underlying print back end can't handle large
  152. // output sizes. For example, the OutputDebugStringA call below drops
  153. // chars beyond about 4096.
  154. size_t length = EA::StdC::Strlen(pMessage); // It might be faster to make a custom Strlen which quits after N chars.
  155. const size_t kMaxLength = 1024;
  156. if(length > kMaxLength)
  157. {
  158. for(size_t i = 0, copiedLength = 0; i < length; i += copiedLength)
  159. {
  160. char8_t buffer[kMaxLength + 1];
  161. size_t c;
  162. copiedLength = ((length - i) >= kMaxLength) ? kMaxLength : (length - i);
  163. for(c = 0; c < copiedLength; c++)
  164. buffer[c] = pMessage[i + c];
  165. buffer[c] = 0;
  166. ReportDefault(buffer);
  167. }
  168. }
  169. else
  170. {
  171. #if defined(EA_PLATFORM_MICROSOFT) && !defined(CS_UNDEFINED_STRING) // No need to do this for Microsoft console platforms, as the fputs below covers that.
  172. OutputDebugStringA(pMessage);
  173. #endif
  174. #if defined(EA_PLATFORM_ANDROID)
  175. // Android doesn't implement stdio (e.g. fputs), though sometimes we use compiler
  176. // linking statements to redirect stdio functions to our own implementations which
  177. // allow it to work.
  178. //
  179. // __android_log_write can write only 512 bytes at a time. Normally we don't write
  180. // so much text in unit test output, but if this becomes a problem then we can loop
  181. // and write blocks of the output. The primary downside to such an approach is that
  182. // __android_log_write appends a \n to your output for each call. See the EAStdC
  183. // EASprintfCore.cpp code for example loop code.
  184. AndroidReport(pMessage);
  185. #else
  186. fputs(pMessage, stdout);
  187. fflush(stdout);
  188. #endif
  189. }
  190. }
  191. EAMAIN_API const char *ExtractPrintServerAddress(int argc, char **argv)
  192. {
  193. CommandLine commandLine(argc, argv);
  194. const char *printServerAddress = NULL;
  195. if (commandLine.FindSwitch("PrintServerIPAddress", false, &printServerAddress, 0, '=') >= 0)
  196. {
  197. if (EA::StdC::Strlen(printServerAddress) > 0)
  198. {
  199. return printServerAddress;
  200. }
  201. }
  202. return NULL;
  203. }
  204. }
  205. ReportFunction gpReportFunction = EA::EAMain::Internal::ReportDefault;
  206. EAMAIN_API void SetReportFunction(ReportFunction pReportFunction)
  207. {
  208. gpReportFunction = pReportFunction;
  209. }
  210. EAMAIN_API ReportFunction GetReportFunction()
  211. {
  212. return gpReportFunction;
  213. }
  214. EAMAIN_API ReportFunction GetDefaultReportFunction()
  215. {
  216. using namespace EA::EAMain::Internal;
  217. return ReportDefault;
  218. }
  219. ///////////////////////////////////////////////////////////////////////////////
  220. // GetVerbosity / SetVerbosity
  221. ///////////////////////////////////////////////////////////////////////////////
  222. unsigned gVerbosity = 0; // 0 means to display just failures.
  223. EAMAIN_API unsigned GetVerbosity()
  224. {
  225. return gVerbosity;
  226. }
  227. EAMAIN_API void SetVerbosity(unsigned verbosity)
  228. {
  229. gVerbosity = verbosity;
  230. }
  231. ///////////////////////////////////////////////////////////////////////////////
  232. // ReportVaList
  233. //
  234. static void ReportVaList(unsigned minVerbosity, ReportFunction pReportFunction, const char8_t* pFormat, va_list arguments)
  235. {
  236. if(pFormat && (GetVerbosity() >= minVerbosity))
  237. {
  238. #if defined(EA_PLATFORM_DESKTOP)
  239. const int kBufferSize = 2048;
  240. #else
  241. const int kBufferSize = 512;
  242. #endif
  243. char buffer[kBufferSize];
  244. #if defined(EA_COMPILER_VA_COPY_REQUIRED)
  245. va_list argumentsSaved;
  246. va_copy(argumentsSaved, arguments);
  247. #endif
  248. const int nReturnValue = EA::StdC::Vsnprintf(buffer, kBufferSize, pFormat, arguments);
  249. if(!pReportFunction)
  250. pReportFunction = gpReportFunction;
  251. if(pReportFunction)
  252. {
  253. if((nReturnValue >= 0) && (nReturnValue < (int)kBufferSize))
  254. pReportFunction(buffer);
  255. else if(nReturnValue < 0) // If we simply didn't have enough buffer space.
  256. {
  257. pReportFunction("Invalid format specified.\n Format: ");
  258. pReportFunction(pFormat);
  259. }
  260. else // Else we simply didn't have enough buffer space.
  261. {
  262. char* pBuffer = static_cast<char *>(calloc(nReturnValue + 1, 1));
  263. if(pBuffer)
  264. {
  265. #if defined(EA_COMPILER_VA_COPY_REQUIRED)
  266. va_end(arguments);
  267. va_copy(arguments, argumentsSaved);
  268. #endif
  269. EA::StdC::Vsnprintf(pBuffer, nReturnValue + 1, pFormat, arguments);
  270. pReportFunction(pBuffer);
  271. free(pBuffer);
  272. }
  273. else
  274. pReportFunction("Unable to allocate buffer space for large printf.\n");
  275. }
  276. }
  277. #if defined(EA_COMPILER_VA_COPY_REQUIRED)
  278. // The caller will call va_end(arguments)
  279. va_end(argumentsSaved);
  280. #endif
  281. }
  282. }
  283. ///////////////////////////////////////////////////////////////////////////////
  284. // Report
  285. //
  286. EAMAIN_API void Report(const char8_t* pFormat, ...)
  287. {
  288. va_list arguments;
  289. va_start(arguments, pFormat);
  290. ReportVaList(0, gpReportFunction, pFormat, arguments);
  291. va_end(arguments);
  292. }
  293. ///////////////////////////////////////////////////////////////////////////////
  294. // ReportVerbosity
  295. //
  296. EAMAIN_API void ReportVerbosity(unsigned minVerbosity, const char8_t* pFormat, ...)
  297. {
  298. va_list arguments;
  299. va_start(arguments, pFormat);
  300. ReportVaList(minVerbosity, gpReportFunction, pFormat, arguments);
  301. va_end(arguments);
  302. }
  303. ///////////////////////////////////////////////////////////////////////////////
  304. // VReport
  305. //
  306. EAMAIN_API void VReport(const char8_t* pFormat, va_list arguments)
  307. {
  308. ReportVaList(0, gpReportFunction, pFormat, arguments);
  309. }
  310. ///////////////////////////////////////////////////////////////////////////////
  311. // VReportVerbosity
  312. //
  313. EAMAIN_API void VReportVerbosity(unsigned minVerbosity, const char8_t* pFormat, va_list arguments)
  314. {
  315. ReportVaList(minVerbosity, gpReportFunction, pFormat, arguments);
  316. }
  317. ///////////////////////////////////////////////////////////////////////////////
  318. // PlatformStartup
  319. //
  320. EAMAIN_API void PlatformStartup()
  321. {
  322. // Routed to EAMainStartup to centralize
  323. // the platform specific startup code.
  324. PlatformStartup(NULL);
  325. }
  326. EAMAIN_API void PlatformStartup(int argc, char **argv)
  327. {
  328. const char *printServerNetworkAddress = Internal::ExtractPrintServerAddress(argc, argv);
  329. PlatformStartup(printServerNetworkAddress);
  330. }
  331. EAMAIN_API void PlatformStartup(const char *printServerNetworkAddress)
  332. {
  333. // Routed to EAMainStartup to centralize
  334. // the platform specific startup code.
  335. EA::EAMain::Internal::EAMainStartup(printServerNetworkAddress);
  336. }
  337. ///////////////////////////////////////////////////////////////////////////////
  338. // PlatformShutdown
  339. //
  340. EAMAIN_API void PlatformShutdown(int errorCount)
  341. {
  342. #ifdef EA_PLATFORM_ANDROID
  343. // The Android reporting functions will flush the output buffers
  344. // when a newline is encountered. Calling AndroidReport with a
  345. // single newline will cause any accumulated output to flush to
  346. // the log.
  347. //
  348. // An alternative would be to call the FlushAndroidReportBuffer
  349. // function but doing so would necessitate having separate locks
  350. // for both FlushAndroidReportBuffer and AndroidReport, as both
  351. // of these could be caused at the same time. To avoid this
  352. // complication, FlushAndroidReportBuffer will only be called by
  353. // AndroidReport or its children.
  354. // -mburke
  355. Internal::AndroidReport("\n");
  356. #endif
  357. // Routed to EAMainShutdown to centralize
  358. // the platform specific shutdown code.
  359. EA::EAMain::Internal::EAMainShutdown(errorCount);
  360. }
  361. ///////////////////////////////////////////////////////////////////////////////
  362. // CommandLine
  363. ///////////////////////////////////////////////////////////////////////////////
  364. CommandLine::CommandLine(int argc, char** argv)
  365. : mArgc(argc)
  366. , mArgv(NULL)
  367. , mCommandLine(NULL)
  368. {
  369. mArgv = static_cast<char **>(calloc(argc + 1, sizeof(char *)));
  370. EA_ASSERT(mArgv != NULL);
  371. for (int i = 0; i < argc; ++i)
  372. {
  373. mArgv[i] = argv[i];
  374. }
  375. mArgv[argc] = NULL;
  376. // Microsoft fails to support argc/argv on Xenon. Sometimes it works; sometimes it doesn't.
  377. }
  378. CommandLine::CommandLine(const char *args)
  379. : mArgc(0),
  380. mArgv(NULL),
  381. mCommandLine(NULL)
  382. {
  383. ParseCommandLine(args, FLAG_NONE);
  384. }
  385. CommandLine::CommandLine(const char *args, unsigned int flags)
  386. : mArgc(0),
  387. mArgv(NULL),
  388. mCommandLine(NULL)
  389. {
  390. ParseCommandLine(args, flags);
  391. }
  392. CommandLine::~CommandLine()
  393. {
  394. if (mArgv)
  395. {
  396. free(mArgv);
  397. mArgv = NULL;
  398. }
  399. if (mCommandLine)
  400. {
  401. free(mCommandLine);
  402. mCommandLine = NULL;
  403. }
  404. }
  405. /// Stristr
  406. /// We implement this here because it isn't consistently present with all compiler-supplied C libraries.
  407. static char* Stristr(const char* s1, const char* s2)
  408. {
  409. const char* cp = s1;
  410. if(!*s2)
  411. return (char*)s1;
  412. while(*cp)
  413. {
  414. const char* s = cp;
  415. const char* t = s2;
  416. while(*s && *t && (tolower(*s) == tolower(*t)))
  417. ++s, ++t;
  418. if(*t == 0)
  419. return (char*)cp;
  420. ++cp;
  421. }
  422. return 0;
  423. }
  424. //Returns position switch is found at. Returns -1 if not found
  425. int CommandLine::FindSwitch(const char* pSwitch, bool bCaseSensitive, const char** pResult, int nStartingIndex, char delimeter) const
  426. {
  427. const char8_t kSwitchIDs[] = { '-', '/' };
  428. const int kSwitchIDCount = sizeof(kSwitchIDs)/sizeof(kSwitchIDs[0]);
  429. static const char sEmptyString[] = { 0 };
  430. if(nStartingIndex < 0)
  431. {
  432. nStartingIndex = 0;
  433. }
  434. if (pResult)
  435. {
  436. *pResult = sEmptyString;
  437. }
  438. // Here we move the input pSwitch past any one leading switch indicator such as '-'.
  439. for(int i = 0; i < kSwitchIDCount; ++i)
  440. {
  441. if(*pSwitch == kSwitchIDs[i])
  442. {
  443. ++pSwitch;
  444. break;
  445. }
  446. }
  447. const size_t nSwitchLength = strlen(pSwitch);
  448. if (!nSwitchLength || (nStartingIndex >= mArgc))
  449. return -1;
  450. for(int i = nStartingIndex; i < mArgc; ++i)
  451. {
  452. const char *sCurrent = mArgv[i];
  453. if(strlen(sCurrent) >= 2) // Enough, for example, for "-x".
  454. {
  455. int j;
  456. // Make sure the string starts with a switch ID (e.g. '-').
  457. for(j = 0; j < kSwitchIDCount; ++j)
  458. {
  459. if(sCurrent[0] == kSwitchIDs[j])
  460. break;
  461. }
  462. if(j < kSwitchIDCount) // If a leading '-' was found...
  463. {
  464. const char* pCurrent = bCaseSensitive ? strstr(sCurrent + 1, pSwitch) : Stristr(sCurrent + 1, pSwitch);
  465. const char* pCStr = sCurrent;
  466. if(pCurrent == (pCStr + 1)) // If the user's input switch matched at least the start of the current argument switch...
  467. {
  468. pCurrent += nSwitchLength; // Move pCurrent past the input switch.
  469. // At this point, we require that *pCurrent is either 0 or delimeter.
  470. if((*pCurrent == 0) || (*pCurrent == delimeter))
  471. {
  472. // We have a match. Now possibly return a result string.
  473. if(*pCurrent == delimeter)
  474. {
  475. if(*++pCurrent)
  476. {
  477. if(pResult)
  478. {
  479. *pResult = pCurrent;
  480. }
  481. }
  482. }
  483. return i;
  484. }
  485. }
  486. }
  487. }
  488. }
  489. return -1;
  490. }
  491. bool CommandLine::HasHelpSwitch() const
  492. {
  493. if((FindSwitch("-help", false, NULL, 0) >= 0) ||
  494. (FindSwitch("-h", false, NULL, 0) >= 0) ||
  495. (FindSwitch("-?", false, NULL, 0) >= 0))
  496. {
  497. return true;
  498. }
  499. return false;
  500. }
  501. void CommandLine::ParseCommandLine(const char *inputCommandLine, unsigned int flags)
  502. {
  503. size_t commandLineLength = strlen(inputCommandLine);
  504. size_t allocSize = commandLineLength + 1;
  505. size_t startOffset = 0;
  506. if (flags & FLAG_NO_PROGRAM_NAME)
  507. {
  508. allocSize += 1;
  509. startOffset = 1;
  510. }
  511. char *commandLine = static_cast<char *>(calloc(allocSize, 1));
  512. EA_ASSERT(commandLine != NULL);
  513. memcpy(commandLine + startOffset, inputCommandLine, commandLineLength);
  514. int argc = 0;
  515. char **argv = static_cast<char **>(calloc(MAX_COMMANDLINE_ARGS, sizeof(char *)));
  516. EA_ASSERT(argv != NULL);
  517. char *start = commandLine + startOffset;
  518. char *ptr = start;
  519. char *end = start + commandLineLength;
  520. bool isQuoted = false;
  521. const char quote = '"';
  522. if (flags & FLAG_NO_PROGRAM_NAME)
  523. {
  524. argv[argc++] = commandLine;
  525. }
  526. while (ptr < end)
  527. {
  528. // The two cases this parser handles for quotes are:
  529. // "this is a quoted parameter"; and
  530. // -D:"this is a quoted parameter"
  531. // The parser does not handle edge cases like
  532. // "this is a quoted parameter"and"this is the same"
  533. char *quoteStart = NULL;
  534. for (;;)
  535. {
  536. while ((ptr < end) && !isspace((unsigned char)*ptr))
  537. {
  538. if (*ptr == quote && !isQuoted)
  539. {
  540. isQuoted = true;
  541. quoteStart = ptr;
  542. }
  543. ++ptr;
  544. }
  545. if (isQuoted)
  546. {
  547. if (*(ptr - 1) == quote)
  548. {
  549. // If we find a quote, shift the whole string back
  550. // by one character, ie:
  551. // -D:"this is a quoted parameter"
  552. // becomes
  553. // -D:this is a quoted parameter"
  554. // The trailing quote is removed below when we place
  555. // a null terminator at the end of our argument.
  556. memmove(quoteStart, quoteStart + 1, (end - quoteStart));
  557. --end;
  558. ptr -= 2;
  559. isQuoted = false;
  560. break;
  561. }
  562. ++ptr;
  563. }
  564. else
  565. {
  566. break;
  567. }
  568. }
  569. if (ptr != start)
  570. {
  571. *ptr = 0;
  572. argv[argc++] = start;
  573. ++ptr;
  574. }
  575. while ((ptr < end) && isspace((unsigned char)*ptr))
  576. {
  577. ++ptr;
  578. }
  579. start = ptr;
  580. }
  581. mArgc = argc;
  582. mArgv = argv;
  583. mCommandLine = commandLine;
  584. }
  585. }
  586. }
  587. // WinRT-based Windows:
  588. #if (defined(EA_PLATFORM_MICROSOFT) && !defined(CS_UNDEFINED_STRING) && !EA_WINAPI_FAMILY_PARTITION(EA_WINAPI_PARTITION_DESKTOP))
  589. EA_DISABLE_VC_WARNING(4350 4571 4625 4626 4265)
  590. #include <future>
  591. EA_RESTORE_ALL_VC_WARNINGS()
  592. extern "C" int EAMain(int argc, char** argv);
  593. namespace EA
  594. {
  595. namespace EAMain
  596. {
  597. class WinRTRunner : public IWinRTRunner
  598. {
  599. private:
  600. // The copy/assignment operator is explicitly inaccessible due to the base class 'IWinRTRunner' containing a member
  601. // that is non-copyable (eg. std::future).
  602. WinRTRunner(const WinRTRunner &);
  603. WinRTRunner& operator=(const WinRTRunner &);
  604. public:
  605. WinRTRunner() {}
  606. virtual void Run(int argc, char** argv) override
  607. {
  608. mResult = std::async(std::launch::async, [=]() {
  609. const char *printServerAddress = Internal::ExtractPrintServerAddress(argc, argv);
  610. EA::EAMain::Internal::EAMainStartup(printServerAddress);
  611. int result = EA::EAMain::Internal::gEAMainFunction(argc, argv);
  612. EA::EAMain::Internal::EAMainShutdown(result);
  613. return result;
  614. });
  615. }
  616. virtual bool IsFinished() override { return mResult.wait_for(std::chrono::milliseconds(33)) == std::future_status::ready; }
  617. virtual void ReportResult() override
  618. {
  619. char output[100];
  620. EA::StdC::Snprintf(output, EAArrayCount(output), "EXIT(%d)\n", mResult.get());
  621. // Using OutputDebugStringA directly here as opposed to Report as someone may overload
  622. // the default reporter. And this is what counts for EARunner to know what to do.
  623. OutputDebugStringA(output);
  624. }
  625. std::future<int> mResult;
  626. };
  627. EAMAIN_API IWinRTRunner* CreateWinRTRunner()
  628. {
  629. return new WinRTRunner();
  630. }
  631. } // namespace EAMain
  632. } // namespace EA
  633. #endif // WinRT-based Windows