code review refactoring

This commit is contained in:
ohyzha
2024-12-21 17:11:29 +02:00
parent 9d6756bbad
commit d3750f2b8a
10 changed files with 88 additions and 117 deletions

View File

@@ -7,50 +7,21 @@
#include "ExeAppendedZipResourceLoader.hpp"
#include "Base/Logger.hpp"
#include "IO/Archive/ArchiveReader.hpp"
#include "Base/Utils.hpp"
#include <filesystem>
#include <fstream>
namespace OpenVulkano
{
std::string ExeAppendedZipResourceLoader::GetResourcePath(const std::string& resourceName)
Array<char> ExeAppendedZipResourceLoader::GetResource(const std::string& resourceName)
{
return GetCurrentExecutablePath();
}
Array<char> ExeAppendedZipResourceLoader::GetResource(const std::string& exeName)
{
std::string ext = std::filesystem::path(exeName).extension().string();
if (ext != ".exe" && ext != "")
{
Logger::DATA->debug("Given file {} is not a valid executable", exeName);
return {};
}
std::ifstream ifs(exeName, std::ios::in | std::ios::binary);
if (!ifs.is_open())
{
Logger::DATA->debug("Could not open file {}.", exeName);
return {};
}
ifs.seekg(0, std::ios_base::end);
size_t zipSize = ifs.tellg();
ifs.seekg(0, std::ios_base::beg);
Array<char> zipData(zipSize);
ifs.read(zipData.Data(), zipSize);
ifs.close();
return zipData;
}
std::vector<std::pair<FileDescription, Array<char>>> ExeAppendedZipResourceLoader::GetZipArchiveFiles(const std::string& exePath)
{
Array<char> zipData = GetResource(exePath);
Array<char> zipData = Utils::ReadFile(GetCurrentExecutablePath());
ArchiveReader reader(zipData.Data(), zipData.Size(), nullptr, ArchiveType::ZIP);
std::vector<std::pair<FileDescription, Array<char>>> filesFromZip;
while (reader.HasNext())
if (auto data = reader.GetFile(resourceName))
{
filesFromZip.push_back(*reader.GetNextFile());
return data->second;
}
return filesFromZip;
return {};
}
}