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"
|
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"
|
|
|
|
|
#include "Misc/FileHelper.h"
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
#define LOCTEXT_NAMESPACE "LandscapeEditor.NewLandscape"
|
|
|
|
|
|
|
|
|
|
TArray<FLandscapeFileResolution> CalculatePossibleRawResolutions(int64 FileSize)
|
|
|
|
|
{
|
|
|
|
|
TArray<FLandscapeFileResolution> PossibleResolutions;
|
|
|
|
|
|
|
|
|
|
// Find all possible heightmap sizes, between 8 and 8192 width/height
|
|
|
|
|
const int32 MinWidth = FMath::Max(8, (int32)FMath::DivideAndRoundUp(FileSize, (int64)8192));
|
2021-01-04 07:59:22 -04:00
|
|
|
const int32 MaxWidth = FMath::TruncToInt(FMath::Sqrt(static_cast<double>(FileSize)));
|
2016-07-18 11:58:33 -04:00
|
|
|
for (int32 Width = MinWidth; Width <= MaxWidth; Width++)
|
|
|
|
|
{
|
|
|
|
|
if (FileSize % Width == 0)
|
|
|
|
|
{
|
|
|
|
|
FLandscapeFileResolution ImportResolution;
|
|
|
|
|
ImportResolution.Width = Width;
|
|
|
|
|
ImportResolution.Height = FileSize / Width;
|
|
|
|
|
PossibleResolutions.Add(ImportResolution);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int32 i = PossibleResolutions.Num() - 1; i >= 0; --i)
|
|
|
|
|
{
|
|
|
|
|
FLandscapeFileResolution ImportResolution = PossibleResolutions[i];
|
|
|
|
|
if (ImportResolution.Width != ImportResolution.Height)
|
|
|
|
|
{
|
|
|
|
|
Swap(ImportResolution.Width, ImportResolution.Height);
|
|
|
|
|
PossibleResolutions.Add(ImportResolution);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PossibleResolutions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileInvalidSize", "The heightmap file has an invalid size (possibly not 16-bit?)");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Result.PossibleResolutions = CalculatePossibleRawResolutions(ImportFileSize / 2);
|
|
|
|
|
|
|
|
|
|
if (Result.PossibleResolutions.Num() == 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_HeightmapFileInvalidSize", "The heightmap file has an invalid size (possibly not 16-bit?)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
FFileHelper::SaveArrayToFile(TempData, HeightmapFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2019-02-26 16:16:59 -05:00
|
|
|
Result.PossibleResolutions = CalculatePossibleRawResolutions(ImportFileSize);
|
2016-07-18 11:58:33 -04:00
|
|
|
|
|
|
|
|
if (Result.PossibleResolutions.Num() == 0)
|
|
|
|
|
{
|
|
|
|
|
Result.ResultCode = ELandscapeImportResult::Error;
|
|
|
|
|
Result.ErrorMessage = LOCTEXT("Import_WeightmapFileInvalidSize", "The layer file has an invalid size");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
FFileHelper::SaveArrayToFile(Data, WeightmapFilename);
|
|
|
|
|
}
|
2016-07-19 06:59:15 -04:00
|
|
|
|
|
|
|
|
#undef LOCTEXT_NAMESPACE
|