Files
OpenVulkano/openVulkanoCpp/Host/ExeAppendedZipLoader.cpp
2024-12-21 17:19:01 +02:00

56 lines
1.6 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ExeAppendedZipLoader.hpp"
#include "Base/Logger.hpp"
#include "IO/Archive/ArchiveReader.hpp"
#include <filesystem>
#include <fstream>
namespace OpenVulkano
{
std::string ExeAppendedZipLoader::GetResourcePath(const std::string& resourceName)
{
return GetCurrentExecutablePath();
}
Array<char> ExeAppendedZipLoader::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>>> ExeAppendedZipLoader::GetZipArchiveFiles(const std::string& exePath)
{
Array<char> zipData = GetResource(exePath);
ArchiveReader reader(zipData.Data(), zipData.Size(), nullptr, ArchiveType::ZIP);
std::vector<std::pair<FileDescription, Array<char>>> filesFromZip;
while (reader.HasNext())
{
filesFromZip.push_back(*reader.GetNextFile());
}
return filesFromZip;
}
}