AEPlayerMode.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/Core/CoreEvents.h>
  12. #include <Atomic/Graphics/GraphicsEvents.h>
  13. #include <Atomic/Graphics/Graphics.h>
  14. #include <Atomic/Graphics/Camera.h>
  15. #include <Atomic/UI/SystemUI/DebugHud.h>
  16. #include <Atomic/UI/SystemUI/SystemUIEvents.h>
  17. #include <Atomic/UI/UI.h>
  18. #include <Atomic/IPC/IPCEvents.h>
  19. #include <Atomic/IPC/IPCWorker.h>
  20. #include <AtomicJS/Javascript/JSVM.h>
  21. #include <AtomicJS/Javascript/JSEvents.h>
  22. #include <AtomicJS/Javascript/JSIPCEvents.h>
  23. #include "AEPlayerEvents.h"
  24. #include "AEPlayerMode.h"
  25. #ifdef ATOMIC_PLATFORM_WINDOWS
  26. #include <windows.h>
  27. #endif
  28. namespace AtomicEditor
  29. {
  30. PlayerMode::PlayerMode(Context* context) :
  31. Object(context),
  32. brokerActive_(false),
  33. launchedByEditor_(false),
  34. licenseModule3D_(false)
  35. {
  36. fd_[0] = INVALID_IPCHANDLE_VALUE;
  37. fd_[1] = INVALID_IPCHANDLE_VALUE;
  38. ipc_ = GetSubsystem<IPC>();
  39. SubscribeToEvent(E_LOGMESSAGE, HANDLER(PlayerMode, HandleLogMessage));
  40. SubscribeToEvent(E_JSERROR, HANDLER(PlayerMode, HandleJSError));
  41. SubscribeToEvent(E_EXITREQUESTED, HANDLER(PlayerMode, HandleExitRequest));
  42. SubscribeToEvent(E_SCREENMODE, HANDLER(PlayerMode, HandlePlayerWindowChanged));
  43. SubscribeToEvent(E_WINDOWPOS, HANDLER(PlayerMode, HandlePlayerWindowChanged));
  44. SubscribeToEvent(E_UPDATESPAUSEDRESUMED, HANDLER(PlayerMode, HandleUpdatesPausedResumed));
  45. // BEGIN LICENSE MANAGEMENT
  46. SubscribeToEvent(E_BEGINVIEWRENDER, HANDLER(PlayerMode, HandleViewRender));
  47. // END LICENSE MANAGEMENT
  48. }
  49. PlayerMode::~PlayerMode()
  50. {
  51. }
  52. void PlayerMode::HandleIPCInitialize(StringHash eventType, VariantMap& eventData)
  53. {
  54. brokerActive_ = true;
  55. JSVM* vm = JSVM::GetJSVM(0);
  56. if (!vm->ExecuteMain())
  57. {
  58. SendEvent(E_EXITREQUESTED);
  59. }
  60. // BEGIN LICENSE MANAGEMENT
  61. licenseModule3D_ = eventData["license3D"].GetBool();
  62. // END LICENSE MANAGEMENT
  63. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  64. if (debugHud)
  65. debugHud->SetMode(eventData["debugHudMode"].GetUInt());
  66. }
  67. void PlayerMode::ProcessArguments() {
  68. const Vector<String>& arguments = GetArguments();
  69. int id = -1;
  70. for (unsigned i = 0; i < arguments.Size(); ++i)
  71. {
  72. if (arguments[i].Length() > 1)
  73. {
  74. String argument = arguments[i].ToLower();
  75. // String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  76. if (argument.StartsWith("--ipc-id="))
  77. {
  78. Vector<String> idc = argument.Split(argument.CString(), '=');
  79. if (idc.Size() == 2)
  80. id = ToInt(idc[1].CString());
  81. }
  82. else if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  83. {
  84. LOGINFOF("Starting IPCWorker %s", argument.CString());
  85. Vector<String> ipc = argument.Split(argument.CString(), '=');
  86. if (ipc.Size() == 2)
  87. {
  88. if (argument.StartsWith("--ipc-server="))
  89. {
  90. #ifdef ATOMIC_PLATFORM_WINDOWS
  91. // clientRead
  92. WString wipc(ipc[1]);
  93. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  94. fd_[0] = pipe;
  95. #else
  96. int fd = ToInt(ipc[1].CString());
  97. fd_[0] = fd;
  98. #endif
  99. }
  100. else
  101. {
  102. #ifdef ATOMIC_PLATFORM_WINDOWS
  103. // clientWrite
  104. WString wipc(ipc[1]);
  105. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  106. fd_[1] = pipe;
  107. #else
  108. int fd = ToInt(ipc[1].CString());
  109. fd_[1] = fd;
  110. #endif
  111. }
  112. }
  113. }
  114. }
  115. }
  116. if (id > 0 && fd_[0] != INVALID_IPCHANDLE_VALUE && fd_[1] != INVALID_IPCHANDLE_VALUE)
  117. {
  118. launchedByEditor_ = true;
  119. SubscribeToEvent(E_IPCINITIALIZE, HANDLER(PlayerMode, HandleIPCInitialize));
  120. ipc_->InitWorker((unsigned) id, fd_[0], fd_[1]);
  121. }
  122. }
  123. void PlayerMode::HandleJSError(StringHash eventType, VariantMap& eventData)
  124. {
  125. if (brokerActive_)
  126. {
  127. if (ipc_.Null())
  128. return;
  129. String errName = eventData[JSError::P_ERRORNAME].GetString();
  130. String errStack = eventData[JSError::P_ERRORSTACK].GetString();
  131. String errMessage = eventData[JSError::P_ERRORMESSAGE].GetString();
  132. String errFilename = eventData[JSError::P_ERRORFILENAME].GetString();
  133. int errLineNumber = eventData[JSError::P_ERRORLINENUMBER].GetInt();
  134. VariantMap ipcErrorData;
  135. ipcErrorData[IPCJSError::P_ERRORNAME] = errName;
  136. ipcErrorData[IPCJSError::P_ERRORSTACK] = errStack;
  137. ipcErrorData[IPCJSError::P_ERRORMESSAGE] = errMessage;
  138. ipcErrorData[IPCJSError::P_ERRORFILENAME] = errFilename;
  139. ipcErrorData[IPCJSError::P_ERRORLINENUMBER] = errLineNumber;
  140. ipc_->SendEventToBroker(E_IPCJSERROR, ipcErrorData);
  141. LOGERROR("SENDING E_IPCJSERROR");
  142. }
  143. }
  144. void PlayerMode::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  145. {
  146. using namespace LogMessage;
  147. if (brokerActive_)
  148. {
  149. if (ipc_.Null())
  150. return;
  151. VariantMap logEvent;
  152. logEvent[IPCWorkerLog::P_LEVEL] = eventData[P_LEVEL].GetInt();
  153. logEvent[IPCWorkerLog::P_MESSAGE] = eventData[P_MESSAGE].GetString();
  154. ipc_->SendEventToBroker(E_IPCWORKERLOG, logEvent);
  155. }
  156. }
  157. void PlayerMode::HandleMessageAck(StringHash eventType, VariantMap& eventData)
  158. {
  159. messageBox_ = 0;
  160. GetSubsystem<UI>()->RequestExit();
  161. }
  162. void PlayerMode::HandleViewRender(StringHash eventType, VariantMap& eventData)
  163. {
  164. // BEGIN LICENSE MANAGEMENT
  165. static bool done = false;
  166. if (!launchedByEditor_ || licenseModule3D_)
  167. return;
  168. Camera* camera = static_cast<Camera*>(eventData[BeginViewRender::P_CAMERA].GetPtr());
  169. if (!camera || camera->IsOrthographic())
  170. return;
  171. if (!done) {
  172. done = true;
  173. 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.");
  174. SubscribeToEvent(messageBox_, SystemUI::E_MESSAGEACK, HANDLER(PlayerMode, HandleMessageAck));
  175. if (brokerActive_)
  176. {
  177. if (ipc_.Null())
  178. return;
  179. VariantMap msgEvent;
  180. msgEvent[IPCMessage::P_MESSAGE] = String("3D Module License Required");
  181. ipc_->SendEventToBroker(E_IPCMESSAGE, msgEvent);
  182. }
  183. }
  184. // END LICENSE MANAGEMENT
  185. }
  186. void PlayerMode::HandlePlayerWindowChanged(StringHash eventType, VariantMap& eventData)
  187. {
  188. Graphics* graphics = GetSubsystem<Graphics>();
  189. using namespace IPCPlayerWindowChanged;
  190. VariantMap data;
  191. data[P_POSX] = graphics->GetWindowPosition().x_;
  192. data[P_POSY] = graphics->GetWindowPosition().y_;
  193. data[P_WIDTH] = graphics->GetWidth();
  194. data[P_HEIGHT] = graphics->GetHeight();
  195. data[P_MONITOR] = graphics->GetCurrentMonitor();
  196. data[P_MAXIMIZED] = graphics->GetMaximized();
  197. ipc_->SendEventToBroker(E_IPCPLAYERWINDOWCHANGED, data);
  198. }
  199. void PlayerMode::HandleUpdatesPausedResumed(StringHash eventType, VariantMap& eventData)
  200. {
  201. ipc_->SendEventToBroker(E_IPCPLAYERUPDATESPAUSEDRESUMED, eventData);
  202. }
  203. void PlayerMode::HandleExitRequest(StringHash eventType, VariantMap& eventData)
  204. {
  205. UnsubscribeFromEvent(E_LOGMESSAGE);
  206. SendEvent(E_PLAYERQUIT);
  207. }
  208. }