AEPlayerMode.cpp 8.5 KB

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