Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource refactory #437

Merged
merged 25 commits into from
Feb 14, 2024
Merged

resource refactory #437

merged 25 commits into from
Feb 14, 2024

Conversation

briaguya-ai
Copy link
Collaborator

@briaguya-ai briaguya-ai commented Feb 11, 2024

SoH: HarbourMasters/Shipwright#3926
Exporter: HarbourMasters/OTRExporter#15

changes:

RegisterResourceFactory now takes in the factory, file format (currently binary or xml), resource type (name as string and and int for enum), and a version

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

ResourceFactory has been simplified

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;
};

example

BlobFactory demonstrates this quite well

before

class BlobFactory : public ResourceFactory {
public:
std::shared_ptr<IResource> ReadResource(std::shared_ptr<ResourceInitData> initData,
std::shared_ptr<BinaryReader> reader) override;
};
class BlobFactoryV0 : public ResourceVersionFactory {
public:
void ParseFileBinary(std::shared_ptr<BinaryReader> reader, std::shared_ptr<IResource> resource) override;
};

std::shared_ptr<IResource> BlobFactory::ReadResource(std::shared_ptr<ResourceInitData> initData,
std::shared_ptr<BinaryReader> reader) {
auto resource = std::make_shared<Blob>(initData);
std::shared_ptr<ResourceVersionFactory> factory = nullptr;
switch (resource->GetInitData()->ResourceVersion) {
case 0:
factory = std::make_shared<BlobFactoryV0>();
break;
}
if (factory == nullptr) {
SPDLOG_ERROR("Failed to load Blob with version {}", resource->GetInitData()->ResourceVersion);
return nullptr;
}
factory->ParseFileBinary(reader, resource);
return resource;
}
void BlobFactoryV0::ParseFileBinary(std::shared_ptr<BinaryReader> reader, std::shared_ptr<IResource> resource) {
std::shared_ptr<Blob> blob = std::static_pointer_cast<Blob>(resource);
ResourceVersionFactory::ParseFileBinary(reader, blob);
uint32_t dataSize = reader->ReadUInt32();
blob->Data.reserve(dataSize);
for (uint32_t i = 0; i < dataSize; i++) {
blob->Data.push_back(reader->ReadUByte());
}
}

after

class ResourceFactoryBinaryBlobV0 : public ResourceFactoryBinary {
public:
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
};

std::shared_ptr<IResource> ResourceFactoryBinaryBlobV0::ReadResource(std::shared_ptr<File> file) {
if (!FileHasValidFormatAndReader(file)) {
return nullptr;
}
auto blob = std::make_shared<Blob>(file->InitData);
uint32_t dataSize = file->Reader->ReadUInt32();
blob->Data.reserve(dataSize);
for (uint32_t i = 0; i < dataSize; i++) {
blob->Data.push_back(file->Reader->ReadUByte());
}
return blob;
}

@briaguya-ai briaguya-ai mentioned this pull request Feb 11, 2024
@Kenix3 Kenix3 merged commit a516b66 into Kenix3:main Feb 14, 2024
4 checks passed
@briaguya-ai briaguya-ai deleted the resource-refactory branch April 29, 2024 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants