Skip to content

Commit

Permalink
clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Feb 11, 2024
1 parent be9f894 commit 4621d4c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/resource/ResourceFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace LUS {
class ResourceFactory {
public:
virtual std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) = 0;

protected:
virtual bool FileHasValidFormatAndReader(std::shared_ptr<File> file) = 0;
};
Expand Down
3 changes: 1 addition & 2 deletions src/resource/ResourceFactoryBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ bool ResourceFactoryBinary::FileHasValidFormatAndReader(std::shared_ptr<File> fi
}

if (file->Reader == nullptr) {
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type,
file->InitData->Path);
SPDLOG_ERROR("Failed to load resource: File has Reader ({} - {})", file->InitData->Type, file->InitData->Path);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/resource/ResourceFactoryXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bool ResourceFactoryXML::FileHasValidFormatAndReader(std::shared_ptr<File> file)

if (file->XmlDocument == nullptr) {
SPDLOG_ERROR("Failed to load resource: File has no XML document ({} - {})", file->InitData->Type,
file->InitData->Path);
file->InitData->Path);
return false;
}

Expand Down
53 changes: 33 additions & 20 deletions src/resource/ResourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,41 @@ ResourceLoader::~ResourceLoader() {
}

void ResourceLoader::RegisterGlobalResourceFactories() {
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY, "Texture", static_cast<uint32_t>(ResourceType::Texture), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY, "Texture", static_cast<uint32_t>(ResourceType::Texture), 1);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryVertexV0>(), RESOURCE_FORMAT_BINARY, "Vertex", static_cast<uint32_t>(ResourceType::Vertex), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryXMLVertexV0>(), RESOURCE_FORMAT_XML, "Vertex", static_cast<uint32_t>(ResourceType::Vertex), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryDisplayListV0>(), RESOURCE_FORMAT_BINARY, "DisplayList", static_cast<uint32_t>(ResourceType::DisplayList), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryXMLDisplayListV0>(), RESOURCE_FORMAT_XML, "DisplayList", static_cast<uint32_t>(ResourceType::DisplayList), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryMatrixV0>(), RESOURCE_FORMAT_BINARY, "Matrix", static_cast<uint32_t>(ResourceType::Matrix), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryArrayV0>(), RESOURCE_FORMAT_BINARY, "Array", static_cast<uint32_t>(ResourceType::Array), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryBlobV0>(), RESOURCE_FORMAT_BINARY, "Blob", static_cast<uint32_t>(ResourceType::Blob), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY, "Texture",
static_cast<uint32_t>(ResourceType::Texture), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY, "Texture",
static_cast<uint32_t>(ResourceType::Texture), 1);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryVertexV0>(), RESOURCE_FORMAT_BINARY, "Vertex",
static_cast<uint32_t>(ResourceType::Vertex), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryXMLVertexV0>(), RESOURCE_FORMAT_XML, "Vertex",
static_cast<uint32_t>(ResourceType::Vertex), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryDisplayListV0>(), RESOURCE_FORMAT_BINARY,
"DisplayList", static_cast<uint32_t>(ResourceType::DisplayList), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryXMLDisplayListV0>(), RESOURCE_FORMAT_XML, "DisplayList",
static_cast<uint32_t>(ResourceType::DisplayList), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryMatrixV0>(), RESOURCE_FORMAT_BINARY, "Matrix",
static_cast<uint32_t>(ResourceType::Matrix), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryArrayV0>(), RESOURCE_FORMAT_BINARY, "Array",
static_cast<uint32_t>(ResourceType::Array), 0);
RegisterResourceFactory(std::make_shared<ResourceFactoryBinaryBlobV0>(), RESOURCE_FORMAT_BINARY, "Blob",
static_cast<uint32_t>(ResourceType::Blob), 0);
}

