2023-10-19 05:23:34 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <libreborn/libreborn.h>
|
|
|
|
#include <symbols/minecraft.h>
|
|
|
|
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
|
|
|
|
#include <mods/misc/misc.h>
|
|
|
|
#include <mods/textures/textures.h>
|
|
|
|
#include "skin-internal.h"
|
|
|
|
|
2023-11-11 05:44:26 +00:00
|
|
|
#include "stb_image.h"
|
|
|
|
|
2023-10-19 05:23:34 +00:00
|
|
|
// Constants
|
|
|
|
#define SKIN_WIDTH 64
|
|
|
|
#define SKIN_HEIGHT 32
|
|
|
|
|
|
|
|
// Loading Pending Skins
|
|
|
|
struct pending_skin {
|
|
|
|
int32_t texture_id;
|
|
|
|
char *data;
|
2023-11-11 05:44:26 +00:00
|
|
|
int size;
|
2023-10-19 05:23:34 +00:00
|
|
|
};
|
|
|
|
static std::vector<pending_skin> &get_pending_skins() {
|
|
|
|
static std::vector<pending_skin> pending_skins;
|
|
|
|
return pending_skins;
|
|
|
|
}
|
|
|
|
static pthread_mutex_t pending_skins_lock = PTHREAD_MUTEX_INITIALIZER;
|
2024-01-06 11:30:23 +00:00
|
|
|
static void load_pending_skins(__attribute__((unused)) Minecraft *minecraft) {
|
2023-10-19 05:23:34 +00:00
|
|
|
// Lock
|
|
|
|
pthread_mutex_lock(&pending_skins_lock);
|
|
|
|
|
|
|
|
// Loop
|
|
|
|
for (pending_skin &skin : get_pending_skins()) {
|
|
|
|
// Read PNG Info
|
2023-11-11 05:44:26 +00:00
|
|
|
int width = 0, height = 0, channels = 0;
|
|
|
|
stbi_uc *img = stbi_load_from_memory((unsigned char *) skin.data, skin.size, &width, &height, &channels, STBI_rgb_alpha);
|
2023-10-19 05:23:34 +00:00
|
|
|
if (width != SKIN_WIDTH || height != SKIN_HEIGHT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load Texture
|
|
|
|
GLint last_texture;
|
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, skin.texture_id);
|
2023-11-11 05:44:26 +00:00
|
|
|
glTexSubImage2D_with_scaling(GL_TEXTURE_2D, 0, 0, 0, width, height, SKIN_WIDTH, SKIN_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, img);
|
2023-10-19 05:23:34 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, last_texture);
|
|
|
|
|
|
|
|
// Free
|
2023-11-11 05:44:26 +00:00
|
|
|
stbi_image_free(img);
|
2023-10-19 05:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free
|
|
|
|
for (pending_skin &skin : get_pending_skins()) {
|
|
|
|
free(skin.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear
|
|
|
|
get_pending_skins().clear();
|
|
|
|
|
|
|
|
// Unlock
|
|
|
|
pthread_mutex_unlock(&pending_skins_lock);
|
|
|
|
}
|
|
|
|
|
2023-10-21 20:36:54 +00:00
|
|
|
// Skin Server
|
|
|
|
static std::string get_skin_server() {
|
|
|
|
const char *custom_server = getenv("MCPI_SKIN_SERVER");
|
2024-04-02 23:22:01 +00:00
|
|
|
if (custom_server != nullptr) {
|
2023-10-21 20:36:54 +00:00
|
|
|
return custom_server;
|
|
|
|
} else {
|
|
|
|
return MCPI_SKIN_SERVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 05:23:34 +00:00
|
|
|
// Skin Loader
|
|
|
|
struct loader_data {
|
|
|
|
int32_t texture_id;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
static void *loader_thread(void *user_data) {
|
|
|
|
// Loader Data
|
|
|
|
loader_data *data = (loader_data *) user_data;
|
|
|
|
|
|
|
|
// Download
|
2023-10-21 20:36:54 +00:00
|
|
|
std::string url = get_skin_server() + '/' + data->name + ".png";
|
2023-10-19 05:23:34 +00:00
|
|
|
int return_code;
|
2024-04-02 23:22:01 +00:00
|
|
|
const char *command[] = {"wget", "-O", "-", url.c_str(), nullptr};
|
2023-11-11 05:44:26 +00:00
|
|
|
size_t output_size = 0;
|
|
|
|
char *output = run_command(command, &return_code, &output_size);
|
2023-10-19 05:23:34 +00:00
|
|
|
|
|
|
|
// Check Success
|
2024-04-02 23:22:01 +00:00
|
|
|
if (output != nullptr && is_exit_status_success(return_code)) {
|
2023-10-19 05:23:34 +00:00
|
|
|
// Success
|
|
|
|
DEBUG("Downloaded Skin: %s", data->name.c_str());
|
|
|
|
|
|
|
|
// Add To Pending Skins
|
|
|
|
pending_skin skin;
|
|
|
|
skin.texture_id = data->texture_id;
|
|
|
|
skin.data = output;
|
2023-11-11 05:44:26 +00:00
|
|
|
skin.size = (int) output_size;
|
2023-10-19 05:23:34 +00:00
|
|
|
pthread_mutex_lock(&pending_skins_lock);
|
|
|
|
get_pending_skins().push_back(skin);
|
|
|
|
pthread_mutex_unlock(&pending_skins_lock);
|
|
|
|
} else {
|
|
|
|
// Failure
|
|
|
|
WARN("Failed To Download Skin: %s", data->name.c_str());
|
|
|
|
free(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free
|
|
|
|
delete data;
|
2024-04-02 23:22:01 +00:00
|
|
|
return nullptr;
|
2023-10-19 05:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Intercept Texture Creation
|
2024-04-03 07:19:12 +00:00
|
|
|
static int32_t Textures_assignTexture_injection(Textures_assignTexture_t original, Textures *textures, std::string *name, unsigned char *data) {
|
2023-10-19 05:23:34 +00:00
|
|
|
// Call Original Method
|
2024-04-03 07:19:12 +00:00
|
|
|
int32_t id = original(textures, name, data);
|
2023-10-19 05:23:34 +00:00
|
|
|
|
|
|
|
// Load Skin
|
2024-01-06 11:30:23 +00:00
|
|
|
if (starts_with(name->c_str(), "$")) {
|
2023-10-19 05:23:34 +00:00
|
|
|
loader_data *user_data = new loader_data;
|
2024-01-06 11:30:23 +00:00
|
|
|
user_data->name = name->substr(1);
|
2023-10-19 05:23:34 +00:00
|
|
|
DEBUG("Loading Skin: %s", user_data->name.c_str());
|
|
|
|
user_data->texture_id = id;
|
|
|
|
// Start Thread
|
|
|
|
pthread_t thread;
|
2024-04-02 23:22:01 +00:00
|
|
|
pthread_create(&thread, nullptr, loader_thread, (void *) user_data);
|
2023-10-19 05:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init
|
|
|
|
void _init_skin_loader() {
|
|
|
|
// Intercept Texture Creation
|
2024-04-03 07:19:12 +00:00
|
|
|
overwrite_calls(Textures_assignTexture, Textures_assignTexture_injection);
|
2023-10-19 05:23:34 +00:00
|
|
|
// Pending Skins
|
|
|
|
misc_run_on_tick(load_pending_skins);
|
2023-10-21 20:36:54 +00:00
|
|
|
// Log
|
|
|
|
DEBUG("Skin Server: %s", get_skin_server().c_str());
|
2023-10-19 05:23:34 +00:00
|
|
|
}
|