code review refactoring
This commit is contained in:
@@ -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 {};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,9 +16,9 @@ namespace OpenVulkano
|
||||
class ExeAppendedZipResourceLoader : public ResourceLoader
|
||||
{
|
||||
public:
|
||||
std::string GetResourcePath(const std::string& resourceName) override;
|
||||
Array<char> GetResource(const std::string& exePath) override;
|
||||
std::vector<std::pair<FileDescription, Array<char>>> GetZipArchiveFiles(const std::string& exePath);
|
||||
std::string GetResourcePath(const std::string& resourceName) final { return ""; }
|
||||
Array<char> GetResource(const std::string& resourceName) override;
|
||||
protected:
|
||||
virtual std::string GetCurrentExecutablePath() const = 0;
|
||||
};
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "ExeAppendedZipLoaderLinux.hpp"
|
||||
#include "ExeAppendedZipResourceLoaderLinux.hpp"
|
||||
#include <unistd.h>
|
||||
#include <climits>
|
||||
|
||||
@@ -12,19 +12,15 @@ namespace OpenVulkano
|
||||
{
|
||||
namespace
|
||||
{
|
||||
void* HANDLE = ResourceLoader::RegisterResourceLoader(std::make_unique<ExeAppendedZipLoaderLinux>());
|
||||
void* HANDLE = ResourceLoader::RegisterResourceLoader(std::make_unique<ExeAppendedZipResourceLoaderLinux>());
|
||||
}
|
||||
|
||||
std::string OpenVulkano::ExeAppendedZipLoaderLinux::GetCurrentExecutablePath() const
|
||||
std::string OpenVulkano::ExeAppendedZipResourceLoaderLinux::GetCurrentExecutablePath() const
|
||||
{
|
||||
char dest[PATH_MAX];
|
||||
memset(dest, 0, sizeof(dest)); // readlink does not null terminate!
|
||||
size_t sz = 0;
|
||||
if ((sz = readlink("/proc/self/exe", dest, PATH_MAX)) != -1)
|
||||
{
|
||||
return std::string(dest, sz);
|
||||
}
|
||||
return "";
|
||||
std::string path(PATH_MAX, '\0');
|
||||
ssize_t sz = readlink("/proc/self/exe", path.data(), path.capacity()));
|
||||
path.resize(std::max<ssize_t>(0, sz));
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,11 +6,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Host/ExeAppendedZipLoader.hpp"
|
||||
#include "Host/ExeAppendedZipResourceLoader.hpp"
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
class ExeAppendedZipLoaderLinux final : public ExeAppendedZipLoader
|
||||
class ExeAppendedZipResourceLoaderLinux final : public ExeAppendedZipResourceLoader
|
||||
{
|
||||
public:
|
||||
std::string GetCurrentExecutablePath() const override;
|
||||
|
||||
@@ -16,9 +16,10 @@ namespace OpenVulkano
|
||||
|
||||
std::string OpenVulkano::ExeAppendedZipResourceLoaderWindows::GetCurrentExecutablePath() const
|
||||
{
|
||||
CHAR nameBuf[MAX_PATH] = {};
|
||||
DWORD len = GetModuleFileNameA(NULL, nameBuf, MAX_PATH);
|
||||
return std::string(nameBuf, len);
|
||||
std::string exe(MAX_PATH, '\0');
|
||||
DWORD len = GetModuleFileNameA(NULL, exe.data(), MAX_PATH);
|
||||
exe.resize(std::max<size_t>(len, exe.size()));
|
||||
return exe;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user