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

@ -95,7 +95,7 @@ static void set_allowed_options(OptionList *allowed_options);
static void print_help(const OptionList &allowed_options); static void print_help(const OptionList &allowed_options);
static void print_allowed_options(const OptionList &allowed_options); static void print_allowed_options(const OptionList &allowed_options);
static void print_version(); static void print_version(std::ostream &os);
static void print_worldspecs(const std::vector<WorldSpec> &worldspecs, static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
std::ostream &os, bool print_name = true, bool print_path = true); std::ostream &os, bool print_name = true, bool print_path = true);
static void print_modified_quicktune_values(); static void print_modified_quicktune_values();
@ -143,6 +143,8 @@ int main(int argc, char *argv[])
g_logger.registerThread("Main"); g_logger.registerThread("Main");
g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION); g_logger.addOutputMaxLevel(&stderr_output, LL_ACTION);
porting::osSpecificInit();
Settings cmd_args; Settings cmd_args;
get_env_opts(cmd_args); get_env_opts(cmd_args);
bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args); bool cmd_args_ok = get_cmdline_opts(argc, argv, &cmd_args);
@ -158,10 +160,13 @@ int main(int argc, char *argv[])
if (cmd_args.getFlag("version")) { if (cmd_args.getFlag("version")) {
porting::attachOrCreateConsole(); porting::attachOrCreateConsole();
print_version(); print_version(std::cout);
return 0; return 0;
} }
// Debug handler
BEGIN_DEBUG_EXCEPTION_HANDLER
if (!setup_log_params(cmd_args)) if (!setup_log_params(cmd_args))
return 1; return 1;
@ -171,22 +176,13 @@ int main(int argc, char *argv[])
} }
porting::signal_handler_init(); porting::signal_handler_init();
#ifdef __ANDROID__
porting::initAndroid();
porting::initializePathsAndroid();
#else
porting::initializePaths(); porting::initializePaths();
#endif
if (!create_userdata_path()) { if (!create_userdata_path()) {
errorstream << "Cannot create user data directory" << std::endl; errorstream << "Cannot create user data directory" << std::endl;
return 1; return 1;
} }
// Debug handler
BEGIN_DEBUG_EXCEPTION_HANDLER
// List gameids if requested // List gameids if requested
if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") { if (cmd_args.exists("gameid") && cmd_args.get("gameid") == "list") {
list_game_ids(); list_game_ids();
@ -215,7 +211,6 @@ int main(int argc, char *argv[])
if (g_settings->getBool("enable_console")) if (g_settings->getBool("enable_console"))
porting::attachOrCreateConsole(); porting::attachOrCreateConsole();
#ifndef __ANDROID__
// Run unit tests // Run unit tests
if (cmd_args.getFlag("run-unittests")) { if (cmd_args.getFlag("run-unittests")) {
#if BUILD_UNITTESTS #if BUILD_UNITTESTS
@ -245,7 +240,6 @@ int main(int argc, char *argv[])
return 1; return 1;
#endif #endif
} }
#endif // __ANDROID__
GameStartData game_params; GameStartData game_params;
#ifdef SERVER #ifdef SERVER
@ -430,19 +424,20 @@ static void print_allowed_options(const OptionList &allowed_options)
} }
} }
static void print_version() static void print_version(std::ostream &os)
{ {
std::cout << PROJECT_NAME_C " " << g_version_hash os << PROJECT_NAME_C " " << g_version_hash
<< " (" << porting::getPlatformName() << ")" << std::endl; << " (" << porting::getPlatformName() << ")" << std::endl;
#ifndef SERVER #ifndef SERVER
std::cout << "Using Irrlicht " IRRLICHT_SDK_VERSION << std::endl; os << "Using Irrlicht " IRRLICHT_SDK_VERSION << std::endl;
#endif #endif
#if USE_LUAJIT #if USE_LUAJIT
std::cout << "Using " << LUAJIT_VERSION << std::endl; os << "Using " << LUAJIT_VERSION << std::endl;
#else #else
std::cout << "Using " << LUA_RELEASE << std::endl; os << "Using " << LUA_RELEASE << std::endl;
#endif #endif
std::cout << g_build_info << std::endl; os << "Running on " << porting::get_sysinfo() << std::endl;
os << g_build_info << std::endl;
} }
static void list_game_ids() static void list_game_ids()
@ -718,10 +713,11 @@ static void uninit_common()
static void startup_message() static void startup_message()
{ {
infostream << PROJECT_NAME_C << " " << g_version_hash print_version(infostream);
<< "\nwith SER_FMT_VER_HIGHEST_READ=" infostream << "SER_FMT_VER_HIGHEST_READ=" <<
<< (int)SER_FMT_VER_HIGHEST_READ << ", " TOSTRING(SER_FMT_VER_HIGHEST_READ) <<
<< g_build_info << std::endl; " LATEST_PROTOCOL_VERSION=" << TOSTRING(LATEST_PROTOCOL_VERSION)
<< std::endl;
} }
static bool read_config_file(const Settings &cmd_args) static bool read_config_file(const Settings &cmd_args)

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

@ -301,6 +301,9 @@ inline const char *getPlatformName()
bool secure_rand_fill_buf(void *buf, size_t len); bool secure_rand_fill_buf(void *buf, size_t len);
// Call once near beginning of main function.
void osSpecificInit();
// This attaches to the parents process console, or creates a new one if it doesnt exist. // This attaches to the parents process console, or creates a new one if it doesnt exist.
void attachOrCreateConsole(); void attachOrCreateConsole();

