diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index 91d8297..985f930 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -1,7 +1,7 @@ [/Script/EngineSettings.GameMapsSettings] -GameDefaultMap=/Engine/Maps/Templates/OpenWorld +GameDefaultMap=/Game/Levels/Test/L_Test.L_Test EditorStartupMap=/Game/Levels/Test/L_Test.L_Test bUseSplitscreen=False GlobalDefaultGameMode=/Game/Blueprints/GameModes/BP_MainGameMode.BP_MainGameMode_C diff --git a/Content/Levels/Test/Actors/RuntimeLoadTest/BP_Test_RuntimeLoad.uasset b/Content/Levels/Test/Actors/RuntimeLoadTest/BP_Test_RuntimeLoad.uasset index 40e68d7..2c3693e 100644 Binary files a/Content/Levels/Test/Actors/RuntimeLoadTest/BP_Test_RuntimeLoad.uasset and b/Content/Levels/Test/Actors/RuntimeLoadTest/BP_Test_RuntimeLoad.uasset differ diff --git a/Content/Levels/Test/L_Test.umap b/Content/Levels/Test/L_Test.umap index c1291f9..7709d9e 100644 Binary files a/Content/Levels/Test/L_Test.umap and b/Content/Levels/Test/L_Test.umap differ diff --git a/Content/Misc/ImportTest/BP_ImportTest_Barrel.uasset b/Content/Misc/ImportTest/BP_ImportTest_Barrel.uasset new file mode 100644 index 0000000..a1b053d Binary files /dev/null and b/Content/Misc/ImportTest/BP_ImportTest_Barrel.uasset differ diff --git a/Source/Lost_Edge/Private/ContentLoader.cpp b/Source/Lost_Edge/Private/ContentLoader.cpp index 67f72f3..0a39ebc 100644 --- a/Source/Lost_Edge/Private/ContentLoader.cpp +++ b/Source/Lost_Edge/Private/ContentLoader.cpp @@ -5,210 +5,117 @@ #include "IPlatformFilePak.h" -#include "UObject/Class.h" -#include "UObject/UObjectIterator.h" - -namespace +void UContentLoader::BeginDestroy() { - constexpr auto mountPath = TEXT("/Game/LoadedContent"); + //if(mountedFiles.Num() > 0) + //{ + // while(mountedFiles.Num() > 0) + // UnmountPak(*mountedFiles.begin()); + // + // FPlatformFileManager::Get().RemovePlatformFile(GetPakPlatformFile()); + //} + + UObject::BeginDestroy(); } -UClass* UContentLoader::MountAndRegisterPak(FString pakFilePath) +UClass* UContentLoader::LoadPak(const FString& pakFilePath) { - if(pakFilePath.IsEmpty()) + UE_LOG(LogTemp, Log, TEXT("ContentLoader: Starting to mount pak %s"), *pakFilePath); + + FPakPlatformFile* pakFileMgr = (FPakPlatformFile*)(FPlatformFileManager::Get().FindPlatformFile(TEXT("PakFile"))); + if(!pakFileMgr) + { + UE_LOG(LogTemp, Warning, TEXT("ContentLoader: Unable to get PakPlatformFile.")); return nullptr; + } - pakFilePath = FPaths::ProjectContentDir() / pakFilePath; - - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("ContentLoader: Starting to mount pak: %s"), *pakFilePath)); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Starting to mount pak: %s"), *pakFilePath); - - if(!MountPak(*pakFilePath)) + if(!pakFileMgr->Mount(*pakFilePath, 0)) + { + UE_LOG(LogTemp, Warning, TEXT("ContentLoader: Unable to mount pak %s."), *pakFilePath); return nullptr; + } + + UE_LOG(LogTemp, Warning, TEXT("ContentLoader: Pak %s mounted successfully."), *pakFilePath); TArray content; - FString mountPoint = GetPakMountContentPath(pakFilePath, content); - RegisterMountPoint(mountPath, mountPoint); - - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("ContentLoader: Pak '%s' contains %d items"), *FPaths::GetCleanFilename(pakFilePath), content.Num())); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Pak '%s' contains %d items"), *FPaths::GetCleanFilename(pakFilePath), content.Num()); + const FString mountPoint = GetPakMountContent(pakFilePath, &content); + UE_LOG(LogTemp, Log, TEXT("ContentLoader: Pak '%s' contains %d items."), *FPaths::GetCleanFilename(pakFilePath), content.Num()); + FPackageName::RegisterMountPoint("/Game/", mountPoint); for(auto& i : content) { if(FPaths::GetCleanFilename(i).StartsWith(TEXT("BP_"))) { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("ContentLoader: Found BP class: '%s'"), *i)); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Found BP class: '%s'"), *i); - return LoadPakObjClassReference(i); + UE_LOG(LogTemp, Log, TEXT("ContentLoader: Found BP class '%s' of pak %s."), *i, *pakFilePath); + return GetPakClass(i); } } return nullptr; } -void UContentLoader::BeginDestroy() +FString UContentLoader::GetPakMountContent(const FString& pakFilePath, TArray* content) { - if(mountedFiles.Num() > 0) - { - while(mountedFiles.Num() > 0) - UnmountPak(*mountedFiles.begin()); + TArray contentProxy; + if(!content) + content = &contentProxy; - FPlatformFileManager::Get().RemovePlatformFile(GetPakPlatformFile()); - } - - UObject::BeginDestroy(); -} - -FPakPlatformFile* UContentLoader::GetPakPlatformFile() -{ - FPakPlatformFile* pakPlatformFile = nullptr; - - IPlatformFile& platformFile = FPlatformFileManager::Get().GetPlatformFile(); - if(!platformFile.GetLowerLevel()) - { - pakPlatformFile = new FPakPlatformFile(); - pakPlatformFile->Initialize(&platformFile, TEXT("")); - FPlatformFileManager::Get().SetPlatformFile(*pakPlatformFile); - } - else - { - pakPlatformFile = (FPakPlatformFile*)(FPlatformFileManager::Get().FindPlatformFile(TEXT("PakFile"))); - } - - if(!pakPlatformFile) - { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("ContentLoader: PakFileManager not found")); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: PakFileManager not found")); - } - - return pakPlatformFile; -} - -bool UContentLoader::MountPak(const FString& path) -{ - FPakPlatformFile* pakFileMgr = GetPakPlatformFile(); - - if(!pakFileMgr) - return false; - - FString mountPoint = FString::Printf(TEXT("%s/%s"), mountPath, *FPaths::GetBaseFilename(*path)); - bool mounted = pakFileMgr->Mount(*path, 0, *mountPoint); - if(!mounted) - { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("ContentLoader: Pak %s mount failed"), *FPaths::GetCleanFilename(path))); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Pak %s mount failed"), *FPaths::GetCleanFilename(path)); - return false; - } - else - { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("ContentLoader: Pak %s mounted at %s"), *FPaths::GetCleanFilename(path), *mountPoint)); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Pak %s mounted at %s"), *FPaths::GetCleanFilename(path), *mountPoint); - - mountedLock.Lock(); - mountedFiles.Add(path); - mountedLock.Unlock(); - } - - return true; -} -bool UContentLoader::UnmountPak(const FString& path) -{ - FPakPlatformFile* pakFileMgr = GetPakPlatformFile(); - - if(!pakFileMgr) - return false; - - bool unmounted = pakFileMgr->Unmount(*path); - if(!unmounted) - { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("ContentLoader: Pak %s unmount failed"), *FPaths::GetCleanFilename(path))); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Pak %s unmount failed"), *FPaths::GetCleanFilename(path)); - return false; - } - else - { - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("ContentLoader: Pak %s unmounted successfully"), *FPaths::GetCleanFilename(path))); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Pak %s unmounted successfully"), *FPaths::GetCleanFilename(path)); - - mountedLock.Lock(); - mountedFiles.Remove(path); - mountedLock.Unlock(); - } - - return true; -} - -void UContentLoader::RegisterMountPoint(const FString& rootPath, const FString& contentPath) -{ - FPackageName::RegisterMountPoint(rootPath, contentPath); -} -void UContentLoader::UnRegisterMountPoint(const FString& rootPath, const FString& contentPath) -{ - FPackageName::UnRegisterMountPoint(rootPath, contentPath); -} - -FString UContentLoader::GetPakMountPoint(const FString& pakFilePath) -{ + FString mountPoint, contentPath, contentPathTail; TRefCountPtr pakFile = new FPakFile(FPlatformFileManager::Get().FindPlatformFile(TEXT("PakFile")), *pakFilePath, false); - + auto pak = pakFile.GetReference(); if(pakFile.GetReference()->IsValid()) { - return pakFile.GetReference()->GetMountPoint(); + mountPoint = pakFile.GetReference()->GetMountPoint(); + mountPoint.Split("/Content/", &contentPath, &contentPathTail); + + for(FPakFile::FFilenameIterator It(*pakFile.GetReference(), false); It; ++It) + if(FPaths::GetExtension(It.Filename()) == TEXT("uasset")) + content->Add(FString::Printf(TEXT("%s%s"), *contentPathTail, *It.Filename())); } - return {}; -} - -TArray UContentLoader::GetPakContent(const FString& pakFilePath, const FString& appendPath) -{ - TRefCountPtr pakFile = new FPakFile(FPlatformFileManager::Get().FindPlatformFile(TEXT("PakFile")), *pakFilePath, false); - TArray pakContent; - - if(pakFile.GetReference()->IsValid()) + if(!contentPath.IsEmpty()) { - for(FPakFile::FFilenameIterator it{ *pakFile, false }; it; ++it) - { - if(FPaths::GetExtension(it.Filename()) == TEXT("uasset")) - { - pakContent.Add(FString::Printf(TEXT("%s%s"), *appendPath, *it.Filename())); - } - } - } - return pakContent; -} - -FString UContentLoader::GetPakMountContentPath(const FString& pakFilePath, TArray& content) -{ - FString contentPath, appendPath; - FString mountPoint = GetPakMountPoint(pakFilePath); - - if(mountPoint.Split("/Content/", &contentPath, &appendPath)) - { - content = GetPakContent(pakFilePath, appendPath); return FString::Printf(TEXT("%s/Content/"), *contentPath); } - else + else if(content->Num()) { - content = GetPakContent(pakFilePath, appendPath); - if(content.Num() > 0) + const FString fullPath = FString::Printf(TEXT("%s%s"), *mountPoint, *((*content)[0])); + if(fullPath.Split("/Content/", &contentPath, &contentPathTail)) { - mountPoint = FString::Printf(TEXT("%s%s"), *mountPoint, *content[0]); - if(mountPoint.Split("/Content/", &contentPath, &appendPath)) - { - return FString::Printf(TEXT("%s/Content/"), *contentPath); - } + return FString::Printf(TEXT("%s/Content/"), *contentPath); } } - return {}; + return FString(""); } -UClass* UContentLoader::LoadPakObjClassReference(const FString& pakContentPath) +UClass* UContentLoader::GetPakClass(const FString& pakContentPath) { - FString assetName = FString::Printf(TEXT("%s/%s.%s_C"), mountPath, *FPaths::GetBaseFilename(pakContentPath, false), *FPaths::GetBaseFilename(pakContentPath, true)); + const FString assetName = FString::Printf(TEXT("/Game/%s.%s"), *FPaths::GetBaseFilename(pakContentPath, false), *FPaths::GetBaseFilename(pakContentPath, true)); + const FString className = assetName + TEXT(".") + FPackageName::GetShortName(assetName) + TEXT("_C"); + auto result = StaticLoadClass(UObject::StaticClass(), nullptr, *className); + if(!result) + UE_LOG(LogTemp, Warning, TEXT("ContentLoader: Unable to load pak class: %s."), *className); + return result; +} - GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("ContentLoader: Loading '%s' class"), *assetName)); - UE_LOG(LogTemp, Log, TEXT("ContentLoader: Loading '%s' class"), *assetName); +bool UContentLoader::UnloadPak(const FString& pakFilePath) +{ + FPakPlatformFile* pakFileMgr = (FPakPlatformFile*)(FPlatformFileManager::Get().FindPlatformFile(TEXT("PakFile"))); + if(!pakFileMgr) + { + UE_LOG(LogTemp, Warning, TEXT("Unable to get PakPlatformFile for pak file (Unmount): %s"), *pakFilePath); + return false; + } - return nullptr;//StaticLoadClass(UObject::StaticClass(), nullptr, *assetName); + if(!pakFileMgr->Unmount(*pakFilePath)) + { + UE_LOG(LogTemp, Warning, TEXT("Unable to unmount pak: %s"), *pakFilePath); + return false; + } + + const FString mountPoint = GetPakMountContent(pakFilePath); + FPackageName::UnRegisterMountPoint("/Game/", mountPoint); + + return true; } \ No newline at end of file diff --git a/Source/Lost_Edge/Private/ContentLoader.h b/Source/Lost_Edge/Private/ContentLoader.h index 8448aca..b169842 100644 --- a/Source/Lost_Edge/Private/ContentLoader.h +++ b/Source/Lost_Edge/Private/ContentLoader.h @@ -13,27 +13,15 @@ class UContentLoader : public UObject public: UFUNCTION(BlueprintCallable, Category = "ContentLoader") - UClass* MountAndRegisterPak(FString pakFilePath); - - + UClass* LoadPak(const FString& pakFilePath); protected: virtual void BeginDestroy() override; - class FPakPlatformFile* GetPakPlatformFile(); + UFUNCTION(BlueprintCallable, Category = "ContentLoader") + bool UnloadPak(const FString& pakFilePath); - bool MountPak(const FString& path); - bool UnmountPak(const FString& path); - - void RegisterMountPoint(const FString& rootPath, const FString& contentPath); - void UnRegisterMountPoint(const FString& rootPath, const FString& contentPath); - - FString GetPakMountPoint(const FString& pakFilePath); - TArray GetPakContent(const FString& pakFilePath, const FString& appendPath); - FString GetPakMountContentPath(const FString& pakFilePath, TArray& content); - - UClass* LoadPakObjClassReference(const FString& pakContentPath); - - TSet mountedFiles; - FCriticalSection mountedLock; +protected: + FString GetPakMountContent(const FString& pakFilePath, TArray* content = nullptr); + UClass* GetPakClass(const FString& pakContentPath); };