I haven’t found anything about working with files. It would be easy if I could just load it as a struct. I would like to build it with Visual Studio, Import it at runtime and have the values the same.
If your binary data matches the way UE serializes data, then you can write a matching struct and parse the data with UE reflection serializer. Something like this
USTRUCT()
struct FMyData
{
UPROPERTY() int32 a;
UPROPERTY() FString b;
UPROPERTY() TArray<float> c;
};
Here is a long version example that could work :
//FString FilePath;
UScriptStruct* StructInfo = FMyData::StaticStruct();
// load file
TArray<uint8> Bytes;
FFileHelper::LoadFileToArray(Bytes, *FilePath);
FMemoryReader Ar(Bytes);
// init memory
void* Container = FMemory::Malloc(StructInfo->GetStructureSize());
StructInfo->InitializeStruct(Container);
// parse bytes into struct
StructInfo->SerializeBin(Ar, Container);
Here is a shorter version that could also work
TArray<uint8> Bytes;
FFileHelper::LoadFileToArray(Bytes, *FilePath);
FMyData Data = *(FMyStruct*)(Bytes.GetData());
In both cases you risk running into alignment issues or data issues, due to how the binary is laid out in the file vs. how it is laid out in memory vs. how the serializer transforms it (if it does).
So it all very much depends on how the binary data was saved in the first place.
If you want to have better control over the deserialization process, write your own serializer :
//reflection not required in this case
struct FMyData
{
int32 a;
FString b;
TArray<float> c;
bool Serialize(FArchive& Ar);
};
template<> struct TStructOpsTypeTraits<FMyData> : public TStructOpsTypeTraitsBase2<FMyData>
{
enum { WithSerializer = true };
};
bool FMyData::Serialize(FArchive& Ar)
{
Ar << a;
Ar << b;
Ar << c;
return true;
}
Then you should be able to use it like first example, or even directly like this
TArray<uint8> Bytes;
FFileHelper::LoadFileToArray(Bytes, *FilePath);
FMemoryReader Ar(Bytes);
FMyData Result;
Result.Serialize(Ar);
return Result;
I found that I can use FStream and it at least compiles (I do not know if it will work yet) I just forgot to use the namespace std