Files
UnrealEngineUWP/Engine/Source/Editor/AddContentDialog/Private/ContentSourceProviders/FeaturePack/FeaturePackContentSource.cpp
Ben Marsh 149375b14b Update copyright notices to 2015.
[CL 2379638 by Ben Marsh in Main branch]
2014-12-07 19:09:38 -05:00

162 lines
5.1 KiB
C++

// 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<uint8> 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<FJsonObject> ManifestObject;
TSharedRef<TJsonReader<>> ManifestReader = TJsonReaderFactory<>::Create(ManifestString);
FJsonSerializer::Deserialize(ManifestReader, ManifestObject);
for (TSharedPtr<FJsonValue> NameValue : ManifestObject->GetArrayField("Name"))
{
TSharedPtr<FJsonObject> LocalizedNameObject = NameValue->AsObject();
LocalizedNames.Add(FLocalizedText(
LocalizedNameObject->GetStringField("Language"),
FText::FromString(LocalizedNameObject->GetStringField("Text"))));
}
for (TSharedPtr<FJsonValue> DescriptionValue : ManifestObject->GetArrayField("Description"))
{
TSharedPtr<FJsonObject> 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<TArray<uint8>> IconImageData = MakeShareable(new TArray<uint8>());
LoadPakFileToBuffer(PakPlatformFile, FPaths::Combine(*MountPoint, TEXT("Media"), *IconFilename), *IconImageData);
IconData = MakeShareable(new FImageData(IconFilename, IconImageData));
const TArray<TSharedPtr<FJsonValue>> ScreenshotFilenameArray = ManifestObject->GetArrayField("Screenshots");
for (const TSharedPtr<FJsonValue> ScreenshotFilename : ScreenshotFilenameArray)
{
TSharedPtr<TArray<uint8>> SingleScreenshotData = MakeShareable(new TArray<uint8>);
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<uint8>& Buffer)
{
bool bResult = false;
TSharedPtr<IFileHandle> FileHandle(PakPlatformFile.OpenRead(*Path));
if( FileHandle.IsValid())
{
Buffer.AddUninitialized(FileHandle->Size());
bResult = FileHandle->Read(Buffer.GetData(), FileHandle->Size());
}
return bResult;
}
TArray<FLocalizedText> FFeaturePackContentSource::GetLocalizedNames()
{
return LocalizedNames;
}
TArray<FLocalizedText> FFeaturePackContentSource::GetLocalizedDescriptions()
{
return LocalizedDescriptions;
}
EContentSourceCategory FFeaturePackContentSource::GetCategory()
{
return Category;
}
TSharedPtr<FImageData> FFeaturePackContentSource::GetIconData()
{
return IconData;
}
TArray<TSharedPtr<FImageData>> 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<FAssetToolsModule>("AssetTools");
TArray<FString> AssetPaths;
AssetPaths.Add(FeaturePackPath);
TArray<UObject*> 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;
}