AEEditorMode.cpp 11 KB

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