minecraft-pi-reborn/mods/src/skin/skin.cpp

98 lines
3.3 KiB
C++
Raw Normal View History

2023-10-19 05:23:34 +00:00
#include <libreborn/libreborn.h>
#include <symbols/minecraft.h>
#include <mods/init/init.h>
2023-10-19 05:56:09 +00:00
#include <mods/feature/feature.h>
2023-10-19 05:23:34 +00:00
#include "skin-internal.h"
// Base64 Encode (https://gist.github.com/tomykaira/f0fd86b6c73063283afe550bc5d77594)
static std::string base64_encode(const std::string data) {
static constexpr char encoding_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
size_t in_len = data.size();
size_t out_len = 4 * ((in_len + 2) / 3);
std::string ret(out_len, '\0');
size_t i;
char *p = const_cast<char*>(ret.c_str());
for (i = 0; i < in_len - 2; i += 3) {
*p++ = encoding_table[(data[i] >> 2) & 0x3f];
*p++ = encoding_table[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xf0) >> 4)];
*p++ = encoding_table[((data[i + 1] & 0xf) << 2) | ((int) (data[i + 2] & 0xc0) >> 6)];
*p++ = encoding_table[data[i + 2] & 0x3f];
}
if (i < in_len) {
*p++ = encoding_table[(data[i] >> 2) & 0x3f];
if (i == (in_len - 1)) {
*p++ = encoding_table[((data[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = encoding_table[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xf0) >> 4)];
*p++ = encoding_table[((data[i + 1] & 0xf) << 2)];
}
*p++ = '=';
}
return ret;
}
// Change Texture For Player Entities
static void Player_username_assign_injection(std::string *target, std::string *username) {
// Call Original Method
*target = *username;
// Get Player
2024-01-06 11:30:23 +00:00
Player *player = (Player *) (((unsigned char *) target) - offsetof(Player, username));
2023-10-19 05:23:34 +00:00
// Get Texture
2024-01-06 11:30:23 +00:00
std::string *texture = &player->texture;
2023-10-19 05:23:34 +00:00
// Set Texture
*texture = '$' + base64_encode(*username);
}
static void Player_username_assign_injection_2(std::string *target, const char *username) {
std::string username_str = username;
Player_username_assign_injection(target, &username_str);
}
// Change Texture For HUD
2024-01-06 11:30:23 +00:00
static int32_t Textures_loadAndBindTexture_injection(Textures *textures, __attribute__((unused)) std::string const& name) {
2023-10-19 05:23:34 +00:00
// Change Texture
static std::string new_texture;
if (new_texture.length() == 0) {
2024-01-07 07:59:04 +00:00
std::string username = base64_encode(Strings_default_username);
2023-10-19 05:23:34 +00:00
new_texture = '$' + username;
}
// Call Original Method
2024-01-06 11:30:23 +00:00
return (*Textures_loadAndBindTexture)(textures, &new_texture);
2023-10-19 05:23:34 +00:00
}
// Init
void init_skin() {
2023-10-19 05:56:09 +00:00
// Check Feature Flag
if (feature_has("Load Custom Skins", server_disabled)) {
// LocalPlayer
overwrite_call((void *) 0x44c28, (void *) Player_username_assign_injection);
// RemotePlayer
overwrite_call((void *) 0x6ce58, (void *) Player_username_assign_injection_2);
// ServerPlayer
overwrite_call((void *) 0x7639c, (void *) Player_username_assign_injection_2);
2023-10-19 05:23:34 +00:00
2023-10-19 05:56:09 +00:00
// HUD
overwrite_call((void *) 0x4c6d0, (void *) Textures_loadAndBindTexture_injection);
2023-10-19 05:23:34 +00:00
2023-10-19 05:56:09 +00:00
// Loader
_init_skin_loader();
}
2023-10-19 05:23:34 +00:00
}