aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/compactbinaryfile.cpp
blob: 9ddafbe15b33a36db46de4c1dfc99eaffc6809be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zencore/compactbinaryfile.h"
#include "zencore/compactbinaryutil.h"

#include <zencore/filesystem.h>

namespace zen {

CbObjectFromFile
LoadCompactBinaryObject(const std::filesystem::path& FilePath)
{
	FileContents ObjectFile = ReadFile(FilePath);

	if (ObjectFile.ErrorCode)
	{
		throw std::system_error(ObjectFile.ErrorCode);
	}

	IoBuffer ObjectBuffer = ObjectFile.Flatten();

	CbValidateError ValidateResult;
	CbObject		Object = ValidateAndReadCompactBinaryObject(IoBuffer(ObjectBuffer), ValidateResult);
	if (ValidateResult == CbValidateError::None)
	{
		const IoHash WorkerId = IoHash::HashBuffer(ObjectBuffer);
		return {.Object = std::move(Object), .Hash = WorkerId};
	}

	return {.Hash = IoHash::Zero};
}

void
WriteCompactBinaryObject(const std::filesystem::path& Path, const CbObject& Object)
{
	// We cannot use CbObject::GetView() here because it may not return a complete
	// view since the type byte can be omitted in arrays.
	CbWriter Writer;
	Writer.AddObject(Object);
	CbFieldIterator Fields = Writer.Save();
	zen::WriteFile(Path, IoBufferBuilder::MakeFromMemory(Fields.GetRangeView()));
}

}  // namespace zen