ScriptEventsBuilderWorker.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AssetBuilderSDK/AssetBuilderBusses.h>
  9. #include <AzCore/Asset/AssetDataStream.h>
  10. #include <AzCore/Asset/AssetManager.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzCore/IO/IOUtils.h>
  13. #include <AzCore/IO/SystemFile.h>
  14. #include <AzCore/Math/Uuid.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/std/smart_ptr/make_shared.h>
  17. #include <AzFramework/StringFunc/StringFunc.h>
  18. #include <Builder/ScriptEventsBuilderWorker.h>
  19. #include <Editor/ScriptEventsSystemEditorComponent.h>
  20. #include <ScriptEvents/ScriptEventsAsset.h>
  21. namespace ScriptEventsBuilder
  22. {
  23. [[maybe_unused]] static const char* s_scriptEventsBuilder = "ScriptEventsBuilder";
  24. Worker::Worker()
  25. {
  26. }
  27. Worker::~Worker()
  28. {
  29. Deactivate();
  30. }
  31. int Worker::GetVersionNumber() const
  32. {
  33. return 1;
  34. }
  35. const char* Worker::GetFingerprintString() const
  36. {
  37. if (m_fingerprintString.empty())
  38. {
  39. // compute it the first time
  40. const AZStd::string runtimeAssetTypeId = azrtti_typeid<ScriptEvents::ScriptEventsAsset>().ToString<AZStd::string>();
  41. m_fingerprintString = AZStd::string::format("%i%s", GetVersionNumber(), runtimeAssetTypeId.c_str());
  42. }
  43. return m_fingerprintString.c_str();
  44. }
  45. void Worker::Activate()
  46. {
  47. }
  48. void Worker::Deactivate()
  49. {
  50. }
  51. void Worker::ShutDown()
  52. {
  53. m_isShuttingDown = true;
  54. }
  55. void Worker::CreateJobs(const AssetBuilderSDK::CreateJobsRequest& request, AssetBuilderSDK::CreateJobsResponse& response) const
  56. {
  57. AZStd::string fullPath;
  58. AzFramework::StringFunc::Path::ConstructFull(request.m_watchFolder.data(), request.m_sourceFile.data(), fullPath, false);
  59. AzFramework::StringFunc::Path::Normalize(fullPath);
  60. AZ_TracePrintf(s_scriptEventsBuilder, "CreateJobs for script events\"%s\"\n", fullPath.data());
  61. AZ::Data::AssetHandler* editorAssetHandler = AZ::Data::AssetManager::Instance().GetHandler(azrtti_typeid<ScriptEvents::ScriptEventsAsset>());
  62. if (!editorAssetHandler)
  63. {
  64. AZ_Error(s_scriptEventsBuilder, false, R"(CreateJobs for %s failed because the ScriptEvents Editor Asset handler is missing.)", fullPath.data());
  65. }
  66. AZStd::shared_ptr<AZ::Data::AssetDataStream> assetDataStream = AZStd::make_shared<AZ::Data::AssetDataStream>();
  67. // Read the asset into a memory buffer, then hand ownership of the buffer to assetDataStream
  68. {
  69. AZ::IO::FileIOStream stream(fullPath.c_str(), AZ::IO::OpenMode::ModeRead);
  70. if (!AZ::IO::RetryOpenStream(stream))
  71. {
  72. AZ_Warning(s_scriptEventsBuilder, false, "CreateJobs for \"%s\" failed because the source file could not be opened.", fullPath.data());
  73. return;
  74. }
  75. AZStd::vector<AZ::u8> fileBuffer(stream.GetLength());
  76. size_t bytesRead = stream.Read(fileBuffer.size(), fileBuffer.data());
  77. if (bytesRead != stream.GetLength())
  78. {
  79. AZ_Warning(s_scriptEventsBuilder, false, "CreateJobs for \"%s\" failed because the source file could not be read.", fullPath.data());
  80. return;
  81. }
  82. assetDataStream->Open(AZStd::move(fileBuffer));
  83. }
  84. AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> asset;
  85. asset.Create(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
  86. if (editorAssetHandler->LoadAssetDataFromStream(asset, assetDataStream, nullptr) != AZ::Data::AssetHandler::LoadResult::LoadComplete)
  87. {
  88. AZ_Warning(s_scriptEventsBuilder, false, "CreateJobs for \"%s\" failed because the asset data could not be loaded from the file", fullPath.data());
  89. return;
  90. }
  91. // Flush asset database events to ensure no asset references are held by closures queued on Ebuses.
  92. AZ::Data::AssetManager::Instance().DispatchEvents();
  93. for (const AssetBuilderSDK::PlatformInfo& info : request.m_enabledPlatforms)
  94. {
  95. AssetBuilderSDK::JobDescriptor jobDescriptor;
  96. jobDescriptor.m_priority = 2;
  97. jobDescriptor.m_critical = true;
  98. jobDescriptor.m_jobKey = ScriptEvents::k_builderJobKey;
  99. jobDescriptor.SetPlatformIdentifier(info.m_identifier.data());
  100. jobDescriptor.m_additionalFingerprintInfo = GetFingerprintString();
  101. response.m_createJobOutputs.push_back(jobDescriptor);
  102. }
  103. response.m_result = AssetBuilderSDK::CreateJobsResultCode::Success;
  104. }
  105. void Worker::ProcessJob(const AssetBuilderSDK::ProcessJobRequest& request, AssetBuilderSDK::ProcessJobResponse& response) const
  106. {
  107. // A runtime script events component is generated, which creates a .scriptevents_compiled file
  108. AZStd::string fullPath;
  109. AZStd::string fileNameOnly;
  110. AzFramework::StringFunc::Path::GetFullFileName(request.m_sourceFile.c_str(), fileNameOnly);
  111. fullPath = request.m_fullPath.c_str();
  112. AzFramework::StringFunc::Path::Normalize(fullPath);
  113. AZ_TracePrintf(s_scriptEventsBuilder, "Processing script events \"%s\".\n", fullPath.c_str());
  114. auto editorAssetHandler = azrtti_cast<ScriptEventsEditor::ScriptEventAssetHandler*>(
  115. AZ::Data::AssetManager::Instance().GetHandler(azrtti_typeid<ScriptEvents::ScriptEventsAsset>()));
  116. if (!editorAssetHandler)
  117. {
  118. AZ_Error(s_scriptEventsBuilder, false, R"(Exporting of .ScriptEvents for "%s" file failed as no editor asset handler was registered for ScriptEvents. The ScriptEvents Gem might not be enabled.)", fullPath.data());
  119. return;
  120. }
  121. AZStd::shared_ptr<AZ::Data::AssetDataStream> assetDataStream = AZStd::make_shared<AZ::Data::AssetDataStream>();
  122. // Read the asset into a memory buffer, then hand ownership of the buffer to assetDataStream
  123. {
  124. AZ::IO::FileIOStream stream(fullPath.c_str(), AZ::IO::OpenMode::ModeRead);
  125. if (!stream.IsOpen())
  126. {
  127. AZ_Warning(s_scriptEventsBuilder, false, "Exporting of .ScriptEvents for \"%s\" failed because the source file could not be opened.", fullPath.c_str());
  128. return;
  129. }
  130. AZStd::vector<AZ::u8> fileBuffer(stream.GetLength());
  131. size_t bytesRead = stream.Read(fileBuffer.size(), fileBuffer.data());
  132. if (bytesRead != stream.GetLength())
  133. {
  134. AZ_Warning(s_scriptEventsBuilder, false, "Exporting of .ScriptEvents for \"%s\" failed because the source file could not be read.", fullPath.c_str());
  135. return;
  136. }
  137. assetDataStream->Open(AZStd::move(fileBuffer));
  138. }
  139. AZ::SerializeContext* context{};
  140. AZ::ComponentApplicationBus::BroadcastResult(context, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  141. AZStd::vector<AssetBuilderSDK::ProductDependency> productDependencies;
  142. AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset preload\n");
  143. AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> asset;
  144. asset.Create(request.m_sourceFileUUID);
  145. if (editorAssetHandler->LoadAssetData(asset, assetDataStream, nullptr) != AZ::Data::AssetHandler::LoadResult::LoadComplete)
  146. {
  147. AZ_Error(s_scriptEventsBuilder, false, R"(Loading of ScriptEvents asset for source file "%s" has failed)", fullPath.data());
  148. return;
  149. }
  150. AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset loaded successfully\n");
  151. // Flush asset manager events to ensure no asset references are held by closures queued on Ebuses.
  152. AZ::Data::AssetManager::Instance().DispatchEvents();
  153. AZStd::string runtimeScriptEventsOutputPath;
  154. AzFramework::StringFunc::Path::Join(request.m_tempDirPath.c_str(), fileNameOnly.c_str(), runtimeScriptEventsOutputPath, true, true);
  155. ScriptEvents::ScriptEvent definition = asset.Get()->m_definition;
  156. definition.Flatten();
  157. // Populate the runtime Asset
  158. AZStd::vector<AZ::u8> byteBuffer;
  159. AZ::IO::ByteContainerStream<decltype(byteBuffer)> byteStream(&byteBuffer);
  160. AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> productionAsset;
  161. productionAsset.Create(request.m_sourceFileUUID);
  162. productionAsset.Get()->m_definition = AZStd::move(definition);
  163. editorAssetHandler->SetSaveAsBinary(true);
  164. AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset presave to object stream for %s\n", fullPath.c_str());
  165. bool productionAssetSaved = editorAssetHandler->SaveAssetData(productionAsset, &byteStream);
  166. if (!productionAssetSaved)
  167. {
  168. AZ_Error(s_scriptEventsBuilder, productionAssetSaved, "Failed to save runtime Script Events to object stream");
  169. return;
  170. }
  171. AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset has been saved to the object stream successfully\n");
  172. // TODO: make this binary
  173. AZ::IO::FileIOStream outFileStream(runtimeScriptEventsOutputPath.data(), AZ::IO::OpenMode::ModeWrite);
  174. if (!outFileStream.IsOpen())
  175. {
  176. AZ_Error(s_scriptEventsBuilder, false, "Failed to open output file %s", runtimeScriptEventsOutputPath.data());
  177. return;
  178. }
  179. productionAssetSaved = outFileStream.Write(byteBuffer.size(), byteBuffer.data()) == byteBuffer.size() && productionAssetSaved;
  180. if (!productionAssetSaved)
  181. {
  182. AZ_Error(s_scriptEventsBuilder, productionAssetSaved, "Unable to save runtime Script Events file %s", runtimeScriptEventsOutputPath.data());
  183. return;
  184. }
  185. // ScriptEvents Editor Asset Copy job
  186. // The SubID is zero as this represents the main asset
  187. AssetBuilderSDK::JobProduct jobProduct;
  188. jobProduct.m_productFileName = runtimeScriptEventsOutputPath;
  189. jobProduct.m_productAssetType = azrtti_typeid<ScriptEvents::ScriptEventsAsset>();
  190. jobProduct.m_productSubID = 0;
  191. jobProduct.m_dependenciesHandled = true; // This builder has no product dependencies.
  192. response.m_outputProducts.push_back(AZStd::move(jobProduct));
  193. response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Success;
  194. AZ_TracePrintf(s_scriptEventsBuilder, "Finished processing Script Events %s\n", fullPath.c_str());
  195. }
  196. AZ::Uuid Worker::GetUUID()
  197. {
  198. return AZ::Uuid::CreateString("{CD64F85A-0147-45EF-B02A-9828E25D99EB}");
  199. }
  200. }