76 lines
2.0 KiB
C++
76 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "WingServer.h"
|
|
#include "UObject/Package.h"
|
|
#include "AssetToolsModule.h"
|
|
#include "IAssetTools.h"
|
|
|
|
// Helper for creating new asset packages. Validates the path and checks for
|
|
// conflicting assets in the constructor. Call Ok() to check, then Make() to
|
|
// actually create the UPackage.
|
|
class WingPackageMaker
|
|
{
|
|
public:
|
|
WingPackageMaker(const FString& InFullPath)
|
|
: FullPath(InFullPath)
|
|
{
|
|
// Path must start with /Game.
|
|
if (!FullPath.StartsWith(TEXT("/Game")))
|
|
{
|
|
UWingServer::Printf(TEXT("ERROR: Package path '%s' must start with '/Game'\n"), *FullPath);
|
|
bError = true;
|
|
return;
|
|
}
|
|
|
|
// Check for an existing asset at this path.
|
|
if (FindObject<UPackage>(nullptr, *FullPath))
|
|
{
|
|
UWingServer::Printf(TEXT("ERROR: An asset already exists at '%s'\n"), *FullPath);
|
|
bError = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool Ok() const { return !bError; }
|
|
|
|
bool Make()
|
|
{
|
|
if (bError) return false;
|
|
Pkg = CreatePackage(*FullPath);
|
|
if (!Pkg)
|
|
{
|
|
UWingServer::Printf(TEXT("ERROR: Failed to create package at '%s'\n"), *FullPath);
|
|
bError = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
UPackage* Package() const { return Pkg; }
|
|
FString Name() const { return FPackageName::GetShortName(FullPath); }
|
|
|
|
template<typename AssetClass, typename FactoryClass>
|
|
AssetClass* CreateAsset()
|
|
{
|
|
if (bError) return nullptr;
|
|
IAssetTools& AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
|
|
FactoryClass* Factory = NewObject<FactoryClass>();
|
|
FString PkgPath = FPackageName::GetLongPackagePath(FullPath);
|
|
FString AssetName = FPackageName::GetShortName(FullPath);
|
|
UObject* NewAsset = AssetTools.CreateAsset(AssetName, PkgPath, AssetClass::StaticClass(), Factory);
|
|
AssetClass* Result = Cast<AssetClass>(NewAsset);
|
|
if (!Result)
|
|
{
|
|
UWingServer::Printf(TEXT("ERROR: Failed to create asset at '%s'\n"), *FullPath);
|
|
bError = true;
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
private:
|
|
FString FullPath;
|
|
UPackage* Pkg = nullptr;
|
|
bool bError = false;
|
|
};
|