Skip to content

Instantly share code, notes, and snippets.

@iUltimateLP
Created March 20, 2016 19:18
Show Gist options
  • Save iUltimateLP/e38a134c4a674a12c1b4 to your computer and use it in GitHub Desktop.
Save iUltimateLP/e38a134c4a674a12c1b4 to your computer and use it in GitHub Desktop.
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "CompressedSaverPrivatePCH.h"
#include "CompressedSaverBPLibrary.h"
UCompressedSaverBPLibrary::UCompressedSaverBPLibrary(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
void UCompressedSaverBPLibrary::SaveLoadData(FArchive& Ar, FBrickGridData& SaveBrickGridData, FTransform& SavePlayerTransform, float& SaveCurrentTimeInDays)
{
Ar << SaveBrickGridData; //This uses a custom operator code !
Ar << SavePlayerTransform;
Ar << SaveCurrentTimeInDays;
}
bool UCompressedSaverBPLibrary::SaveCompressed(FBrickGridData brickGridData, FTransform playerTransform, float currentTimeInDays)
{
FString SavePath = "C:\\Users\\Johnny\\Desktop\\BrickGame\\mysavefile_compressed.save";
FBufferArchive ToBinary;
SaveLoadData(ToBinary, brickGridData, playerTransform, currentTimeInDays);
UE_LOG(LogWindows, Warning, TEXT("Precompressed File Size: %s"), *FString::FromInt(ToBinary.Num()));
TArray<uint8> compressedData;
FArchiveSaveCompressedProxy compressor = FArchiveSaveCompressedProxy(compressedData, COMPRESS_ZLIB);
compressor << ToBinary;
compressor.Flush();
UE_LOG(LogWindows, Warning, TEXT("Compressed File Size: %s"), *FString::FromInt(compressedData.Num()));
if (FFileHelper::SaveArrayToFile(compressedData, *SavePath))
{
compressor.FlushCache();
compressedData.Empty();
ToBinary.FlushCache();
ToBinary.Empty();
ToBinary.Close();
UE_LOG(LogWindows, Warning, TEXT("Save successfull!!"));
return true;
}
else
{
compressor.FlushCache();
compressedData.Empty();
ToBinary.FlushCache();
ToBinary.Empty();
UE_LOG(LogWindows, Error, TEXT("File could not be saved!"));
return false;
}
}
bool UCompressedSaverBPLibrary::LoadCompressed(FBrickGridData& brickGridData, FTransform& playerTransform, float& currentTimeInDays)
{
FString SavePath = "C:\\Users\\Johnny\\Desktop\\BrickGame\\mysavefile_compressed.save";
TArray<uint8> compressedData;
if (!FFileHelper::LoadFileToArray(compressedData, *SavePath))
{
UE_LOG(LogWindows, Error, TEXT("File seems invalid!"));
return false;
}
FArchiveLoadCompressedProxy decompressor = FArchiveLoadCompressedProxy(compressedData, COMPRESS_ZLIB);
if (decompressor.GetError())
{
UE_LOG(LogWindows, Error, TEXT("Error decompressing file! Did you compress the file?"));
return false;
}
FBufferArchive decompressedBinaryArray;
decompressor << decompressedBinaryArray;
FMemoryReader fromBinary = FMemoryReader(decompressedBinaryArray, true);
fromBinary.Seek(0);
SaveLoadData(fromBinary, brickGridData, playerTransform, currentTimeInDays);
compressedData.Empty();
decompressor.FlushCache();
fromBinary.FlushCache();
decompressedBinaryArray.Empty();
decompressedBinaryArray.Close();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment