// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. #include "AddContentDialogPCH.h" #include "FeaturePackContentSource.h" #include "AssetToolsModule.h" #include "IPlatformFilePak.h" DEFINE_LOG_CATEGORY_STATIC(LogFeaturePack, Log, All); FFeaturePackContentSource::FFeaturePackContentSource(FString InFeaturePackPath) { FeaturePackPath = InFeaturePackPath; bPackValid = false; // Create a pak platform file and mount the feature pack file. FPakPlatformFile PakPlatformFile; FString CommandLine; PakPlatformFile.Initialize(&FPlatformFileManager::Get().GetPlatformFile(), TEXT("")); FString MountPoint = "root:/"; PakPlatformFile.Mount(*InFeaturePackPath, 0, *MountPoint); // Gets the manifest file as a JSon string TArray ManifestBuffer; if( LoadPakFileToBuffer(PakPlatformFile, FPaths::Combine(*MountPoint, TEXT("manifest.json")), ManifestBuffer) == false ) { UE_LOG(LogFeaturePack, Warning, TEXT("Error in Feature pack %s. Cannot find manifest."), *InFeaturePackPath); Category = EContentSourceCategory::Unknown; return; } FString ManifestString; FFileHelper::BufferToString(ManifestString, ManifestBuffer.GetData(), ManifestBuffer.Num()); // Populate text fields from the manifest. TSharedPtr ManifestObject; TSharedRef> ManifestReader = TJsonReaderFactory<>::Create(ManifestString); FJsonSerializer::Deserialize(ManifestReader, ManifestObject); for (TSharedPtr NameValue : ManifestObject->GetArrayField("Name")) { TSharedPtr LocalizedNameObject = NameValue->AsObject(); LocalizedNames.Add(FLocalizedText( LocalizedNameObject->GetStringField("Language"), FText::FromString(LocalizedNameObject->GetStringField("Text")))); } for (TSharedPtr DescriptionValue : ManifestObject->GetArrayField("Description")) { TSharedPtr LocalizedDescriptionObject = DescriptionValue->AsObject(); LocalizedDescriptions.Add(FLocalizedText( LocalizedDescriptionObject->GetStringField("Language"), FText::FromString(LocalizedDescriptionObject->GetStringField("Text")))); } FString CategoryString = ManifestObject->GetStringField("Category"); if (CategoryString == "CodeFeature") { Category = EContentSourceCategory::CodeFeature; } else if (CategoryString == "BlueprintFeature") { Category = EContentSourceCategory::BlueprintFeature; } else if (CategoryString == "Content") { Category = EContentSourceCategory::Content; } else { Category = EContentSourceCategory::Unknown; } // Load image data FString IconFilename = ManifestObject->GetStringField("Thumbnail"); TSharedPtr> IconImageData = MakeShareable(new TArray()); LoadPakFileToBuffer(PakPlatformFile, FPaths::Combine(*MountPoint, TEXT("Media"), *IconFilename), *IconImageData); IconData = MakeShareable(new FImageData(IconFilename, IconImageData)); const TArray> ScreenshotFilenameArray = ManifestObject->GetArrayField("Screenshots"); for (const TSharedPtr ScreenshotFilename : ScreenshotFilenameArray) { TSharedPtr> SingleScreenshotData = MakeShareable(new TArray); LoadPakFileToBuffer(PakPlatformFile, FPaths::Combine(*MountPoint, TEXT("Media"), *ScreenshotFilename->AsString()), *SingleScreenshotData); ScreenshotData.Add(MakeShareable(new FImageData(ScreenshotFilename->AsString(), SingleScreenshotData))); } bPackValid = true; } bool FFeaturePackContentSource::LoadPakFileToBuffer(FPakPlatformFile& PakPlatformFile, FString Path, TArray& Buffer) { bool bResult = false; TSharedPtr FileHandle(PakPlatformFile.OpenRead(*Path)); if( FileHandle.IsValid()) { Buffer.AddUninitialized(FileHandle->Size()); bResult = FileHandle->Read(Buffer.GetData(), FileHandle->Size()); } return bResult; } TArray FFeaturePackContentSource::GetLocalizedNames() { return LocalizedNames; } TArray FFeaturePackContentSource::GetLocalizedDescriptions() { return LocalizedDescriptions; } EContentSourceCategory FFeaturePackContentSource::GetCategory() { return Category; } TSharedPtr FFeaturePackContentSource::GetIconData() { return IconData; } TArray> FFeaturePackContentSource::GetScreenshotData() { return ScreenshotData; } bool FFeaturePackContentSource::InstallToProject(FString InstallPath) { bool bResult = false; if( IsDataValid() == false ) { UE_LOG(LogFeaturePack, Warning, TEXT("Trying to install invalid pack %s"), *InstallPath); } else { FAssetToolsModule& AssetToolsModule = FModuleManager::Get().LoadModuleChecked("AssetTools"); TArray AssetPaths; AssetPaths.Add(FeaturePackPath); TArray ImportedObjects = AssetToolsModule.Get().ImportAssets(AssetPaths, InstallPath); if( ImportedObjects.Num() == 0 ) { UE_LOG(LogFeaturePack, Warning, TEXT("No objects imported installing pack %s"), *InstallPath); } else { bResult = true; } } return bResult; } FFeaturePackContentSource::~FFeaturePackContentSource() { } bool FFeaturePackContentSource::IsDataValid() const { if( bPackValid == false ) { return false; } // To Do maybe validate other data here return true; }