Skip to content

Commit e6382dd

Browse files
committed
variant
1 parent ab027b5 commit e6382dd

10 files changed

+74
-64
lines changed

src/resource/File.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <string>
4+
#include <variant>
45
#include <vector>
56
#include <memory>
67
#include <stdint.h>
@@ -28,8 +29,7 @@ struct File {
2829
std::shared_ptr<Archive> Parent;
2930
std::shared_ptr<ResourceInitData> InitData;
3031
std::shared_ptr<std::vector<char>> Buffer;
31-
std::shared_ptr<tinyxml2::XMLDocument> XmlDocument;
32-
std::shared_ptr<BinaryReader> Reader;
32+
std::variant<std::shared_ptr<tinyxml2::XMLDocument>, std::shared_ptr<BinaryReader>> Reader;
3333
bool IsLoaded = false;
3434
};
3535
} // namespace LUS

src/resource/ResourceFactoryBinary.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ResourceFactoryBinary.h"
2+
#include <variant>
23
#include "spdlog/spdlog.h"
34

45
namespace LUS {
@@ -8,7 +9,7 @@ bool ResourceFactoryBinary::FileHasValidFormatAndReader(std::shared_ptr<File> fi
89
return false;
910
}
1011

11-
if (file->Reader == nullptr) {
12+
if (!std::holds_alternative<std::shared_ptr<BinaryReader>>(file->Reader)) {
1213
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type, file->InitData->Path);
1314
return false;
1415
}

src/resource/ResourceFactoryXML.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ bool ResourceFactoryXML::FileHasValidFormatAndReader(std::shared_ptr<File> file)
88
return false;
99
}
1010

