AEEditorMode.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/Log.h>
  23. #include <Atomic/IPC/IPC.h>
  24. #include <Atomic/IPC/IPCEvents.h>
  25. #include <Atomic/IPC/IPCBroker.h>
  26. #include <Atomic/Core/CoreEvents.h>
  27. #include <Atomic/Input/InputEvents.h>
  28. #include <ToolCore/ToolEnvironment.h>
  29. #include <ToolCore/ToolSystem.h>
  30. #include <ToolCore/License/LicenseSystem.h>
  31. #include <ToolCore/Project/Project.h>
  32. #include <ToolCore/Project/ProjectSettings.h>
  33. #include <ToolCore/NETTools/NETProjectSystem.h>
  34. #include <ToolCore/NETTools/NETBuildSystem.h>
  35. #include <AtomicJS/Javascript/JSIPCEvents.h>
  36. #include <Atomic/UI/SystemUI/DebugHud.h>
  37. #include <AtomicApp/Player/IPCPlayerAppEvents.h>
  38. #include "AEEditorMode.h"
  39. using namespace ToolCore;
  40. namespace AtomicEditor
  41. {
  42. EditorMode::EditorMode(Context* context) :
  43. Object(context),
  44. playerEnabled_(false),
  45. debug_(false)
  46. {
  47. }
  48. EditorMode::~EditorMode()
  49. {
  50. }
  51. void EditorMode::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
  52. {
  53. VariantMap startupData;
  54. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  55. startupData["debugHudMode"] = debugHud ? debugHud->GetMode() : (unsigned) 0;
  56. startupData["debugHudProfilerMode"] = (unsigned) (debugHud ? debugHud->GetProfilerMode() : DEBUG_HUD_PROFILE_PERFORMANCE);
  57. SendEvent(E_EDITORPLAYREQUEST);
  58. playerBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  59. SendEvent(E_EDITORPLAYERSTARTED);
  60. playerEnabled_ = true;
  61. }
  62. void EditorMode::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  63. {
  64. //SendEvent(E_EDITORPLAYSTOP);
  65. if (eventData[IPCWorkerExit::P_BROKER] == playerBroker_)
  66. {
  67. playerBroker_ = 0;
  68. playerEnabled_ = false;
  69. UnsubscribeFromEvent(E_IPCWORKERSTART);
  70. UnsubscribeFromEvent(E_IPCPLAYERPAUSERESUMEREQUEST);
  71. UnsubscribeFromEvent(E_IPCPLAYERUPDATESPAUSEDRESUMED);
  72. UnsubscribeFromEvent(E_IPCPLAYERPAUSESTEPREQUEST);
  73. UnsubscribeFromEvent(E_IPCPLAYEREXITREQUEST);
  74. SendEvent(E_EDITORPLAYERSTOPPED);
  75. }
  76. else
  77. {
  78. ATOMIC_LOGERROR("EditorMode::HandleIPCWorkerExit - Unknown Broker");
  79. }
  80. }
  81. void EditorMode::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  82. {
  83. using namespace IPCWorkerLog;
  84. // convert to a player log
  85. VariantMap playerLogData;
  86. playerLogData["message"] = eventData[P_MESSAGE].GetString();
  87. playerLogData["level"] = eventData[P_LEVEL].GetInt();
  88. SendEvent("EditorPlayerLog", playerLogData);
  89. }
  90. void EditorMode::HandleIPCJSError(StringHash eventType, VariantMap& eventData)
  91. {
  92. }
  93. void EditorMode::HandleNETBuildResult(StringHash eventType, VariantMap& eventData)
  94. {
  95. using namespace NETBuildResult;
  96. if (eventData[P_SUCCESS].GetBool())
  97. {
  98. NETProjectSystem* netProjectSystem = GetSubsystem<NETProjectSystem>();
  99. if (!netProjectSystem->GetSolutionAvailable() || netProjectSystem->GetProjectAssemblyDirty())
  100. {
  101. ATOMIC_LOGERROR("EditorMode::HandleNETBuildResult() - NETBuild was successful, however project still reported as dirty or missing");
  102. }
  103. PlayProjectInternal(additionalArgs_, debug_);
  104. }
  105. additionalArgs_.Clear();
  106. debug_ = false;
  107. }
  108. bool EditorMode::PlayProject(String addArgs, bool debug)
  109. {
  110. additionalArgs_ = addArgs;
  111. debug_ = debug;
  112. NETProjectSystem* netProjectSystem = GetSubsystem<NETProjectSystem>();
  113. // If we're a net project, with a solution, and the project assembly is dirty build before playing
  114. if (netProjectSystem && netProjectSystem->GetSolutionAvailable() && netProjectSystem->GetProjectAssemblyDirty())
  115. {
  116. NETBuild* build = netProjectSystem->BuildAtomicProject();
  117. if (!build)
  118. {
  119. ATOMIC_LOGERROR("EditorMode::PlayProject() - Unable to instantiate C# build");
  120. return false;
  121. }
  122. SubscribeToEvent(build, E_NETBUILDRESULT, ATOMIC_HANDLER(EditorMode, HandleNETBuildResult));
  123. return true;
  124. }
  125. return PlayProjectInternal(addArgs, debug);
  126. }
  127. bool EditorMode::PlayProjectInternal(const String &addArgs, bool debug)
  128. {
  129. if (playerBroker_.NotNull())
  130. return false;
  131. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  132. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  133. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  134. Project* project = tsystem->GetProject();
  135. if (!project)
  136. return false;
  137. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  138. String playerBinary = env->GetEditorBinary();
  139. ProjectSettings* settings = project->GetProjectSettings();
  140. String projectAssembly = settings->GetName() + ".dll";
  141. String projectExe = settings->GetName() + ".exe";
  142. // TODO: We need to configure project as managed
  143. bool managed = false;
  144. if (fileSystem->FileExists(project->GetResourcePath() + projectAssembly))
  145. {
  146. managed = true;
  147. #ifdef ATOMIC_DEV_BUILD
  148. #ifdef ATOMIC_DEBUG
  149. playerBinary = project->GetProjectPath() + "AtomicNET/Debug/Bin/Desktop/" + projectExe;
  150. #else
  151. playerBinary = project->GetProjectPath() + "AtomicNET/Release/Bin/Desktop/" + projectExe;
  152. #endif
  153. #else
  154. // TODO: We are using the release build of the managed project here, how and when to use debug?
  155. playerBinary = project->GetProjectPath() + "AtomicNET/Release/Bin/Desktop/" + projectExe;
  156. #endif
  157. if (!fileSystem->FileExists(playerBinary))
  158. {
  159. ATOMIC_LOGERRORF("Managed player: %s does not exist", playerBinary.CString());
  160. }
  161. }
  162. Vector<String> paths;
  163. paths.Push(env->GetCoreDataDir());
  164. paths.Push(env->GetPlayerDataDir());
  165. paths.Push(project->GetResourcePath());
  166. // fixme: this is for loading from cache
  167. // https://github.com/AtomicGameEngine/AtomicGameEngine/issues/1037
  168. paths.Push(project->GetProjectPath());
  169. paths.Push(project->GetProjectPath() + "Cache");
  170. String resourcePaths;
  171. resourcePaths.Join(paths, "!");
  172. Vector<String> vargs;
  173. String args = ToString("--player --project \"%s\"", AddTrailingSlash(project->GetProjectPath()).CString());
  174. vargs = args.Split(' ');
  175. if (managed)
  176. {
  177. #ifdef ATOMIC_DEV_BUILD
  178. vargs.Insert(0, ToString("\"%s/Resources/\"", tenv->GetRootSourceDir().CString()));
  179. #else
  180. #ifdef ATOMIC_PLATFORM_OSX
  181. vargs.Insert(0, ToString("\"%s\"", (fileSystem->GetProgramDir() + "../Resources/").CString()));
  182. #else
  183. vargs.Insert(0, ToString("\"%s\"", (fileSystem->GetProgramDir() + "Resources/").CString()));
  184. #endif
  185. #endif
  186. vargs.Insert(0, "--resourcePrefix");
  187. }
  188. if (debug)
  189. vargs.Insert(0, "--debug");
  190. // If the debug hud up is up with metrics info, pass the --autometrics to player
  191. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  192. if (debugHud)
  193. {
  194. if (debugHud->GetMode() & Atomic::SystemUI::DEBUGHUD_SHOW_PROFILER)
  195. {
  196. if (debugHud->GetProfilerMode() == DEBUG_HUD_PROFILE_METRICS)
  197. vargs.Insert(0, "--autometrics");
  198. }
  199. }
  200. if (addArgs.Length() > 0)
  201. vargs.Insert(0, addArgs.Split(' '));
  202. #ifndef ATOMIC_PLATFORM_WINDOWS
  203. if (managed)
  204. {
  205. vargs.Insert(0, playerBinary);
  206. #ifdef ATOMIC_PLATFORM_OSX
  207. playerBinary = tenv->GetMonoExecutableDir() + "mono64";
  208. #else
  209. playerBinary = "mono";
  210. #endif
  211. }
  212. #endif
  213. String dump;
  214. dump.Join(vargs, " ");
  215. ATOMIC_LOGINFOF("Launching Broker %s %s", playerBinary.CString(), dump.CString());
  216. IPC* ipc = GetSubsystem<IPC>();
  217. playerBroker_ = ipc->SpawnWorker(playerBinary, vargs);
  218. if (playerBroker_)
  219. {
  220. SubscribeToEvent(playerBroker_, E_IPCWORKERSTART, ATOMIC_HANDLER(EditorMode, HandleIPCWorkerStarted));
  221. SubscribeToEvent(E_IPCPLAYERPAUSERESUMEREQUEST, ATOMIC_HANDLER(EditorMode, HandleIPCPlayerPauseResumeRequest));
  222. SubscribeToEvent(E_IPCPLAYERUPDATESPAUSEDRESUMED, ATOMIC_HANDLER(EditorMode, HandleIPCPlayerUpdatesPausedResumed));
  223. SubscribeToEvent(E_IPCPLAYERPAUSESTEPREQUEST, ATOMIC_HANDLER(EditorMode, HandleIPCPlayerPauseStepRequest));
  224. SubscribeToEvent(E_IPCPLAYEREXITREQUEST, ATOMIC_HANDLER(EditorMode, HandleIPCPlayerExitRequest));
  225. SubscribeToEvent(playerBroker_, E_IPCJSERROR, ATOMIC_HANDLER(EditorMode, HandleIPCJSError));
  226. SubscribeToEvent(playerBroker_, E_IPCWORKEREXIT, ATOMIC_HANDLER(EditorMode, HandleIPCWorkerExit));
  227. SubscribeToEvent(playerBroker_, E_IPCWORKERLOG, ATOMIC_HANDLER(EditorMode, HandleIPCWorkerLog));
  228. }
  229. return playerBroker_.NotNull();
  230. }
  231. void EditorMode::HandleIPCPlayerPauseResumeRequest(StringHash eventType, VariantMap& eventData)
  232. {
  233. if (!playerBroker_) return;
  234. VariantMap noEventData;
  235. playerBroker_->PostMessage(E_PAUSERESUMEREQUESTED, noEventData);
  236. }
  237. void EditorMode::HandleIPCPlayerUpdatesPausedResumed(StringHash eventType, VariantMap& eventData)
  238. {
  239. using namespace UpdatesPaused;
  240. bool paused = eventData[P_PAUSED].GetBool();
  241. if (paused)
  242. SendEvent(E_EDITORPLAYERPAUSED);
  243. else
  244. SendEvent(E_EDITORPLAYERRESUMED);
  245. }
  246. void EditorMode::HandleIPCPlayerPauseStepRequest(StringHash eventType, VariantMap& eventData)
  247. {
  248. if (!playerBroker_) return;
  249. VariantMap noEventData;
  250. playerBroker_->PostMessage(E_PAUSESTEPREQUESTED, noEventData);
  251. }
  252. void EditorMode::HandleIPCPlayerExitRequest(StringHash eventType, VariantMap& eventData)
  253. {
  254. if (!playerBroker_) return;
  255. VariantMap noEventData;
  256. playerBroker_->PostMessage(E_EXITREQUESTED, noEventData);
  257. }
  258. bool EditorMode::IsPlayerEnabled()
  259. {
  260. return playerEnabled_;
  261. }
  262. }