Add ResourceLoaderAppDirLinux
This commit is contained in:
@@ -28,10 +28,14 @@ namespace OpenVulkano
|
||||
#endif
|
||||
}
|
||||
|
||||
Array<char> Utils::ReadFile(const std::string& filePath)
|
||||
Array<char> Utils::ReadFile(const std::string& filePath, bool emptyOnMissing)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::ate | std::ios::binary);
|
||||
if (!file.is_open()) throw std::runtime_error("Failed to open file '" + filePath + "'!");
|
||||
if (!file.is_open())
|
||||
{
|
||||
if (emptyOnMissing) return { 0 };
|
||||
throw std::runtime_error("Failed to open file '" + filePath + "'!");
|
||||
}
|
||||
const size_t fileSize = static_cast<size_t>(file.tellg());
|
||||
Array<char> data(fileSize);
|
||||
file.seekg(0);
|
||||
|
||||
@@ -134,6 +134,6 @@ namespace OpenVulkano
|
||||
else return {str.substr(0, pos), str.substr(pos + 1)};
|
||||
}
|
||||
|
||||
static Array<char> ReadFile(const std::string& filePath);
|
||||
static Array<char> ReadFile(const std::string& filePath, bool emptyOnMissing = false);
|
||||
};
|
||||
}
|
||||
|
||||
47
openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp
Normal file
47
openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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 "ResourceLoaderAppDirLinux.hpp"
|
||||
#include "Base/Utils.hpp"
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
namespace
|
||||
{
|
||||
void* HANDLE = ResourceLoader::RegisterResourceLoader(std::make_unique<ResourceLoaderAppDirLinux>());
|
||||
|
||||
std::string FindAppDir()
|
||||
{
|
||||
char buffer[PATH_MAX];
|
||||
size_t size;
|
||||
if ((size = readlink("/proc/self/exe", buffer, sizeof(buffer))) != -1)
|
||||
{
|
||||
std::string_view appDirPath(buffer, size);
|
||||
appDirPath = appDirPath.substr(0, appDirPath.find_last_of('/') + 1);
|
||||
return std::string(appDirPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Failed to find real path to application!";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
const std::string& GetAppDir()
|
||||
{
|
||||
static const std::string appDirPath = FindAppDir();
|
||||
return appDirPath;
|
||||
}
|
||||
}
|
||||
|
||||
Array<char> ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName)
|
||||
{
|
||||
return Utils::ReadFile(GetAppDir() + resourceName);
|
||||
}
|
||||
}
|
||||
18
openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp
Normal file
18
openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Host/ResourceLoader.hpp"
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
class ResourceLoaderAppDirLinux final : public ResourceLoader
|
||||
{
|
||||
public:
|
||||
Array<char> GetResource(const std::string& resourceName) override;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user