2019-12-26 15:33:43 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
#include "LandscapeFileFormatRaw.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
|
|
|
|
|
#include "Containers/Array.h"
|
|
|
|
|
#include "Containers/UnrealString.h"
|
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
|
|
|
#include "HAL/FileManager.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
#include "HAL/PlatformCrt.h"
|
|
|
|
|
#include "HAL/UnrealMemory.h"
|
|
|
|
|
#include "Internationalization/Internationalization.h"
|
|
|
|
|
#include "Internationalization/Text.h"
|
|
|
|
|
#include "Math/Vector.h"
|
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none
==========================
MAJOR FEATURES + CHANGES
==========================
Change 3209340 on 2016/11/23 by Ben.Marsh
Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.
Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.
* Every header now includes everything it needs to compile.
* There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
* There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
* Every .cpp file includes its matching .h file first.
* This helps validate that each header is including everything it needs to compile.
* No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
* You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
* There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
* No engine code explicitly includes a precompiled header any more.
* We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
* PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.
Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.
[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
|
|
|
#include "Misc/FileHelper.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
#include "Templates/UnrealTemplate.h"
|
2016-07-18 11:58:33 -04:00
|
|
|
|
2023-04-05 17:37:25 -04:00
|
|
|
#include "Serialization/JsonReader.h"
|
|
|
|
|
#include "Serialization/JsonWriter.h"
|
|
|
|
|
#include "Serialization/JsonSerializer.h"
|
|
|
|
|
|
2016-07-18 11:58:33 -04:00
|
|
|
#define LOCTEXT_NAMESPACE "LandscapeEditor.NewLandscape"
|
|
|
|
|
|
2023-04-05 17:37:25 -04:00
|
|
|
namespace
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
void WriteMetaData(const TCHAR* InMapFilename, FLandscapeFileResolution InDataResolution, int32 InBitsPerPixel)
|
2023-04-04 19:16:31 -04:00
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
FString RawMetadataFilename = FPaths::SetExtension(InMapFilename, ".json");
|
|
|
|
|
FString JsonStr;
|
|
|
|
|
|
|
|
|
|
TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<TCHAR>::Create(&JsonStr);
|
|
|
|
|
TSharedRef<FJsonObject> JsonObject = MakeShareable(new FJsonObject);;
|
|
|
|
|
JsonObject->SetField("width", MakeShared<FJsonValueNumber>(InDataResolution.Width));
|
|
|
|
|
JsonObject->SetField("height", MakeShared<FJsonValueNumber>(InDataResolution.Height));
|
|
|
|
|
JsonObject->SetField("bpp", MakeShared<FJsonValueNumber>(InBitsPerPixel));
|
|
|
|
|
|
|
|
|
|
if (FJsonSerializer::Serialize<TCHAR>(JsonObject, JsonWriter))
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
FFileHelper::SaveStringToFile(JsonStr, *RawMetadataFilename);
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-05 17:37:25 -04:00
|
|
|
}
|
2016-07-18 11:58:33 -04:00
|
|
|
|
2023-04-05 17:37:25 -04:00
|
|
|
bool GetRawResolution(const TCHAR* InFilename, FLandscapeFileResolution& OutResolution, int32& OutBitsPerPixel)
|
|
|
|
|
{
|
|
|
|
|
FString Extension = FPaths::GetExtension(InFilename);
|
|
|
|
|
FString RawMetadataFilename = FPaths::SetExtension(InFilename, ".json");
|
|
|
|
|
|
2023-08-02 17:37:59 -04:00
|
|
|
OutBitsPerPixel = 0;
|
|
|
|
|
|
2023-04-05 17:37:25 -04:00
|
|
|
if (FPaths::FileExists(RawMetadataFilename))
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
FString JsonStr;
|
|
|
|
|
FFileHelper::LoadFileToString(JsonStr, *RawMetadataFilename);
|
|
|
|
|
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonStr);
|
|
|
|
|
TSharedPtr<FJsonObject> JsonObject;
|
|
|
|
|
if (FJsonSerializer::Deserialize(JsonReader, JsonObject))
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-10-06 08:18:42 -04:00
|
|
|
OutResolution.Width = JsonObject->GetIntegerField(TEXT("width"));
|
|
|
|
|
OutResolution.Height = JsonObject->GetIntegerField(TEXT("height"));
|
2023-04-05 17:37:25 -04:00
|
|
|
|
2023-10-06 08:18:42 -04:00
|
|
|
OutBitsPerPixel = JsonObject->GetIntegerField(TEXT("bpp"));
|
2023-04-05 17:37:25 -04:00
|
|
|
|
|
|
|
|
return true;
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
2023-04-05 17:37:25 -04:00
|
|
|
|
|
|
|
|
return false;
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
2023-10-06 08:18:42 -04:00
|
|
|
const bool bIs16Bit = Extension == FString(TEXT("r16"));
|
|
|
|
|
const bool bIs8Bit = Extension == FString(TEXT("r8"));
|
2023-04-05 17:37:25 -04:00
|
|
|
|
|
|
|
|
if (!(bIs16Bit || bIs8Bit))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int32 BytesPerPixel = bIs16Bit ? 2 : 1;
|
|
|
|
|
int64 FileSize = IFileManager::Get().FileSize(InFilename);
|
|
|
|
|
|
2023-04-21 00:15:30 -04:00
|
|
|
const int64 NumPixels = FileSize / BytesPerPixel;
|
|
|
|
|
const uint32 Dimension = FMath::TruncToInt32(FMath::Sqrt(static_cast<double>(NumPixels)));
|
2023-04-05 17:37:25 -04:00
|
|
|
if (Dimension * Dimension == NumPixels)
|
|
|
|
|
{
|
|
|
|
|
OutResolution.Width = Dimension;
|
|
|
|
|
OutResolution.Height = Dimension;
|
|
|
|
|
|
|
|
|
|
OutBitsPerPixel = BytesPerPixel * 8;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FLandscapeHeightmapFileFormat_Raw::FLandscapeHeightmapFileFormat_Raw()
|
|
|
|
|
{
|
|
|
|
|
FileTypeInfo.Description = LOCTEXT("FileFormatRaw_HeightmapDesc", "Heightmap .r16/.raw files");
|
|
|
|
|
FileTypeInfo.Extensions.Add(".r16");
|
|
|
|
|
FileTypeInfo.Extensions.Add(".raw");
|
|
|
|
|
FileTypeInfo.bSupportsExport = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeFileInfo FLandscapeHeightmapFileFormat_Raw::Validate(const TCHAR* HeightmapFilename, FName LayerName) const
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeFileInfo Result;
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
int64 ImportFileSize = IFileManager::Get().FileSize(HeightmapFilename);
|
|
|
|
|
|
|
|
|
|
if (ImportFileSize < 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileReadError", "Error reading heightmap file");
|
|
|
|
|
}
|
|
|
|
|
else if (ImportFileSize == 0 || ImportFileSize % 2 != 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
2023-08-07 20:01:44 -04:00
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileInvalidSizeNot16bit", "The heightmap file has an invalid size (possibly not 16-bit?)");
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
FLandscapeFileResolution Resolution;
|
|
|
|
|
int32 BitsPerPixel;
|
|
|
|
|
if (GetRawResolution(HeightmapFilename, Resolution, BitsPerPixel))
|
|
|
|
|
{
|
|
|
|
|
if (BitsPerPixel != 16)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
2023-04-08 13:47:46 -04:00
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileBitsPerPixel", "Height file has an invalid number of bits per pixel");
|
2023-04-05 17:37:25 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.PossibleResolutions = { Resolution };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-08-02 17:37:59 -04:00
|
|
|
if (BitsPerPixel == 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_RawFileInvalidExtension", "The file bit depth unknown bit depth use .r16 (height) or .r8 (weight)");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
2023-08-03 11:58:54 -04:00
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileInvalidSize", "The heightmap file has an invalid size (possibly not 16-bit?)");
|
2023-08-02 17:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeImportData<uint16> FLandscapeHeightmapFileFormat_Raw::Import(const TCHAR* HeightmapFilename, FName LayerName, FLandscapeFileResolution ExpectedResolution) const
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeImportData<uint16> Result;
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
TArray<uint8> TempData;
|
|
|
|
|
if (!FFileHelper::LoadFileToArray(TempData, HeightmapFilename, FILEREAD_Silent))
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileReadError", "Error reading heightmap file");
|
|
|
|
|
}
|
|
|
|
|
else if (TempData.Num() != (ExpectedResolution.Width * ExpectedResolution.Height * 2))
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapResolutionMismatch", "The heightmap file's resolution does not match the requested resolution");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.Data.Empty(ExpectedResolution.Width * ExpectedResolution.Height);
|
|
|
|
|
Result.Data.AddUninitialized(ExpectedResolution.Width * ExpectedResolution.Height);
|
|
|
|
|
FMemory::Memcpy(Result.Data.GetData(), TempData.GetData(), ExpectedResolution.Width * ExpectedResolution.Height * 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 07:40:43 -04:00
|
|
|
void FLandscapeHeightmapFileFormat_Raw::Export(const TCHAR* HeightmapFilename, FName LayerName, TArrayView<const uint16> Data, FLandscapeFileResolution DataResolution, FVector Scale) const
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
|
|
|
|
TArray<uint8> TempData;
|
|
|
|
|
TempData.Empty(DataResolution.Width * DataResolution.Height * 2);
|
|
|
|
|
TempData.AddUninitialized(DataResolution.Width * DataResolution.Height * 2);
|
|
|
|
|
FMemory::Memcpy(TempData.GetData(), Data.GetData(), DataResolution.Width * DataResolution.Height * 2);
|
|
|
|
|
|
2023-04-05 17:37:25 -04:00
|
|
|
if (FFileHelper::SaveArrayToFile(TempData, HeightmapFilename))
|
|
|
|
|
{
|
|
|
|
|
FString Extension = FPaths::GetExtension(HeightmapFilename);
|
|
|
|
|
|
|
|
|
|
if (Extension == "raw")
|
|
|
|
|
{
|
|
|
|
|
WriteMetaData(HeightmapFilename, DataResolution, 16);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FLandscapeWeightmapFileFormat_Raw::FLandscapeWeightmapFileFormat_Raw()
|
|
|
|
|
{
|
|
|
|
|
FileTypeInfo.Description = LOCTEXT("FileFormatRaw_WeightmapDesc", "Layer .r8/.raw files");
|
|
|
|
|
FileTypeInfo.Extensions.Add(".r8");
|
|
|
|
|
FileTypeInfo.Extensions.Add(".raw");
|
|
|
|
|
FileTypeInfo.bSupportsExport = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeFileInfo FLandscapeWeightmapFileFormat_Raw::Validate(const TCHAR* WeightmapFilename, FName LayerName) const
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeFileInfo Result;
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
int64 ImportFileSize = IFileManager::Get().FileSize(WeightmapFilename);
|
|
|
|
|
|
|
|
|
|
if (ImportFileSize < 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_LayerFileReadError", "Error reading layer file");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
FLandscapeFileResolution Resolution;
|
|
|
|
|
int32 BitsPerPixel;
|
|
|
|
|
if (GetRawResolution(WeightmapFilename, Resolution, BitsPerPixel))
|
|
|
|
|
{
|
|
|
|
|
if (BitsPerPixel != 8)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_WeightmapFileBitsPerPixel", "Weightmap file has an invalid number of bits per pixel");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.PossibleResolutions = { Resolution };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-08-02 17:37:59 -04:00
|
|
|
if (BitsPerPixel == 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_RawFileInvalidExtension", "The file bit depth unknown bit depth use .r16 (height) or .r8 (weight)");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_WeightmapFileInvalidSize", "The layer file has an invalid size");
|
|
|
|
|
}
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeImportData<uint8> FLandscapeWeightmapFileFormat_Raw::Import(const TCHAR* WeightmapFilename, FName LayerName, FLandscapeFileResolution ExpectedResolution) const
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2020-10-21 07:40:43 -04:00
|
|
|
FLandscapeImportData<uint8> Result;
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
TArray<uint8> TempData;
|
|
|
|
|
if (!FFileHelper::LoadFileToArray(TempData, WeightmapFilename, FILEREAD_Silent))
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_LayerFileReadError", "Error reading layer file");
|
|
|
|
|
}
|
|
|
|
|
else if (TempData.Num() != (ExpectedResolution.Width * ExpectedResolution.Height))
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_LayerResolutionMismatch", "The layer file's resolution does not match the requested resolution");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.Data = MoveTemp(TempData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 07:40:43 -04:00
|
|
|
void FLandscapeWeightmapFileFormat_Raw::Export(const TCHAR* WeightmapFilename, FName LayerName, TArrayView<const uint8> Data, FLandscapeFileResolution DataResolution, FVector Scale) const
|
2016-07-18 11:58:33 -04:00
|
|
|
{
|
2023-04-05 17:37:25 -04:00
|
|
|
if (FFileHelper::SaveArrayToFile(Data, WeightmapFilename))
|
|
|
|
|
{
|
|
|
|
|
FString Extension = FPaths::GetExtension(WeightmapFilename);
|
|
|
|
|
|
|
|
|
|
if (Extension == "raw")
|
|
|
|
|
{
|
|
|
|
|
WriteMetaData(WeightmapFilename, DataResolution, 8);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-18 11:58:33 -04:00
|
|
|
}
|
2016-07-19 06:59:15 -04:00
|
|
|
|
|
|
|
|
#undef LOCTEXT_NAMESPACE
|