From 77278a03297113632ac95e5f558f4e4591e168d1 Mon Sep 17 00:00:00 2001 From: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> Date: Fri, 14 May 2021 11:18:15 -0500 Subject: [PATCH] First version of the slice-to-prefab converter It converts .slice and .ly files to .prefab files, but doesn't handle nested or complicated slices correctly yet. --- .../Tools/SerializeContextTools/Converter.cpp | 4 +- Code/Tools/SerializeContextTools/Converter.h | 2 +- Code/Tools/SerializeContextTools/Dumper.cpp | 2 +- .../SerializeContextTools/SliceConverter.cpp | 263 ++++++++++++++++++ .../SerializeContextTools/SliceConverter.h | 53 ++++ .../Tools/SerializeContextTools/Utilities.cpp | 32 ++- Code/Tools/SerializeContextTools/Utilities.h | 2 +- Code/Tools/SerializeContextTools/main.cpp | 13 + .../serializecontexttools_files.cmake | 2 + 9 files changed, 358 insertions(+), 15 deletions(-) create mode 100644 Code/Tools/SerializeContextTools/SliceConverter.cpp create mode 100644 Code/Tools/SerializeContextTools/SliceConverter.h diff --git a/Code/Tools/SerializeContextTools/Converter.cpp b/Code/Tools/SerializeContextTools/Converter.cpp index b15bb4fa8e..0b19d4e981 100644 --- a/Code/Tools/SerializeContextTools/Converter.cpp +++ b/Code/Tools/SerializeContextTools/Converter.cpp @@ -117,7 +117,7 @@ namespace AZ } return true; }; - if (!Utilities::InspectSerializedFile(filePath, convertSettings.m_serializeContext, callback)) + if (!Utilities::InspectSerializedFile(filePath.c_str(), convertSettings.m_serializeContext, callback)) { AZ_Warning("Convert", false, "Failed to load '%s'. File may not contain an object stream.", filePath.c_str()); result = false; @@ -287,7 +287,7 @@ namespace AZ } return true; }; - if (!Utilities::InspectSerializedFile(filePath, convertSettings.m_serializeContext, callback)) + if (!Utilities::InspectSerializedFile(filePath.c_str(), convertSettings.m_serializeContext, callback)) { AZ_Warning("Convert", false, "Failed to load '%s'. File may not contain an object stream.", filePath.c_str()); result = false; diff --git a/Code/Tools/SerializeContextTools/Converter.h b/Code/Tools/SerializeContextTools/Converter.h index 18f30cc562..c6338b8d29 100644 --- a/Code/Tools/SerializeContextTools/Converter.h +++ b/Code/Tools/SerializeContextTools/Converter.h @@ -42,7 +42,7 @@ namespace AZ //! Can be used to convert *.ini and *.cfg files static bool ConvertConfigFile(Application& application); - private: + protected: using PathDocumentPair = AZStd::pair; using PathDocumentContainer = AZStd::vector; diff --git a/Code/Tools/SerializeContextTools/Dumper.cpp b/Code/Tools/SerializeContextTools/Dumper.cpp index 55c9581602..590ee65571 100644 --- a/Code/Tools/SerializeContextTools/Dumper.cpp +++ b/Code/Tools/SerializeContextTools/Dumper.cpp @@ -85,7 +85,7 @@ namespace AZ::SerializeContextTools result = false; } }; - if (!Utilities::InspectSerializedFile(filePath, sc, callback)) + if (!Utilities::InspectSerializedFile(filePath.c_str(), sc, callback)) { result = false; continue; diff --git a/Code/Tools/SerializeContextTools/SliceConverter.cpp b/Code/Tools/SerializeContextTools/SliceConverter.cpp new file mode 100644 index 0000000000..2df3888607 --- /dev/null +++ b/Code/Tools/SerializeContextTools/SliceConverter.cpp @@ -0,0 +1,263 @@ +/* +* 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// SliceConverter reads in a slice file (saved in an ObjectStream format), instantiates it, creates a prefab out of the data, +// and saves the prefab in a JSON format. This can be used for one-time migrations of slices or slice-based levels to prefabs. +// This converter is still in an early state. It can convert trivial slices, but it cannot handle nested slices yet. +// +// If the slice contains legacy data, it will print out warnings / errors about the data that couldn't be serialized. +// The prefab will be generated without that data. + +namespace AZ +{ + namespace SerializeContextTools + { + bool SliceConverter::ConvertSliceFiles(Application& application) + { + using namespace AZ::JsonSerializationResult; + + const AZ::CommandLine* commandLine = application.GetAzCommandLine(); + if (!commandLine) + { + AZ_Error("SerializeContextTools", false, "Command line not available."); + return false; + } + + JsonSerializerSettings convertSettings; + convertSettings.m_keepDefaults = commandLine->HasSwitch("keepdefaults"); + convertSettings.m_registrationContext = application.GetJsonRegistrationContext(); + convertSettings.m_serializeContext = application.GetSerializeContext(); + if (!convertSettings.m_serializeContext) + { + AZ_Error("Convert-Slice", false, "No serialize context found."); + return false; + } + if (!convertSettings.m_registrationContext) + { + AZ_Error("Convert-Slice", false, "No json registration context found."); + return false; + } + AZStd::string logggingScratchBuffer; + SetupLogging(logggingScratchBuffer, convertSettings.m_reporting, *commandLine); + + bool isDryRun = commandLine->HasSwitch("dryrun"); + + JsonDeserializerSettings verifySettings; + verifySettings.m_registrationContext = application.GetJsonRegistrationContext(); + verifySettings.m_serializeContext = application.GetSerializeContext(); + SetupLogging(logggingScratchBuffer, verifySettings.m_reporting, *commandLine); + + auto archiveInterface = AZ::Interface::Get(); + + // Find the Prefab System Component for use in creating and saving the prefab + AZ::Entity* systemEntity = application.FindEntity(AZ::SystemEntityId); + AZ_Assert(systemEntity != nullptr, "System entity doesn't exist."); + auto prefabSystemComponent = systemEntity->FindComponent(); + AZ_Assert(prefabSystemComponent != nullptr, "Prefab System component doesn't exist"); + + bool result = true; + rapidjson::StringBuffer scratchBuffer; + + AZStd::vector fileList = Utilities::ReadFileListFromCommandLine(application, "files"); + for (AZStd::string& filePath : fileList) + { + bool packOpened = false; + + AZ::IO::Path outputPath = filePath; + outputPath.ReplaceExtension("prefab"); + + AZ_Printf("Convert-Slice", "------------------------------------------------------------------------------------------\n"); + AZ_Printf("Convert-Slice", "Converting '%s' to '%s'\n", filePath.c_str(), outputPath.c_str()); + + AZ::IO::Path inputPath = filePath; + auto fileExtension = inputPath.Extension(); + if (fileExtension == ".ly") + { + // Special case: for level files, we need to open the .ly zip file and convert the levelentities.editor_xml file + // inside of it. All the other files can be ignored as they are deprecated legacy system files that are no longer + // loaded with prefab-based levels. + packOpened = archiveInterface->OpenPack(filePath); + inputPath.ReplaceFilename("levelentities.editor_xml"); + AZ_Warning("Convert-Slice", packOpened, " '%s' could not be opened as a pack file.\n", filePath.c_str()); + } + else + { + AZ_Warning( + "Convert-Slice", (fileExtension == ".slice"), + " Warning: Only .ly and .slice files are supported, conversion of '%.*s' may not work.\n", + AZ_STRING_ARG(fileExtension.Native())); + } + + auto callback = [prefabSystemComponent, &outputPath, isDryRun] + (void* classPtr, const Uuid& classId, [[maybe_unused]] SerializeContext* context) + { + if (classId != azrtti_typeid()) + { + AZ_Printf("Convert-Slice", " File not converted: Slice root is not an entity.\n"); + return false; + } + + AZ::Entity* rootEntity = reinterpret_cast(classPtr); + return ConvertSliceFile(prefabSystemComponent, outputPath, isDryRun, rootEntity); + }; + + if (!Utilities::InspectSerializedFile(inputPath.c_str(), convertSettings.m_serializeContext, callback)) + { + AZ_Warning("Convert-Slice", false, "Failed to load '%s'. File may not contain an object stream.", inputPath.c_str()); + result = false; + } + + if (packOpened) + { + [[maybe_unused]] bool closeResult = archiveInterface->ClosePack(filePath); + AZ_Warning("Convert-Slice", !closeResult, "Failed to close '%s'.", filePath.c_str()); + } + + AZ_Printf("Convert-Slice", "Finished converting '%s' to '%s'\n", filePath.c_str(), outputPath.c_str()); + AZ_Printf("Convert-Slice", "------------------------------------------------------------------------------------------\n"); + } + + return result; + } + + bool SliceConverter::ConvertSliceFile( + AzToolsFramework::Prefab::PrefabSystemComponent* prefabSystemComponent, AZ::IO::PathView outputPath, bool isDryRun, + AZ::Entity* rootEntity) + { + // Find the slice from the root entity. + SliceComponent* sliceComponent = AZ::EntityUtils::FindFirstDerivedComponent(rootEntity); + if (sliceComponent == nullptr) + { + AZ_Printf("Convert-Slice", " File not converted: Root entity did not contain a slice component.\n"); + return false; + } + + // Get all of the entities from the slice. + SliceComponent::EntityList sliceEntities; + bool getEntitiesResult = sliceComponent->GetEntities(sliceEntities); + if ((!getEntitiesResult) || (sliceEntities.empty())) + { + AZ_Printf("Convert-Slice", " File not converted: Slice entities could not be retrieved.\n"); + return false; + } + + // Create the Prefab with the entities from the slice + AZStd::unique_ptr sourceInstance( + prefabSystemComponent->CreatePrefab(sliceEntities, {}, outputPath)); + + // Dispatch events here, because prefab creation might trigger asset loads in rare circumstances. + AZ::Data::AssetManager::Instance().DispatchEvents(); + + // Set up the Prefab container entity to be a proper Editor entity. (This logic is normally triggered + // via an EditorRequests EBus in CreatePrefab, but the subsystem that listens for it isn't present in this tool.) + AzToolsFramework::Prefab::EntityOptionalReference container = sourceInstance->GetContainerEntity(); + AzToolsFramework::EditorEntityContextRequestBus::Broadcast( + &AzToolsFramework::EditorEntityContextRequestBus::Events::AddRequiredComponents, container->get()); + container->get().AddComponent(aznew AzToolsFramework::Prefab::EditorPrefabComponent()); + + // Reparent any root-level slice entities to the container entity. + for (auto entity : sliceEntities) + { + AzToolsFramework::Components::TransformComponent* transformComponent = + entity->FindComponent(); + if (transformComponent) + { + if (!transformComponent->GetParentId().IsValid()) + { + transformComponent->SetParent(container->get().GetId()); + } + } + } + + auto templateId = sourceInstance->GetTemplateId(); + + if (templateId == AzToolsFramework::Prefab::InvalidTemplateId) + { + AZ_Printf("Convert-Slice", " Path error. Path could be invalid, or the prefab may not be loaded in this level.\n"); + return false; + } + + // Update the prefab template with the fixed-up data in our prefab instance. + AzToolsFramework::Prefab::PrefabDom prefabDom; + bool storeResult = AzToolsFramework::Prefab::PrefabDomUtils::StoreInstanceInPrefabDom(*sourceInstance, prefabDom); + if (storeResult == false) + { + AZ_Printf("Convert-Slice", " Failed to convert prefab instance data to a PrefabDom.\n"); + return false; + } + prefabSystemComponent->UpdatePrefabTemplate(templateId, prefabDom); + + // Dispatch events here, because prefab serialization might trigger asset loads in rare circumstances. + AZ::Data::AssetManager::Instance().DispatchEvents(); + + if (isDryRun) + { + PrintPrefab(prefabDom, sourceInstance->GetTemplateSourcePath()); + return true; + } + else + { + return SavePrefab(templateId); + } + } + + void SliceConverter::PrintPrefab(const AzToolsFramework::Prefab::PrefabDom& prefabDom, const AZ::IO::Path& templatePath) + { + rapidjson::StringBuffer prefabBuffer; + rapidjson::PrettyWriter writer(prefabBuffer); + prefabDom.Accept(writer); + AZ_Printf("Convert-Slice", "JSON for %s:\n", templatePath.c_str()); + + // We use Output() to print out the JSON because AZ_Printf has a 4096-character limit. + AZ::Debug::Trace::Instance().Output("", prefabBuffer.GetString()); + AZ::Debug::Trace::Instance().Output("", "\n"); + } + + bool SliceConverter::SavePrefab(AzToolsFramework::Prefab::TemplateId templateId) + { + auto prefabLoaderInterface = AZ::Interface::Get(); + + if (!prefabLoaderInterface->SaveTemplate(templateId)) + { + AZ_Printf("Convert-Slice", " Could not save prefab - internal error (Json write operation failure).\n"); + return false; + } + + return true; + } + + } // namespace SerializeContextTools +} // namespace AZ diff --git a/Code/Tools/SerializeContextTools/SliceConverter.h b/Code/Tools/SerializeContextTools/SliceConverter.h new file mode 100644 index 0000000000..90dfa0d50a --- /dev/null +++ b/Code/Tools/SerializeContextTools/SliceConverter.h @@ -0,0 +1,53 @@ +/* +* 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace AZ +{ + class CommandLine; + class Entity; + class ModuleEntity; + class SerializeContext; + struct Uuid; + + namespace SerializeContextTools + { + class Application; + + class SliceConverter : public Converter + { + public: + static bool ConvertSliceFiles(Application& application); + + private: + + static bool ConvertSliceFile(AzToolsFramework::Prefab::PrefabSystemComponent* prefabSystemComponent, + AZ::IO::PathView outputPath, bool isDryRun, AZ::Entity* rootEntity); + + static void PrintPrefab(const AzToolsFramework::Prefab::PrefabDom& prefabDom, const AZ::IO::Path& templatePath); + static bool SavePrefab(AzToolsFramework::Prefab::TemplateId templateId); + }; + } // namespace SerializeContextTools +} // namespace AZ diff --git a/Code/Tools/SerializeContextTools/Utilities.cpp b/Code/Tools/SerializeContextTools/Utilities.cpp index 1c50466443..cfd75a44e7 100644 --- a/Code/Tools/SerializeContextTools/Utilities.cpp +++ b/Code/Tools/SerializeContextTools/Utilities.cpp @@ -209,30 +209,42 @@ namespace AZ::SerializeContextTools return result; } - bool Utilities::InspectSerializedFile(const AZStd::string& filePath, SerializeContext* sc, const ObjectStream::ClassReadyCB& classCallback) + bool Utilities::InspectSerializedFile(const char* filePath, SerializeContext* sc, const ObjectStream::ClassReadyCB& classCallback) { - if (!AZ::IO::SystemFile::Exists(filePath.c_str())) + if (!AZ::IO::FileIOBase::GetInstance()->Exists(filePath)) { - AZ_Error("Verify", false, "Unable to open file '%s' as it doesn't exist.", filePath.c_str()); + AZ_Error("Verify", false, "Unable to open file '%s' as it doesn't exist.", filePath); return false; } - u64 fileLength = AZ::IO::SystemFile::Length(filePath.c_str()); - if (fileLength == 0) + AZ::IO::HandleType fileHandle; + auto openResult = AZ::IO::FileIOBase::GetInstance()->Open(filePath, AZ::IO::OpenMode::ModeRead, fileHandle); + if (!openResult) { - AZ_Error("Verify", false, "File '%s' doesn't have content.", filePath.c_str()); + AZ_Error("Verify", false, "File '%s' could not be opened.", filePath); + return false; + } + + u64 fileLength = 0; + auto sizeResult = AZ::IO::FileIOBase::GetInstance()->Size(fileHandle, fileLength); + if (!sizeResult || (fileLength == 0)) + { + AZ_Error("Verify", false, "File '%s' doesn't have content.", filePath); return false; } AZStd::vector data; data.resize_no_construct(fileLength); - u64 bytesRead = AZ::IO::SystemFile::Read(filePath.c_str(), data.data()); - if (bytesRead != fileLength) + u64 bytesRead = 0; + auto readResult = AZ::IO::FileIOBase::GetInstance()->Read(fileHandle, data.data(), fileLength, true, &bytesRead); + if (!readResult || (bytesRead != fileLength)) { - AZ_Error("Verify", false, "Unable to read file '%s'.", filePath.c_str()); + AZ_Error("Verify", false, "Unable to read file '%s'.", filePath); return false; } + AZ::IO::FileIOBase::GetInstance()->Close(fileHandle); + AZ::IO::MemoryStream stream(data.data(), fileLength); ObjectStream::FilterDescriptor filter; @@ -241,7 +253,7 @@ namespace AZ::SerializeContextTools filter.m_assetCB = AZ::Data::AssetFilterNoAssetLoading; if (!ObjectStream::LoadBlocking(&stream, *sc, classCallback, filter)) { - AZ_Printf("Verify", "Failed to deserialize '%s'\n", filePath.c_str()); + AZ_Printf("Verify", "Failed to deserialize '%s'\n", filePath); return false; } return true; diff --git a/Code/Tools/SerializeContextTools/Utilities.h b/Code/Tools/SerializeContextTools/Utilities.h index d04f67a304..f08bfc2f64 100644 --- a/Code/Tools/SerializeContextTools/Utilities.h +++ b/Code/Tools/SerializeContextTools/Utilities.h @@ -39,7 +39,7 @@ namespace AZ static AZStd::vector GetSystemComponents(const Application& application); - static bool InspectSerializedFile(const AZStd::string& filePath, SerializeContext* sc, const ObjectStream::ClassReadyCB& classCallback); + static bool InspectSerializedFile(const char* filePath, SerializeContext* sc, const ObjectStream::ClassReadyCB& classCallback); private: Utilities() = delete; diff --git a/Code/Tools/SerializeContextTools/main.cpp b/Code/Tools/SerializeContextTools/main.cpp index fb2dc442ed..d9f2cfbcc2 100644 --- a/Code/Tools/SerializeContextTools/main.cpp +++ b/Code/Tools/SerializeContextTools/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include void PrintHelp() @@ -74,6 +75,14 @@ void PrintHelp() AZ_Printf("Help", R"( On Windows the should be in quotes, as \"/\" is treated as command option prefix)" "\n"); AZ_Printf("Help", R"( [opt] -verbose: Report additional details during the conversion process.)" "\n"); AZ_Printf("Help", R"( example: 'convert-ini --files=AssetProcessorPlatformConfig.ini;bootstrap.cfg --ext=setreg)" "\n"); + AZ_Printf("Help", " 'convert-slice': Converts ObjectStream-based slice files or legacy levels to a JSON-based prefab.\n"); + AZ_Printf("Help", " [arg] -files=: -separated list of files to convert. Supports wildcards.\n"); + AZ_Printf("Help", " [opt] -dryrun: Processes as normal, but doesn't write files.\n"); + AZ_Printf("Help", " [opt] -keepdefaults: Fields are written if a default value was found.\n"); + AZ_Printf("Help", " [opt] -verbose: Report additional details during the conversion process.\n"); + AZ_Printf("Help", " example: 'convert-slice -files=*.slice -specializations=editor\n"); + AZ_Printf("Help", " example: 'convert-slice -files=Levels/TestLevel/TestLevel.ly -specializations=editor\n"); + AZ_Printf("Help", "\n"); } int main(int argc, char** argv) @@ -114,6 +123,10 @@ int main(int argc, char** argv) { result = Converter::ConvertConfigFile(application); } + else if (AZ::StringFunc::Equal("convert-slice", action.c_str())) + { + result = SliceConverter::ConvertSliceFiles(application); + } else { PrintHelp(); diff --git a/Code/Tools/SerializeContextTools/serializecontexttools_files.cmake b/Code/Tools/SerializeContextTools/serializecontexttools_files.cmake index db584ff385..814c55ea08 100644 --- a/Code/Tools/SerializeContextTools/serializecontexttools_files.cmake +++ b/Code/Tools/SerializeContextTools/serializecontexttools_files.cmake @@ -17,6 +17,8 @@ set(FILES Dumper.h Dumper.cpp main.cpp + SliceConverter.h + SliceConverter.cpp Utilities.h Utilities.cpp )