AEPlayerMode.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <Atomic/IO/IOEvents.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/Input/InputEvents.h>
  4. #include <Atomic/Core/ProcessUtils.h>
  5. #include <Atomic/UI/SystemUI/DebugHud.h>
  6. #include <Atomic/IPC/IPCEvents.h>
  7. #include <Atomic/IPC/IPCWorker.h>
  8. #include <AtomicJS/Javascript/JSVM.h>
  9. #include <AtomicJS/Javascript/JSEvents.h>
  10. #include <AtomicJS/Javascript/JSIPCEvents.h>
  11. #include "AEPlayerMode.h"
  12. namespace AtomicEditor
  13. {
  14. PlayerMode::PlayerMode(Context* context) :
  15. Object(context),
  16. brokerActive_(false),
  17. launchedByEditor_(false)
  18. {
  19. fd_[0] = INVALID_IPCHANDLE_VALUE;
  20. fd_[1] = INVALID_IPCHANDLE_VALUE;
  21. ipc_ = GetSubsystem<IPC>();
  22. SubscribeToEvent(E_LOGMESSAGE, HANDLER(PlayerMode, HandleLogMessage));
  23. SubscribeToEvent(E_JSERROR, HANDLER(PlayerMode, HandleJSError));
  24. }
  25. PlayerMode::~PlayerMode()
  26. {
  27. }
  28. void PlayerMode::HandleIPCInitialize(StringHash eventType, VariantMap& eventData)
  29. {
  30. brokerActive_ = true;
  31. JSVM* vm = JSVM::GetJSVM(0);
  32. if (!vm->ExecuteMain())
  33. {
  34. SendEvent(E_EXITREQUESTED);
  35. }
  36. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  37. if (debugHud)
  38. debugHud->SetMode(eventData["debugHudMode"].GetUInt());
  39. }
  40. void PlayerMode::ProcessArguments() {
  41. const Vector<String>& arguments = GetArguments();
  42. int id = -1;
  43. for (unsigned i = 0; i < arguments.Size(); ++i)
  44. {
  45. if (arguments[i].Length() > 1)
  46. {
  47. String argument = arguments[i].ToLower();
  48. // String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  49. if (argument.StartsWith("--ipc-id="))
  50. {
  51. Vector<String> idc = argument.Split(argument.CString(), '=');
  52. if (idc.Size() == 2)
  53. id = ToInt(idc[1].CString());
  54. }
  55. else if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  56. {
  57. LOGINFOF("Starting IPCWorker %s", argument.CString());
  58. Vector<String> ipc = argument.Split(argument.CString(), '=');
  59. if (ipc.Size() == 2)
  60. {
  61. if (argument.StartsWith("--ipc-server="))
  62. {
  63. #ifdef ATOMIC_PLATFORM_WINDOWS
  64. // clientRead
  65. WString wipc(ipc[1]);
  66. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  67. fd_[0] = pipe;
  68. #else
  69. int fd = ToInt(ipc[1].CString());
  70. fd_[0] = fd;
  71. #endif
  72. }
  73. else
  74. {
  75. #ifdef ATOMIC_PLATFORM_WINDOWS
  76. // clientWrite
  77. WString wipc(ipc[1]);
  78. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  79. fd_[1] = pipe;
  80. #else
  81. int fd = ToInt(ipc[1].CString());
  82. fd_[1] = fd;
  83. #endif
  84. }
  85. }
  86. }
  87. }
  88. }
  89. if (id > 0 && fd_[0] != INVALID_IPCHANDLE_VALUE && fd_[1] != INVALID_IPCHANDLE_VALUE)
  90. {
  91. launchedByEditor_ = true;
  92. SubscribeToEvent(E_IPCINITIALIZE, HANDLER(PlayerMode, HandleIPCInitialize));
  93. ipc_->InitWorker((unsigned) id, fd_[0], fd_[1]);
  94. }
  95. }
  96. void PlayerMode::HandleJSError(StringHash eventType, VariantMap& eventData)
  97. {
  98. if (brokerActive_)
  99. {
  100. if (ipc_.Null())
  101. return;
  102. String errName = eventData[JSError::P_ERRORNAME].GetString();
  103. String errStack = eventData[JSError::P_ERRORSTACK].GetString();
  104. String errMessage = eventData[JSError::P_ERRORMESSAGE].GetString();
  105. String errFilename = eventData[JSError::P_ERRORFILENAME].GetString();
  106. int errLineNumber = eventData[JSError::P_ERRORLINENUMBER].GetInt();
  107. VariantMap ipcErrorData;
  108. ipcErrorData[IPCJSError::P_ERRORNAME] = errName;
  109. ipcErrorData[IPCJSError::P_ERRORSTACK] = errStack;
  110. ipcErrorData[IPCJSError::P_ERRORMESSAGE] = errMessage;
  111. ipcErrorData[IPCJSError::P_ERRORFILENAME] = errFilename;
  112. ipcErrorData[IPCJSError::P_ERRORLINENUMBER] = errLineNumber;
  113. ipc_->SendEventToBroker(E_IPCJSERROR, ipcErrorData);
  114. LOGERROR("SENDING E_IPCJSERROR");
  115. }
  116. }
  117. void PlayerMode::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  118. {
  119. using namespace LogMessage;
  120. if (brokerActive_)
  121. {
  122. if (ipc_.Null())
  123. return;
  124. VariantMap logEvent;
  125. logEvent[IPCWorkerLog::P_LEVEL] = eventData[P_LEVEL].GetInt();
  126. logEvent[IPCWorkerLog::P_MESSAGE] = eventData[P_MESSAGE].GetString();
  127. ipc_->SendEventToBroker(E_IPCWORKERLOG, logEvent);
  128. }
  129. }
  130. }