AEEditorMode.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/Input/InputEvents.h>
  27. #include <ToolCore/ToolEnvironment.h>
  28. #include <ToolCore/ToolSystem.h>
  29. #include <ToolCore/License/LicenseSystem.h>
  30. #include <ToolCore/Project/Project.h>
  31. #include <AtomicJS/Javascript/JSIPCEvents.h>
  32. #include <Atomic/UI/SystemUI/DebugHud.h>
  33. #include "../PlayerMode/AEPlayerEvents.h"
  34. #include "AEEditorMode.h"
  35. using namespace ToolCore;
  36. namespace AtomicEditor
  37. {
  38. EditorMode::EditorMode(Context* context) :
  39. Object(context)
  40. {
  41. SubscribeToEvent(E_IPCWORKERSTART, HANDLER(EditorMode, HandleIPCWorkerStarted));
  42. SubscribeToEvent(E_IPCPLAYEREXITREQUEST, HANDLER(EditorMode, HandleIPCPlayerExitRequest));
  43. }
  44. EditorMode::~EditorMode()
  45. {
  46. }
  47. void EditorMode::HandleIPCWorkerStarted(StringHash eventType, VariantMap& eventData)
  48. {
  49. LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
  50. VariantMap startupData;
  51. SystemUI::DebugHud* debugHud = GetSubsystem<SystemUI::DebugHud>();
  52. // BEGIN LICENSE MANAGEMENT
  53. startupData["license3D"] = licenseSystem->GetLicenseModule3D();
  54. // END LICENSE MANAGEMENT
  55. startupData["debugHudMode"] = debugHud ? debugHud->GetMode() : (unsigned) 0;
  56. SendEvent(E_EDITORPLAYREQUEST);
  57. playerBroker_->PostMessage(E_IPCINITIALIZE, startupData);
  58. SendEvent(E_EDITORPLAYERSTARTED);
  59. playerEnabled_ = true;
  60. }
  61. void EditorMode::HandleIPCWorkerExit(StringHash eventType, VariantMap& eventData)
  62. {
  63. //SendEvent(E_EDITORPLAYSTOP);
  64. if (eventData[IPCWorkerExit::P_BROKER] == playerBroker_)
  65. {
  66. playerBroker_ = 0;
  67. playerEnabled_ = false;
  68. SendEvent(E_EDITORPLAYERSTOPPED);
  69. }
  70. }
  71. void EditorMode::HandleIPCWorkerLog(StringHash eventType, VariantMap& eventData)
  72. {
  73. using namespace IPCWorkerLog;
  74. // convert to a player log
  75. VariantMap playerLogData;
  76. playerLogData["message"] = eventData[P_MESSAGE].GetString();
  77. playerLogData["level"] = eventData[P_LEVEL].GetInt();
  78. SendEvent("EditorPlayerLog", playerLogData);
  79. }
  80. void EditorMode::HandleIPCJSError(StringHash eventType, VariantMap& eventData)
  81. {
  82. }
  83. bool EditorMode::PlayProject(String addArgs, bool debug)
  84. {
  85. ToolEnvironment* env = GetSubsystem<ToolEnvironment>();
  86. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  87. const String& editorBinary = env->GetEditorBinary();
  88. Project* project = tsystem->GetProject();
  89. if (!project)
  90. return false;
  91. Vector<String> paths;
  92. paths.Push(env->GetCoreDataDir());
  93. paths.Push(env->GetPlayerDataDir());
  94. paths.Push(project->GetResourcePath());
  95. // fixme: this is for loading from cache
  96. paths.Push(project->GetProjectPath());
  97. paths.Push(project->GetProjectPath() + "Cache");
  98. String resourcePaths;
  99. resourcePaths.Join(paths, "!");
  100. Vector<String> vargs;
  101. String args = ToString("--player --project \"%s\"", AddTrailingSlash(project->GetProjectPath()).CString());
  102. vargs = args.Split(' ');
  103. if (debug)
  104. vargs.Insert(0, "--debug");
  105. if (addArgs.Length() > 0)
  106. vargs.Insert(0, addArgs.Split(' '));
  107. String dump;
  108. dump.Join(vargs, " ");
  109. LOGINFOF("Launching Broker %s %s", editorBinary.CString(), dump.CString());
  110. IPC* ipc = GetSubsystem<IPC>();
  111. playerBroker_ = ipc->SpawnWorker(editorBinary, vargs);
  112. if (playerBroker_)
  113. {
  114. SubscribeToEvent(playerBroker_, E_IPCJSERROR, HANDLER(EditorMode, HandleIPCJSError));
  115. SubscribeToEvent(playerBroker_, E_IPCWORKEREXIT, HANDLER(EditorMode, HandleIPCWorkerExit));
  116. SubscribeToEvent(playerBroker_, E_IPCWORKERLOG, HANDLER(EditorMode, HandleIPCWorkerLog));
  117. }
  118. return playerBroker_.NotNull();
  119. }
  120. void EditorMode::HandleIPCPlayerExitRequest(StringHash eventType, VariantMap& eventData)
  121. {
  122. if (!playerBroker_) return;
  123. VariantMap noEventData;
  124. playerBroker_->PostMessage(E_EXITREQUESTED, noEventData);
  125. }
  126. bool EditorMode::IsPlayerEnabled()
  127. {
  128. return playerEnabled_;
  129. }
  130. }