Load Mods From ~/.minecraft-pi; Prompt User For Custom Username On Startup

This commit is contained in:
TheBrokenRail 2020-10-02 18:43:43 -04:00
parent 121a92f5ac
commit 73afa13af4
4 changed files with 76 additions and 37 deletions

View File

@ -19,8 +19,6 @@ static int ends_with(const char *s, const char *t) {
return strcmp(s + slen - tlen, t) == 0; return strcmp(s + slen - tlen, t) == 0;
} }
#define MODS_FOLDER "./mods/"
static void set_and_print_env(char *name, char *value) { static void set_and_print_env(char *name, char *value) {
int length = strlen(value); int length = strlen(value);
if (value[length - 1] == ':') { if (value[length - 1] == ':') {
@ -36,20 +34,10 @@ static char *get_env_safe(const char *name) {
return ret != NULL ? ret : ""; return ret != NULL ? ret : "";
} }
int main(__attribute__((unused)) int argc, char *argv[]) { static void load(char **ld_preload, char *folder) {
fprintf(stderr, "Configuring Game...\n"); int folder_name_length = strlen(folder);
while (1) {
char *ld_path = NULL; DIR *dp = opendir(folder);
char *cwd = getcwd(NULL, 0);
asprintf(&ld_path, "%s:/usr/arm-linux-gnueabihf/lib:%s", cwd, get_env_safe("LD_LIBRARY_PATH"));
free(cwd);
set_and_print_env("LD_LIBRARY_PATH", ld_path);
free(ld_path);
char *ld_preload = NULL;
asprintf(&ld_preload, "%s", get_env_safe("LD_PRELOAD"));
int folder_name_length = strlen(MODS_FOLDER);
DIR *dp = opendir(MODS_FOLDER);
if (dp != NULL) { if (dp != NULL) {
struct dirent *entry = NULL; struct dirent *entry = NULL;
errno = 0; errno = 0;
@ -62,7 +50,7 @@ int main(__attribute__((unused)) int argc, char *argv[]) {
char name[total_length + 1]; char name[total_length + 1];
for (int i = 0; i < folder_name_length; i++) { for (int i = 0; i < folder_name_length; i++) {
name[i] = MODS_FOLDER[i]; name[i] = folder[i];
} }
for (int i = 0; i < name_length; i++) { for (int i = 0; i < name_length; i++) {
name[folder_name_length + i] = entry->d_name[i]; name[folder_name_length + i] = entry->d_name[i];
@ -70,7 +58,7 @@ int main(__attribute__((unused)) int argc, char *argv[]) {
name[total_length] = '\0'; name[total_length] = '\0';
asprintf(&ld_preload, "%s:%s", name, ld_preload); asprintf(ld_preload, "%s:%s", name, *ld_preload);
} }
} else if (errno != 0) { } else if (errno != 0) {
fprintf(stderr, "Error Reading Directory: %s\n", strerror(errno)); fprintf(stderr, "Error Reading Directory: %s\n", strerror(errno));
@ -79,11 +67,44 @@ int main(__attribute__((unused)) int argc, char *argv[]) {
break; break;
} }
} }
closedir(dp);
return;
} else if (errno == ENOENT) {
char *cmd = NULL;
asprintf(&cmd, "mkdir -p %s", folder);
int ret = system(cmd);
if (ret != 0) {
exit(ret);
}
} else { } else {
fprintf(stderr, "Error Opening Directory: %s\n", strerror(errno)); fprintf(stderr, "Error Opening Directory: %s\n", strerror(errno));
exit(1); exit(1);
} }
closedir(dp); }
}
int main(__attribute__((unused)) int argc, char *argv[]) {
fprintf(stderr, "Configuring Game...\n");
char *ld_path = NULL;
char *cwd = getcwd(NULL, 0);
asprintf(&ld_path, "%s:/usr/arm-linux-gnueabihf/lib:%s", cwd, get_env_safe("LD_LIBRARY_PATH"));
free(cwd);
set_and_print_env("LD_LIBRARY_PATH", ld_path);
free(ld_path);
char *ld_preload = NULL;
asprintf(&ld_preload, "%s", get_env_safe("LD_PRELOAD"));
load(&ld_preload, "./mods/");
char *home_mods = NULL;
asprintf(&home_mods, "%s/.minecraft/mods/", getenv("HOME"));
load(&ld_preload, home_mods);
free(home_mods);
set_and_print_env("LD_PRELOAD", ld_preload); set_and_print_env("LD_PRELOAD", ld_preload);
free(ld_preload); free(ld_preload);

View File

@ -5,8 +5,10 @@ set -e
# Ensure Features Are Selected # Ensure Features Are Selected
if [ -z "${MCPI_SUBSHELL}" ]; then if [ -z "${MCPI_SUBSHELL}" ]; then
MCPI_FEATURES="$(zenity --class minecraft-pi --list --checklist --column 'Enabled' --column 'Feature' FALSE 'Touch GUI' FALSE 'Survival Mode' FALSE 'Fix Bow & Arrow' FALSE 'Fix Attacking' FALSE 'Mob Spawning' FALSE 'Show Clouds')" MCPI_FEATURES="$(zenity --class minecraft-pi --list --checklist --column 'Enabled' --column 'Feature' FALSE 'Touch GUI' FALSE 'Survival Mode' FALSE 'Fix Bow & Arrow' FALSE 'Fix Attacking' FALSE 'Mob Spawning' FALSE 'Show Clouds')"
MCPI_USERNAME="$(zenity --class minecraft-pi --entry --text 'Minecraft Username:' --entry-text 'StevePi')"
fi fi
export MCPI_FEATURES export MCPI_FEATURES
export MCPI_USERNAME
# Start VirGL # Start VirGL
virgl_test_server & virgl_test_server &

View File

@ -10,3 +10,4 @@ services:
environment: environment:
- 'DISPLAY=unix${DISPLAY}' - 'DISPLAY=unix${DISPLAY}'
- 'MCPI_FEATURES=${MCPI_FEATURES}' - 'MCPI_FEATURES=${MCPI_FEATURES}'
- 'MCPI_USERNAME=${MCPI_USERNAME}'

View File

@ -118,6 +118,14 @@ static void setIsCreativeMode_injection(unsigned char *this, int32_t new_game_mo
revert_overwrite((void *) setIsCreativeMode, setIsCreativeMode_original); revert_overwrite((void *) setIsCreativeMode, setIsCreativeMode_original);
} }
static char *get_username() {
char *username = getenv("MCPI_USERNAME");
if (username == NULL) {
username = "StevePi";
}
return username;
}
__attribute__((constructor)) static void init() { __attribute__((constructor)) static void init() {
if (has_feature("Touch GUI")) { if (has_feature("Touch GUI")) {
// Use Touch UI // Use Touch UI
@ -174,4 +182,11 @@ __attribute__((constructor)) static void init() {
// Allow Connecting To Non-Pi Servers // Allow Connecting To Non-Pi Servers
unsigned char patch_data_9[4] = {0x0f, 0x00, 0x00, 0xea}; unsigned char patch_data_9[4] = {0x0f, 0x00, 0x00, 0xea};
patch((void *) 0x6dc70, patch_data_9); patch((void *) 0x6dc70, patch_data_9);
// Change Username
const char *username = get_username();
uint32_t username_addr = (uint32_t) username;
fprintf(stderr, "0x%08x", username_addr);
unsigned char username_patch[4] = {username_addr & 0xff, (username_addr >> 8) & 0xff, (username_addr >> 16) & 0xff, (username_addr >> 24) & 0xff};
patch((void *) 0x18fd4, username_patch);
} }