minecraft-pi-reborn/mods/src/options/options.c

127 lines
4.3 KiB
C
Raw Normal View History

2020-12-02 23:18:49 +00:00
#include <string.h>
2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2020-12-02 23:18:49 +00:00
2022-06-25 21:30:08 +00:00
#include <mods/feature/feature.h>
#include <mods/init/init.h>
2020-12-02 23:18:49 +00:00
2021-07-04 23:02:45 +00:00
// Force Mob Spawning
static bool LevelData_getSpawnMobs_injection(__attribute__((unused)) unsigned char *level_data) {
return 1;
2020-12-02 23:18:49 +00:00
}
2021-02-02 00:35:30 +00:00
// Get Custom Render Distance
static int get_render_distance() {
char *distance_str = getenv("MCPI_RENDER_DISTANCE");
if (distance_str == NULL) {
distance_str = "Short";
}
if (strcmp("Far", distance_str) == 0) {
return 0;
} else if (strcmp("Normal", distance_str) == 0) {
return 1;
} else if (strcmp("Short", distance_str) == 0) {
return 2;
} else if (strcmp("Tiny", distance_str) == 0) {
return 3;
} else {
ERR("Invalid Render Distance: %s", distance_str);
}
}
2021-06-17 21:32:24 +00:00
2022-04-13 00:38:44 +00:00
// Get Custom Username
static char *get_username() {
char *username = getenv("MCPI_USERNAME");
if (username == NULL) {
username = "StevePi";
}
return username;
}
2022-07-20 06:58:14 +00:00
static char *safe_username = NULL;
__attribute__((destructor)) static void _free_safe_username() {
free(safe_username);
}
2022-04-13 00:38:44 +00:00
2021-12-01 02:25:04 +00:00
static int fancy_graphics;
static int peaceful_mode;
static int anaglyph;
static int smooth_lighting;
2021-02-02 00:35:30 +00:00
static int render_distance;
2021-12-01 02:25:04 +00:00
static int server_visible;
2020-12-02 23:18:49 +00:00
// Configure Options
2021-12-01 02:25:04 +00:00
static void Minecraft_init_injection(unsigned char *this) {
2020-12-02 23:18:49 +00:00
// Call Original Method
2021-12-01 02:25:04 +00:00
(*Minecraft_init)(this);
2020-12-02 23:18:49 +00:00
2021-12-01 02:25:04 +00:00
unsigned char *options = this + Minecraft_options_property_offset;
// Enable Fancy Graphics
*(options + Options_fancy_graphics_property_offset) = fancy_graphics;
2022-04-22 23:38:15 +00:00
// Enable Crosshair In Touch GUI
*(options + Options_split_controls_property_offset) = 1;
2021-12-01 02:25:04 +00:00
// Peaceful Mode
*(int32_t *) (options + Options_game_difficulty_property_offset) = peaceful_mode ? 0 : 2;
// 3D Anaglyph
*(options + Options_3d_anaglyph_property_offset) = anaglyph;
// Smooth Lighting
*(options + Options_ambient_occlusion_property_offset) = smooth_lighting;
2022-04-22 23:38:15 +00:00
// Render Distance
*(int32_t *) (options + Options_render_distance_property_offset) = render_distance;
2021-12-01 02:25:04 +00:00
// Server Visible
*(options + Options_server_visible_property_offset) = server_visible;
2020-12-02 23:18:49 +00:00
}
2021-06-19 23:07:09 +00:00
// Init
2020-12-02 23:18:49 +00:00
void init_options() {
2021-07-04 23:02:45 +00:00
// Force Mob Spawning
2022-04-10 00:01:16 +00:00
if (feature_has("Force Mob Spawning", server_auto)) {
2021-07-05 01:23:12 +00:00
overwrite((void *) LevelData_getSpawnMobs, (void *) LevelData_getSpawnMobs_injection);
2021-07-04 23:02:45 +00:00
}
2020-12-02 23:18:49 +00:00
2021-12-01 02:25:04 +00:00
// Enable Fancy Graphics
fancy_graphics = feature_has("Fancy Graphics", server_disabled);
// Peaceful Mode
peaceful_mode = feature_has("Peaceful Mode", server_auto);
// 3D Anaglyph
anaglyph = feature_has("3D Anaglyph", server_disabled);
2021-02-02 00:35:30 +00:00
// Render Distance
2021-06-17 21:32:24 +00:00
render_distance = get_render_distance();
2022-07-09 02:40:56 +00:00
DEBUG("Setting Render Distance: %i", render_distance);
2021-12-01 02:25:04 +00:00
// Server Visible
server_visible = !feature_has("Disable Hosting LAN Worlds", server_disabled);
2020-12-02 23:18:49 +00:00
// Set Options
2022-04-22 23:38:15 +00:00
overwrite_calls((void *) Minecraft_init, (void *) Minecraft_init_injection);
2020-12-02 23:18:49 +00:00
2022-04-13 00:38:44 +00:00
// Change Username
const char *username = get_username();
2022-07-09 02:40:56 +00:00
DEBUG("Setting Username: %s", username);
2022-04-13 00:38:44 +00:00
if (strcmp(*default_username, "StevePi") != 0) {
2022-04-15 01:12:42 +00:00
ERR("Default Username Is Invalid");
2022-04-13 00:38:44 +00:00
}
2022-07-20 06:58:14 +00:00
safe_username = to_cp437(username);
patch_address((void *) default_username, (void *) safe_username);
2021-12-01 02:25:04 +00:00
unsigned char username_length_patch[4] = {(unsigned char) strlen(safe_username), 0x20, 0xa0, 0xe3}; // "mov r2, #USERNAME_LENGTH"
patch((void *) 0x1ba2c, username_length_patch);
2022-04-13 00:38:44 +00:00
2021-10-04 23:42:55 +00:00
// Disable Autojump By Default
2022-04-10 00:01:16 +00:00
if (feature_has("Disable Autojump By Default", server_disabled)) {
2021-12-01 02:25:04 +00:00
unsigned char autojump_patch[4] = {0x00, 0x00, 0xa0, 0xe3}; // "mov r0, #0x0"
patch((void *) 0x5b148, autojump_patch);
2020-12-02 23:18:49 +00:00
}
2021-10-04 23:42:55 +00:00
// Display Nametags By Default
2022-04-10 00:01:16 +00:00
if (feature_has("Display Nametags By Default", server_disabled)) {
2021-10-27 22:14:30 +00:00
// r6 = 0x1
2021-12-01 02:25:04 +00:00
// r12 = 0x0
unsigned char display_nametags_patch[4] = {0x1d, 0x60, 0xc4, 0xe5}; // "strb r6, [r4, #0x1d]"
patch((void *) 0xf2d44, display_nametags_patch);
2021-02-04 01:58:43 +00:00
}
2020-12-02 23:18:49 +00:00
2021-12-01 02:25:04 +00:00
// Enable Smooth Lighting
smooth_lighting = feature_has("Smooth Lighting", server_disabled);
if (smooth_lighting) {
unsigned char smooth_lighting_patch[4] = {0x01, 0x00, 0x53, 0xe3}; // "cmp r3, #0x1"
patch((void *) 0x73b74, smooth_lighting_patch);
}
2021-06-19 23:07:09 +00:00
}