LifePlay-Revival/Source/LifePlay/LPVictoryCompat.cpp

81 lines
2.8 KiB
C++
Raw Permalink Normal View History

// Portage UE 5.5 - voir LPVictoryCompat.h
#include "LPVictoryCompat.h"
#include "Engine/Texture2D.h"
#include "HAL/PlatformApplicationMisc.h"
#include "IImageWrapper.h"
#include "IImageWrapperModule.h"
#include "Misc/FileHelper.h"
#include "Modules/ModuleManager.h"
namespace
{
EImageFormat GetJoyImageFormat(EJoyImageFormats JoyFormat)
{
switch (JoyFormat)
{
case EJoyImageFormats::JPG: return EImageFormat::JPEG;
case EJoyImageFormats::PNG: return EImageFormat::PNG;
case EJoyImageFormats::BMP: return EImageFormat::BMP;
case EJoyImageFormats::ICO: return EImageFormat::ICO;
case EJoyImageFormats::EXR: return EImageFormat::EXR;
case EJoyImageFormats::ICNS: return EImageFormat::ICNS;
}
return EImageFormat::JPEG;
}
}
UTexture2D* ULPVictoryCompat::Victory_LoadTexture2D_FromFile(const FString& FullFilePath, EJoyImageFormats ImageFormat, bool& IsValid, int32& Width, int32& Height)
{
IsValid = false;
UTexture2D* LoadedT2D = nullptr;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(GetJoyImageFormat(ImageFormat));
// Load From File
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath)) return nullptr;
// Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
TArray<uint8> UncompressedBGRA;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
// Valid?
if (!LoadedT2D) return nullptr;
// Out!
Width = ImageWrapper->GetWidth();
Height = ImageWrapper->GetHeight();
// Copy!
// 5.1+ : UTexture2D::PlatformData est prive, on passe par GetPlatformData()
void* TextureData = LoadedT2D->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
LoadedT2D->GetPlatformData()->Mips[0].BulkData.Unlock();
// Update!
LoadedT2D->UpdateResource();
}
}
// Success!
// NOTE : comportement repris tel quel du plugin d'origine. IsValid ne distingue que
// le fichier introuvable (false, sortie anticipee ci-dessus) du reste (true), y compris
// quand l'image est illisible et que le retour est nul. Volontairement non corrige :
// les Blueprints existants branchent sur ce flag, changer sa semantique changerait
// leur comportement. Voir Docs/Architecture/VictoryPlugin_Architecture.md (R-14).
IsValid = true;
return LoadedT2D;
}
void ULPVictoryCompat::Victory_SaveStringToOSClipboard(const FString& ToClipboard)
{
FPlatformApplicationMisc::ClipboardCopy(*ToClipboard);
}