diff --git a/openVulkanoCpp/Base/Utils.cpp b/openVulkanoCpp/Base/Utils.cpp index 1ce3028..e0ee2fe 100644 --- a/openVulkanoCpp/Base/Utils.cpp +++ b/openVulkanoCpp/Base/Utils.cpp @@ -28,10 +28,14 @@ namespace OpenVulkano #endif } - Array Utils::ReadFile(const std::string& filePath) + Array 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(file.tellg()); Array data(fileSize); file.seekg(0); diff --git a/openVulkanoCpp/Base/Utils.hpp b/openVulkanoCpp/Base/Utils.hpp index c6e45ea..23db9a2 100644 --- a/openVulkanoCpp/Base/Utils.hpp +++ b/openVulkanoCpp/Base/Utils.hpp @@ -134,6 +134,6 @@ namespace OpenVulkano else return {str.substr(0, pos), str.substr(pos + 1)}; } - static Array ReadFile(const std::string& filePath); + static Array ReadFile(const std::string& filePath, bool emptyOnMissing = false); }; } diff --git a/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp new file mode 100644 index 0000000..f977b8a --- /dev/null +++ b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp @@ -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 +#include +#include + +namespace OpenVulkano +{ + namespace + { + void* HANDLE = ResourceLoader::RegisterResourceLoader(std::make_unique()); + + 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 ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName) + { + return Utils::ReadFile(GetAppDir() + resourceName); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp new file mode 100644 index 0000000..f8351e4 --- /dev/null +++ b/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.hpp @@ -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 GetResource(const std::string& resourceName) override; + }; +} \ No newline at end of file