aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/oplogreferencedset.cpp
blob: f34b80b1a088625c6f6321e66fe86bf6b31cb762 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zenstore/oplogreferencedset.h>

#include <zenstore/projectstore.h>

#include <zencore/iobuffer.h>
#include <zencore/logging.h>
#include <zencore/string.h>

namespace zen {

std::optional<OplogReferencedSet>
OplogReferencedSet::LoadFromChunk(const IoBuffer& ChunkData)
{
	using namespace std::literals;

	if (!ChunkData || ChunkData.Size() == 0)
	{
		return std::optional<OplogReferencedSet>();
	}
	std::string_view ChunkText(reinterpret_cast<const char*>(ChunkData.Data()), ChunkData.Size());

	AsciiSet	TrimWhitespace(" \t\r\n");
	const char* FirstNonComment = nullptr;
	uint64_t	Version			= 0;

	// Parse the initial comment block: the leading block of empty or comment lines.
	ForEachStrTok(ChunkText, '\n', [&Version, &FirstNonComment, &TrimWhitespace](std::string_view Line) {
		Line = AsciiSet::TrimSuffixWith(AsciiSet::TrimPrefixWith(Line, TrimWhitespace), TrimWhitespace);
		if (Line.empty())
		{
			return true;  // empty line, keep reading
		}
		if (!Line.starts_with('#'))
		{
			FirstNonComment = Line.data();
			return false;  // non-comment line, stop
		}

		// Comment line in the header block of comments in the file. Interpret it if it is a version line.
		// Skip past the '#' and whitespace.
		Line.remove_prefix(1);
		Line = AsciiSet::TrimPrefixWith(Line, TrimWhitespace);

		// Parse "# Version <uint64>".
		constexpr std::string_view VersionStr("Version "sv);
		if (Line.starts_with(VersionStr))
		{
			std::string_view VersionToken = Line;
			VersionToken.remove_prefix(VersionStr.length());
			VersionToken.remove_suffix(AsciiSet::TrimPrefixWithout(VersionToken, TrimWhitespace).length());
			std::optional<uint64_t> ParsedVersion = ParseInt<uint64_t>(VersionToken);
			if (ParsedVersion)
			{
				Version = *ParsedVersion;
			}
		}
		return true;
	});

	// Report no referencedset if the version is below our minimum version.
	constexpr uint64_t MinSupportedVersion = 1;
	if (Version < MinSupportedVersion)
	{
		ZEN_WARN("ReferencedSet is below the minimum supported version, ignoring it. Version: {}, minimum version: {}.",
				 Version,
				 MinSupportedVersion);
		return std::optional<OplogReferencedSet>();
	}

	// Parse the remaining lines after the leading comment block.
	ChunkText.remove_prefix(FirstNonComment ? FirstNonComment - ChunkText.data() : ChunkText.length());

	eastl::fixed_vector<uint8_t, 256> TmpBuffer;

	OplogReferencedSet Result;
	ForEachStrTok(ChunkText, '\n', [&Result, &TrimWhitespace, &TmpBuffer](std::string_view Line) {
		Line = AsciiSet::TrimSuffixWith(AsciiSet::TrimPrefixWith(Line, TrimWhitespace), TrimWhitespace);
		if (!Line.empty() && !Line.starts_with('#'))
		{
			Result.Set.emplace(OpKeyStringAsOid(Line, TmpBuffer));
		}
		return true;
	});
	return std::optional<OplogReferencedSet>(std::move(Result));
}

void
OplogReferencedSet::Clear()
{
	Set.clear();
}

}  // namespace zen