AEPlayerMode.cpp 8.7 KB

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