GlowService.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // Copyright (c) 2014-2017 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/IO/FileSystem.h>
  24. #include <Atomic/IPC/IPCEvents.h>
  25. #include <Atomic/Graphics/StaticModel.h>
  26. #include "GlowProcess.h"
  27. #include "GlowServiceEvents.h"
  28. #include "GlowService.h"
  29. namespace AtomicGlow
  30. {
  31. GlowService::GlowService(Context* context) : Object(context),
  32. bakeCanceled_(false)
  33. {
  34. }
  35. GlowService::~GlowService()
  36. {
  37. }
  38. void GlowService::OnGlowProcessExited()
  39. {
  40. if (glowProcess_.NotNull())
  41. {
  42. if (!glowProcess_->GetExitCalled() || bakeCanceled_)
  43. {
  44. using namespace AtomicGlowServiceBakeResult;
  45. VariantMap eventData;
  46. eventData[P_RESULT] = bakeCanceled_ ? "Bake canceled" : "GlowService::OnGlowProcessExited() - Glow process exited unexpectedly";
  47. eventData[P_SUCCESS] = false;
  48. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  49. }
  50. }
  51. if (scene_)
  52. {
  53. scene_->LoadLightmaps(true);
  54. }
  55. projectPath_ = String::EMPTY;
  56. glowProcess_ = 0;
  57. scene_ = 0;
  58. }
  59. void GlowService::OnBakeError(const String& result)
  60. {
  61. ATOMIC_LOGERRORF("%s", result.CString());
  62. glowProcess_->Exit();
  63. using namespace AtomicGlowServiceBakeResult;
  64. VariantMap eventData;
  65. eventData[P_RESULT] = result;
  66. eventData[P_SUCCESS] = false;
  67. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  68. }
  69. void GlowService::OnBakeSuccess()
  70. {
  71. glowProcess_->Exit();
  72. using namespace AtomicGlowServiceBakeResult;
  73. VariantMap eventData;
  74. eventData[P_RESULT] = "success";
  75. eventData[P_SUCCESS] = true;
  76. SendEvent(E_ATOMICGLOWSERVICEBAKERESULT, eventData);
  77. }
  78. void GlowService::ProcessBakeData(VectorBuffer& bakeData)
  79. {
  80. if (!scene_)
  81. {
  82. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - called with null scene");
  83. return;
  84. }
  85. unsigned count = bakeData.ReadUInt();
  86. PODVector<Node*> children;
  87. scene_->GetChildrenWithComponent<StaticModel>(children, true);
  88. for (unsigned i = 0; i < count; i++)
  89. {
  90. Node* node = 0;
  91. StaticModel* staticModel = 0;
  92. unsigned nodeID = bakeData.ReadUInt();
  93. unsigned staticModelID = bakeData.ReadUInt();
  94. unsigned lightMask = bakeData.ReadUInt();
  95. unsigned lightmapIndex = bakeData.ReadUInt();
  96. Vector4 tilingOffset = bakeData.ReadVector4();
  97. for (unsigned j = 0; j < children.Size(); j++)
  98. {
  99. if (children[j]->GetID() == nodeID)
  100. {
  101. node = children[j];
  102. staticModel = node->GetComponent<StaticModel>();
  103. if (!staticModel || staticModel->GetID() != staticModelID)
  104. {
  105. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - mismatched node <-> static model ID");
  106. return;
  107. }
  108. break;
  109. }
  110. }
  111. if (!node)
  112. {
  113. ATOMIC_LOGERROR("GlowService::ProcessBakeData() - unable to find node ID");
  114. return;
  115. }
  116. staticModel->SetLightMask(lightMask);
  117. staticModel->SetLightmapIndex(lightmapIndex);
  118. staticModel->SetLightmapTilingOffset(tilingOffset);
  119. }
  120. }
  121. bool GlowService::Bake(const String& projectPath, Scene* scene, const GlowSettings& settings)
  122. {
  123. if (!scene)
  124. {
  125. ATOMIC_LOGERROR("GlowService::Bake() - Called with null scene");
  126. return false;
  127. }
  128. String sceneName = scene->GetFileName();
  129. if (!sceneName.Length())
  130. {
  131. ATOMIC_LOGERROR("GlowService::Bake() - Called with unnamed scene");
  132. return false;
  133. }
  134. if (!projectPath.Length())
  135. {
  136. ATOMIC_LOGERROR("GlowService::Bake() - zero length projectPath");
  137. return false;
  138. }
  139. if (glowProcess_.NotNull())
  140. {
  141. ATOMIC_LOGERROR("GlowService::Bake() - Called with existing glow process");
  142. return false;
  143. }
  144. if (!glowBinaryPath_.Length())
  145. {
  146. ATOMIC_LOGERROR("GlowService::Bake() - Called with empty glowBinaryPath_");
  147. return false;
  148. }
  149. bakeCanceled_ = false;
  150. result_.Clear();
  151. glowProcess_ = new GlowProcess(context_);
  152. projectPath_ = projectPath;
  153. StringVector args;
  154. args.Push("--project");
  155. args.Push(projectPath_);
  156. args += glowBaseArgs_;
  157. if (!glowProcess_->Start(glowBinaryPath_, args))
  158. {
  159. ATOMIC_LOGERRORF("GlowService::Bake() - Glow process failed to start: %s", glowBinaryPath_.CString());
  160. return false;
  161. }
  162. scene_ = scene;
  163. projectPath_ = projectPath;
  164. using namespace IPCCmd;
  165. VariantMap cmdMap;
  166. cmdMap[P_COMMAND] = "bake";
  167. cmdMap["scenename"] = sceneName;
  168. // settings
  169. VectorBuffer settingsBuffer;
  170. settings.Pack(settingsBuffer);
  171. cmdMap["settings"] = settingsBuffer;
  172. glowProcess_->QueueCommand(cmdMap);
  173. return true;
  174. }
  175. void GlowService::CancelBake()
  176. {
  177. if (glowProcess_.Null())
  178. {
  179. ATOMIC_LOGERROR("GlowService::CancelBake() - Called without existing glow process");
  180. return;
  181. }
  182. bakeCanceled_ = true;
  183. glowProcess_->Exit();
  184. }
  185. bool GlowService::LocateServiceExecutable()
  186. {
  187. glowBinaryPath_ = String::EMPTY;
  188. glowBaseArgs_.Clear();
  189. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  190. #ifdef ATOMIC_DEV_BUILD
  191. String rootSourceDir = ATOMIC_ROOT_SOURCE_DIR;
  192. rootSourceDir = AddTrailingSlash(rootSourceDir);
  193. glowBinaryPath_ = rootSourceDir + "Artifacts/Build/AtomicGlow/AtomicGlow";
  194. #else
  195. #ifdef ATOMIC_PLATFORM_OSX
  196. String resourcesDir = GetPath(RemoveTrailingSlash(fileSystem->GetProgramDir())) + "Resources/";
  197. #else
  198. String resourcesDir = fileSystem->GetProgramDir() + "Resources/";
  199. #endif
  200. glowBinaryPath_ = resourcesDir + "ToolData/AtomicGlow/AtomicGlow";
  201. #endif
  202. #ifdef ATOMIC_PLATFORM_WINDOWS
  203. glowBinaryPath_ += ".exe";
  204. #endif
  205. if (!fileSystem->FileExists(glowBinaryPath_))
  206. {
  207. ATOMIC_LOGERRORF("AtomicGlow binary not found: %s", glowBinaryPath_.CString());
  208. glowBinaryPath_.Clear();
  209. glowBaseArgs_.Clear();
  210. return false;
  211. }
  212. return true;
  213. }
  214. bool GlowService::GetServiceExecutable(String& execPath, Vector<String>& baseArgs) const
  215. {
  216. execPath.Clear();
  217. baseArgs.Clear();
  218. if (!glowBinaryPath_.Length())
  219. {
  220. return false;
  221. }
  222. execPath = glowBinaryPath_;
  223. baseArgs = glowBaseArgs_;
  224. return true;
  225. }
  226. bool GlowService::Start()
  227. {
  228. if (!LocateServiceExecutable())
  229. {
  230. ATOMIC_LOGERROR("GlowService::Start() - Unable to start AtomicGlow service");
  231. return false;
  232. }
  233. return true;
  234. }
  235. }