Skip to content

Commit cc38a86

Browse files
committed
checking in what i have to test something
1 parent 825bd12 commit cc38a86

12 files changed

+101
-62
lines changed

src/CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,14 @@ set(Source_Files__Resource__Archive
373373
)
374374
source_group("resource/archive" FILES ${Source_Files__Resource__Archive})
375375

376-
target_sources(libultraship PRIVATE ${Source_Files__Resource} ${Source_Files__Resource__Types} ${Source_Files__Resource__Factories} ${Source_Files__Resource__Archive})
376+
set(Source_Files__Resource__ReaderBox
377+
${CMAKE_CURRENT_SOURCE_DIR}/resource/readerbox/ReaderBox.h
378+
${CMAKE_CURRENT_SOURCE_DIR}/resource/readerbox/BinaryReaderBox.h
379+
${CMAKE_CURRENT_SOURCE_DIR}/resource/readerbox/XMLReaderBox.h
380+
)
381+
source_group("resource/readerbox" FILES ${Source_Files__Resource__ReaderBox})
382+
383+
target_sources(libultraship PRIVATE ${Source_Files__Resource} ${Source_Files__Resource__Types} ${Source_Files__Resource__Factories} ${Source_Files__Resource__Archive} ${Source_Files__Resource__ReaderBox})
377384

378385
#=================== Graphic ===================
379386

src/resource/ResourceFactory.cpp

-22
This file was deleted.

src/resource/ResourceFactory.h

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

33
#include <memory>
4-
#include "utils/binarytools/BinaryReader.h"
5-
#include "utils/binarytools/BinaryWriter.h"
6-
#include <tinyxml2.h>
4+
#include "readerbox/ReaderBox.h"
75
#include "Resource.h"
86

97
namespace LUS {
10-
class ResourceManager;
118
class ResourceFactory {
129
public:
1310
virtual std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
14-
std::shared_ptr<BinaryReader> reader) = 0;
15-
virtual std::shared_ptr<IResource> ReadResourceXML(std::shared_ptr<ResourceInitData> initData,
16-
tinyxml2::XMLElement* reader);
17-
};
18-
19-
class ResourceVersionFactory {
20-
public:
21-
virtual void ParseFileBinary(std::shared_ptr<BinaryReader> reader, std::shared_ptr<IResource> resource);
22-
virtual void ParseFileXML(tinyxml2::XMLElement* reader, std::shared_ptr<IResource> resource);
23-
virtual void WriteFileBinary(std::shared_ptr<BinaryWriter> writer, std::shared_ptr<IResource> resource);
24-
virtual void WriteFileXML(tinyxml2::XMLElement* writer, std::shared_ptr<IResource> resource);
11+
std::shared_ptr<ReaderBox> readerBox) = 0;
2512
};
2613
} // namespace LUS

src/resource/ResourceLoader.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ResourceLoader::~ResourceLoader() {
2323
}
2424

