Skip to content

Commit 5c11453

Browse files
committed
start swapping everything over
1 parent e118c40 commit 5c11453

17 files changed

+117
-235
lines changed

src/resource/ResourceFactory.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#pragma once
22

33
#include <memory>
4-
#include "readerbox/ReaderBox.h"
4+
#include "File.h"
55
#include "Resource.h"
66

77
namespace LUS {
88
class ResourceFactory {
99
public:
10-
virtual std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
11-
std::shared_ptr<ReaderBox> readerBox) = 0;
10+
virtual std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) = 0;
1211
};
1312
} // namespace LUS

src/resource/ResourceLoader.cpp

+1-42
Original file line numberDiff line numberDiff line change
@@ -87,53 +87,12 @@ std::shared_ptr<IResource> ResourceLoader::LoadResource(std::shared_ptr<File> fi
8787
return result;
8888
}
8989

90-
// call method to get factory based on factorykey (generate using params)
91-
// make a method that takes in a string instead of an int for the type too
92-
// make those protected
93-
94-
95-
9690
auto factory = GetFactory(fileToLoad->InitData->Format, fileToLoad->InitData->Type, fileToLoad->InitData->ResourceVersion);
9791
if (factory == nullptr) {
9892
SPDLOG_ERROR("Failed to load resource: Factory does not exist ({} - {})", fileToLoad->InitData->Type,
9993
fileToLoad->InitData->Path);
10094
}
10195

102-
// todo: figure out a better way to handle this
103-
// probably just have it so ReadReasource takes in the pointer to fileToLoad directly
104-
switch (fileToLoad->InitData->Format) {
105-
case RESOURCE_FORMAT_BINARY:
106-
factory->ReadResource(fileToLoad->InitData, std::make_shared<BinaryReaderBox>())
107-
case RESOURCE_FORMAT_XML:
108-
109-
default:
110-
SPDLOG_ERROR("invalid resource format {}", fileToLoad->InitData->Format);
111-
return nullptr;
112-
}
113-
114-
if (fileToLoad->InitData->IsXml) {
115-
if (fileToLoad->XmlDocument == nullptr) {
116-
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", fileToLoad->InitData->Type,
117-
fileToLoad->InitData->Path);
118-
return result;
119-
}
120-
121-
result = factory->ReadResourceXML(fileToLoad->InitData, fileToLoad->XmlDocument->FirstChildElement());
122-
} else {
123-
if (fileToLoad->Reader == nullptr) {
124-
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", fileToLoad->InitData->Type,
125-
fileToLoad->InitData->Path);
126-
return result;
127-
}
128-
129-
result = factory->ReadResource(fileToLoad->InitData, fileToLoad->Reader);
130-
}
131-
132-
if (result == nullptr) {
133-
SPDLOG_ERROR("Failed to load resource of type {} \"{}\"", fileToLoad->InitData->Type,
134-
fileToLoad->InitData->Path);
135-
}
136-
137-
return result;
96+
return factory->ReadResource(fileToLoad);
13897
}
13998
} // namespace LUS

src/resource/factory/ArrayFactory.cpp

+24-27
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,60 @@
11
#include "resource/factory/ArrayFactory.h"
22
#include "resource/type/Array.h"
3-
#include "resource/readerbox/BinaryReaderBox.h"
43
#include "spdlog/spdlog.h"
54

