AEPlayerMode.cpp 4.4 KB

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