Skip to content

Commit 52a0bc8

Browse files
committed
start refactoring, legacy load still working
1 parent a516b66 commit 52a0bc8

File tree

2 files changed

+71
-36
lines changed

2 files changed

+71
-36
lines changed

src/resource/archive/Archive.cpp

+67-36
Original file line numberDiff line numberDiff line change
@@ -106,59 +106,90 @@ void Archive::AddFile(const std::string& filePath) {
106106
(*mHashes)[CRC64(filePath.c_str())] = filePath;
107107
}
108108

109-
std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
110-
auto fileToLoad = LoadFileRaw(filePath);
109+
std::shared_ptr<ResourceInitData> Archive::ReadResourceInitData(const std::string& filePath, std::shared_ptr<File> metaFileToLoad) {
110+
111+
}
111112

113+
std::shared_ptr<ResourceInitData> Archive::ReadResourceInitDataLegacy(const std::string& filePath, std::shared_ptr<File> fileToLoad) {
112114
// Determine if file is binary or XML...
113115
if (fileToLoad->Buffer->at(0) == '<') {
114116
// File is XML
115117
// Read the xml document
116118
auto stream = std::make_shared<MemoryStream>(fileToLoad->Buffer);
117119
auto binaryReader = std::make_shared<BinaryReader>(stream);
118-
fileToLoad->Reader = std::make_shared<tinyxml2::XMLDocument>();
119-
auto xmlReader = std::get<std::shared_ptr<tinyxml2::XMLDocument>>(fileToLoad->Reader);
120+
121+
auto xmlReader = std::make_shared<tinyxml2::XMLDocument>();
120122

121123
xmlReader->Parse(binaryReader->ReadCString().data());
122124
if (xmlReader->Error()) {
123125
SPDLOG_ERROR("Failed to parse XML file {}. Error: {}", filePath, xmlReader->ErrorStr());
124126
return nullptr;
125127
}
126-
fileToLoad->InitData = ReadResourceInitDataXml(filePath, xmlReader);
128+
return ReadResourceInitDataXml(filePath, xmlReader);
127129
} else {
128-
// File is Binary
129-
auto fileToLoadMeta = LoadFileMeta(filePath);
130-
131-
// Split out the header for reading.
132-
if (fileToLoadMeta != nullptr) {
133-
fileToLoad->Buffer =
134-
std::make_shared<std::vector<char>>(fileToLoad->Buffer->begin(), fileToLoad->Buffer->end());
135-
} else {
136-
auto headerBuffer = std::make_shared<std::vector<char>>(fileToLoad->Buffer->begin(),
137-
fileToLoad->Buffer->begin() + OTR_HEADER_SIZE);
138-
139-
if (headerBuffer->size() < OTR_HEADER_SIZE) {
140-
SPDLOG_ERROR("Failed to parse ResourceInitData, buffer size too small. File: {}. Got {} bytes and "
141-
"needed {} bytes.",
142-
filePath, headerBuffer->size(), OTR_HEADER_SIZE);
143-
return nullptr;
144-
}
145-
146-
fileToLoad->Buffer = std::make_shared<std::vector<char>>(fileToLoad->Buffer->begin() + OTR_HEADER_SIZE,
147-
fileToLoad->Buffer->end());
148-
149-
// Create a reader for the header buffer
150-
auto headerStream = std::make_shared<MemoryStream>(headerBuffer);
151-
auto headerReader = std::make_shared<BinaryReader>(headerStream);
152-
fileToLoadMeta = ReadResourceInitDataBinary(filePath, headerReader);
130+
auto headerBuffer = std::make_shared<std::vector<char>>(fileToLoad->Buffer->begin(),
131+
fileToLoad->Buffer->begin() + OTR_HEADER_SIZE);
132+
133+
if (headerBuffer->size() < OTR_HEADER_SIZE) {
134+
SPDLOG_ERROR("Failed to parse ResourceInitData, buffer size too small. File: {}. Got {} bytes and "
135+
"needed {} bytes.",
136+
filePath, headerBuffer->size(), OTR_HEADER_SIZE);
137+
return nullptr;
153138
}
154139

155-
// Create a reader for the data buffer
156-
auto stream = std::make_shared<MemoryStream>(fileToLoad->Buffer);
157-
fileToLoad->Reader = std::make_shared<BinaryReader>(stream);
140+
// Factories expect the buffer to not include the header,
141+
// so we need to remove it from the buffer on the file
142+
fileToLoad->Buffer = std::make_shared<std::vector<char>>(fileToLoad->Buffer->begin() + OTR_HEADER_SIZE,
143+
fileToLoad->Buffer->end());
144+
145+
// Create a reader for the header buffer
146+
auto headerStream = std::make_shared<MemoryStream>(headerBuffer);
147+
auto headerReader = std::make_shared<BinaryReader>(headerStream);
148+
return ReadResourceInitDataBinary(filePath, headerReader);
149+
}
150+
}
151+
152+
std::shared_ptr<BinaryReader> Archive::CreateBinaryReader(std::shared_ptr<File> fileToLoad) {
153+
auto stream = std::make_shared<MemoryStream>(fileToLoad->Buffer);
154+
auto reader = std::make_shared<BinaryReader>(stream);
155+
reader->SetEndianness(fileToLoad->InitData->ByteOrder);
156+
return reader;
157+
}
158+
159+
std::shared_ptr<tinyxml2::XMLDocument> Archive::CreateXMLReader(std::shared_ptr<File> fileToLoad) {
160+
auto stream = std::make_shared<MemoryStream>(fileToLoad->Buffer);
161+
auto binaryReader = std::make_shared<BinaryReader>(stream);
162+
163+
auto xmlReader = std::make_shared<tinyxml2::XMLDocument>();
164+
165+
xmlReader->Parse(binaryReader->ReadCString().data());
166+
if (xmlReader->Error()) {
167+
SPDLOG_ERROR("Failed to parse XML file {}. Error: {}", fileToLoad->InitData->Path, xmlReader->ErrorStr());
168+
return nullptr;
169+
}
170+
171+
return xmlReader;
172+
}
173+
174+
std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
175+
auto fileToLoad = LoadFileRaw(filePath);
176+
177+
auto metaFilePath = filePath + ".meta";
178+
auto metaFileToLoad = LoadFileRaw(metaFilePath);
179+
180+
if (metaFileToLoad != nullptr) {
181+
fileToLoad->InitData = ReadResourceInitData(filePath, metaFileToLoad);
182+
} else {
183+
fileToLoad->InitData = ReadResourceInitDataLegacy(filePath, fileToLoad);
184+
}
158185

159-
fileToLoad->InitData = fileToLoadMeta;
160-
auto binaryReader = std::get<std::shared_ptr<BinaryReader>>(fileToLoad->Reader);
161-
binaryReader->SetEndianness(fileToLoad->InitData->ByteOrder);
186+
switch (fileToLoad->InitData->Format) {
187+
case RESOURCE_FORMAT_BINARY:
188+
fileToLoad->Reader = CreateBinaryReader(fileToLoad);
189+
break;
190+
case RESOURCE_FORMAT_XML:
191+
fileToLoad->Reader = CreateXMLReader(fileToLoad);
192+
break;
162193
}
163194

164195
return fileToLoad;

src/resource/archive/Archive.h

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class Archive {
5353

5454
private:
5555
static std::shared_ptr<ResourceInitData> CreateDefaultResourceInitData();
56+
std::shared_ptr<ResourceInitData> ReadResourceInitData(const std::string& filePath, std::shared_ptr<File> metaFileToLoad);
57+
std::shared_ptr<ResourceInitData> ReadResourceInitDataLegacy(const std::string& filePath, std::shared_ptr<File> fileToLoad);
58+
std::shared_ptr<BinaryReader> CreateBinaryReader(std::shared_ptr<File> fileToLoad);
59+
std::shared_ptr<tinyxml2::XMLDocument> CreateXMLReader(std::shared_ptr<File> fileToLoad);
5660

5761
bool mIsLoaded;
5862
bool mHasGameVersion;

0 commit comments

Comments
 (0)