2525
void ResourceLoader::RegisterGlobalResourceFactories() {
26+
// include strings so we register types for mResourceTypes
2627
RegisterResourceFactory(static_cast<uint32_t>(ResourceType::Texture), std::make_shared<TextureFactory>());
2728
RegisterResourceFactory(static_cast<uint32_t>(ResourceType::Vertex), std::make_shared<VertexFactory>());
2829
RegisterResourceFactory(static_cast<uint32_t>(ResourceType::DisplayList), std::make_shared<DisplayListFactory>());
@@ -31,17 +32,19 @@ void ResourceLoader::RegisterGlobalResourceFactories() {
3132
RegisterResourceFactory(static_cast<uint32_t>(ResourceType::Blob), std::make_shared<BlobFactory>());
3233
}
3334

34-
bool ResourceLoader::RegisterResourceFactory(uint32_t resourceType, std::shared_ptr<ResourceFactory> factory) {
35-
if (mFactories.contains(resourceType)) {
35+
bool ResourceLoader::RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format, std::string typeName, uint32_t type, uint32_t version) {
36+
if (mResourceTypes.contains(typeName)) {
3637
return false;
3738
}
39+
mResourceTypes[typeName] = type;
3840

39-
mFactories[resourceType] = factory;
40-
return true;
41-
}
41+
ResourceFactoryKey key = {resourceFormat: format, resourceType: type, resourceVersion: version};
42+
if (mFactories.contains(key)) {
43+
return false;
44+
}
45+
mFactories[key] = factory;
4246

43-
uint32_t ResourceLoader::GetVersionFromString(const std::string& version) {
44-
return mFactoriesTypes[version];
47+
return true;
4548
}
4649

4750
std::shared_ptr<IResource> ResourceLoader::LoadResource(std::shared_ptr<File> fileToLoad) {
@@ -57,6 +60,10 @@ std::shared_ptr<IResource> ResourceLoader::LoadResource(std::shared_ptr<File> fi
5760
return result;
5861
}
5962

63+
// call method to get factory based on factorykey (generate using params)
64+
// make a method that takes in a string instead of an int for the type too
65+
// make those protected
66+
6067
auto factory = mFactories[fileToLoad->InitData->Type];
6168
if (factory == nullptr) {
6269
SPDLOG_ERROR("Failed to load resource: Factory does not exist ({} - {})", fileToLoad->InitData->Type,

src/resource/ResourceLoader.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@
99
namespace LUS {
1010
struct File;
1111

12+
struct ResourceFactoryKey {
13+
uint32_t resourceFormat;
14+
uint32_t resourceType;
15+
uint32_t resourceVersion;
16+
};
17+
1218
class ResourceLoader {
1319
public:
1420
ResourceLoader();
1521
~ResourceLoader();
1622

1723
std::shared_ptr<IResource> LoadResource(std::shared_ptr<File> fileToLoad);
18-
bool RegisterResourceFactory(uint32_t resourceType, std::shared_ptr<ResourceFactory> factory);
19-
uint32_t GetVersionFromString(const std::string& version);
24+
bool RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format, std::string typeName, uint32_t type, uint32_t version);
2025

2126
protected:
2227
void RegisterGlobalResourceFactories();
2328

2429
private:
25-
std::unordered_map<uint32_t, std::shared_ptr<ResourceFactory>> mFactories;
30+
uint32_t GetResourceType(const std::string& type);
31+
std::unordered_map<std::string, uint32_t> mResourceTypes;
32+
std::unordered_map<ResourceFactoryKey, std::shared_ptr<ResourceFactory>> mFactories;
2633
};
2734
} // namespace LUS

src/resource/ResourceType.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum class ResourceType {
3636
SOH_SceneCommand = 0x4F52434D, // ORCM
3737
};
3838

39-
inline std::unordered_map<std::string, uint32_t> mFactoriesTypes{
39+
inline std::unordered_map<std::string, uint32_t> mResourceTypes {
4040
{ "None", static_cast<uint32_t>(ResourceType::None) },
4141
{ "Archive", static_cast<uint32_t>(ResourceType::Archive) },
4242
{ "DisplayList", static_cast<uint32_t>(ResourceType::DisplayList) },

src/resource/factory/DisplayListFactory.h

+5
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ class DisplayListFactoryV0 : public ResourceVersionFactory {
1919

2020
uint32_t GetCombineLERPValue(std::string valStr);
2121
};
22+
23+
// XMLDisplayListV0
24+
// XMLDisplayListV0Factory
25+
// ResourceFactoryXMLDisplayListV0
26+
2227
} // namespace LUS

src/resource/factory/TextureFactory.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
#include "resource/factory/TextureFactory.h"
22
#include "resource/type/Texture.h"
3+
#include "resource/readerbox/BinaryReaderBox.h"
34
#include "spdlog/spdlog.h"
45

56
namespace LUS {
67

7-
std::shared_ptr<IResource> TextureFactory::ReadResource(std::shared_ptr<ResourceInitData> initData,
8-
std::shared_ptr<BinaryReader> reader) {
9-
auto resource = std::make_shared<Texture>(initData);
8+
std::shared_ptr<IResource> ResourceFactoryBinaryTextureV0::ReadResource(std::shared_ptr<ResourceInitData> initData,
9+
std::shared_ptr<ReaderBox> readerBox) {
10+
auto binaryReaderBox = std::dynamic_pointer_cast<BinaryReaderBox>(readerBox);
11+
if (binaryReaderBox == nullptr) {
12+
SPDLOG_ERROR("ReaderBox must be a BinaryReaderBox.");
13+
return nullptr;
14+
}
15+
16+
auto reader = binaryReaderBox->GetReader();
17+
if (reader == nullptr) {
18+
SPDLOG_ERROR("null reader in box.");
19+
return nullptr;
20+
}
21+
22+
auto texture = std::make_shared<Texture>(initData);
23+
24+
25+
1026
std::shared_ptr<ResourceVersionFactory> factory = nullptr;
1127

1228
switch (resource->GetInitData()->ResourceVersion) {
@@ -29,7 +45,7 @@ std::shared_ptr<IResource> TextureFactory::ReadResource(std::shared_ptr<Resource
2945
}
3046

3147
void TextureFactoryV0::ParseFileBinary(std::shared_ptr<BinaryReader> reader, std::shared_ptr<IResource> resource) {
32-
std::shared_ptr<Texture> texture = std::static_pointer_cast<Texture>(resource);
48+
// std::shared_ptr<Texture> texture = std::static_pointer_cast<Texture>(resource);
3349
ResourceVersionFactory::ParseFileBinary(reader, texture);
3450

3551
texture->Type = (TextureType)reader->ReadUInt32();

src/resource/factory/TextureFactory.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
#include "resource/Resource.h"
44
#include "resource/ResourceFactory.h"
5+
#include "resource/readerbox/BinaryReaderBox.h"
56

67
namespace LUS {
7-
class TextureFactory : public ResourceFactory {
8+
// ResourceFactoryXMLDisplayListV0
9+
class ResourceFactoryBinaryTextureV0 : public ResourceFactory {
810
public:
911
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
10-
std::shared_ptr<BinaryReader> reader) override;
12+
std::shared_ptr<ReaderBox> readerBox) override;
1113
};
1214

13-
class TextureFactoryV0 : public ResourceVersionFactory {
15+
class ResourceFactoryBinaryTextureV1 : public ResourceFactory {
1416
public:
15-
void ParseFileBinary(std::shared_ptr<BinaryReader> reader, std::shared_ptr<IResource> resource) override;
16-
};
17-
18-
class TextureFactoryV1 : public ResourceVersionFactory {
19-
public:
20-
void ParseFileBinary(std::shared_ptr<BinaryReader> reader, std::shared_ptr<IResource> resource) override;
17+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
18+
std::shared_ptr<ReaderBox> readerBox) override;
2119
};
2220
} // namespace LUS
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "ReaderBox.h"
2+
#include "utils/binarytools/BinaryReader.h"
3+
4+
namespace LUS {
5+
class BinaryReaderBox : public ReaderBox {
6+
public:
7+
BinaryReaderBox(std::shared_ptr<BinaryReader> reader) {
8+
mReader = reader;
9+
};
10+
std::shared_ptr<BinaryReader> GetReader() {
11+
return mReader;
12+
};
13+
~BinaryReaderBox() {
14+
mReader = nullptr;
15+
};
16+
17+
private:
18+
std::shared_ptr<BinaryReader> mReader;
19+
};
20+
} // namespace LUS

src/resource/readerbox/ReaderBox.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace LUS {
2+
class ReaderBox {};
3+
} // namespace LUS

src/resource/readerbox/XMLReaderBox.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "ReaderBox.h"
2+
3+
namespace LUS {
4+
class XMLReaderBox : public ReaderBox {
5+
public:
6+
// std::shared_ptr<XMLReader> GetXMLReader();
7+
8+
private:
9+
10+
};
11+
} // namespace LUS

0 commit comments

Comments
 (0)