200 lines
6.6 KiB
C++
200 lines
6.6 KiB
C++
|
|
#include "AssetLookup.h"
|
|
#include "AssetRegistry/IAssetRegistry.h"
|
|
#include "AssetRegistry/AssetData.h"
|
|
#include "AssetRegistry/AssetRegistryState.h"
|
|
#include "LuprexGameModeBase.h"
|
|
#include "Components/Widget.h"
|
|
#include "WidgetBlueprint.h"
|
|
#include "Blueprint/UserWidget.h"
|
|
#include "Engine/StaticMesh.h"
|
|
|
|
void UlxAssetLookup::RebuildIndex()
|
|
{
|
|
CachedTangibles.Empty();
|
|
CachedStaticMeshes.Empty();
|
|
IAssetRegistry::GetChecked().WaitForCompletion();
|
|
ScanTangibles();
|
|
ScanStaticMeshes();
|
|
ScanWidgets();
|
|
}
|
|
|
|
void UlxAssetLookup::ScanTangibles()
|
|
{
|
|
TArray<FAssetData> FoundData;
|
|
FARFilter AssetFilter;
|
|
AssetFilter.PackagePaths.Add(FName(TEXT("/Game/Tangibles")));
|
|
AssetFilter.ClassPaths.Add(UBlueprint::StaticClass()->GetClassPathName());
|
|
AssetFilter.bIncludeOnlyOnDiskAssets = true;
|
|
AssetFilter.bRecursivePaths = true;
|
|
IAssetRegistry::GetChecked().GetAssets(AssetFilter, FoundData);
|
|
|
|
UE_LOG(LogLuprexIntegration, Display, TEXT("Found %d assets in /Game/Tangibles"), FoundData.Num());
|
|
for (const FAssetData &Data : FoundData)
|
|
{
|
|
FString Path = Data.GetObjectPathString() + TEXT("_C");
|
|
CachedTangibles.Add(Data.AssetName, Path);
|
|
}
|
|
}
|
|
|
|
void UlxAssetLookup::ScanStaticMeshes()
|
|
{
|
|
TArray<FAssetData> FoundData;
|
|
FARFilter AssetFilter;
|
|
AssetFilter.PackagePaths.Add(FName(TEXT("/Game/StaticMeshes")));
|
|
AssetFilter.ClassPaths.Add(UStaticMesh::StaticClass()->GetClassPathName());
|
|
AssetFilter.bIncludeOnlyOnDiskAssets = true;
|
|
AssetFilter.bRecursivePaths = true;
|
|
IAssetRegistry::GetChecked().GetAssets(AssetFilter, FoundData);
|
|
|
|
UE_LOG(LogLuprexIntegration, Display, TEXT("Found %d assets in /Game/StaticMeshes"), FoundData.Num());
|
|
for (const FAssetData &Data : FoundData)
|
|
{
|
|
FString Path = Data.GetObjectPathString();
|
|
CachedStaticMeshes.Add(Data.AssetName, Path);
|
|
}
|
|
}
|
|
|
|
void UlxAssetLookup::ScanWidgets()
|
|
{
|
|
TArray<FAssetData> FoundData;
|
|
FARFilter AssetFilter;
|
|
AssetFilter.PackagePaths.Add(FName(TEXT("/Game/Widgets")));
|
|
AssetFilter.ClassPaths.Add(UWidgetBlueprint::StaticClass()->GetClassPathName());
|
|
AssetFilter.bIncludeOnlyOnDiskAssets = true;
|
|
AssetFilter.bRecursivePaths = true;
|
|
IAssetRegistry::GetChecked().GetAssets(AssetFilter, FoundData);
|
|
|
|
UE_LOG(LogLuprexIntegration, Display, TEXT("Found %d assets in /Game/Widgets"), FoundData.Num());
|
|
for (const FAssetData &Data : FoundData)
|
|
{
|
|
FString Path = Data.GetObjectPathString() + TEXT("_C");
|
|
CachedWidgets.Add(Data.AssetName, Path);
|
|
}
|
|
}
|
|
|
|
FString UlxAssetLookup::TangibleLoadPath(const FName &AssetName) const
|
|
{
|
|
const FString *Result = CachedTangibles.Find(AssetName);
|
|
if (Result == nullptr) return TEXT("");
|
|
return *Result;
|
|
}
|
|
|
|
FString UlxAssetLookup::StaticMeshLoadPath(const FName &AssetName) const
|
|
{
|
|
const FString *Result = CachedStaticMeshes.Find(AssetName);
|
|
if (Result == nullptr) return TEXT("");
|
|
return *Result;
|
|
}
|
|
|
|
FString UlxAssetLookup::WidgetLoadPath(const FName &AssetName) const
|
|
{
|
|
const FString *Result = CachedWidgets.Find(AssetName);
|
|
if (Result == nullptr) return TEXT("");
|
|
return *Result;
|
|
}
|
|
|
|
void UlxAssetLookup::LogMaybeError(bool Error, const TCHAR *Message, const TCHAR *Path)
|
|
{
|
|
if (Error)
|
|
{
|
|
UE_LOG(LogLuprexIntegration, Error, TEXT("%s: %s"), Message, Path);
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogLuprexIntegration, Display, TEXT("%s: %s"), Message, Path);
|
|
}
|
|
}
|
|
|
|
UStaticMesh *UlxAssetLookup::LoadStaticMeshAsset(const UObject *Context, const FString &Name, bool ErrorIfNotFound)
|
|
{
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(Context);
|
|
FString Path = mode->GetAssetLookup()->StaticMeshLoadPath(FName(FString("SM_") + Name));
|
|
if (Path.IsEmpty())
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Static mesh not on search path"), *Name);
|
|
return nullptr;
|
|
}
|
|
UStaticMesh *Result = LoadObject<UStaticMesh>(nullptr, *Path);
|
|
if (Result == nullptr)
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Cannot load static mesh"), *Path);
|
|
return nullptr;
|
|
}
|
|
|
|
return Result;
|
|
}
|
|
|
|
TSubclassOf<AActor> UlxAssetLookup::LoadTangibleBlueprintAsset(const UObject *Context, const FString &Name, bool ErrorIfNotFound) {
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(Context);
|
|
FString Path = mode->GetAssetLookup()->TangibleLoadPath(FName(FString("TAN_") + Name));
|
|
if (Path.IsEmpty())
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Tangible not on search path"), *Name);
|
|
return nullptr;
|
|
}
|
|
UClass *Result = LoadObject<UClass>(nullptr, *Path);
|
|
if (Result == nullptr)
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Cannot load tangible class"), *Path);
|
|
return nullptr;
|
|
}
|
|
if (!Result->IsChildOf(AActor::StaticClass()))
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Tangible class is not an actor"), *Path);
|
|
return nullptr;
|
|
}
|
|
UFunction *aqchanged = Result->FindFunctionByName(FName(TEXT("Animation Queue Changed")));
|
|
if ((aqchanged == nullptr)||(aqchanged->ParmsSize != 0))
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Tangible does not have 'Animation Queue Changed' function"), *Path);
|
|
return nullptr;
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
TSubclassOf<UUserWidget> UlxAssetLookup::LoadUserWidgetAsset(const UObject *Context, const FString &Name, bool ErrorIfNotFound) {
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(Context);
|
|
FString Path = mode->GetAssetLookup()->WidgetLoadPath(FName(FString("WB_") + Name));
|
|
if (Path.IsEmpty())
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Widget not on search path"), *Name);
|
|
return nullptr;
|
|
}
|
|
UClass *Result = LoadObject<UClass>(nullptr, *Path);
|
|
if (Result == nullptr)
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Cannot load widget blueprint"), *Path);
|
|
return nullptr;
|
|
}
|
|
if (!Result->IsChildOf(UUserWidget::StaticClass()))
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Blueprint does not derive from UUserWidget"), *Path);
|
|
return nullptr;
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
TSubclassOf<UlxLuaWidget> UlxAssetLookup::LoadLuaWidgetAsset(const UObject *Context, const FString &Name, bool ErrorIfNotFound) {
|
|
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(Context);
|
|
FString Path = mode->GetAssetLookup()->WidgetLoadPath(FName(FString("WB_") + Name));
|
|
if (Path.IsEmpty())
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Widget not on search path"), *Name);
|
|
return nullptr;
|
|
}
|
|
UClass *Result = LoadObject<UClass>(nullptr, *Path);
|
|
if (Result == nullptr)
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Cannot load widget blueprint"), *Path);
|
|
return nullptr;
|
|
}
|
|
if (!Result->IsChildOf(UlxLuaWidget::StaticClass()))
|
|
{
|
|
LogMaybeError(ErrorIfNotFound, TEXT("Blueprint does not derive from UlxLuaWidget"), *Path);
|
|
return nullptr;
|
|
}
|
|
return Result;
|
|
}
|
|
|