Use whereami to guess executable path
This commit is contained in:
parent
7aa6c8f33e
commit
9ea5920210
84
include/whereami/whereami++.cpp
Executable file
84
include/whereami/whereami++.cpp
Executable file
@ -0,0 +1,84 @@
|
||||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2016 nabijaczleweli
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
#include "whereami++.hpp"
|
||||
#include "whereami.h"
|
||||
#include <utility>
|
||||
|
||||
|
||||
// wai_get*Path returns an incorrect length on Windows (1 more than required)
|
||||
#ifdef _WIN32
|
||||
#define WHEREAMI_CPP_MISPADDING 1
|
||||
#else
|
||||
#define WHEREAMI_CPP_MISPADDING 0
|
||||
#endif
|
||||
|
||||
|
||||
using whereami_func_t = int (*)(char * out, int capacity, int * dirname_length);
|
||||
|
||||
|
||||
static std::string whereami_path(whereami_func_t whereami_func) {
|
||||
const auto length = whereami_func(nullptr, 0, nullptr);
|
||||
if(length == -1)
|
||||
return "";
|
||||
|
||||
std::string ret(length - WHEREAMI_CPP_MISPADDING, '\0');
|
||||
whereami_func(&ret[0], length - WHEREAMI_CPP_MISPADDING, nullptr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static std::pair<std::string, std::string> whereami_segmented(whereami_func_t whereami_func) {
|
||||
const auto length = whereami_func(nullptr, 0, nullptr);
|
||||
if(length == -1)
|
||||
return {"", ""};
|
||||
|
||||
// Mispadding correction breaks libcode here and is unnecessary because we're constructing via C-string anyway
|
||||
int path_length;
|
||||
std::string buf(length, '\0');
|
||||
whereami_func(&buf[0], length, &path_length);
|
||||
return {{buf.c_str(), static_cast<std::size_t>(path_length)}, &buf[path_length + 1]};
|
||||
}
|
||||
|
||||
|
||||
std::string whereami::executable_path() {
|
||||
return whereami_path(wai_getExecutablePath);
|
||||
}
|
||||
|
||||
std::string whereami::module_path() {
|
||||
return whereami_path(wai_getModulePath);
|
||||
}
|
||||
|
||||
std::string whereami::executable_name() {
|
||||
return whereami_segmented(wai_getExecutablePath).second;
|
||||
}
|
||||
|
||||
std::string whereami::module_name() {
|
||||
return whereami_segmented(wai_getModulePath).second;
|
||||
}
|
||||
|
||||
std::string whereami::executable_dir() {
|
||||
return whereami_segmented(wai_getExecutablePath).first;
|
||||
}
|
||||
|
||||
std::string whereami::module_dir() {
|
||||
return whereami_segmented(wai_getModulePath).first;
|
||||
}
|
38
include/whereami/whereami++.hpp
Executable file
38
include/whereami/whereami++.hpp
Executable file
@ -0,0 +1,38 @@
|
||||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2016 nabijaczleweli
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
// this software and associated documentation files (the "Software"), to deal in
|
||||
// the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace whereami {
|
||||
std::string executable_path();
|
||||
std::string module_path();
|
||||
|
||||
std::string executable_name();
|
||||
std::string module_name();
|
||||
|
||||
std::string executable_dir();
|
||||
std::string module_dir();
|
||||
}
|
679
include/whereami/whereami.c
Executable file
679
include/whereami/whereami.c
Executable file
@ -0,0 +1,679 @@
|
||||
// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses
|
||||
// without any warranty.
|
||||
// by Gregory Pakosz (@gpakosz)
|
||||
// https://github.com/gpakosz/whereami
|
||||
|
||||
// in case you want to #include "whereami.c" in a larger compilation unit
|
||||
#if !defined(WHEREAMI_H)
|
||||
#include <whereami/whereami.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#if !defined(WAI_MALLOC)
|
||||
#define WAI_MALLOC(size) malloc(size)
|
||||
#endif
|
||||
|
||||
#if !defined(WAI_FREE)
|
||||
#define WAI_FREE(p) free(p)
|
||||
#endif
|
||||
|
||||
#if !defined(WAI_REALLOC)
|
||||
#define WAI_REALLOC(p, size) realloc(p, size)
|
||||
#endif
|
||||
|
||||
#ifndef WAI_NOINLINE
|
||||
#if defined(_MSC_VER)
|
||||
#define WAI_NOINLINE __declspec(noinline)
|
||||
#elif defined(__GNUC__)
|
||||
#define WAI_NOINLINE __attribute__((noinline))
|
||||
#else
|
||||
#error unsupported compiler
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define WAI_RETURN_ADDRESS() _ReturnAddress()
|
||||
#elif defined(__GNUC__)
|
||||
#define WAI_RETURN_ADDRESS() __builtin_extract_return_addr(__builtin_return_address(0))
|
||||
#else
|
||||
#error unsupported compiler
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push, 3)
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <intrin.h>
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
wchar_t buffer1[MAX_PATH];
|
||||
wchar_t buffer2[MAX_PATH];
|
||||
wchar_t* path = NULL;
|
||||
int length = -1;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
DWORD size;
|
||||
int length_, length__;
|
||||
|
||||
size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0]));
|
||||
|
||||
if (size == 0)
|
||||
break;
|
||||
else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0])))
|
||||
{
|
||||
DWORD size_ = size;
|
||||
do
|
||||
{
|
||||
wchar_t* path_;
|
||||
|
||||
path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2);
|
||||
if (!path_)
|
||||
break;
|
||||
size_ *= 2;
|
||||
path = path_;
|
||||
size = GetModuleFileNameW(module, path, size_);
|
||||
}
|
||||
while (size == size_);
|
||||
|
||||
if (size == size_)
|
||||
break;
|
||||
}
|
||||
else
|
||||
path = buffer1;
|
||||
|
||||
if (!_wfullpath(buffer2, path, MAX_PATH))
|
||||
break;
|
||||
length_ = (int)wcslen(buffer2);
|
||||
length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_ , out, capacity, NULL, NULL);
|
||||
|
||||
if (length__ == 0)
|
||||
length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, NULL, NULL);
|
||||
if (length__ == 0)
|
||||
break;
|
||||
|
||||
if (length__ <= capacity && dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length__ - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '\\')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
length = length__;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (path != buffer1)
|
||||
WAI_FREE(path);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
WAI_NOINLINE WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length);
|
||||
}
|
||||
|
||||
WAI_NOINLINE WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
HMODULE module;
|
||||
int length = -1;
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4054)
|
||||
#endif
|
||||
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)WAI_RETURN_ADDRESS(), &module))
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
{
|
||||
length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || defined(WAI_USE_PROC_SELF_EXE)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if defined(__linux__)
|
||||
#include <linux/limits.h>
|
||||
#else
|
||||
#include <limits.h>
|
||||
#endif
|
||||
#ifndef __STDC_FORMAT_MACROS
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#endif
|
||||
#include <inttypes.h>
|
||||
|
||||
#if !defined(WAI_PROC_SELF_EXE)
|
||||
#if defined(__sun)
|
||||
#define WAI_PROC_SELF_EXE "/proc/self/path/a.out"
|
||||
#else
|
||||
#define WAI_PROC_SELF_EXE "/proc/self/exe"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer[PATH_MAX];
|
||||
char* resolved = NULL;
|
||||
int length = -1;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
resolved = realpath(WAI_PROC_SELF_EXE, buffer);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#if !defined(WAI_PROC_SELF_MAPS_RETRY)
|
||||
#define WAI_PROC_SELF_MAPS_RETRY 5
|
||||
#endif
|
||||
|
||||
#if !defined(WAI_PROC_SELF_MAPS)
|
||||
#if defined(__sun)
|
||||
#define WAI_PROC_SELF_MAPS "/proc/self/map"
|
||||
#else
|
||||
#define WAI_PROC_SELF_MAPS "/proc/self/maps"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__ANDROID__) || defined(ANDROID)
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
WAI_NOINLINE WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
int length = -1;
|
||||
FILE* maps = NULL;
|
||||
|
||||
for (int r = 0; r < WAI_PROC_SELF_MAPS_RETRY; ++r)
|
||||
{
|
||||
maps = fopen(WAI_PROC_SELF_MAPS, "r");
|
||||
if (!maps)
|
||||
break;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX];
|
||||
uint64_t low, high;
|
||||
char perms[5];
|
||||
uint64_t offset;
|
||||
uint32_t major, minor;
|
||||
char path[PATH_MAX];
|
||||
uint32_t inode;
|
||||
|
||||
if (!fgets(buffer, sizeof(buffer), maps))
|
||||
break;
|
||||
|
||||
if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
|
||||
{
|
||||
uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS();
|
||||
if (low <= addr && addr <= high)
|
||||
{
|
||||
char* resolved;
|
||||
|
||||
resolved = realpath(path, buffer);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
#if defined(__ANDROID__) || defined(ANDROID)
|
||||
if (length > 4
|
||||
&&buffer[length - 1] == 'k'
|
||||
&&buffer[length - 2] == 'p'
|
||||
&&buffer[length - 3] == 'a'
|
||||
&&buffer[length - 4] == '.')
|
||||
{
|
||||
int fd = open(path, O_RDONLY);
|
||||
char* begin;
|
||||
char* p;
|
||||
|
||||
begin = (char*)mmap(0, offset + sizeof(p), PROT_READ, MAP_SHARED, fd, 0);
|
||||
p = begin + offset;
|
||||
|
||||
while (p >= begin) // scan backwards
|
||||
{
|
||||
if (*((uint32_t*)p) == 0x04034b50UL) // local file header found
|
||||
{
|
||||
uint16_t length_ = *((uint16_t*)(p + 26));
|
||||
|
||||
if (length + 2 + length_ < (int)sizeof(buffer))
|
||||
{
|
||||
memcpy(&buffer[length], "!/", 2);
|
||||
memcpy(&buffer[length + 2], p + 30, length_);
|
||||
length += 2 + length_;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
--p;
|
||||
}
|
||||
|
||||
munmap(begin, offset);
|
||||
close(fd);
|
||||
}
|
||||
#endif
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(maps);
|
||||
maps = NULL;
|
||||
|
||||
if (length != -1)
|
||||
break;
|
||||
}
|
||||
|
||||
if (maps)
|
||||
fclose(maps);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
|
||||
#define _DARWIN_BETTER_REALPATH
|
||||
#include <mach-o/dyld.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer1[PATH_MAX];
|
||||
char buffer2[PATH_MAX];
|
||||
char* path = buffer1;
|
||||
char* resolved = NULL;
|
||||
int length = -1;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
uint32_t size = (uint32_t)sizeof(buffer1);
|
||||
if (_NSGetExecutablePath(path, &size) == -1)
|
||||
{
|
||||
path = (char*)WAI_MALLOC(size);
|
||||
if (!_NSGetExecutablePath(path, &size))
|
||||
break;
|
||||
}
|
||||
|
||||
resolved = realpath(path, buffer2);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (path != buffer1)
|
||||
WAI_FREE(path);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
WAI_NOINLINE WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer[PATH_MAX];
|
||||
char* resolved = NULL;
|
||||
int length = -1;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
Dl_info info;
|
||||
|
||||
if (dladdr(WAI_RETURN_ADDRESS(), &info))
|
||||
{
|
||||
resolved = realpath(info.dli_fname, buffer);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#elif defined(__QNXNTO__)
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#if !defined(WAI_PROC_SELF_EXE)
|
||||
#define WAI_PROC_SELF_EXE "/proc/self/exefile"
|
||||
#endif
|
||||
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer1[PATH_MAX];
|
||||
char buffer2[PATH_MAX];
|
||||
char* resolved = NULL;
|
||||
FILE* self_exe = NULL;
|
||||
int length = -1;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
self_exe = fopen(WAI_PROC_SELF_EXE, "r");
|
||||
if (!self_exe)
|
||||
break;
|
||||
|
||||
if (!fgets(buffer1, sizeof(buffer1), self_exe))
|
||||
break;
|
||||
|
||||
resolved = realpath(buffer1, buffer2);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
fclose(self_exe);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer[PATH_MAX];
|
||||
char* resolved = NULL;
|
||||
int length = -1;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
Dl_info info;
|
||||
|
||||
if (dladdr(WAI_RETURN_ADDRESS(), &info))
|
||||
{
|
||||
resolved = realpath(info.dli_fname, buffer);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
|
||||
defined(__FreeBSD_kernel__) || defined(__NetBSD__)
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer1[PATH_MAX];
|
||||
char buffer2[PATH_MAX];
|
||||
char* path = buffer1;
|
||||
char* resolved = NULL;
|
||||
int length = -1;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
#if defined(__NetBSD__)
|
||||
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME };
|
||||
#else
|
||||
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
|
||||
#endif
|
||||
size_t size = sizeof(buffer1);
|
||||
|
||||
if (sysctl(mib, (u_int)(sizeof(mib) / sizeof(mib[0])), path, &size, NULL, 0) != 0)
|
||||
break;
|
||||
|
||||
resolved = realpath(path, buffer2);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (path != buffer1)
|
||||
WAI_FREE(path);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
WAI_NOINLINE WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
|
||||
{
|
||||
char buffer[PATH_MAX];
|
||||
char* resolved = NULL;
|
||||
int length = -1;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
Dl_info info;
|
||||
|
||||
if (dladdr(WAI_RETURN_ADDRESS(), &info))
|
||||
{
|
||||
resolved = realpath(info.dli_fname, buffer);
|
||||
if (!resolved)
|
||||
break;
|
||||
|
||||
length = (int)strlen(resolved);
|
||||
if (length <= capacity)
|
||||
{
|
||||
memcpy(out, resolved, length);
|
||||
|
||||
if (dirname_length)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = length - 1; i >= 0; --i)
|
||||
{
|
||||
if (out[i] == '/')
|
||||
{
|
||||
*dirname_length = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#error unsupported platform
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
67
include/whereami/whereami.h
Executable file
67
include/whereami/whereami.h
Executable file
@ -0,0 +1,67 @@
|
||||
// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses
|
||||
// without any warranty.
|
||||
// by Gregory Pakosz (@gpakosz)
|
||||
// https://github.com/gpakosz/whereami
|
||||
|
||||
#ifndef WHEREAMI_H
|
||||
#define WHEREAMI_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef WAI_FUNCSPEC
|
||||
#define WAI_FUNCSPEC
|
||||
#endif
|
||||
#ifndef WAI_PREFIX
|
||||
#define WAI_PREFIX(function) wai_##function
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Returns the path to the current executable.
|
||||
*
|
||||
* Usage:
|
||||
* - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to
|
||||
* retrieve the length of the path
|
||||
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
|
||||
* - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the
|
||||
* path
|
||||
* - add a terminal NUL character with `path[length] = '\0';`
|
||||
*
|
||||
* @param out destination buffer, optional
|
||||
* @param capacity destination buffer capacity
|
||||
* @param dirname_length optional recipient for the length of the dirname part
|
||||
* of the path.
|
||||
*
|
||||
* @return the length of the executable path on success (without a terminal NUL
|
||||
* character), otherwise `-1`
|
||||
*/
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length);
|
||||
|
||||
/**
|
||||
* Returns the path to the current module
|
||||
*
|
||||
* Usage:
|
||||
* - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve
|
||||
* the length of the path
|
||||
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
|
||||
* - call `wai_getModulePath(path, length, NULL)` again to retrieve the path
|
||||
* - add a terminal NUL character with `path[length] = '\0';`
|
||||
*
|
||||
* @param out destination buffer, optional
|
||||
* @param capacity destination buffer capacity
|
||||
* @param dirname_length optional recipient for the length of the dirname part
|
||||
* of the path.
|
||||
*
|
||||
* @return the length of the module path on success (without a terminal NUL
|
||||
* character), otherwise `-1`
|
||||
*/
|
||||
WAI_FUNCSPEC
|
||||
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // #ifndef WHEREAMI_H
|
@ -1,6 +1,6 @@
|
||||
project(
|
||||
'jujube',
|
||||
'cpp',
|
||||
['cpp', 'c'],
|
||||
default_options : ['cpp_std=c++17'],
|
||||
version : '0.1.0-alpha'
|
||||
)
|
||||
@ -23,6 +23,8 @@ sources = [
|
||||
'include/imgui/imgui_draw.cpp',
|
||||
'include/imgui/imgui_widgets.cpp',
|
||||
'include/imgui-sfml/imgui-SFML.cpp',
|
||||
'include/whereami/whereami.c',
|
||||
'include/whereami/whereami++.cpp',
|
||||
'src/Data/Buttons.hpp',
|
||||
'src/Data/Buttons.cpp',
|
||||
'src/Data/Chart.cpp',
|
||||
|
@ -72,8 +72,13 @@ namespace Data {
|
||||
Layout layout;
|
||||
Options options;
|
||||
KeyMapping key_mapping;
|
||||
ghc::filesystem::path jujube_path;
|
||||
|
||||
Preferences() : screen(), layout() {
|
||||
Preferences(const ghc::filesystem::path& t_jujube_path) :
|
||||
screen(),
|
||||
layout(),
|
||||
jujube_path(t_jujube_path)
|
||||
{
|
||||
load_from_file();
|
||||
}
|
||||
|
||||
@ -82,7 +87,7 @@ namespace Data {
|
||||
}
|
||||
|
||||
void load_from_file() {
|
||||
auto path = ghc::filesystem::path("data/preferences.json");
|
||||
auto path = jujube_path/"data"/"preferences.json";
|
||||
if (ghc::filesystem::exists(path)) {
|
||||
std::ifstream prefs_file;
|
||||
prefs_file.open(path);
|
||||
@ -99,7 +104,7 @@ namespace Data {
|
||||
};
|
||||
|
||||
void save_to_file() {
|
||||
auto data_folder = ghc::filesystem::path("data");
|
||||
auto data_folder = jujube_path/"data";
|
||||
if (not ghc::filesystem::exists(data_folder)) {
|
||||
ghc::filesystem::create_directory(data_folder);
|
||||
}
|
||||
@ -107,7 +112,7 @@ namespace Data {
|
||||
std::cerr << "Can't create data folder to save preferences, a file named 'data' exists" << std::endl;
|
||||
}
|
||||
std::ofstream preferences_file;
|
||||
preferences_file.open(data_folder / "preferences.json", std::ofstream::trunc | std::ofstream::out);
|
||||
preferences_file.open(data_folder/"preferences.json", std::ofstream::trunc | std::ofstream::out);
|
||||
{
|
||||
cereal::JSONOutputArchive archive{preferences_file};
|
||||
serialize(archive);
|
||||
|
@ -43,10 +43,10 @@ namespace Data {
|
||||
}
|
||||
}
|
||||
|
||||
SongList::SongList() :
|
||||
SongList::SongList(const fs::path& jujube_path) :
|
||||
songs()
|
||||
{
|
||||
fs::path song_folder = "songs/";
|
||||
fs::path song_folder = jujube_path/"songs";
|
||||
|
||||
if (fs::exists(song_folder) and fs::is_directory(song_folder)) {
|
||||
for (const auto& dir_item : fs::directory_iterator("songs/")) {
|
||||
|
@ -68,7 +68,7 @@ namespace Data {
|
||||
// Class holding all the necessary song data to run the Music Select screen
|
||||
class SongList {
|
||||
public:
|
||||
SongList();
|
||||
SongList(const fs::path& jujube_path);
|
||||
std::list<std::shared_ptr<Song>> songs;
|
||||
};
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cereal/archives/json.hpp>
|
||||
#include <whereami/whereami++.hpp>
|
||||
|
||||
#include "Data/Song.hpp"
|
||||
#include "Data/Preferences.hpp"
|
||||
@ -16,8 +17,9 @@
|
||||
|
||||
int main(int, char const **) {
|
||||
|
||||
Data::Preferences preferences;
|
||||
auto markers = Resources::load_markers();
|
||||
const std::string jujube_path = whereami::executable_dir();
|
||||
Data::Preferences preferences{jujube_path};
|
||||
auto markers = Resources::load_markers(jujube_path);
|
||||
if (markers.find(preferences.options.marker) == markers.end()) {
|
||||
preferences.options.marker = markers.begin()->first;
|
||||
}
|
||||
@ -33,7 +35,7 @@ int main(int, char const **) {
|
||||
preferences.screen.fullscreen ? sf::Style::Fullscreen : sf::Style::Default,
|
||||
settings
|
||||
};
|
||||
Data::SongList song_list;
|
||||
Data::SongList song_list{jujube_path};
|
||||
MusicSelect::Screen music_select{
|
||||
song_list,
|
||||
preferences,
|
||||
|
@ -6,14 +6,6 @@
|
||||
|
||||
namespace fs = ghc::filesystem;
|
||||
|
||||
Textures::CoverAltas::CoverAltas() :
|
||||
path_to_index(),
|
||||
index_to_path(),
|
||||
textures()
|
||||
{
|
||||
this->emplace_back("assets/textures/fallback_cover.png");
|
||||
}
|
||||
|
||||
sf::Sprite Textures::CoverAltas::at(const fs::path& path) const {
|
||||
|
||||
std::size_t index;
|
||||
|
@ -186,10 +186,11 @@ namespace Resources {
|
||||
}
|
||||
}
|
||||
|
||||
Markers load_markers() {
|
||||
Markers load_markers(const fs::path& jujube_path) {
|
||||
Markers res;
|
||||
if (fs::exists(fs::path("markers"))) {
|
||||
for (auto& p : fs::directory_iterator("markers")) {
|
||||
auto markers_folder = jujube_path/"markers";
|
||||
if (fs::exists(markers_folder)) {
|
||||
for (auto& p : fs::directory_iterator(markers_folder)) {
|
||||
if (p.is_directory()) {
|
||||
try {
|
||||
Marker m{p.path()};
|
||||
|
@ -65,7 +65,7 @@ namespace Resources {
|
||||
};
|
||||
|
||||
struct Marker {
|
||||
Marker(const ghc::filesystem::path& marker_folder);
|
||||
explicit Marker(const ghc::filesystem::path& marker_folder);
|
||||
std::optional<sf::Sprite> get_sprite(const MarkerAnimation& state, float seconds) const;
|
||||
std::optional<sf::Sprite> get_sprite(const MarkerAnimation& state, const std::size_t frame) const;
|
||||
void load_and_check(sf::Texture& spritesheet, const MarkerAnimationMetadata& metadata);
|
||||
@ -83,5 +83,5 @@ namespace Resources {
|
||||
};
|
||||
|
||||
using Markers = std::map<std::string, Marker>;
|
||||
Markers load_markers();
|
||||
Markers load_markers(const ghc::filesystem::path& jujube_path);
|
||||
}
|
||||
|
@ -9,30 +9,29 @@
|
||||
|
||||
namespace MusicSelect {
|
||||
|
||||
FallbackFont::FallbackFont() :
|
||||
FallbackFont::FallbackFont(const ghc::filesystem::path& jujube_path) :
|
||||
light(),
|
||||
medium(),
|
||||
black()
|
||||
{
|
||||
if (not light.loadFromFile("assets/fonts/M_PLUS_Rounded_1c/MPLUSRounded1c-Light.ttf")) {
|
||||
throw std::runtime_error("Unable to load assets/fonts/M_PLUS_Rounded_1c/MPLUSRounded1c-Light.ttf");
|
||||
auto fallback_font_folder = jujube_path/"assets"/"fonts"/"M_PLUS_Rounded_1c";
|
||||
if (not light.loadFromFile(fallback_font_folder/"MPLUSRounded1c-Light.ttf")) {
|
||||
throw std::runtime_error("Unable to load MPLUSRounded1c-Light.ttf");
|
||||
}
|
||||
if (not medium.loadFromFile("assets/fonts/M_PLUS_Rounded_1c/RoundedMplus1c-Medium+NotoSansSymbols+Symbola.ttf")) {
|
||||
throw std::runtime_error("Unable to load assets/fonts/M_PLUS_Rounded_1c/RoundedMplus1c-Medium+NotoSansSymbols+Symbola.ttf");
|
||||
if (not medium.loadFromFile(fallback_font_folder/"RoundedMplus1c-Medium+NotoSansSymbols+Symbola.ttf")) {
|
||||
throw std::runtime_error("Unable to load RoundedMplus1c-Medium+NotoSansSymbols+Symbola.ttf");
|
||||
}
|
||||
if (not black.loadFromFile("assets/fonts/M_PLUS_Rounded_1c/MPLUSRounded1c-Black.ttf")) {
|
||||
throw std::runtime_error("Unable to load assets/fonts/M_PLUS_Rounded_1c/MPLUSRounded1c-Black.ttf");
|
||||
if (not black.loadFromFile(fallback_font_folder/"MPLUSRounded1c-Black.ttf")) {
|
||||
throw std::runtime_error("Unable to load MPLUSRounded1c-Black.ttf");
|
||||
}
|
||||
}
|
||||
|
||||
SharedResources::SharedResources(Data::Preferences& p, const Resources::Markers& m) :
|
||||
Data::HoldsPreferences(p),
|
||||
markers(m)
|
||||
markers(m),
|
||||
fallback_font(p.jujube_path)
|
||||
{
|
||||
covers.reserve(256);
|
||||
if (not fallback_cover.loadFromFile("assets/textures/fallback_cover.png")) {
|
||||
throw std::runtime_error("Unable to load assets/textures/fallback_cover.png");
|
||||
}
|
||||
std::cout << "Loaded MusicSelect::SharedResources" << std::endl;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
|
||||
#include <ghc/filesystem.hpp>
|
||||
#include <jbcoe/polymorphic_value.h>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
@ -33,7 +34,7 @@ namespace MusicSelect {
|
||||
};
|
||||
|
||||
struct FallbackFont {
|
||||
FallbackFont();
|
||||
FallbackFont(const ghc::filesystem::path& jujube_path);
|
||||
sf::Font light;
|
||||
sf::Font medium;
|
||||
sf::Font black;
|
||||
@ -44,7 +45,6 @@ namespace MusicSelect {
|
||||
SharedResources(Data::Preferences& p, const Resources::Markers& m);
|
||||
|
||||
Textures::TextureCache covers;
|
||||
sf::Texture fallback_cover;
|
||||
|
||||
FallbackFont fallback_font;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user