@ -39,23 +39,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
extern int main(int argc, char *argv[]); extern int main(int argc, char *argv[]);
namespace porting {
void cleanupAndroid(); // used here
bool setSystemPaths(); // used in porting.cpp
}
void android_main(android_app *app) void android_main(android_app *app)
{ {
int retval = 0;
porting::app_global = app; porting::app_global = app;
Thread::setName("Main"); Thread::setName("Main");
char *argv[] = {strdup(PROJECT_NAME), strdup("--verbose"), nullptr}; char *argv[] = {strdup(PROJECT_NAME), strdup("--verbose"), nullptr};
try { int retval = main(ARRLEN(argv) - 1, argv);
main(ARRLEN(argv) - 1, argv);
} catch (std::exception &e) {
errorstream << "Uncaught exception in main thread: " << e.what() << std::endl;
retval = -1;
} catch (...) {
errorstream << "Uncaught exception in main thread!" << std::endl;
retval = -1;
}
free(argv[0]); free(argv[0]);
free(argv[1]); free(argv[1]);
@ -65,8 +61,8 @@ void android_main(android_app *app)
} }
namespace porting { namespace porting {
android_app *app_global; android_app *app_global = nullptr;
JNIEnv *jnienv; JNIEnv *jnienv = nullptr;
jclass nativeActivity; jclass nativeActivity;
jclass findClass(const std::string &classname) jclass findClass(const std::string &classname)
@ -86,9 +82,8 @@ jclass findClass(const std::string &classname)
return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName); return (jclass) jnienv->CallObjectMethod(cls, findClass, strClassName);
} }
void initAndroid() void osSpecificInit()
{ {
porting::jnienv = nullptr;
JavaVM *jvm = app_global->activity->vm; JavaVM *jvm = app_global->activity->vm;
JavaVMAttachArgs lJavaVMAttachArgs; JavaVMAttachArgs lJavaVMAttachArgs;
lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.version = JNI_VERSION_1_6;
@ -108,8 +103,7 @@ void initAndroid()
#ifdef GPROF #ifdef GPROF
// in the start-up code // in the start-up code
__android_log_print(ANDROID_LOG_ERROR, PROJECT_NAME_C, warningstream << "Initializing GPROF profiler" << std::endl;
"Initializing GPROF profiler");
monstartup("libMinetest.so"); monstartup("libMinetest.so");
#endif #endif
} }
@ -117,7 +111,7 @@ void initAndroid()
void cleanupAndroid() void cleanupAndroid()
{ {
#ifdef GPROF #ifdef GPROF
errorstream << "Shutting down GPROF profiler" << std::endl; warningstream << "Shutting down GPROF profiler" << std::endl;
setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1); setenv("CPUPROFILE", (path_user + DIR_DELIM + "gmon.out").c_str(), 1);
moncleanup(); moncleanup();
#endif #endif
@ -137,7 +131,7 @@ static std::string readJavaString(jstring j_str)
return str; return str;
} }
void initializePathsAndroid() bool setSystemPaths()
{ {
// Set user and share paths // Set user and share paths
{ {
@ -149,7 +143,6 @@ void initializePathsAndroid()
std::string str = readJavaString((jstring) result); std::string str = readJavaString((jstring) result);
path_user = str; path_user = str;
path_share = str; path_share = str;
path_locale = str + DIR_DELIM + "locale";
} }
// Set cache path // Set cache path
@ -160,9 +153,9 @@ void initializePathsAndroid()
"porting::initializePathsAndroid unable to find Java getCachePath method"); "porting::initializePathsAndroid unable to find Java getCachePath method");
jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath); jobject result = jnienv->CallObjectMethod(app_global->activity->clazz, getCachePath);
path_cache = readJavaString((jstring) result); path_cache = readJavaString((jstring) result);
migrateCachePath();
} }
return true;
} }
void showInputDialog(const std::string &acceptButton, const std::string &hint, void showInputDialog(const std::string &acceptButton, const std::string &hint,
@ -183,7 +176,7 @@ void showInputDialog(const std::string &acceptButton, const std::string &hint,
jacceptButton, jhint, jcurrent, jeditType); jacceptButton, jhint, jcurrent, jeditType);
} }
void openURIAndroid(const std::string &url) void openURIAndroid(const char *url)
{ {
jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI", jmethodID url_open = jnienv->GetMethodID(nativeActivity, "openURI",
"(Ljava/lang/String;)V"); "(Ljava/lang/String;)V");
@ -191,7 +184,7 @@ void openURIAndroid(const std::string &url)
FATAL_ERROR_IF(url_open == nullptr, FATAL_ERROR_IF(url_open == nullptr,
"porting::openURIAndroid unable to find Java openURI method"); "porting::openURIAndroid unable to find Java openURI method");
jstring jurl = jnienv->NewStringUTF(url.c_str()); jstring jurl = jnienv->NewStringUTF(url);
jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl); jnienv->CallVoidMethod(app_global->activity->clazz, url_open, jurl);
} }

@ -36,16 +36,6 @@ extern android_app *app_global;
// java <-> c++ interaction interface // java <-> c++ interaction interface
extern JNIEnv *jnienv; extern JNIEnv *jnienv;
// do initialization required on android only
void initAndroid();
void cleanupAndroid();
/**
* Initializes path_* variables for Android
*/
void initializePathsAndroid();
/** /**
* show text input dialog in java * show text input dialog in java
* @param acceptButton text to display on accept button * @param acceptButton text to display on accept button
@ -57,8 +47,6 @@ void initializePathsAndroid();
void showInputDialog(const std::string &acceptButton, void showInputDialog(const std::string &acceptButton,
const std::string &hint, const std::string &current, int editType); const std::string &hint, const std::string &current, int editType);
void openURIAndroid(const std::string &url);
/** /**
* Opens a share intent to the file at path * Opens a share intent to the file at path
* *