Base64 encoding in 'Reborn mode' API

This commit is contained in:
Bigjango13 2024-10-27 01:25:17 -07:00
parent 0b6675231e
commit ccd4a7b73b
7 changed files with 115 additions and 65 deletions

View File

@ -33,6 +33,7 @@ set(SRC
src/misc/graphics.cpp src/misc/graphics.cpp
src/misc/ui.cpp src/misc/ui.cpp
src/misc/tinting.cpp src/misc/tinting.cpp
src/misc/base64.cpp
# options # options
src/options/options.cpp src/options/options.cpp
src/options/ui.cpp src/options/ui.cpp

View File

@ -12,6 +12,6 @@ std::string chat_send_api_command(const Minecraft *minecraft, const std::string
// Override using the HOOK() macro to provide customized chat behavior. // Override using the HOOK() macro to provide customized chat behavior.
void chat_send_message(ServerSideNetworkHandler *server_side_network_handler, const char *username, const char *message); void chat_send_message(ServerSideNetworkHandler *server_side_network_handler, const char *username, const char *message);
void chat_handle_packet_send(const Minecraft *minecraft, ChatPacket *packet); void chat_handle_packet_send(const Minecraft *minecraft, ChatPacket *packet);
bool is_sending_to_chat(); bool chat_is_sending();
} }

View File

@ -32,4 +32,7 @@ std::map<int, std::string> &misc_get_entity_names();
std::string misc_get_entity_name(Entity *entity); std::string misc_get_entity_name(Entity *entity);
Entity *misc_make_entity_from_id(Level *level, int id); Entity *misc_make_entity_from_id(Level *level, int id);
std::string misc_base64_encode(const std::string &data);
std::string misc_base64_decode(const std::string &input);
static constexpr int line_height = 8; static constexpr int line_height = 8;

View File

