Files
UnrealEngineUWP/Engine/Source/Editor/UnrealEd/Private/Commandlets/PopulateDialogueWaveFromCharacterSheetCommandlet.cpp
robert manuszewski d1443992e1 Deprecating ANY_PACKAGE.
This change consists of multiple changes:

Core:
- Deprecation of ANY_PACKAGE macro. Added ANY_PACKAGE_DEPRECATED macro which can still be used for backwards compatibility purposes (only used in CoreUObject)
- Deprecation of StaticFindObjectFast* functions that take bAnyPackage parameter
- Added UStruct::GetStructPathName function that returns FTopLevelAssetPath representing the path name (package + object FName, super quick compared to UObject::GetPathName) + wrapper UClass::GetClassPathName to make it look better when used with UClasses
- Added (Static)FindFirstObject* functions that find a first object given its Name (no Outer). These functions are used in places I consider valid to do global UObject (UClass) lookups like parsing command line parameters / checking for unique object names
- Added static UClass::TryFindType function which serves a similar purpose as FindFirstObject however it's going to throw a warning (with a callstack / maybe ensure in the future?) if short class name is provided. This function is used  in places that used to use short class names but now should have been converted to use path names to catch any potential regressions and or edge cases I missed.
- Added static UClass::TryConvertShortNameToPathName utility function
- Added static UClass::TryFixShortClassNameExportPath utility function
- Object text export paths will now also include class path (Texture2D'/Game/Textures/Grass.Grass' -> /Script/Engine.Texture2D'/Game/Textures/Grass.Grass')
- All places that manually generated object export paths for objects will now use FObjectPropertyBase::GetExportPath
- Added a new startup test that checks for short type names in UClass/FProperty MetaData values

AssetRegistry:
- Deprecated any member variables (FAssetData / FARFilter) or functions that use FNames to represent class names and replaced them with FTopLevelAssetPath
- Added new member variables and new function overloads that use FTopLevelAssetPath to represent class names
- This also applies to a few other modules' APIs to match AssetRegistry changes

Everything else:
- Updated code that used ANY_PACKAGE (depending on the use case) to use FindObject(nullptr, PathToObject), UClass::TryFindType (used when path name is expected, warns if it's a short name) or FindFirstObject (usually for finding types based on user input but there's been a few legitimate use cases not related to user input)
- Updated code that used AssetRegistry API to use FTopLevelAssetPaths and USomeClass::StaticClass()->GetClassPathName() instead of GetFName()
- Updated meta data and hardcoded FindObject(ANY_PACKAGE, "EEnumNameOrClassName") calls to use path names

#jira UE-99463
#rb many.people
[FYI] Marcus.Wassmer
#preflight 629248ec2256738f75de9b32

#codereviewnumbers 20320742, 20320791, 20320799, 20320756, 20320809, 20320830, 20320840, 20320846, 20320851, 20320863, 20320780, 20320765, 20320876, 20320786

#ROBOMERGE-OWNER: robert.manuszewski
#ROBOMERGE-AUTHOR: robert.manuszewski
#ROBOMERGE-SOURCE: CL 20430220 via CL 20433854 via CL 20435474 via CL 20435484
#ROBOMERGE-BOT: UE5 (Release-Engine-Staging -> Main) (v949-20362246)

[CL 20448496 by robert manuszewski in ue5-main branch]
2022-06-01 03:46:59 -04:00

