IPCPlayerApp.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include <Atomic/Core/CoreEvents.h>//
  22. #include <Atomic/IO/IOEvents.h>
  23. #include <Atomic/Input/InputEvents.h>
  24. #include <Atomic/Graphics/Graphics.h>
  25. #include <Atomic/Graphics/GraphicsEvents.h>
  26. #include <Atomic/IPC/IPCEvents.h>
  27. #include <AtomicJS/Javascript/JSIPCEvents.h>
  28. #include "IPCPlayerAppEvents.h"
  29. #include <Atomic/Engine/Engine.h>
  30. #include <Atomic/IPC/IPC.h>
  31. #include <AtomicJS/Javascript/Javascript.h>
  32. #include <Atomic/UI/SystemUI/DebugHud.h>
  33. #include "IPCPlayerApp.h"
  34. namespace Atomic
  35. {
  36. IPCPlayerApp::IPCPlayerApp(Context* context) :
  37. PlayerApp(context),
  38. subprocess_(false),
  39. debugPlayer_(false),
  40. brokerActive_(false)
  41. {
  42. fd_[0] = INVALID_IPCHANDLE_VALUE;
  43. fd_[1] = INVALID_IPCHANDLE_VALUE;
  44. }
  45. IPCPlayerApp::~IPCPlayerApp()
  46. {
  47. }
  48. void IPCPlayerApp::Setup()
  49. {
  50. PlayerApp::Setup();
  51. // This should be configurable
  52. engineParameters_.InsertNew("LogLevel", LOG_DEBUG);
  53. ipc_ = new IPC(context_);
  54. context_->RegisterSubsystem(ipc_);
  55. }
  56. void IPCPlayerApp::ProcessArguments()
  57. {
  58. PlayerApp::ProcessArguments();
  59. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  60. if (!fileSystem)
  61. {
  62. ErrorExit("IPCPlayerApp::ProcessArguments FileSystem subsystem does not exist");
  63. }
  64. String resourcePrefix;
  65. engineParameters_["ResourcePrefixPaths"] = "";
  66. for (unsigned i = 0; i < arguments_.Size(); ++i)
  67. {
  68. if (arguments_[i].Length() > 1)
  69. {
  70. String argument = arguments_[i].ToLower();
  71. String value = i + 1 < arguments_.Size() ? arguments_[i + 1] : String::EMPTY;
  72. if (argument.StartsWith("--ipc-server=") || argument.StartsWith("--ipc-client="))
  73. {
  74. subprocess_ = true;
  75. }
  76. else if (argument == "--debug")
  77. {
  78. debugPlayer_ = true;
  79. }
  80. else if (argument == "--resourceprefix" && value.Length())
  81. {
  82. resourcePrefix = value;
  83. engineParameters_["ResourcePrefixPaths"] = resourcePrefix;
  84. }
  85. else if (argument == "--project" && value.Length())
  86. {
  87. value = AddTrailingSlash(value);
  88. AddEngineConfigSearchPath(value + "Settings/");
  89. // check that cache exists
  90. if (!fileSystem->DirExists(value + "Cache"))
  91. {
  92. ErrorExit("Project cache folder does not exist, projects must be loaded into the Atomic Editor at least once before using the --player command line mode");
  93. return;
  94. }
  95. #ifdef ATOMIC_DEV_BUILD
  96. String resourcePaths = ToString("%s/Resources/CoreData;%s/Resources/PlayerData;%sResources;%s;%sCache",
  97. ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR, value.CString(), value.CString(), value.CString());
  98. #else
  99. #ifdef __APPLE__
  100. if (!resourcePrefix.Length())
  101. {
  102. engineParameters_["ResourcePrefixPaths"] = fileSystem->GetProgramDir() + "../Resources";
  103. }
  104. #else
  105. if (!resourcePrefix.Length())
  106. {
  107. engineParameters_["ResourcePrefixPaths"] = fileSystem->GetProgramDir() + "Resources";
  108. }
  109. #endif
  110. String resourcePaths = ToString("CoreData;PlayerData;%s/;%s/Resources;%s;%sCache",
  111. value.CString(), value.CString(), value.CString(), value.CString());
  112. #endif
  113. ATOMIC_LOGINFOF("Adding ResourcePaths: %s", resourcePaths.CString());
  114. engineParameters_["ResourcePaths"] = resourcePaths;
  115. }
  116. }
  117. }
  118. // IPC client player should not auto exit if a subprocess
  119. if (subprocess_)
  120. engine_->SetAutoExit(false);
  121. }
  122. void IPCPlayerApp::Start()
  123. {
  124. if (subprocess_)
  125. {
  126. // do not execute main in the player app
  127. executeJSMain_ = false;
  128. }
  129. PlayerApp::Start();
  130. int id = -1;
  131. if (IPC::ProcessArguments(arguments_, id, fd_[0], fd_[1]))
  132. {
  133. SubscribeToEvent(E_IPCINITIALIZE, ATOMIC_HANDLER(IPCPlayerApp, HandleIPCInitialize));
  134. SubscribeToEvent(E_LOGMESSAGE, ATOMIC_HANDLER(IPCPlayerApp, HandleLogMessage));
  135. SubscribeToEvent(E_JSERROR, ATOMIC_HANDLER(IPCPlayerApp, HandleJSError));
  136. SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(IPCPlayerApp, HandleExitRequest));
  137. SubscribeToEvent(E_SCREENMODE, ATOMIC_HANDLER(IPCPlayerApp, HandlePlayerWindowChanged));
  138. SubscribeToEvent(E_WINDOWPOS, ATOMIC_HANDLER(IPCPlayerApp, HandlePlayerWindowChanged));
  139. SubscribeToEvent(E_UPDATESPAUSEDRESUMED, ATOMIC_HANDLER(IPCPlayerApp, HandleUpdatesPausedResumed));
  140. if (ipc_->InitWorker((unsigned)id, fd_[0], fd_[1]))
  141. {
  142. brokerActive_ = true;
  143. }
  144. else if (subprocess_)
  145. {
  146. ATOMIC_LOGERROR("IPCPlayerApp::Start() - Unable to initialize IPC Worker");
  147. }
  148. }
  149. if (subprocess_)
  150. {
  151. JSVM* vm = JSVM::GetJSVM(0);
  152. if (!vm->ExecuteMain())
  153. {
  154. SendEvent(E_EXITREQUESTED);
  155. }
  156. SubscribeToEvent(E_PLAYERQUIT, ATOMIC_HANDLER(IPCPlayerApp, HandleQuit));
  157. }
  158. GetSubsystem<Graphics>()->RaiseWindow();
  159. }
  160. void IPCPlayerApp::HandleQuit(StringHash eventType, VariantMap& eventData)
  161. {
  162. engine_->Exit();
  163. }
  164. void IPCPlayerApp::Stop()
  165. {
  166. PlayerApp::Stop();
  167. }
  168. void IPCPlayerApp::HandleIPCInitialize(StringHash eventType, VariantMap& eventData)
  169. {
  170. brokerActive_ = true;
  171. // If the parent application has a profile mode up, sync
  172. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  173. if (debugHud)
  174. {
  175. unsigned mode = eventData["debugHudMode"].GetUInt();
  176. // Only set if we haven't set the mode in player code
  177. if (mode && !debugHud->GetMode())
  178. {
  179. debugHud->SetMode(mode);
  180. debugHud->SetProfilerMode((DebugHudProfileMode)eventData["debugHudProfilerMode"].GetUInt());
  181. }
  182. }
  183. }
  184. void IPCPlayerApp::HandleJSError(StringHash eventType, VariantMap& eventData)
  185. {
  186. if (brokerActive_)
  187. {
  188. if (ipc_.Null())
  189. return;
  190. String errName = eventData[JSError::P_ERRORNAME].GetString();
  191. String errStack = eventData[JSError::P_ERRORSTACK].GetString();
  192. String errMessage = eventData[JSError::P_ERRORMESSAGE].GetString();
  193. String errFilename = eventData[JSError::P_ERRORFILENAME].GetString();
  194. int errLineNumber = eventData[JSError::P_ERRORLINENUMBER].GetInt();
  195. VariantMap ipcErrorData;
  196. ipcErrorData[IPCJSError::P_ERRORNAME] = errName;
  197. ipcErrorData[IPCJSError::P_ERRORSTACK] = errStack;
  198. ipcErrorData[IPCJSError::P_ERRORMESSAGE] = errMessage;
  199. ipcErrorData[IPCJSError::P_ERRORFILENAME] = errFilename;
  200. ipcErrorData[IPCJSError::P_ERRORLINENUMBER] = errLineNumber;
  201. ipc_->SendEventToBroker(E_IPCJSERROR, ipcErrorData);
  202. ATOMIC_LOGERROR("SENDING E_IPCJSERROR");
  203. }
  204. }
  205. void IPCPlayerApp::HandlePlayerWindowChanged(StringHash eventType, VariantMap& eventData)
  206. {
  207. Graphics* graphics = GetSubsystem<Graphics>();
  208. using namespace IPCPlayerWindowChanged;
  209. VariantMap data;
  210. data[P_POSX] = graphics->GetWindowPosition().x_;
  211. data[P_POSY] = graphics->GetWindowPosition().y_;
  212. data[P_WIDTH] = graphics->GetWidth();
  213. data[P_HEIGHT] = graphics->GetHeight();
  214. data[P_MONITOR] = graphics->GetCurrentMonitor();
  215. data[P_MAXIMIZED] = graphics->GetMaximized();
  216. ipc_->SendEventToBroker(E_IPCPLAYERWINDOWCHANGED, data);
  217. }
  218. void IPCPlayerApp::HandleUpdatesPausedResumed(StringHash eventType, VariantMap& eventData)
  219. {
  220. ipc_->SendEventToBroker(E_IPCPLAYERUPDATESPAUSEDRESUMED, eventData);
  221. }
  222. void IPCPlayerApp::HandleExitRequest(StringHash eventType, VariantMap& eventData)
  223. {
  224. UnsubscribeFromEvent(E_LOGMESSAGE);
  225. SendEvent(E_PLAYERQUIT);
  226. }
  227. void IPCPlayerApp::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  228. {
  229. using namespace LogMessage;
  230. if (brokerActive_)
  231. {
  232. if (ipc_.Null())
  233. return;
  234. VariantMap logEvent;
  235. logEvent[IPCWorkerLog::P_LEVEL] = eventData[P_LEVEL].GetInt();
  236. logEvent[IPCWorkerLog::P_MESSAGE] = eventData[P_MESSAGE].GetString();
  237. ipc_->SendEventToBroker(E_IPCWORKERLOG, logEvent);
  238. }
  239. }
  240. }