Files
OpenVulkano/openVulkanoCpp/Host/Linux/ResourceLoaderAppDirLinux.cpp
2023-11-22 16:25:44 +01:00

47 lines
1.1 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 "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, true);
}
}