1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Clean up OS-specific initialization

This commit is contained in:
sfan5 2023-12-19 21:41:48 +01:00
parent ad5e9aa5e3
commit 93c2aff2cf
5 changed files with 84 additions and 98 deletions

View file

@ -68,9 +68,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h"
#include "log.h"
#include "util/string.h"
#include <list>
#include <vector>
#include <cstdarg>
#include <cstdio>
#include <signal.h>
#if !defined(SERVER) && defined(_WIN32)
// On Windows export some driver-specific variables to encourage Minetest to be
@ -88,7 +89,7 @@ namespace porting
Signal handler (grabs Ctrl-C on POSIX systems)
*/
bool g_killed = false;
static bool g_killed = false;
bool *signal_handler_killstatus()
{
@ -96,9 +97,8 @@ bool *signal_handler_killstatus()
}
#if !defined(_WIN32) // POSIX
#include <signal.h>
void signal_handler(int sig)
static void signal_handler(int sig)
{
if (!g_killed) {
if (sig == SIGINT) {
@ -127,9 +127,8 @@ void signal_handler_init(void)
}
#else // _WIN32
#include <signal.h>
BOOL WINAPI event_handler(DWORD sig)
static BOOL WINAPI event_handler(DWORD sig)
{
switch (sig) {
case CTRL_C_EVENT:
@ -164,11 +163,11 @@ void signal_handler_init(void)
Path mangler
*/
// Default to RUN_IN_PLACE style relative paths
std::string path_share = "..";
std::string path_user = "..";
std::string path_locale = path_share + DIR_DELIM + "locale";
std::string path_cache = path_user + DIR_DELIM + "cache";
// Nobody should be reading these before initializePaths() is called
std::string path_share = "UNINITIALIZED";
std::string path_user = "UNINITIALIZED";
std::string path_locale = "UNINITIALIZED";
std::string path_cache = "UNINITIALIZED";
std::string getDataPath(const char *subpath)
@ -176,7 +175,7 @@ std::string getDataPath(const char *subpath)
return path_share + DIR_DELIM + subpath;
}
void pathRemoveFile(char *path, char delim)
[[maybe_unused]] static void pathRemoveFile(char *path, char delim)
{
// Remove filename and path delimiter
int i;
@ -267,9 +266,9 @@ bool getCurrentWorkingDir(char *buf, size_t len)
}
bool getExecPathFromProcfs(char *buf, size_t buflen)
static bool getExecPathFromProcfs(char *buf, size_t buflen)
{
#ifndef _WIN32
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
buflen--;
ssize_t len;
@ -396,10 +395,7 @@ bool getCurrentExecPath(char *buf, size_t len)
#endif
//// Non-Windows
#if !defined(_WIN32)
const char *getHomeOrFail()
[[maybe_unused]] static inline const char *getHomeOrFail()
{
const char *home = getenv("HOME");
// In rare cases the HOME environment variable may be unset
@ -408,8 +404,6 @@ const char *getHomeOrFail()
return home;
}
#endif
//// Windows
#if defined(_WIN32)
@ -449,6 +443,13 @@ bool setSystemPaths()
}
//// Android
#elif defined(__ANDROID__)
extern bool setSystemPaths(); // defined in porting_android.cpp
//// Linux
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
@ -457,11 +458,7 @@ bool setSystemPaths()
char buf[BUFSIZ];
if (!getCurrentExecPath(buf, sizeof(buf))) {
#ifdef __ANDROID__
errorstream << "Unable to read bindir "<< std::endl;
#else
FATAL_ERROR("Unable to read bindir");
#endif
return false;
}
@ -470,7 +467,7 @@ bool setSystemPaths()
// Find share directory from these.
// It is identified by containing the subdirectory "builtin".
std::list<std::string> trylist;
std::vector<std::string> trylist;
std::string static_sharedir = STATIC_SHAREDIR;
if (!static_sharedir.empty() && static_sharedir != ".")
trylist.push_back(static_sharedir);
@ -479,12 +476,7 @@ bool setSystemPaths()
DIR_DELIM + PROJECT_NAME);
trylist.push_back(bindir + DIR_DELIM "..");
#ifdef __ANDROID__
trylist.push_back(path_user);
#endif
for (std::list<std::string>::const_iterator
i = trylist.begin(); i != trylist.end(); ++i) {
for (auto i = trylist.begin(); i != trylist.end(); ++i) {
const std::string &trypath = *i;
if (!fs::PathExists(trypath) ||
!fs::PathExists(trypath + DIR_DELIM + "builtin")) {
@ -503,7 +495,6 @@ bool setSystemPaths()
break;
}
#ifndef __ANDROID__
const char *const minetest_user_path = getenv("MINETEST_USER_PATH");
if (minetest_user_path && minetest_user_path[0] != '\0') {
path_user = std::string(minetest_user_path);
@ -511,7 +502,6 @@ bool setSystemPaths()
path_user = std::string(getHomeOrFail()) + DIR_DELIM "."
+ PROJECT_NAME;
}
#endif
return true;
}
@ -586,10 +576,9 @@ void migrateCachePath()
void initializePaths()
{
#if RUN_IN_PLACE
char buf[BUFSIZ];
infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl;
char buf[BUFSIZ];
bool success =
getCurrentExecPath(buf, sizeof(buf)) ||
getExecPathFromProcfs(buf, sizeof(buf));
@ -627,17 +616,18 @@ void initializePaths()
path_user = execpath;
}
path_cache = path_user + DIR_DELIM + "cache";
#else
infostream << "Using system-wide paths (NOT RUN_IN_PLACE)" << std::endl;
if (!setSystemPaths())
errorstream << "Failed to get one or more system-wide path" << std::endl;
# ifdef _WIN32
# ifdef __ANDROID__
sanity_check(!path_cache.empty());
# elif defined(_WIN32)
path_cache = path_user + DIR_DELIM + "cache";
# else
// Initialize path_cache
// First try $XDG_CACHE_HOME/PROJECT_NAME
const char *cache_dir = getenv("XDG_CACHE_HOME");
const char *home_dir = getenv("HOME");
@ -651,9 +641,11 @@ void initializePaths()
// If neither works, use $PATH_USER/cache
path_cache = path_user + DIR_DELIM + "cache";
}
# endif // _WIN32
// Migrate cache folder to new location if possible
migrateCachePath();
# endif // _WIN32
#endif // RUN_IN_PLACE
infostream << "Detected share path: " << path_share << std::endl;
@ -727,6 +719,15 @@ bool secure_rand_fill_buf(void *buf, size_t len)
#endif
#ifndef __ANDROID__
void osSpecificInit()
{
// nothing here yet
}
#endif
void attachOrCreateConsole()
{
#ifdef _WIN32
@ -794,6 +795,11 @@ int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...)
return c;
}
#ifdef __ANDROID__
// defined in porting_android.cpp
extern void openURIAndroid(const char *url);
#endif
static bool open_uri(const std::string &uri)
{
if (uri.find_first_of("\r\n") != std::string::npos) {
@ -804,7 +810,7 @@ static bool open_uri(const std::string &uri)
#if defined(_WIN32)
return (intptr_t)ShellExecuteA(NULL, NULL, uri.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
#elif defined(__ANDROID__)
openURIAndroid(uri);
openURIAndroid(uri.c_str());
return true;
#elif defined(__APPLE__)
const char *argv[] = {"open", uri.c_str(), NULL};