65
namespace LUS {
7-
std::shared_ptr<IResource> ResourceFactoryBinaryArrayV0::ReadResource(std::shared_ptr<ResourceInitData> initData,
8-
std::shared_ptr<ReaderBox> readerBox) {
9-
auto binaryReaderBox = std::dynamic_pointer_cast<BinaryReaderBox>(readerBox);
10-
if (binaryReaderBox == nullptr) {
11-
SPDLOG_ERROR("ReaderBox must be a BinaryReaderBox.");
6+
std::shared_ptr<IResource> ResourceFactoryBinaryArrayV0::ReadResource(std::shared_ptr<File> file) {
7+
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
8+
SPDLOG_ERROR("resource file format does not match factory format.");
129
return nullptr;
1310
}
1411

15-
auto reader = binaryReaderBox->GetReader();
16-
if (reader == nullptr) {
17-
SPDLOG_ERROR("null reader in box.");
12+
if (file->Reader == nullptr) {
13+
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
14+
file->InitData->Path);
1815
return nullptr;
1916
}
2017

21-
auto array = std::make_shared<Array>(initData);
18+
auto array = std::make_shared<Array>(file->InitData);
2219

23-
uint32_t dataSize = reader->ReadUInt32();
20+
uint32_t dataSize = file->Reader->ReadUInt32();
2421

25-
array->ArrayType = (ArrayResourceType)reader->ReadUInt32();
26-
array->ArrayCount = reader->ReadUInt32();
22+
array->ArrayType = (ArrayResourceType)file->Reader->ReadUInt32();
23+
array->ArrayCount = file->Reader->ReadUInt32();
2724

2825
for (uint32_t i = 0; i < array->ArrayCount; i++) {
2926
if (array->ArrayType == ArrayResourceType::Vertex) {
3027
// OTRTODO: Implement Vertex arrays as just a vertex resource.
3128
Vtx data;
32-
data.v.ob[0] = reader->ReadInt16();
33-
data.v.ob[1] = reader->ReadInt16();
34-
data.v.ob[2] = reader->ReadInt16();
35-
data.v.flag = reader->ReadUInt16();
36-
data.v.tc[0] = reader->ReadInt16();
37-
data.v.tc[1] = reader->ReadInt16();
38-
data.v.cn[0] = reader->ReadUByte();
39-
data.v.cn[1] = reader->ReadUByte();
40-
data.v.cn[2] = reader->ReadUByte();
41-
data.v.cn[3] = reader->ReadUByte();
29+
data.v.ob[0] = file->Reader->ReadInt16();
30+
data.v.ob[1] = file->Reader->ReadInt16();
31+
data.v.ob[2] = file->Reader->ReadInt16();
32+
data.v.flag = file->Reader->ReadUInt16();
33+
data.v.tc[0] = file->Reader->ReadInt16();
34+
data.v.tc[1] = file->Reader->ReadInt16();
35+
data.v.cn[0] = file->Reader->ReadUByte();
36+
data.v.cn[1] = file->Reader->ReadUByte();
37+
data.v.cn[2] = file->Reader->ReadUByte();
38+
data.v.cn[3] = file->Reader->ReadUByte();
4239
array->Vertices.push_back(data);
4340
} else {
44-
array->ArrayScalarType = (ScalarType)reader->ReadUInt32();
41+
array->ArrayScalarType = (ScalarType)file->Reader->ReadUInt32();
4542

4643
int iter = 1;
4744

4845
if (array->ArrayType == ArrayResourceType::Vector) {
49-
iter = reader->ReadUInt32();
46+
iter = file->Reader->ReadUInt32();
5047
}
5148

5249
for (int k = 0; k < iter; k++) {
5350
ScalarData data;
5451

5552
switch (array->ArrayScalarType) {
5653
case ScalarType::ZSCALAR_S16:
57-
data.s16 = reader->ReadInt16();
54+
data.s16 = file->Reader->ReadInt16();
5855
break;
5956
case ScalarType::ZSCALAR_U16:
60-
data.u16 = reader->ReadUInt16();
57+
data.u16 = file->Reader->ReadUInt16();
6158
break;
6259
default:
6360
// OTRTODO: IMPLEMENT OTHER TYPES!

src/resource/factory/ArrayFactory.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace LUS {
77
class ResourceFactoryBinaryArrayV0 : public ResourceFactory {
88
public:
9-
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
10-
std::shared_ptr<ReaderBox> readerBox) override;
9+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1110
};
1211
} // namespace LUS

src/resource/factory/BlobFactory.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@
44
#include "spdlog/spdlog.h"
55

66
namespace LUS {
7-
std::shared_ptr<IResource> ResourceFactoryBinaryBlobV0::ReadResource(std::shared_ptr<ResourceInitData> initData,
8-
std::shared_ptr<ReaderBox> readerBox) {
9-
auto binaryReaderBox = std::dynamic_pointer_cast<BinaryReaderBox>(readerBox);
10-
if (binaryReaderBox == nullptr) {
11-
SPDLOG_ERROR("ReaderBox must be a BinaryReaderBox.");
7+
std::shared_ptr<IResource> ResourceFactoryBinaryBlobV0::ReadResource(std::shared_ptr<File> file) {
8+
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
9+
SPDLOG_ERROR("resource file format does not match factory format.");
1210
return nullptr;
1311
}
1412

15-
auto reader = binaryReaderBox->GetReader();
16-
if (reader == nullptr) {
17-
SPDLOG_ERROR("null reader in box.");
13+
if (file->Reader == nullptr) {
14+
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
15+
file->InitData->Path);
1816
return nullptr;
1917
}
2018

21-
auto blob = std::make_shared<Blob>(initData);
19+
auto blob = std::make_shared<Blob>(file->InitData);
2220

23-
uint32_t dataSize = reader->ReadUInt32();
21+
uint32_t dataSize = file->Reader->ReadUInt32();
2422

2523
blob->Data.reserve(dataSize);
2624

2725
for (uint32_t i = 0; i < dataSize; i++) {
28-
blob->Data.push_back(reader->ReadUByte());
26+
blob->Data.push_back(file->Reader->ReadUByte());
2927
}
3028

3129
return blob;

src/resource/factory/BlobFactory.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace LUS {
77
class ResourceFactoryBinaryBlobV0 : public ResourceFactory {
88
public:
9-
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
10-
std::shared_ptr<ReaderBox> readerBox) override;
9+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1110
};
1211
}; // namespace LUS

src/resource/factory/DisplayListFactory.cpp

+23-26
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,28 @@ uint32_t ResourceFactoryDisplayList::GetCombineLERPValue(std::string valStr) {
130130
return G_CCMUX_1;
131131
}
132132

133-
std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std::shared_ptr<ResourceInitData> initData,
134-
std::shared_ptr<ReaderBox> readerBox) {
135-
auto binaryReaderBox = std::dynamic_pointer_cast<BinaryReaderBox>(readerBox);
136-
if (binaryReaderBox == nullptr) {
137-
SPDLOG_ERROR("ReaderBox must be a BinaryReaderBox.");
133+
std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std::shared_ptr<File> file) {
134+
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
135+
SPDLOG_ERROR("resource file format does not match factory format.");
138136
return nullptr;
139137
}
140138

141-
auto reader = binaryReaderBox->GetReader();
142-
if (reader == nullptr) {
143-
SPDLOG_ERROR("null reader in box.");
139+
if (file->Reader == nullptr) {
140+
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
141+
file->InitData->Path);
144142
return nullptr;
145143
}
146144

147-
auto displayList = std::make_shared<DisplayList>(initData);
145+
auto displayList = std::make_shared<DisplayList>(file->InitData);
148146

149-
while (reader->GetBaseAddress() % 8 != 0) {
150-
reader->ReadInt8();
147+
while (file->Reader->GetBaseAddress() % 8 != 0) {
148+
file->Reader->ReadInt8();
151149
}
152150

153151
while (true) {
154152
Gfx command;
155-
command.words.w0 = reader->ReadUInt32();
156-
command.words.w1 = reader->ReadUInt32();
153+
command.words.w0 = file->Reader->ReadUInt32();
154+
command.words.w1 = file->Reader->ReadUInt32();
157155

158156
displayList->Instructions.push_back(command);
159157

@@ -162,8 +160,8 @@ std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std:
162160
// These are 128-bit commands, so read an extra 64 bits...
163161
if (opcode == G_SETTIMG_OTR_HASH || opcode == G_DL_OTR_HASH || opcode == G_VTX_OTR_HASH ||
164162
opcode == G_BRANCH_Z_OTR || opcode == G_MARKER || opcode == G_MTX_OTR) {
165-
command.words.w0 = reader->ReadUInt32();
166-
command.words.w1 = reader->ReadUInt32();
163+
command.words.w0 = file->Reader->ReadUInt32();
164+
command.words.w1 = file->Reader->ReadUInt32();
167165

168166
displayList->Instructions.push_back(command);
169167
}
@@ -176,23 +174,22 @@ std::shared_ptr<IResource> ResourceFactoryBinaryDisplayListV0::ReadResource(std:
176174
return displayList;
177175
}
178176

179-
std::shared_ptr<IResource> ResourceFactoryXMLDisplayListV0::ReadResource(std::shared_ptr<ResourceInitData> initData,
180-
std::shared_ptr<ReaderBox> readerBox) {
181-
auto xmlReaderBox = std::dynamic_pointer_cast<XMLReaderBox>(readerBox);
182-
if (xmlReaderBox == nullptr) {
183-
SPDLOG_ERROR("ReaderBox must be an XMLReaderBox.");
177+
std::shared_ptr<IResource> ResourceFactoryXMLDisplayListV0::ReadResource(std::shared_ptr<File> file) {
178+
if (file->InitData->Format != RESOURCE_FORMAT_XML) {
179+
SPDLOG_ERROR("resource file format does not match factory format.");
184180
return nullptr;
185181
}
186182

187-
auto reader = xmlReaderBox->GetReader();
188-
if (reader == nullptr) {
189-
SPDLOG_ERROR("null reader in box.");
190-
return nullptr;
183+
if (file->XmlDocument == nullptr) {
184+
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", file->InitData->Type,
185+
file->InitData->Path);
186+
return result;
191187
}
192188

193-
auto dl = std::make_shared<DisplayList>(initData);
194189

195-
auto child = reader->FirstChildElement()->FirstChildElement();
190+
auto dl = std::make_shared<DisplayList>(file->InitData);
191+
192+
auto child = file->XmlDocument->FirstChildElement()->FirstChildElement();
196193

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

src/resource/factory/DisplayListFactory.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ class ResourceFactoryDisplayList : public ResourceFactory {
1111

1212
class ResourceFactoryBinaryDisplayListV0 : public ResourceFactoryDisplayList {
1313
public:
14-
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
15-
std::shared_ptr<ReaderBox> readerBox) override;
14+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1615
};
1716

1817
class ResourceFactoryXMLDisplayListV0 : public ResourceFactoryDisplayList {
1918
public:
20-
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
21-
std::shared_ptr<ReaderBox> readerBox) override;
19+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
2220
};
2321
} // namespace LUS

src/resource/factory/MatrixFactory.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
#include "spdlog/spdlog.h"
55

66
namespace LUS {
7-
std::shared_ptr<IResource> ResourceFactoryBinaryMatrixV0::ReadResource(std::shared_ptr<ResourceInitData> initData,
8-
std::shared_ptr<ReaderBox> readerBox) {
9-
auto binaryReaderBox = std::dynamic_pointer_cast<BinaryReaderBox>(readerBox);
10-
if (binaryReaderBox == nullptr) {
11-
SPDLOG_ERROR("ReaderBox must be a BinaryReaderBox.");
7+
std::shared_ptr<IResource> ResourceFactoryBinaryMatrixV0::ReadResource(std::shared_ptr<File> file) {
8+
if (file->InitData->Format != RESOURCE_FORMAT_BINARY) {
9+
SPDLOG_ERROR("resource file format does not match factory format.");
1210
return nullptr;
1311
}
1412

15-
auto reader = binaryReaderBox->GetReader();
16-
if (reader == nullptr) {
17-
SPDLOG_ERROR("null reader in box.");
13+
if (file->Reader == nullptr) {
14+
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
15+
file->InitData->Path);
1816
return nullptr;
1917
}
2018

21-
auto matrix = std::make_shared<Matrix>(initData);
19+
auto matrix = std::make_shared<Matrix>(file->InitData);
2220

2321
for (size_t i = 0; i < 4; i++) {
2422
for (size_t j = 0; j < 4; j++) {
25-
matrix->Matrx.m[i][j] = reader->ReadInt32();
23+
matrix->Matrx.m[i][j] = file->Reader->ReadInt32();
2624
}
2725
}
2826

src/resource/factory/MatrixFactory.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace LUS {
77
class ResourceFactoryBinaryMatrixV0 : public ResourceFactory {
88
public:
9-
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
10-
std::shared_ptr<ReaderBox> readerBox) override;
9+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
1110
};
1211
} // namespace LUS

0 commit comments

Comments
 (0)