@ -76,15 +76,6 @@ static std::string getEntityData(CommandServer *commandserver, Entity *entity) {
std::to_string(z); std::to_string(z);
} }
static std::string replace(std::string str, std::string s1, std::string s2) {
size_t i = str.find(s1, 0);
while (i != std::string::npos) {
str.replace(i, s1.length(), s2);
i = str.find(s1, i + s2.length());
}
return str;
}
static float distance_between(Entity *e1, Entity *e2) { static float distance_between(Entity *e1, Entity *e2) {
if (e1 == NULL || e2 == NULL) return 0; if (e1 == NULL || e2 == NULL) return 0;
float dx = e2->x - e1->x; float dx = e2->x - e1->x;
@ -245,7 +236,7 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
ChatEvent ce = pop_circular_queue(chatEvents, chat_events_start, chat_events_at); ChatEvent ce = pop_circular_queue(chatEvents, chat_events_start, chat_events_at);
std::string message = ce.toString(); std::string message = ce.toString();
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
else replace(message, "|", "\\|"); else message = misc_base64_encode(message);
ret += (compat_mode ? "0," : "") + message + "|"; ret += (compat_mode ? "0," : "") + message + "|";
} }
if (ret.size() > 1) ret.pop_back(); if (ret.size() > 1) ret.pop_back();
@ -257,7 +248,7 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
if (ce.from_player) { if (ce.from_player) {
std::string message = ce.toString(); std::string message = ce.toString();
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
else replace(message, "|", "\\|"); else message = misc_base64_encode(message);
ret += (compat_mode ? "0," : "") + message + "|"; ret += (compat_mode ? "0," : "") + message + "|";
} }
} }
@ -427,10 +418,10 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
if (ret == 3) return ""; if (ret == 3) return "";
SignTileEntity *sign = (SignTileEntity *) commandserver->minecraft->level->getTileEntity(x, y, z); SignTileEntity *sign = (SignTileEntity *) commandserver->minecraft->level->getTileEntity(x, y, z);
if (sign == NULL || sign->type != 4) return ""; if (sign == NULL || sign->type != 4) return "";
if (ret > 5) sign->lines[0] = l1; if (ret > 5) sign->lines[0] = compat_mode ? l1 : misc_base64_decode(l1);
if (ret > 6) sign->lines[1] = l2; if (ret > 6) sign->lines[1] = compat_mode ? l2 : misc_base64_decode(l2);
if (ret > 7) sign->lines[2] = l3; if (ret > 7) sign->lines[2] = compat_mode ? l3 : misc_base64_decode(l3);
if (ret > 8) sign->lines[3] = l4; if (ret > 8) sign->lines[3] = compat_mode ? l4 : misc_base64_decode(l4);
} else if (cmd == "world.spawnEntity") { } else if (cmd == "world.spawnEntity") {
// Parse // Parse
float x, y, z; float x, y, z;
@ -554,7 +545,7 @@ static void Gui_addMessage_injection(Gui_addMessage_t original, Gui *gui, const
} else { } else {
push_circular_queue(ChatEvent{ push_circular_queue(ChatEvent{
text, text,
is_sending_to_chat() chat_is_sending()
}, chatEvents, chat_events_start, chat_events_at); }, chatEvents, chat_events_start, chat_events_at);
recursing = true; recursing = true;
original(gui, text); original(gui, text);

View File

@ -76,14 +76,14 @@ static void ServerSideNetworkHandler_handle_ChatPacket_injection(ServerSideNetwo
} }
// Send Message // Send Message
static bool _is_sending_to_chat = false; static bool _chat_is_sending = false;
bool is_sending_to_chat() { bool chat_is_sending() {
return _is_sending_to_chat; return _chat_is_sending;
} }
void _chat_send_message(const Minecraft *minecraft, const char *message) { void _chat_send_message(const Minecraft *minecraft, const char *message) {
_is_sending_to_chat= true; _chat_is_sending = true;
send_api_chat_command(minecraft, message); send_api_chat_command(minecraft, message);
_is_sending_to_chat = false; _chat_is_sending = false;
} }
// Allow Reading Longer ChatPacket Messages // Allow Reading Longer ChatPacket Messages

95
mods/src/misc/base64.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <cstdint>
#include <string>
// Base64 encode/decode (https://gist.github.com/tomykaira/f0fd86b6c73063283afe550bc5d77594)
std::string misc_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', '+', '/'
};
const size_t in_len = data.size();
const 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;
}
std::string misc_base64_decode(const std::string &input) {
static constexpr unsigned char decoding_table[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64
};
size_t in_len = input.size();
if (in_len % 4 != 0)
return "";
std::string out = "";
size_t out_len = in_len / 4 * 3;
if (in_len >= 1 && input[in_len - 1] == '=') out_len--;
if (in_len >= 2 && input[in_len - 2] == '=') out_len--;
out.resize(out_len);
for (size_t i = 0, j = 0; i < in_len;) {
uint32_t a = input[i] == '=' ? 0 & i++
: decoding_table[static_cast<int>(input[i++])];
uint32_t b = input[i] == '=' ? 0 & i++
: decoding_table[static_cast<int>(input[i++])];
uint32_t c = input[i] == '=' ? 0 & i++
: decoding_table[static_cast<int>(input[i++])];
uint32_t d = input[i] == '=' ? 0 & i++
: decoding_table[static_cast<int>(input[i++])];
uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
if (j < out_len)
out[j++] = (triple >> 2 * 8) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 1 * 8) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 0 * 8) & 0xFF;
}
return out;
}

View File

@ -2,50 +2,10 @@
#include <symbols/minecraft.h> #include <symbols/minecraft.h>
#include <mods/init/init.h> #include <mods/init/init.h>
#include <mods/misc/misc.h>
#include <mods/feature/feature.h> #include <mods/feature/feature.h>
#include "skin-internal.h" #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', '+', '/'
};
const size_t in_len = data.size();
const 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 // Change Texture For Player Entities
static void Player_username_assign_injection(std::string *target, std::string *username) { static void Player_username_assign_injection(std::string *target, std::string *username) {
// Call Original Method // Call Original Method
@ -57,7 +17,7 @@ static void Player_username_assign_injection(std::string *target, std::string *u
std::string *texture = &player->texture; std::string *texture = &player->texture;
// Set Texture // Set Texture
*texture = '$' + base64_encode(*username); *texture = '$' + misc_base64_encode(*username);
} }
static void Player_username_assign_injection_2(std::string *target, const char *username) { static void Player_username_assign_injection_2(std::string *target, const char *username) {
std::string username_str = username; std::string username_str = username;
@ -69,7 +29,7 @@ static int32_t Textures_loadAndBindTexture_injection(Textures *textures, __attri
// Change Texture // Change Texture
static std::string new_texture; static std::string new_texture;
if (new_texture.length() == 0) { if (new_texture.length() == 0) {
const std::string username = base64_encode(Strings::default_username); const std::string username = misc_base64_encode(Strings::default_username);
new_texture = '$' + username; new_texture = '$' + username;
} }