AEPlayerMode.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/IOEvents.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Input/InputEvents.h>
  25. #include <Atomic/Core/ProcessUtils.h>
  26. #include <Atomic/Graphics/GraphicsEvents.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #include <Atomic/UI/SystemUI/DebugHud.h>
  30. #include <Atomic/UI/SystemUI/SystemUIEvents.h>
  31. #include <Atomic/UI/UI.h>
  32. #include <Atomic/IPC/IPCEvents.h>
  33. #include <Atomic/IPC/IPCWorker.h>
  34. #include <AtomicJS/Javascript/JSVM.h>
  35. #include <AtomicJS/Javascript/JSEvents.h>
  36. #include <AtomicJS/Javascript/JSIPCEvents.h>
  37. #include "AEPlayerEvents.h"
  38. #include "AEPlayerMode.h"
  39. #ifdef ATOMIC_PLATFORM_WINDOWS
  40. #include <windows.h>
  41. #endif
  42. namespace AtomicEditor
  43. {
  44. PlayerMode::PlayerMode(Context* context) :
  45. Object(context),
  46. brokerActive_(false),
  47. launchedByEditor_(false),
  48. licenseModule3D_(false)
  49. {
  50. fd_[0] = INVALID_IPCHANDLE_VALUE;
  51. fd_[1] = INVALID_IPCHANDLE_VALUE;
  52. ipc_ = GetSubsystem<IPC>();
  53. SubscribeToEvent(E_LOGMESSAGE, HANDLER(PlayerMode, HandleLogMessage));
  54. SubscribeToEvent(E_JSERROR, HANDLER(PlayerMode, HandleJSError));
  55. SubscribeToEvent(E_EXITREQUESTED, HANDLER(PlayerMode, HandleExitRequest));
  56. SubscribeToEvent(E_SCREENMODE, HANDLER(PlayerMode, HandlePlayerWindowChanged));
  57. SubscribeToEvent(E_WINDOWPOS, HANDLER(PlayerMode, HandlePlayerWindowChanged));
  58. // BEGIN LICENSE MANAGEMENT
  59. SubscribeToEvent(E_BEGINVIEWRENDER, HANDLER(PlayerMode, HandleViewRender));
  60. // END LICENSE MANAGEMENT
  61. }
  62. PlayerMode::~PlayerMode()
  63. {
  64. }
  65. void PlayerMode::HandleIPCInitialize(StringHash eventType, VariantMap& eventData)
  66. {
  67. brokerActive_ = true;
  68. JSVM* vm = JSVM::GetJSVM(0);
  69. if (!vm->ExecuteMain())
  70. {
  71. SendEvent(E_EXITREQUESTED);
  72. }
  73. // BEGIN LICENSE MANAGEMENT
  74. licenseModule3D_ = eventData["license3D"].GetBool();
  75. // END LICENSE MANAGEMENT
  76. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  77. if (debugHud)
  78. debugHud->SetMode(eventData["debugHudMode"].GetUInt());
  79. }
  80. void PlayerMode::ProcessArguments() {
  81. const Vector<String>& arguments = GetArguments();
  82. int id = -1;
  83. for (unsigned i = 0; i < arguments.Size(); ++i)
  84. {
  85. if (arguments[i].Length() > 1)
  86. {
  87. String argument = arguments[i].ToLower();
  88. // String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  89. if (argument.StartsWith("--ipc-id="))
  90. {
  91. Vector<String> idc = argument.Split(argument.CString(), '=');
  92. if (idc.Size() == 2)
  93. id = ToInt(idc[1].CString());
  94. }
  95. else if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  96. {
  97. LOGINFOF("Starting IPCWorker %s", argument.CString());
  98. Vector<String> ipc = argument.Split(argument.CString(), '=');
  99. if (ipc.Size() == 2)
  100. {
  101. if (argument.StartsWith("--ipc-server="))
  102. {
  103. #ifdef ATOMIC_PLATFORM_WINDOWS
  104. // clientRead
  105. WString wipc(ipc[1]);
  106. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  107. fd_[0] = pipe;
  108. #else
  109. int fd = ToInt(ipc[1].CString());
  110. fd_[0] = fd;
  111. #endif
  112. }
  113. else
  114. {
  115. #ifdef ATOMIC_PLATFORM_WINDOWS
  116. // clientWrite
  117. WString wipc(ipc[1]);
  118. HANDLE pipe = reinterpret_cast<HANDLE>(_wtoi64(wipc.CString()));
  119. fd_[1] = pipe;
  120. #else
  121. int fd = ToInt(ipc[1].CString());
  122. fd_[1] = fd;
  123. #endif
  124. }
  125. }
  126. }
  127. }
  128. }
  129. if (id > 0 && fd_[0] != INVALID_IPCHANDLE_VALUE && fd_[1] != INVALID_IPCHANDLE_VALUE)
  130. {
  131. launchedByEditor_ = true;
  132. SubscribeToEvent(E_IPCINITIALIZE, HANDLER(PlayerMode, HandleIPCInitialize));
  133. ipc_->InitWorker((unsigned) id, fd_[0], fd_[1]);
  134. }
  135. }
  136. void PlayerMode::HandleJSError(StringHash eventType, VariantMap& eventData)
  137. {
  138. if (brokerActive_)
  139. {
  140. if (ipc_.Null())
  141. return;
  142. String errName = eventData[JSError::P_ERRORNAME].GetString();
  143. String errStack = eventData[JSError::P_ERRORSTACK].GetString();
  144. String errMessage = eventData[JSError::P_ERRORMESSAGE].GetString();
  145. String errFilename = eventData[JSError::P_ERRORFILENAME].GetString();
  146. int errLineNumber = eventData[JSError::P_ERRORLINENUMBER].GetInt();
  147. VariantMap ipcErrorData;
  148. ipcErrorData[IPCJSError::P_ERRORNAME] = errName;
  149. ipcErrorData[IPCJSError::P_ERRORSTACK] = errStack;
  150. ipcErrorData[IPCJSError::P_ERRORMESSAGE] = errMessage;
  151. ipcErrorData[IPCJSError::P_ERRORFILENAME] = errFilename;
  152. ipcErrorData[IPCJSError::P_ERRORLINENUMBER] = errLineNumber;
  153. ipc_->SendEventToBroker(E_IPCJSERROR, ipcErrorData);
  154. LOGERROR("SENDING E_IPCJSERROR");
  155. }
  156. }
  157. void PlayerMode::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  158. {
  159. using namespace LogMessage;
  160. if (brokerActive_)
  161. {
  162. if (ipc_.Null())
  163. return;
  164. VariantMap logEvent;
  165. logEvent[IPCWorkerLog::P_LEVEL] = eventData[P_LEVEL].GetInt();
  166. logEvent[IPCWorkerLog::P_MESSAGE] = eventData[P_MESSAGE].GetString();
  167. ipc_->SendEventToBroker(E_IPCWORKERLOG, logEvent);
  168. }
  169. }
  170. void PlayerMode::HandleMessageAck(StringHash eventType, VariantMap& eventData)
  171. {
  172. messageBox_ = 0;
  173. GetSubsystem<UI>()->RequestExit();
  174. }
  175. void PlayerMode::HandleViewRender(StringHash eventType, VariantMap& eventData)
  176. {
  177. // BEGIN LICENSE MANAGEMENT
  178. static bool done = false;
  179. if (!launchedByEditor_ || licenseModule3D_)
  180. return;
  181. Camera* camera = static_cast<Camera*>(eventData[BeginViewRender::P_CAMERA].GetPtr());
  182. if (!camera || camera->IsOrthographic())
  183. return;
  184. if (!done) {
  185. done = true;
  186. 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.");
  187. SubscribeToEvent(messageBox_, SystemUI::E_MESSAGEACK, HANDLER(PlayerMode, HandleMessageAck));
  188. if (brokerActive_)
  189. {
  190. if (ipc_.Null())
  191. return;
  192. VariantMap msgEvent;
  193. msgEvent[IPCMessage::P_MESSAGE] = String("3D Module License Required");
  194. ipc_->SendEventToBroker(E_IPCMESSAGE, msgEvent);
  195. }
  196. }
  197. // END LICENSE MANAGEMENT
  198. }
  199. void PlayerMode::HandlePlayerWindowChanged(StringHash eventType, VariantMap& eventData)
  200. {
  201. Graphics* graphics = GetSubsystem<Graphics>();
  202. using namespace IPCPlayerWindowChanged;
  203. VariantMap data;
  204. data[P_POSX] = graphics->GetWindowPosition().x_;
  205. data[P_POSY] = graphics->GetWindowPosition().y_;
  206. data[P_WIDTH] = graphics->GetWidth();
  207. data[P_HEIGHT] = graphics->GetHeight();
  208. data[P_MONITOR] = graphics->GetCurrentMonitor();
  209. data[P_MAXIMIZED] = graphics->GetMaximized();
  210. ipc_->SendEventToBroker(E_IPCPLAYERWINDOWCHANGED, data);
  211. }
  212. void PlayerMode::HandleExitRequest(StringHash eventType, VariantMap& eventData)
  213. {
  214. UnsubscribeFromEvent(E_LOGMESSAGE);
  215. SendEvent(E_PLAYERQUIT);
  216. }
  217. }