AEPlayerMode.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/IOEvents.h>
  8. #include <Atomic/IO/Log.h>
  9. #include <Atomic/Input/InputEvents.h>
  10. #include <Atomic/Core/ProcessUtils.h>
  11. #include <Atomic/Graphics/GraphicsEvents.h>
  12. #include <Atomic/Graphics/Camera.h>
  13. #include <Atomic/UI/SystemUI/DebugHud.h>
  14. #include <Atomic/UI/SystemUI/SystemUIEvents.h>
  15. #include <Atomic/UI/UI.h>
  16. #include <Atomic/IPC/IPCEvents.h>
  17. #include <Atomic/IPC/IPCWorker.h>
  18. #include <AtomicJS/Javascript/JSVM.h>
  19. #include <AtomicJS/Javascript/JSEvents.h>
  20. #include <AtomicJS/Javascript/JSIPCEvents.h>
  21. #include "AEPlayerMode.h"
  22. #ifdef ATOMIC_PLATFORM_WINDOWS
  23. #include <windows.h>
  24. #endif
  25. namespace AtomicEditor
  26. {
  27. PlayerMode::PlayerMode(Context* context) :
  28. Object(context),
  29. brokerActive_(false),
  30. launchedByEditor_(false),
  31. licenseModule3D_(false)
  32. {
  33. fd_[0] = INVALID_IPCHANDLE_VALUE;
  34. fd_[1] = INVALID_IPCHANDLE_VALUE;
  35. ipc_ = GetSubsystem<IPC>();
  36. SubscribeToEvent(E_LOGMESSAGE, HANDLER(PlayerMode, HandleLogMessage));
  37. SubscribeToEvent(E_JSERROR, HANDLER(PlayerMode, HandleJSError));
  38. // BEGIN LICENSE MANAGEMENT
  39. SubscribeToEvent(E_BEGINVIEWRENDER, HANDLER(PlayerMode, HandleViewRender));
  40. // END LICENSE MANAGEMENT
  41. }
  42. PlayerMode::~PlayerMode()
  43. {
  44. }
  45. void PlayerMode::HandleIPCInitialize(StringHash eventType, VariantMap& eventData)
  46. {
  47. brokerActive_ = true;
  48. JSVM* vm = JSVM::GetJSVM(0);
  49. if (!vm->ExecuteMain())
  50. {
  51. SendEvent(E_EXITREQUESTED);
  52. }
  53. // BEGIN LICENSE MANAGEMENT
  54. licenseModule3D_ = eventData["license3D"].GetBool();
  55. // END LICENSE MANAGEMENT
  56. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  57. if (debugHud)
  58. debugHud->SetMode(eventData["debugHudMode"].GetUInt());
  59. }
  60. void PlayerMode::ProcessArguments() {
  61. const Vector<String>& arguments = GetArguments();
  62. int id = -1;
  63. for (unsigned i = 0; i < arguments.Size(); ++i)
  64. {
  65. if (arguments[i].Length() > 1)
  66. {
  67. String argument = arguments[i].ToLower();
  68. // String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  69. if (argument.StartsWith("--ipc-id="))
  70. {
  71. Vector<String> idc = argument.Split(argument.CString(), '=');
  72. if (idc.Size() == 2)
  73. id = ToInt(idc[1].CString());
  74. }
  75. else if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  76. {
  77. LOGINFOF("Starting IPCWorker %s", argument.CString());
  78. Vector<String> ipc = argument.Split(argument.CString(), '=');
  79. if (ipc.Size() == 2)
  80. {
  81. if (argument.StartsWith("--ipc-server="))
  82. {
  83. #ifdef ATOMIC_PLATFORM_WINDOWS
  84. // clientRead
  85. WString wipc(ipc[1]);
  86. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  87. fd_[0] = pipe;
  88. #else
  89. int fd = ToInt(ipc[1].CString());
  90. fd_[0] = fd;
  91. #endif
  92. }
  93. else
  94. {
  95. #ifdef ATOMIC_PLATFORM_WINDOWS
  96. // clientWrite
  97. WString wipc(ipc[1]);
  98. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  99. fd_[1] = pipe;
  100. #else
  101. int fd = ToInt(ipc[1].CString());
  102. fd_[1] = fd;
  103. #endif
  104. }
  105. }
  106. }
  107. }
  108. }
  109. if (id > 0 && fd_[0] != INVALID_IPCHANDLE_VALUE && fd_[1] != INVALID_IPCHANDLE_VALUE)
  110. {
  111. launchedByEditor_ = true;
  112. SubscribeToEvent(E_IPCINITIALIZE, HANDLER(PlayerMode, HandleIPCInitialize));
  113. ipc_->InitWorker((unsigned) id, fd_[0], fd_[1]);
  114. }
  115. }
  116. void PlayerMode::HandleJSError(StringHash eventType, VariantMap& eventData)
  117. {
  118. if (brokerActive_)
  119. {
  120. if (ipc_.Null())
  121. return;
  122. String errName = eventData[JSError::P_ERRORNAME].GetString();
  123. String errStack = eventData[JSError::P_ERRORSTACK].GetString();
  124. String errMessage = eventData[JSError::P_ERRORMESSAGE].GetString();
  125. String errFilename = eventData[JSError::P_ERRORFILENAME].GetString();
  126. int errLineNumber = eventData[JSError::P_ERRORLINENUMBER].GetInt();
  127. VariantMap ipcErrorData;
  128. ipcErrorData[IPCJSError::P_ERRORNAME] = errName;
  129. ipcErrorData[IPCJSError::P_ERRORSTACK] = errStack;
  130. ipcErrorData[IPCJSError::P_ERRORMESSAGE] = errMessage;
  131. ipcErrorData[IPCJSError::P_ERRORFILENAME] = errFilename;
  132. ipcErrorData[IPCJSError::P_ERRORLINENUMBER] = errLineNumber;
  133. ipc_->SendEventToBroker(E_IPCJSERROR, ipcErrorData);
  134. LOGERROR("SENDING E_IPCJSERROR");
  135. }
  136. }
  137. void PlayerMode::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  138. {
  139. using namespace LogMessage;
  140. if (brokerActive_)
  141. {
  142. if (ipc_.Null())
  143. return;
  144. VariantMap logEvent;
  145. logEvent[IPCWorkerLog::P_LEVEL] = eventData[P_LEVEL].GetInt();
  146. logEvent[IPCWorkerLog::P_MESSAGE] = eventData[P_MESSAGE].GetString();
  147. ipc_->SendEventToBroker(E_IPCWORKERLOG, logEvent);
  148. }
  149. }
  150. void PlayerMode::HandleMessageAck(StringHash eventType, VariantMap& eventData)
  151. {
  152. messageBox_ = 0;
  153. GetSubsystem<UI>()->RequestExit();
  154. }
  155. void PlayerMode::HandleViewRender(StringHash eventType, VariantMap& eventData)
  156. {
  157. // BEGIN LICENSE MANAGEMENT
  158. static bool done = false;
  159. if (!launchedByEditor_ || licenseModule3D_)
  160. return;
  161. Camera* camera = static_cast<Camera*>(eventData[BeginViewRender::P_CAMERA].GetPtr());
  162. if (!camera || camera->IsOrthographic())
  163. return;
  164. if (!done) {
  165. done = true;
  166. messageBox_ = GetSubsystem<UI>()->ShowSystemMessageBox("3D Module License Required", "A 3D Module License is required to display 3D content.\n\nUpgrade to Atomic Pro for all features and platforms.");
  167. SubscribeToEvent(messageBox_, SystemUI::E_MESSAGEACK, HANDLER(PlayerMode, HandleMessageAck));
  168. if (brokerActive_)
  169. {
  170. if (ipc_.Null())
  171. return;
  172. VariantMap msgEvent;
  173. msgEvent[IPCMessage::P_MESSAGE] = String("3D Module License Required");
  174. ipc_->SendEventToBroker(E_IPCMESSAGE, msgEvent);
  175. }
  176. }
  177. // END LICENSE MANAGEMENT
  178. }
  179. }