11-
if (file->XmlDocument == nullptr) {
11+
if (!std::holds_alternative<std::shared_ptr<tinyxml2::XMLDocument>>(file->Reader)) {
1212
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", file->InitData->Type,
1313
file->InitData->Path);
1414
return false;

src/resource/archive/Archive.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
114114
// File is XML
115115
// Read the xml document
116116
auto stream = std::make_shared<MemoryStream>(fileToLoad->Buffer);
117-
auto reader = fileToLoad->Reader = std::make_shared<BinaryReader>(stream);
118-
fileToLoad->XmlDocument = std::make_shared<tinyxml2::XMLDocument>();
119-
fileToLoad->XmlDocument->Parse(reader->ReadCString().data());
120-
if (fileToLoad->XmlDocument->Error()) {
121-
SPDLOG_ERROR("Failed to parse XML file {}. Error: {}", filePath, fileToLoad->XmlDocument->ErrorStr());
117+
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+
xmlReader->Parse(binaryReader->ReadCString().data());
122+
if (xmlReader->Error()) {
123+
SPDLOG_ERROR("Failed to parse XML file {}. Error: {}", filePath, xmlReader->ErrorStr());
122124
return nullptr;
123125
}
124-
fileToLoad->InitData = ReadResourceInitDataXml(filePath, fileToLoad->XmlDocument);
126+
fileToLoad->InitData = ReadResourceInitDataXml(filePath, xmlReader);
125127
} else {
126128
// File is Binary
127129
auto fileToLoadMeta = LoadFileMeta(filePath);
@@ -155,7 +157,8 @@ std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
155157
fileToLoad->Reader = std::make_shared<BinaryReader>(stream);
156158

157159
fileToLoad->InitData = fileToLoadMeta;
158-
fileToLoad->Reader->SetEndianness(fileToLoad->InitData->ByteOrder);
160+
auto binaryReader = std::get<std::shared_ptr<BinaryReader>>(fileToLoad->Reader);
161+
binaryReader->SetEndianness(fileToLoad->InitData->ByteOrder);
159162
}
160163

161164
return fileToLoad;

src/resource/factory/ArrayFactory.cpp

+17-16
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@ std::shared_ptr<IResource> ResourceFactoryBinaryArrayV0::ReadResource(std::share
99
}
1010

1111
auto array = std::make_shared<Array>(file->InitData);
12+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
1213

13-
array->ArrayType = (ArrayResourceType)file->Reader->ReadUInt32();
14-
array->ArrayCount = file->Reader->ReadUInt32();
14+
array->ArrayType = (ArrayResourceType)reader->ReadUInt32();
15+
array->ArrayCount = reader->ReadUInt32();
1516

1617
for (uint32_t i = 0; i < array->ArrayCount; i++) {
1718
if (array->ArrayType == ArrayResourceType::Vertex) {
1819
// OTRTODO: Implement Vertex arrays as just a vertex resource.
1920
Vtx data;
20-
data.v.ob[0] = file->Reader->ReadInt16();
21-
data.v.ob[1] = file->Reader->ReadInt16();
22-
data.v.ob[2] = file->Reader->ReadInt16();
23-
data.v.flag = file->Reader->ReadUInt16();
24-
data.v.tc[0] = file->Reader->ReadInt16();
25-
data.v.tc[1] = file->Reader->ReadInt16();
26-
data.v.cn[0] = file->Reader->ReadUByte();
27-
data.v.cn[1] = file->Reader->ReadUByte();
28-
data.v.cn[2] = file->Reader->ReadUByte();
29-
data.v.cn[3] = file->Reader->ReadUByte();
21+
data.v.ob[0] = reader->ReadInt16();
22+
data.v.ob[1] = reader->ReadInt16();
23+
data.v.ob[2] = reader->ReadInt16();
24+
data.v.flag = reader->ReadUInt16();
25+
data.v.tc[0] = reader->ReadInt16();
26+
data.v.tc[1] = reader->ReadInt16();
27+
data.v.cn[0] = reader->ReadUByte();
28+
data.v.cn[1] = reader->ReadUByte();
29+
data.v.cn[2] = reader->ReadUByte();
30+
data.v.cn[3] = reader->ReadUByte();
3031
array->Vertices.push_back(data);
3132
} else {
32-
array->ArrayScalarType = (ScalarType)file->Reader->ReadUInt32();
33+
array->ArrayScalarType = (ScalarType)reader->ReadUInt32();
3334

3435
int iter = 1;
3536

3637
if (array->ArrayType == ArrayResourceType::Vector) {
37-
iter = file->Reader->ReadUInt32();
38+
iter = reader->ReadUInt32();
3839
}
3940

4041
for (int k = 0; k < iter; k++) {
4142
ScalarData data;
4243

4344
switch (array->ArrayScalarType) {
4445
case ScalarType::ZSCALAR_S16:
45-
data.s16 = file->Reader->ReadInt16();
46+
data.s16 = reader->ReadInt16();
4647
break;
4748
case ScalarType::ZSCALAR_U16:
48-
data.u16 = file->Reader->ReadUInt16();
49+
data.u16 = reader->ReadUInt16();
4950
break;
5051
default:
5152
// OTRTODO: IMPLEMENT OTHER TYPES!

src/resource/factory/BlobFactory.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ std::shared_ptr<IResource> ResourceFactoryBinaryBlobV0::ReadResource(std::shared
99
}
1010

1111
auto blob = std::make_shared<Blob>(file->InitData);
12+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
1213

13-
uint32_t dataSize = file->Reader->ReadUInt32();
14+
uint32_t dataSize = reader->ReadUInt32();
1415

1516
blob->Data.reserve(dataSize);
1617

1718
for (uint32_t i = 0; i < dataSize; i++) {
18-
blob->Data.push_back(file->Reader->ReadUByte());
19+
blob->Data.push_back(reader->ReadUByte());
1920
}
2021

2122
return blob;

src/resource/factory/DisplayListFactory.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std:
134134
}
135135

136136
auto displayList = std::make_shared<DisplayList>(file->InitData);
137+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
137138

138-
while (file->Reader->GetBaseAddress() % 8 != 0) {
139-
file->Reader->ReadInt8();
139+
while (reader->GetBaseAddress() % 8 != 0) {
140+
reader->ReadInt8();
140141
}
141142

142143
while (true) {
143144
Gfx command;
144-
command.words.w0 = file->Reader->ReadUInt32();
145-
command.words.w1 = file->Reader->ReadUInt32();
145+
command.words.w0 = reader->ReadUInt32();
146+
command.words.w1 = reader->ReadUInt32();
146147

147148
displayList->Instructions.push_back(command);
148149

@@ -151,8 +152,8 @@ std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std:
151152
// These are 128-bit commands, so read an extra 64 bits...
152153
if (opcode == G_SETTIMG_OTR_HASH || opcode == G_DL_OTR_HASH || opcode == G_VTX_OTR_HASH ||
153154
opcode == G_BRANCH_Z_OTR || opcode == G_MARKER || opcode == G_MTX_OTR) {
154-
command.words.w0 = file->Reader->ReadUInt32();
155-
command.words.w1 = file->Reader->ReadUInt32();
155+
command.words.w0 = reader->ReadUInt32();
156+
command.words.w1 = reader->ReadUInt32();
156157

157158
displayList->Instructions.push_back(command);
158159
}
@@ -171,8 +172,7 @@ std::shared_ptr<IResource> ResourceFactoryXMLDisplayListV0::ReadResource(std::sh
171172
}
172173

173174
auto dl = std::make_shared<DisplayList>(file->InitData);
174-
175-
auto child = file->XmlDocument->FirstChildElement()->FirstChildElement();
175+
auto child = std::get<std::shared_ptr<tinyxml2::XMLDocument>>(file->Reader)->FirstChildElement()->FirstChildElement();
176176

177177
while (child != nullptr) {
178178
std::string childName = child->Name();

src/resource/factory/MatrixFactory.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ std::shared_ptr<IResource> ResourceFactoryBinaryMatrixV0::ReadResource(std::shar
99
}
1010

1111
auto matrix = std::make_shared<Matrix>(file->InitData);
12+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
1213

1314
for (size_t i = 0; i < 4; i++) {
1415
for (size_t j = 0; j < 4; j++) {
15-
matrix->Matrx.m[i][j] = file->Reader->ReadInt32();
16+
matrix->Matrx.m[i][j] = reader->ReadInt32();
1617
}
1718
}
1819

src/resource/factory/TextureFactory.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ std::shared_ptr<IResource> ResourceFactoryBinaryTextureV0::ReadResource(std::sha
1010
}
1111

1212
auto texture = std::make_shared<Texture>(file->InitData);
13+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
1314

14-
texture->Type = (TextureType)file->Reader->ReadUInt32();
15-
texture->Width = file->Reader->ReadUInt32();
16-
texture->Height = file->Reader->ReadUInt32();
17-
texture->ImageDataSize = file->Reader->ReadUInt32();
15+
texture->Type = (TextureType)reader->ReadUInt32();
16+
texture->Width = reader->ReadUInt32();
17+
texture->Height = reader->ReadUInt32();
18+
texture->ImageDataSize = reader->ReadUInt32();
1819
texture->ImageData = new uint8_t[texture->ImageDataSize];
1920

20-
file->Reader->Read((char*)texture->ImageData, texture->ImageDataSize);
21+
reader->Read((char*)texture->ImageData, texture->ImageDataSize);
2122

2223
return texture;
2324
}
@@ -28,17 +29,18 @@ std::shared_ptr<IResource> ResourceFactoryBinaryTextureV1::ReadResource(std::sha
2829
}
2930

3031
auto texture = std::make_shared<Texture>(file->InitData);
31-
32-
texture->Type = (TextureType)file->Reader->ReadUInt32();
33-
texture->Width = file->Reader->ReadUInt32();
34-
texture->Height = file->Reader->ReadUInt32();
35-
texture->Flags = file->Reader->ReadUInt32();
36-
texture->HByteScale = file->Reader->ReadFloat();
37-
texture->VPixelScale = file->Reader->ReadFloat();
38-
texture->ImageDataSize = file->Reader->ReadUInt32();
32+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
33+
34+
texture->Type = (TextureType)reader->ReadUInt32();
35+
texture->Width = reader->ReadUInt32();
36+
texture->Height = reader->ReadUInt32();
37+
texture->Flags = reader->ReadUInt32();
38+
texture->HByteScale = reader->ReadFloat();
39+
texture->VPixelScale = reader->ReadFloat();
40+
texture->ImageDataSize = reader->ReadUInt32();
3941
texture->ImageData = new uint8_t[texture->ImageDataSize];
4042

41-
file->Reader->Read((char*)texture->ImageData, texture->ImageDataSize);
43+
reader->Read((char*)texture->ImageData, texture->ImageDataSize);
4244

4345
return texture;
4446
}

src/resource/factory/VertexFactory.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ std::shared_ptr<IResource> ResourceFactoryBinaryVertexV0::ReadResource(std::shar
99
}
1010

1111
auto vertex = std::make_shared<Vertex>(file->InitData);
12+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
1213

13-
uint32_t count = file->Reader->ReadUInt32();
14+
uint32_t count = reader->ReadUInt32();
1415
vertex->VertexList.reserve(count);
1516

1617
for (uint32_t i = 0; i < count; i++) {
1718
Vtx data;
18-
data.v.ob[0] = file->Reader->ReadInt16();
19-
data.v.ob[1] = file->Reader->ReadInt16();
20-
data.v.ob[2] = file->Reader->ReadInt16();
21-
data.v.flag = file->Reader->ReadUInt16();
22-
data.v.tc[0] = file->Reader->ReadInt16();
23-
data.v.tc[1] = file->Reader->ReadInt16();
24-
data.v.cn[0] = file->Reader->ReadUByte();
25-
data.v.cn[1] = file->Reader->ReadUByte();
26-
data.v.cn[2] = file->Reader->ReadUByte();
27-
data.v.cn[3] = file->Reader->ReadUByte();
19+
data.v.ob[0] = reader->ReadInt16();
20+
data.v.ob[1] = reader->ReadInt16();
21+
data.v.ob[2] = reader->ReadInt16();
22+
data.v.flag = reader->ReadUInt16();
23+
data.v.tc[0] = reader->ReadInt16();
24+
data.v.tc[1] = reader->ReadInt16();
25+
data.v.cn[0] = reader->ReadUByte();
26+
data.v.cn[1] = reader->ReadUByte();
27+
data.v.cn[2] = reader->ReadUByte();
28+
data.v.cn[3] = reader->ReadUByte();
2829
vertex->VertexList.push_back(data);
2930
}
3031

@@ -38,7 +39,7 @@ std::shared_ptr<IResource> ResourceFactoryXMLVertexV0::ReadResource(std::shared_
3839

3940
auto vertex = std::make_shared<Vertex>(file->InitData);
4041

41-
auto child = file->XmlDocument->FirstChildElement()->FirstChildElement();
42+
auto child = std::get<std::shared_ptr<tinyxml2::XMLDocument>>(file->Reader)->FirstChildElement()->FirstChildElement();
4243

4344
while (child != nullptr) {
4445
std::string childName = child->Name();

0 commit comments

Comments
 (0)