284 lines
11 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "Commandlets/PopulateDialogueWaveFromCharacterSheetCommandlet.h"
#include "Misc/FileHelper.h"
#include "Modules/ModuleManager.h"
#include "AssetRegistry/AssetData.h"
#include "Sound/SoundWave.h"
#include "Sound/DialogueWave.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "LocalizedAssetUtil.h"
#include "LocalizationSourceControlUtil.h"
#include "Serialization/Csv/CsvParser.h"
DEFINE_LOG_CATEGORY_STATIC(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Log, All);
namespace CharacterDialogueScript
{
const uint32 HeaderRowIndex = 12 - 1;
const uint32 DialogLineColumn = 5 - 1;
const uint32 VoiceInflectionColumn = 6 - 1;
const uint32 VoicePowerColumn = 7 - 1;
const uint32 AudioFileNameColumn = 8 - 1;
const uint32 NotesColumn = 10 - 1;
}
int32 UPopulateDialogueWaveFromCharacterSheetCommandlet::Main(const FString& Params)
{
// Prepare asset registry.
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry"));
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();
AssetRegistry.SearchAllAssets(true);
// Parse command line.
TArray<FString> Tokens;
TArray<FString> Switches;
TMap<FString, FString> Parameters;
UCommandlet::ParseCommandLine(*Params, Tokens, Switches, Parameters);
// Get file name from parameters.
FString DialogTextFileName;
{
FString* const DialogTextFileParameterValue = Parameters.Find(TEXT("DialogTextFile"));
if (!DialogTextFileParameterValue)
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Missing argument DialogTextFile."));
return -1;
}
DialogTextFileName = *DialogTextFileParameterValue;
}
// Load file contents to string.
FString DialogTextFileContents;
if (!FFileHelper::LoadFileToString(DialogTextFileContents, *DialogTextFileName))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Failed to load contents of dialog text file (%s)."), *DialogTextFileName);
return -1;
}
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Log, TEXT("Using dialog text file (%s)."), *DialogTextFileName);
TSharedPtr<FLocalizationSCC> SourceControlInfo;
const bool bEnableSourceControl = Switches.Contains(TEXT("EnableSCC"));
if (bEnableSourceControl)
{
SourceControlInfo = MakeShareable(new FLocalizationSCC());
FText SCCErrorStr;
if (!SourceControlInfo->IsReady(SCCErrorStr))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Source Control error: %s"), *SCCErrorStr.ToString());
return -1;
}
}
// Parse file contents.
FCsvParser DialogTextFileParser(DialogTextFileContents);
const FCsvParser::FRows& Rows = DialogTextFileParser.GetRows();
// Validate row count.
if (!Rows.IsValidIndex(CharacterDialogueScript::HeaderRowIndex))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file has insufficient rows to be properly formed, expected at least %u rows."), CharacterDialogueScript::HeaderRowIndex + 1);
return -1;
}
// Validate header row?
// Character,Section Type,Category,Dialog Line (English),Voice Inflection,Voice Power,Audio File Name,Heard By,Notes,Video Name,Status
// We only want dialogue wave assets that exist within the Game content directory.
TArray<FAssetData> AllDialogueWaves;
if (!FLocalizedAssetUtil::GetAssetsByPathAndClass(AssetRegistry, FName("/Game"), UDialogueWave::StaticClass()->GetClassPathName(), /*bIncludeLocalizedAssets*/false, AllDialogueWaves))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Unable to get dialogue wave asset data from asset registry."));
return -1;
}
// We only want sound wave assets that exist within the Game content directory.
TArray<FAssetData> AllSoundWaves;
if (!FLocalizedAssetUtil::GetAssetsByPathAndClass(AssetRegistry, FName("/Game"), USoundWave::StaticClass()->GetClassPathName(), /*bIncludeLocalizedAssets*/false, AllSoundWaves))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Unable to get sound wave asset data from asset registry."));
return -1;
}
// Iterate over rows of dialogue data.
for (int32 i = CharacterDialogueScript::HeaderRowIndex + 1; i < Rows.Num(); ++i)
{
const TArray<const TCHAR*>& ColumnsInRow = Rows[i];
// Validate Dialog Line column.
if (!ColumnsInRow.IsValidIndex(CharacterDialogueScript::DialogLineColumn))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file row (%d) has insufficient columns to be properly formed, missing dialog file name column (%u)."), i, CharacterDialogueScript::DialogLineColumn + 1);
continue;
}
// Validate Voice Inflection column.
if (!ColumnsInRow.IsValidIndex(CharacterDialogueScript::VoiceInflectionColumn))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file row (%d) has insufficient columns to be properly formed, missing dialog file name column (%u)."), i, CharacterDialogueScript::VoiceInflectionColumn + 1);
continue;
}
// Validate Voice Power column.
if (!ColumnsInRow.IsValidIndex(CharacterDialogueScript::VoicePowerColumn))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file row (%d) has insufficient columns to be properly formed, missing dialog file name column (%u)."), i, CharacterDialogueScript::VoicePowerColumn + 1);
continue;
}
// Validate Audio File Name column.
if (!ColumnsInRow.IsValidIndex(CharacterDialogueScript::AudioFileNameColumn))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file row (%d) has insufficient columns to be properly formed, missing audio file name column (%u)."), i, CharacterDialogueScript::AudioFileNameColumn + 1);
continue;
}
// Validate Notes column.
if (!ColumnsInRow.IsValidIndex(CharacterDialogueScript::NotesColumn))
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file row (%d) has insufficient columns to be properly formed, missing dialog file name column (%u)."), i, CharacterDialogueScript::NotesColumn + 1);
continue;
}
// Find reference audio file by its file name.
const FName AudioFileName = ColumnsInRow[CharacterDialogueScript::AudioFileNameColumn];
TArray<FAssetData> AssetDataArrayForPackageName = AllDialogueWaves.FilterByPredicate([&](const FAssetData& AssetData) -> bool
{
return AssetData.AssetName == AudioFileName;
});
// If we couldn't find a dialogue wave with this name, try searching for a sound wave instead.
if (AssetDataArrayForPackageName.Num() == 0)
{
TArray<FAssetData> SoundWaveArrayForPackageName = AllSoundWaves.FilterByPredicate([&](const FAssetData& AssetData) -> bool
{
return AssetData.AssetName == AudioFileName;
});
// If we found a single sound wave with this name, try and find an auto-converted dialogue wave that's using it.
if (SoundWaveArrayForPackageName.Num() == 1)
{
const FName AutoConvertedAudioFileName = *(AudioFileName.ToString() + TEXT("_Dialogue"));
TArray<FAssetData> DialogueWaveArrayForAutoConvertedPackageName = AllDialogueWaves.FilterByPredicate([&](const FAssetData& AssetData) -> bool
{
return AssetData.AssetName == AutoConvertedAudioFileName;
});
if (DialogueWaveArrayForAutoConvertedPackageName.Num() == 1)
{
UDialogueWave* const AutoConvertedDialogueWave = Cast<UDialogueWave>(DialogueWaveArrayForAutoConvertedPackageName[0].GetAsset());
if (AutoConvertedDialogueWave)
{
const bool bIsValidDialogueWave = AutoConvertedDialogueWave->ContextMappings.ContainsByPredicate([&](const FDialogueContextMapping& Mapping) -> bool
{
return Mapping.SoundWave && Mapping.SoundWave->GetFName() == AudioFileName;
});
if (bIsValidDialogueWave)
{
AssetDataArrayForPackageName = DialogueWaveArrayForAutoConvertedPackageName;
}
}
}
}
}
// Verify that the number of matching dialogue waves is singular.
if (AssetDataArrayForPackageName.Num() != 1)
{
if (AssetDataArrayForPackageName.Num() < 1)
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file references absent audio file name (%s) at (%d, %d). No dialogue waves use this name."), *AudioFileName.ToString(), i + 1, CharacterDialogueScript::AudioFileNameColumn + 1);
}
if (AssetDataArrayForPackageName.Num() > 1)
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file references ambiguous audio file name (%s) at (%d, %d). Multiple dialogue waves use this name."), *AudioFileName.ToString(), i + 1, CharacterDialogueScript::AudioFileNameColumn + 1);
}
continue;
}
const FAssetData& AssetData = AssetDataArrayForPackageName[0];
// Verify that the found asset is a dialogue wave.
if (AssetData.GetClass() != UDialogueWave::StaticClass())
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file references audio file name (%s) at (%u, %u), but the asset with this name is not actually a dialogue wave."), *AudioFileName.ToString(), i + 1, CharacterDialogueScript::AudioFileNameColumn + 1);
continue;
}
// Get the dialogue wave to populate with subtitles.
UDialogueWave* const DialogueWave = Cast<UDialogueWave>(AssetData.GetAsset());
// Verify that the dialogue wave was loaded.
if (!DialogueWave)
{
UE_LOG(LogPopulateDialogueWaveFromCharacterSheetCommandlet, Error, TEXT("Dialog text file references audio file name (%s) at (%u, %u), but the dialogue wave could not be accessed."), *AudioFileName.ToString(), i + 1, CharacterDialogueScript::AudioFileNameColumn + 1);
continue;
}
bool bHasChanged = false;
// Set dialogue wave spoken text to dialogue line if not identical.
{
const TCHAR* const DialogueLine = ColumnsInRow[CharacterDialogueScript::DialogLineColumn];
if (!DialogueWave->SpokenText.Equals(DialogueLine, ESearchCase::CaseSensitive))
{
bHasChanged = true;
DialogueWave->SpokenText = DialogueLine;
DialogueWave->MarkPackageDirty();
}
}
// Set voice actor notes to the inflection, power, and notes if not identical.
{
const TCHAR* const VoiceInflection = ColumnsInRow[CharacterDialogueScript::VoiceInflectionColumn];
const TCHAR* const VoicePower = ColumnsInRow[CharacterDialogueScript::VoicePowerColumn];
const TCHAR* const Notes = ColumnsInRow[CharacterDialogueScript::NotesColumn];
FString VoiceActorDirection = VoiceInflection;
if (FCString::Strlen(VoicePower) > 0)
{
if (VoiceActorDirection.Len() > 0)
{
VoiceActorDirection += TEXT(". ");
}
VoiceActorDirection += VoicePower;
}
if (FCString::Strlen(Notes) > 0)
{
if (VoiceActorDirection.Len() > 0)
{
VoiceActorDirection += TEXT(". ");
}
VoiceActorDirection += Notes;
}
if (VoiceActorDirection.Len() > 0 && !VoiceActorDirection.EndsWith(TEXT(".")))
{
VoiceActorDirection += TEXT(".");
}
if (!DialogueWave->VoiceActorDirection.Equals(VoiceActorDirection, ESearchCase::CaseSensitive))
{
bHasChanged = true;
DialogueWave->VoiceActorDirection = VoiceActorDirection;
DialogueWave->MarkPackageDirty();
}
}
if (!bHasChanged)
{
continue;
}
// Save package for dialogue wave.
if (!FLocalizedAssetSCCUtil::SaveAssetWithSCC(SourceControlInfo, DialogueWave))
{
continue;
}
}
return 0;
}