bool ResourceLoader::RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format, std::string typeName, uint32_t type, uint32_t version) {
bool ResourceLoader::RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format,
std::string typeName, uint32_t type, uint32_t version) {
if (mResourceTypes.contains(typeName)) {
if(mResourceTypes[typeName] != type) {
if (mResourceTypes[typeName] != type) {
SPDLOG_ERROR("Failed to register resource factory: conflicting types for name {}", typeName);
return false;
}
} else {
mResourceTypes[typeName] = type;
}

ResourceFactoryKey key = {resourceFormat: format, resourceType: type, resourceVersion: version};
ResourceFactoryKey key = { resourceFormat : format, resourceType : type, resourceVersion : version };
if (mFactories.contains(key)) {
SPDLOG_ERROR("Failed to register resource factory: factory with key {}{}{} already exists", format, type, version);
SPDLOG_ERROR("Failed to register resource factory: factory with key {}{}{} already exists", format, type,
version);
return false;
}
mFactories[key] = factory;
Expand All @@ -55,7 +66,7 @@ bool ResourceLoader::RegisterResourceFactory(std::shared_ptr<ResourceFactory> fa
}

std::shared_ptr<ResourceFactory> ResourceLoader::GetFactory(uint32_t format, uint32_t type, uint32_t version) {
ResourceFactoryKey key = {resourceFormat: format, resourceType: type, resourceVersion: version};
ResourceFactoryKey key = { resourceFormat : format, resourceType : type, resourceVersion : version };
if (!mFactories.contains(key)) {
SPDLOG_ERROR("Could not find resource factory with key {}{}{}", format, type, version);
return nullptr;
Expand Down Expand Up @@ -84,14 +95,16 @@ std::shared_ptr<IResource> ResourceLoader::LoadResource(std::shared_ptr<File> fi
return nullptr;
}

auto factory = GetFactory(fileToLoad->InitData->Format, fileToLoad->InitData->Type, fileToLoad->InitData->ResourceVersion);
auto factory =
GetFactory(fileToLoad->InitData->Format, fileToLoad->InitData->Type, fileToLoad->InitData->ResourceVersion);
if (factory == nullptr) {
SPDLOG_ERROR("Failed to load resource: Factory does not exist.\n" \
"Path: {}\n" \
"Type: {}\n" \
"Format: {}\n" \
SPDLOG_ERROR("Failed to load resource: Factory does not exist.\n"
"Path: {}\n"
"Type: {}\n"
"Format: {}\n"
"Version: {}",
fileToLoad->InitData->Path, fileToLoad->InitData->Type, fileToLoad->InitData->Format, fileToLoad->InitData->ResourceVersion);
fileToLoad->InitData->Path, fileToLoad->InitData->Type, fileToLoad->InitData->Format,
fileToLoad->InitData->ResourceVersion);
return nullptr;
}

Expand Down
19 changes: 11 additions & 8 deletions src/resource/ResourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ namespace LUS {
struct File;

struct ResourceFactoryKey {
uint32_t resourceFormat;
uint32_t resourceType;
uint32_t resourceVersion;
uint32_t resourceFormat;
uint32_t resourceType;
uint32_t resourceVersion;

bool operator==(const ResourceFactoryKey& o) const {
return (resourceFormat == o.resourceFormat) && (resourceType == o.resourceType) && (resourceVersion == o.resourceVersion);
}
bool operator==(const ResourceFactoryKey& o) const {
return (resourceFormat == o.resourceFormat) && (resourceType == o.resourceType) &&
(resourceVersion == o.resourceVersion);
}
};

struct ResourceFactoryKeyHash {
std::size_t operator()(const ResourceFactoryKey& key) const {
return std::hash<int>()(key.resourceFormat) ^ std::hash<int>()(key.resourceType) ^ std::hash<int>()(key.resourceVersion);
return std::hash<int>()(key.resourceFormat) ^ std::hash<int>()(key.resourceType) ^
std::hash<int>()(key.resourceVersion);
}
};

Expand All @@ -31,7 +33,8 @@ class ResourceLoader {
~ResourceLoader();

std::shared_ptr<IResource> LoadResource(std::shared_ptr<File> fileToLoad);
bool RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format, std::string typeName, uint32_t type, uint32_t version);
bool RegisterResourceFactory(std::shared_ptr<ResourceFactory> factory, uint32_t format, std::string typeName,
uint32_t type, uint32_t version);

uint32_t GetResourceType(const std::string& type);

Expand Down
3 changes: 2 additions & 1 deletion src/resource/archive/Archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ std::shared_ptr<ResourceInitData> Archive::ReadResourceInitDataXml(const std::st
resourceInitData->Format = RESOURCE_FORMAT_XML;

auto root = document->FirstChildElement();
resourceInitData->Type = Context::GetInstance()->GetResourceManager()->GetResourceLoader()->GetResourceType(root->Name());
resourceInitData->Type =
Context::GetInstance()->GetResourceManager()->GetResourceLoader()->GetResourceType(root->Name());
resourceInitData->ResourceVersion = root->IntAttribute("Version");

return resourceInitData;
Expand Down
2 changes: 1 addition & 1 deletion src/resource/factory/DisplayListFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class ResourceFactoryBinaryDisplayListV0 : public ResourceFactoryDisplayList, pu

class ResourceFactoryXMLDisplayListV0 : public ResourceFactoryDisplayList, public ResourceFactoryXML {
public:
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
};
} // namespace LUS
6 changes: 3 additions & 3 deletions src/resource/factory/TextureFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ std::shared_ptr<IResource> ResourceFactoryBinaryTextureV0::ReadResource(std::sha
}

std::shared_ptr<IResource> ResourceFactoryBinaryTextureV1::ReadResource(std::shared_ptr<File> file) {
if (!FileHasValidFormatAndReader(file)) {
return nullptr;
}
if (!FileHasValidFormatAndReader(file)) {
return nullptr;
}

auto texture = std::make_shared<Texture>(file->InitData);

Expand Down
2 changes: 1 addition & 1 deletion src/resource/factory/TextureFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class ResourceFactoryBinaryTextureV0 : public ResourceFactoryBinary {

class ResourceFactoryBinaryTextureV1 : public ResourceFactoryBinary {
public:
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
};
} // namespace LUS
2 changes: 1 addition & 1 deletion src/resource/factory/VertexFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class ResourceFactoryBinaryVertexV0 : public ResourceFactoryBinary {

class ResourceFactoryXMLVertexV0 : public ResourceFactoryXML {
public:
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
};
} // namespace LUS

0 comments on commit 4621d4c

Please sign in to comment.