@@ -106,59 +106,90 @@ void Archive::AddFile(const std::string& filePath) {
106
106
(*mHashes )[CRC64 (filePath.c_str ())] = filePath;
107
107
}
108
108
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
+ }
111
112
113
+ std::shared_ptr<ResourceInitData> Archive::ReadResourceInitDataLegacy (const std::string& filePath, std::shared_ptr<File> fileToLoad) {
112
114
// Determine if file is binary or XML...
113
115
if (fileToLoad->Buffer ->at (0 ) == ' <' ) {
114
116
// File is XML
115
117
// Read the xml document
116
118
auto stream = std::make_shared<MemoryStream>(fileToLoad->Buffer );
117
119
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>( );
120
122
121
123
xmlReader->Parse (binaryReader->ReadCString ().data ());
122
124
if (xmlReader->Error ()) {
123
125
SPDLOG_ERROR (" Failed to parse XML file {}. Error: {}" , filePath, xmlReader->ErrorStr ());
124
126
return nullptr ;
125
127
}
126
- fileToLoad-> InitData = ReadResourceInitDataXml (filePath, xmlReader);
128
+ return ReadResourceInitDataXml (filePath, xmlReader);
127
129
} 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 ;
153
138
}
154
139
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
+ }
158
185
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 ;
162
193
}
163
194
164
195
return fileToLoad;
0 commit comments