2022-03-06 20:53:27 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
#include <libreborn/libreborn.h>
|
2021-09-12 03:18:12 +00:00
|
|
|
#include <symbols/minecraft.h>
|
2021-06-17 21:32:24 +00:00
|
|
|
|
|
|
|
#include "home.h"
|
|
|
|
#include "../init/init.h"
|
|
|
|
|
|
|
|
// Minecraft Pi User Data Root
|
|
|
|
#ifndef MCPI_SERVER_MODE
|
|
|
|
// Store Game Data In "~/.minecraft-pi" Instead Of "~/.minecraft" To Avoid Conflicts
|
|
|
|
#define NEW_PATH "/.minecraft-pi"
|
|
|
|
#else
|
|
|
|
// Store Game Data In Launch Directory
|
|
|
|
#define NEW_PATH ""
|
|
|
|
|
2021-06-28 02:16:37 +00:00
|
|
|
// Store Launch Directory
|
2021-11-09 21:26:02 +00:00
|
|
|
__attribute__((constructor)) static char *get_launch_directory() {
|
2021-08-27 02:52:18 +00:00
|
|
|
static char *launch_directory = NULL;
|
|
|
|
if (launch_directory == NULL) {
|
|
|
|
launch_directory = getcwd(NULL, 0);
|
|
|
|
}
|
|
|
|
return launch_directory;
|
|
|
|
}
|
|
|
|
__attribute__((destructor)) static void free_launch_directory() {
|
|
|
|
free(get_launch_directory());
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 02:16:37 +00:00
|
|
|
// Pretend $HOME Is Launch Directory
|
2021-06-17 21:32:24 +00:00
|
|
|
HOOK(getenv, char *, (const char *name)) {
|
|
|
|
if (strcmp(name, "HOME") == 0) {
|
2021-08-27 02:52:18 +00:00
|
|
|
return get_launch_directory();
|
2021-06-17 21:32:24 +00:00
|
|
|
} else {
|
|
|
|
ensure_getenv();
|
|
|
|
return (*real_getenv)(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Get MCPI Home Directory
|
|
|
|
char *home_get() {
|
2021-06-28 20:00:52 +00:00
|
|
|
static char *dir = NULL;
|
|
|
|
// Load
|
|
|
|
if (dir == NULL) {
|
2021-08-27 02:52:18 +00:00
|
|
|
safe_asprintf(&dir, "%s" NEW_PATH, getenv("HOME"));
|
2021-06-28 20:00:52 +00:00
|
|
|
}
|
|
|
|
// Return
|
2021-06-17 21:32:24 +00:00
|
|
|
return dir;
|
|
|
|
}
|
2021-06-28 20:00:52 +00:00
|
|
|
// Free
|
|
|
|
__attribute__((destructor)) static void _free_home() {
|
|
|
|
free(home_get());
|
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
|
|
|
|
// Init
|
|
|
|
void init_home() {
|
|
|
|
// Store Data In ~/.minecraft-pi Instead Of ~/.minecraft
|
|
|
|
patch_address((void *) default_path, (void *) NEW_PATH);
|
2021-12-19 23:24:59 +00:00
|
|
|
|
|
|
|
// Change Directory To Binary Directory Manually
|
|
|
|
unsigned char nop_patch[4] = {0x00, 0xf0, 0x20, 0xe3}; // "nop"
|
|
|
|
patch((void *) 0xe0ac, nop_patch);
|
|
|
|
char *binary_directory = get_binary_directory();
|
2022-03-06 20:53:27 +00:00
|
|
|
if (chdir(binary_directory) != 0) {
|
|
|
|
ERR("Unable To Change Directory: %s", strerror(errno));
|
|
|
|
}
|
2021-12-19 23:24:59 +00:00
|
|
|
free(binary_directory);
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|