Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,44 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
namespace ScriptEventsBuilder
{
class BuilderSystemComponent
: public AZ::Component
{
public:
AZ_COMPONENT(BuilderSystemComponent, "{6CE4EF5D-5A18-4E25-A676-501644676B58}");
BuilderSystemComponent();
~BuilderSystemComponent() override;
static void Reflect(AZ::ReflectContext* context);
static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
////////////////////////////////////////////////////////////////////////
// AZ::Component interface implementation
void Init() override;
void Activate() override;
void Deactivate() override;
////////////////////////////////////////////////////////////////////////
private:
BuilderSystemComponent(const BuilderSystemComponent&) = delete;
};
}
@@ -0,0 +1,83 @@
/*
* All or portions of this file Copyright(c) Amazon.com, Inc.or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution(the "License").All use of this software is governed by the License,
*or, if provided, by the license below or the license accompanying this file.Do not
* remove or modify any license notices.This file is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <precompiled.h>
#include <Builder/ScriptEventsBuilderComponent.h>
#include <AssetBuilderSDK/AssetBuilderBusses.h>
#include <AzToolsFramework/ToolsComponents/ToolsAssetCatalogBus.h>
#include <ScriptEvents/ScriptEventsAsset.h>
namespace ScriptEventsBuilder
{
void ScriptEventsBuilderComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
{
provided.push_back(AZ_CRC("ScriptEventsBuilderService", 0x049e945c));
}
void ScriptEventsBuilderComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
{
AZ_UNUSED(required);
}
void ScriptEventsBuilderComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
{
dependent.push_back(AZ_CRC("AssetCatalogService", 0xc68ffc57));
}
void ScriptEventsBuilderComponent::Activate()
{
// Register ScriptEvents Builder
AssetBuilderSDK::AssetBuilderDesc builderDescriptor;
builderDescriptor.m_name = "Script Events Builder";
builderDescriptor.m_patterns.push_back(AssetBuilderSDK::AssetBuilderPattern("*.scriptevents", AssetBuilderSDK::AssetBuilderPattern::PatternType::Wildcard));
builderDescriptor.m_busId = ScriptEventsBuilder::Worker::GetUUID();
builderDescriptor.m_createJobFunction = AZStd::bind(&Worker::CreateJobs, &m_scriptEventsBuilder, AZStd::placeholders::_1, AZStd::placeholders::_2);
builderDescriptor.m_processJobFunction = AZStd::bind(&Worker::ProcessJob, &m_scriptEventsBuilder, AZStd::placeholders::_1, AZStd::placeholders::_2);
// changing the version number invalidates all assets and will rebuild everything.
builderDescriptor.m_version = m_scriptEventsBuilder.GetVersionNumber();
// changing the analysis fingerprint just invalidates analysis (ie, not the assets themselves)
// which will cause the "CreateJobs" function to be called, for each asset, even if the
// source file has not changed, but won't actually do the jobs unless the source file has changed
// or the fingerprint of the individual job is different.
builderDescriptor.m_analysisFingerprint = m_scriptEventsBuilder.GetFingerprintString();
m_scriptEventsBuilder.BusConnect(builderDescriptor.m_busId);
AssetBuilderSDK::AssetBuilderBus::Broadcast(&AssetBuilderSDK::AssetBuilderBus::Handler::RegisterBuilderInformation, builderDescriptor);
AzToolsFramework::ToolsAssetSystemBus::Broadcast(&AzToolsFramework::ToolsAssetSystemRequests::RegisterSourceAssetType, azrtti_typeid<ScriptEvents::ScriptEventsAsset>(), ScriptEvents::ScriptEventsAsset::GetFileFilter());
m_scriptEventsBuilder.Activate();
}
void ScriptEventsBuilderComponent::Deactivate()
{
// Finish all queued work
AZ::Data::AssetBus::ExecuteQueuedEvents();
AzToolsFramework::ToolsAssetSystemBus::Broadcast(&AzToolsFramework::ToolsAssetSystemRequests::UnregisterSourceAssetType, azrtti_typeid<ScriptEvents::ScriptEventsAsset>());
m_scriptEventsBuilder.Deactivate();
m_scriptEventsBuilder.BusDisconnect();
}
void ScriptEventsBuilderComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<ScriptEventsBuilderComponent, AZ::Component>()
->Version(0)
->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }));
;
}
}
}
@@ -0,0 +1,50 @@
/*
* All or portions of this file Copyright(c) Amazon.com, Inc.or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution(the "License").All use of this software is governed by the License,
*or, if provided, by the license below or the license accompanying this file.Do not
* remove or modify any license notices.This file is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include "ScriptEventsBuilderWorker.h"
#include <AssetBuilderSDK/AssetBuilderBusses.h>
#include <AssetBuilderSDK/AssetBuilderSDK.h>
#include <AzCore/Component/Component.h>
namespace ScriptEventsBuilder
{
//! ScriptEventsBuilder is responsible for turning editor ScriptEvents Assets into runtime script canvas assets
class ScriptEventsBuilderComponent
: public AZ::Component
{
public:
AZ_COMPONENT(ScriptEventsBuilderComponent, "{A402F019-0DD4-4CFF-B8A0-A90F818021E4}")
static void Reflect(AZ::ReflectContext* context);
ScriptEventsBuilderComponent() = default;
~ScriptEventsBuilderComponent() override = default;
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
//////////////////////////////////////////////////////////////////////////
// AZ::Component
void Activate() override;
void Deactivate() override;
//////////////////////////////////////////////////////////////////////////
private:
ScriptEventsBuilderComponent(const ScriptEventsBuilderComponent&) = delete;
Worker m_scriptEventsBuilder;
};
}
@@ -0,0 +1,258 @@
/*
* All or portions of this file Copyright(c) Amazon.com, Inc.or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution(the "License").All use of this software is governed by the License,
*or, if provided, by the license below or the license accompanying this file.Do not
* remove or modify any license notices.This file is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "precompiled.h"
#include <AssetBuilderSDK/AssetBuilderBusses.h>
#include <AzCore/Asset/AssetDataStream.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/IO/FileIO.h>
#include <AzCore/IO/IOUtils.h>
#include <AzCore/IO/SystemFile.h>
#include <AzCore/Math/Uuid.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include <AzFramework/StringFunc/StringFunc.h>
#include <Builder/ScriptEventsBuilderWorker.h>
#include <Editor/ScriptEventsSystemEditorComponent.h>
#include <ScriptEvents/ScriptEventsAsset.h>
namespace ScriptEventsBuilder
{
static const char* s_scriptEventsBuilder = "ScriptEventsBuilder";
Worker::Worker()
{
}
Worker::~Worker()
{
Deactivate();
}
int Worker::GetVersionNumber() const
{
return 1;
}
const char* Worker::GetFingerprintString() const
{
if (m_fingerprintString.empty())
{
// compute it the first time
const AZStd::string runtimeAssetTypeId = azrtti_typeid<ScriptEvents::ScriptEventsAsset>().ToString<AZStd::string>();
m_fingerprintString = AZStd::string::format("%i%s", GetVersionNumber(), runtimeAssetTypeId.c_str());
}
return m_fingerprintString.c_str();
}
void Worker::Activate()
{
}
void Worker::Deactivate()
{
}
void Worker::ShutDown()
{
m_isShuttingDown = true;
}
void Worker::CreateJobs(const AssetBuilderSDK::CreateJobsRequest& request, AssetBuilderSDK::CreateJobsResponse& response) const
{
AZStd::string fullPath;
AzFramework::StringFunc::Path::ConstructFull(request.m_watchFolder.data(), request.m_sourceFile.data(), fullPath, false);
AzFramework::StringFunc::Path::Normalize(fullPath);
AZ_TracePrintf(s_scriptEventsBuilder, "CreateJobs for script events\"%s\"\n", fullPath.data());
AZ::Data::AssetHandler* editorAssetHandler = AZ::Data::AssetManager::Instance().GetHandler(azrtti_typeid<ScriptEvents::ScriptEventsAsset>());
if (!editorAssetHandler)
{
AZ_Error(s_scriptEventsBuilder, false, R"(CreateJobs for %s failed because the ScriptEvents Editor Asset handler is missing.)", fullPath.data());
}
AZStd::shared_ptr<AZ::Data::AssetDataStream> assetDataStream = AZStd::make_shared<AZ::Data::AssetDataStream>();
// Read the asset into a memory buffer, then hand ownership of the buffer to assetDataStream
{
AZ::IO::FileIOStream stream(fullPath.c_str(), AZ::IO::OpenMode::ModeRead);
if (!AZ::IO::RetryOpenStream(stream))
{
AZ_Warning(s_scriptEventsBuilder, false, "CreateJobs for \"%s\" failed because the source file could not be opened.", fullPath.data());
return;
}
AZStd::vector<AZ::u8> fileBuffer(stream.GetLength());
size_t bytesRead = stream.Read(fileBuffer.size(), fileBuffer.data());
if (bytesRead != stream.GetLength())
{
AZ_Warning(s_scriptEventsBuilder, false, "CreateJobs for \"%s\" failed because the source file could not be read.", fullPath.data());
return;
}
assetDataStream->Open(AZStd::move(fileBuffer));
}
AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> asset;
asset.Create(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
if (editorAssetHandler->LoadAssetDataFromStream(asset, assetDataStream, nullptr) != AZ::Data::AssetHandler::LoadResult::LoadComplete)
{
AZ_Warning(s_scriptEventsBuilder, false, "CreateJobs for \"%s\" failed because the asset data could not be loaded from the file", fullPath.data());
return;
}
// Flush asset database events to ensure no asset references are held by closures queued on Ebuses.
AZ::Data::AssetManager::Instance().DispatchEvents();
for (const AssetBuilderSDK::PlatformInfo& info : request.m_enabledPlatforms)
{
AssetBuilderSDK::JobDescriptor jobDescriptor;
jobDescriptor.m_priority = 2;
jobDescriptor.m_critical = true;
jobDescriptor.m_jobKey = "Script Events";
jobDescriptor.SetPlatformIdentifier(info.m_identifier.data());
jobDescriptor.m_additionalFingerprintInfo = GetFingerprintString();
response.m_createJobOutputs.push_back(jobDescriptor);
}
response.m_result = AssetBuilderSDK::CreateJobsResultCode::Success;
}
void Worker::ProcessJob(const AssetBuilderSDK::ProcessJobRequest& request, AssetBuilderSDK::ProcessJobResponse& response) const
{
// A runtime script events component is generated, which creates a .scriptevents_compiled file
AZStd::string fullPath;
AZStd::string fileNameOnly;
AzFramework::StringFunc::Path::GetFullFileName(request.m_sourceFile.c_str(), fileNameOnly);
fullPath = request.m_fullPath.c_str();
AzFramework::StringFunc::Path::Normalize(fullPath);
AZ_TracePrintf(s_scriptEventsBuilder, "Processing script events \"%s\".\n", fullPath.c_str());
auto editorAssetHandler = azrtti_cast<ScriptEventsEditor::ScriptEventAssetHandler*>(
AZ::Data::AssetManager::Instance().GetHandler(azrtti_typeid<ScriptEvents::ScriptEventsAsset>()));
if (!editorAssetHandler)
{
AZ_Error(s_scriptEventsBuilder, false, R"(Exporting of .ScriptEvents for "%s" file failed as no editor asset handler was registered for scrit events. The ScriptEvents Gem might not be enabled.)", fullPath.data());
return;
}
AZStd::shared_ptr<AZ::Data::AssetDataStream> assetDataStream = AZStd::make_shared<AZ::Data::AssetDataStream>();
// Read the asset into a memory buffer, then hand ownership of the buffer to assetDataStream
{
AZ::IO::FileIOStream stream(fullPath.c_str(), AZ::IO::OpenMode::ModeRead);
if (!stream.IsOpen())
{
AZ_Warning(s_scriptEventsBuilder, false, "Exporting of .ScriptEvents for \"%s\" failed because the source file could not be opened.", fullPath.c_str());
return;
}
AZStd::vector<AZ::u8> fileBuffer(stream.GetLength());
size_t bytesRead = stream.Read(fileBuffer.size(), fileBuffer.data());
if (bytesRead != stream.GetLength())
{
AZ_Warning(s_scriptEventsBuilder, false, "Exporting of .ScriptEvents for \"%s\" failed because the source file could not be read.", fullPath.c_str());
return;
}
assetDataStream->Open(AZStd::move(fileBuffer));
}
AZ::SerializeContext* context{};
AZ::ComponentApplicationBus::BroadcastResult(context, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
AZStd::vector<AssetBuilderSDK::ProductDependency> productDependencies;
AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset preload\n");
AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> asset;
asset.Create(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
if (editorAssetHandler->LoadAssetData(asset, assetDataStream, nullptr) != AZ::Data::AssetHandler::LoadResult::LoadComplete)
{
AZ_Error(s_scriptEventsBuilder, false, R"(Loading of ScriptEvents asset for source file "%s" has failed)", fullPath.data());
return;
}
AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset loaded successfully\n");
// Flush asset manager events to ensure no asset references are held by closures queued on Ebuses.
AZ::Data::AssetManager::Instance().DispatchEvents();
AZStd::string runtimeScriptEventsOutputPath;
AzFramework::StringFunc::Path::Join(request.m_tempDirPath.c_str(), fileNameOnly.c_str(), runtimeScriptEventsOutputPath, true, true);
ScriptEvents::ScriptEvent definition = asset.Get()->m_definition;
definition.Flatten();
// Populate the runtime Asset
AZStd::vector<AZ::u8> byteBuffer;
AZ::IO::ByteContainerStream<decltype(byteBuffer)> byteStream(&byteBuffer);
AZ::Data::Asset<ScriptEvents::ScriptEventsAsset> productionAsset;
productionAsset.Create(AZ::Uuid::CreateRandom());
productionAsset.Get()->m_definition = AZStd::move(definition);
editorAssetHandler->SetSaveAsBinary(true);
AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset presave to object stream for %s\n", fullPath.c_str());
bool productionAssetSaved = editorAssetHandler->SaveAssetData(productionAsset, &byteStream);
if (!productionAssetSaved)
{
AZ_Error(s_scriptEventsBuilder, productionAssetSaved, "Failed to save runtime Script Events to object stream");
return;
}
AZ_TracePrintf(s_scriptEventsBuilder, "Script Events Asset has been saved to the object stream successfully\n");
// TODO: make this binary
AZ::IO::FileIOStream outFileStream(runtimeScriptEventsOutputPath.data(), AZ::IO::OpenMode::ModeWrite);
if (!outFileStream.IsOpen())
{
AZ_Error(s_scriptEventsBuilder, false, "Failed to open output file %s", runtimeScriptEventsOutputPath.data());
return;
}
productionAssetSaved = outFileStream.Write(byteBuffer.size(), byteBuffer.data()) == byteBuffer.size() && productionAssetSaved;
if (!productionAssetSaved)
{
AZ_Error(s_scriptEventsBuilder, productionAssetSaved, "Unable to save runtime Script Events file %s", runtimeScriptEventsOutputPath.data());
return;
}
// ScriptEvents Editor Asset Copy job
// The SubID is zero as this represents the main asset
AssetBuilderSDK::JobProduct jobProduct;
jobProduct.m_productFileName = runtimeScriptEventsOutputPath;
jobProduct.m_productAssetType = azrtti_typeid<ScriptEvents::ScriptEventsAsset>();
jobProduct.m_productSubID = 0;
jobProduct.m_dependenciesHandled = true; // This builder has no product dependencies.
response.m_outputProducts.push_back(AZStd::move(jobProduct));
response.m_resultCode = AssetBuilderSDK::ProcessJobResult_Success;
AZ_TracePrintf(s_scriptEventsBuilder, "Finished processing Script Events %s\n", fullPath.c_str());
}
AZ::Uuid Worker::GetUUID()
{
return AZ::Uuid::CreateString("{CD64F85A-0147-45EF-B02A-9828E25D99EB}");
}
}
@@ -0,0 +1,58 @@
/*
* All or portions of this file Copyright(c) Amazon.com, Inc.or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution(the "License").All use of this software is governed by the License,
*or, if provided, by the license below or the license accompanying this file.Do not
* remove or modify any license notices.This file is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AssetBuilderSDK/AssetBuilderBusses.h>
#include <AssetBuilderSDK/AssetBuilderSDK.h>
#include <AzCore/Outcome/Outcome.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/Asset/AssetCommon.h>
namespace ScriptEventsEditor { class ScriptEventAssetHandler; }
namespace ScriptEventsBuilder
{
class Worker
: public AssetBuilderSDK::AssetBuilderCommandBus::Handler
{
public:
Worker();
~Worker();
int GetVersionNumber() const;
const char* GetFingerprintString() const;
//! Asset Builder Callback Functions
void CreateJobs(const AssetBuilderSDK::CreateJobsRequest& request, AssetBuilderSDK::CreateJobsResponse& response) const;
void ProcessJob(const AssetBuilderSDK::ProcessJobRequest& request, AssetBuilderSDK::ProcessJobResponse& response) const;
//////////////////////////////////////////////////////////////////////////
//!AssetBuilderSDK::AssetBuilderCommandBus interface
void ShutDown() override;
//////////////////////////////////////////////////////////////////////////
void Activate();
void Deactivate();
static AZ::Uuid GetUUID();
private:
Worker(const Worker&) = delete;
bool m_isShuttingDown = false;
// cached on first time query
mutable AZStd::string m_fingerprintString;
};
}