EAMain.cpp 26 KB

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