mirror of
https://gitea.sffempire.ru/Kolyah35/minecraft-pe-0.6.1.git
synced 2026-03-19 22:43:32 +00:00
Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
@@ -18,6 +18,20 @@
|
||||
#include "../../network/packet/SendInventoryPacket.h"
|
||||
#include "../../network/packet/EntityEventPacket.h"
|
||||
#include "../../network/packet/PlayerActionPacket.h"
|
||||
#include <vector>
|
||||
#include <cctype>
|
||||
#include "../../platform/log.h"
|
||||
#include "../../platform/HttpClient.h"
|
||||
#include "../../platform/CThread.h"
|
||||
#include "../../util/StringUtils.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <direct.h>
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
||||
#ifndef STANDALONE_SERVER
|
||||
#include "../gui/Screen.h"
|
||||
#include "../gui/screens/FurnaceScreen.h"
|
||||
@@ -32,6 +46,250 @@
|
||||
#include "../../world/item/ArmorItem.h"
|
||||
#include "../../network/packet/PlayerArmorEquipmentPacket.h"
|
||||
|
||||
namespace {
|
||||
|
||||
static bool isBase64(unsigned char c) {
|
||||
return (std::isalnum(c) || (c == '+') || (c == '/'));
|
||||
}
|
||||
|
||||
static std::string base64Decode(const std::string& encoded) {
|
||||
static const std::string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
std::string out;
|
||||
int in_len = (int)encoded.size();
|
||||
int i = 0;
|
||||
int in_ = 0;
|
||||
unsigned char char_array_4[4], char_array_3[3];
|
||||
|
||||
while (in_len-- && (encoded[in_] != '=') && isBase64(encoded[in_])) {
|
||||
char_array_4[i++] = encoded[in_]; in_++;
|
||||
if (i == 4) {
|
||||
for (i = 0; i < 4; i++)
|
||||
char_array_4[i] = (unsigned char)base64Chars.find(char_array_4[i]);
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
out += char_array_3[i];
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (int j = i; j < 4; j++)
|
||||
char_array_4[j] = 0;
|
||||
for (int j = 0; j < 4; j++)
|
||||
char_array_4[j] = (unsigned char)base64Chars.find(char_array_4[j]);
|
||||
|
||||
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||
|
||||
for (int j = 0; (j < i - 1); j++)
|
||||
out += char_array_3[j];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static std::string extractJsonString(const std::string& json, const std::string& key) {
|
||||
std::string search = "\"" + key + "\"";
|
||||
size_t pos = json.find(search);
|
||||
if (pos == std::string::npos) return "";
|
||||
pos = json.find(':', pos + search.size());
|
||||
if (pos == std::string::npos) return "";
|
||||
pos++;
|
||||
while (pos < json.size() && std::isspace((unsigned char)json[pos])) pos++;
|
||||
if (pos >= json.size() || json[pos] != '"') return "";
|
||||
pos++;
|
||||
size_t end = json.find('"', pos);
|
||||
if (end == std::string::npos) return "";
|
||||
return json.substr(pos, end - pos);
|
||||
}
|
||||
|
||||
static std::string getTextureUrlForUsername(const std::string& username, const std::string& textureKey) {
|
||||
if (username.empty()) {
|
||||
LOGI("[%s] username empty\n", textureKey.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
LOGI("[%s] resolving UUID for user '%s'...\n", textureKey.c_str(), username.c_str());
|
||||
std::vector<unsigned char> body;
|
||||
std::string apiUrl = "http://api.mojang.com/users/profiles/minecraft/" + username;
|
||||
if (!HttpClient::download(apiUrl, body)) {
|
||||
LOGW("[%s] failed to download UUID for %s\n", textureKey.c_str(), username.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string response(body.begin(), body.end());
|
||||
std::string uuid = extractJsonString(response, "id");
|
||||
if (uuid.empty()) {
|
||||
LOGW("[%s] no UUID found in Mojang response for %s\n", textureKey.c_str(), username.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
LOGI("[%s] UUID=%s for user %s\n", textureKey.c_str(), uuid.c_str(), username.c_str());
|
||||
|
||||
std::string profileUrl = "http://sessionserver.mojang.com/session/minecraft/profile/" + uuid;
|
||||
if (!HttpClient::download(profileUrl, body)) {
|
||||
LOGW("[%s] failed to download profile for UUID %s\n", textureKey.c_str(), uuid.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
response.assign(body.begin(), body.end());
|
||||
std::string encoded = extractJsonString(response, "value");
|
||||
if (encoded.empty()) {
|
||||
LOGW("[%s] no value field in profile response for UUID %s\n", textureKey.c_str(), uuid.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string decoded = base64Decode(encoded);
|
||||
|
||||
std::string searchKey = "\"" + textureKey + "\"";
|
||||
size_t texturePos = decoded.find(searchKey);
|
||||
if (texturePos == std::string::npos) {
|
||||
LOGW("[%s] no %s entry in decoded profile for UUID %s\n", textureKey.c_str(), textureKey.c_str(), uuid.c_str());
|
||||
return "";
|
||||
}
|
||||
size_t urlPos = decoded.find("\"url\"", texturePos);
|
||||
if (urlPos == std::string::npos) {
|
||||
LOGW("[%s] no url field under %s for UUID %s\n", textureKey.c_str(), textureKey.c_str(), uuid.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
// extract the URL value from the substring starting at urlPos
|
||||
std::string urlFragment = decoded.substr(urlPos);
|
||||
std::string textureUrl = extractJsonString(urlFragment, "url");
|
||||
if (textureUrl.empty()) {
|
||||
LOGW("[%s] failed to parse %s URL for UUID %s\n", textureKey.c_str(), textureKey.c_str(), uuid.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
LOGI("[%s] %s URL for %s: %s\n", textureKey.c_str(), textureKey.c_str(), username.c_str(), textureUrl.c_str());
|
||||
return textureUrl;
|
||||
}
|
||||
|
||||
static std::string getSkinUrlForUsername(const std::string& username) {
|
||||
return getTextureUrlForUsername(username, "SKIN");
|
||||
}
|
||||
|
||||
static std::string getCapeUrlForUsername(const std::string& username) {
|
||||
return getTextureUrlForUsername(username, "CAPE");
|
||||
}
|
||||
|
||||
static bool ensureDirectoryExists(const std::string& path) {
|
||||
#if defined(_WIN32)
|
||||
return _mkdir(path.c_str()) == 0 || errno == EEXIST;
|
||||
#else
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode))
|
||||
return true;
|
||||
return mkdir(path.c_str(), 0755) == 0 || errno == EEXIST;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool fileExists(const std::string& path) {
|
||||
struct stat st;
|
||||
if (stat(path.c_str(), &st) != 0)
|
||||
return false;
|
||||
|
||||
#if defined(_WIN32)
|
||||
return (st.st_mode & _S_IFREG) != 0;
|
||||
#else
|
||||
return S_ISREG(st.st_mode);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void* fetchSkinForPlayer(void* param) {
|
||||
LocalPlayer* player = (LocalPlayer*)param;
|
||||
if (!player) return NULL;
|
||||
|
||||
LOGI("[Skin] starting skin download for %s\n", player->name.c_str());
|
||||
|
||||
const std::string cacheDir = "data/images/skins";
|
||||
if (!ensureDirectoryExists(cacheDir)) {
|
||||
LOGW("[Skin] failed to create cache directory %s\n", cacheDir.c_str());
|
||||
}
|
||||
|
||||
std::string cacheFile = cacheDir + "/" + player->name + ".png";
|
||||
if (fileExists(cacheFile)) {
|
||||
LOGI("[Skin] using cached skin for %s\n", player->name.c_str());
|
||||
player->setTextureName("skins/" + player->name + ".png");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string skinUrl = getSkinUrlForUsername(player->name);
|
||||
if (skinUrl.empty()) {
|
||||
LOGW("[Skin] skin URL lookup failed for %s\n", player->name.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LOGI("[Skin] downloading skin from %s\n", skinUrl.c_str());
|
||||
std::vector<unsigned char> skinData;
|
||||
if (!HttpClient::download(skinUrl, skinData) || skinData.empty()) {
|
||||
LOGW("[Skin] download failed for %s\n", skinUrl.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Save to cache
|
||||
FILE* fp = fopen(cacheFile.c_str(), "wb");
|
||||
if (fp) {
|
||||
fwrite(skinData.data(), 1, skinData.size(), fp);
|
||||
fclose(fp);
|
||||
LOGI("[Skin] cached skin to %s\n", cacheFile.c_str());
|
||||
} else {
|
||||
LOGW("[Skin] failed to write skin cache %s\n", cacheFile.c_str());
|
||||
}
|
||||
|
||||
player->setTextureName("skins/" + player->name + ".png");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void* fetchCapeForPlayer(void* param) {
|
||||
LocalPlayer* player = (LocalPlayer*)param;
|
||||
if (!player) return NULL;
|
||||
|
||||
LOGI("[Cape] starting cape download for %s\n", player->name.c_str());
|
||||
|
||||
const std::string cacheDir = "data/images/capes";
|
||||
if (!ensureDirectoryExists(cacheDir)) {
|
||||
LOGW("[Cape] failed to create cache directory %s\n", cacheDir.c_str());
|
||||
}
|
||||
|
||||
std::string cacheFile = cacheDir + "/" + player->name + ".png";
|
||||
if (fileExists(cacheFile)) {
|
||||
LOGI("[Cape] using cached cape for %s\n", player->name.c_str());
|
||||
player->setCapeTextureName("capes/" + player->name + ".png");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string capeUrl = getCapeUrlForUsername(player->name);
|
||||
if (capeUrl.empty()) {
|
||||
LOGW("[Cape] cape URL lookup failed for %s\n", player->name.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LOGI("[Cape] downloading cape from %s\n", capeUrl.c_str());
|
||||
std::vector<unsigned char> capeData;
|
||||
if (!HttpClient::download(capeUrl, capeData) || capeData.empty()) {
|
||||
LOGW("[Cape] download failed for %s\n", capeUrl.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Save to cache
|
||||
FILE* fp = fopen(cacheFile.c_str(), "wb");
|
||||
if (fp) {
|
||||
fwrite(capeData.data(), 1, capeData.size(), fp);
|
||||
fclose(fp);
|
||||
LOGI("[Cape] cached cape to %s\n", cacheFile.c_str());
|
||||
} else {
|
||||
LOGW("[Cape] failed to write cape cache %s\n", cacheFile.c_str());
|
||||
}
|
||||
|
||||
player->setCapeTextureName("capes/" + player->name + ".png");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//@note: doesn't work completely, since it doesn't care about stairs rotation
|
||||
static bool isJumpable(int tileId) {
|
||||
return tileId != Tile::fence->id
|
||||
@@ -43,6 +301,8 @@ static bool isJumpable(int tileId) {
|
||||
&& (Tile::tiles[tileId] != NULL && Tile::tiles[tileId]->getRenderShape() != Tile::SHAPE_STAIRS);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
LocalPlayer::LocalPlayer(Minecraft* minecraft, Level* level, User* user, int dimension, bool isCreative)
|
||||
: Player(level, isCreative),
|
||||
minecraft(minecraft),
|
||||
@@ -58,10 +318,11 @@ LocalPlayer::LocalPlayer(Minecraft* minecraft, Level* level, User* user, int dim
|
||||
this->dimension = dimension;
|
||||
_init();
|
||||
|
||||
if (user != NULL) {
|
||||
if (user->name.length() > 0)
|
||||
//customTextureUrl = "http://s3.amazonaws.com/MinecraftSkins/" + user.name + ".png";
|
||||
this->name = user->name;
|
||||
if (user != NULL && !user->name.empty()) {
|
||||
this->name = user->name;
|
||||
// Fetch user skin and cape from Mojang servers in the background (avoids blocking the main thread)
|
||||
new CThread(fetchSkinForPlayer, this);
|
||||
new CThread(fetchCapeForPlayer, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +445,7 @@ void LocalPlayer::aiStep() {
|
||||
// Sprint: detect W double-tap
|
||||
{
|
||||
bool forwardHeld = (input->ya > 0);
|
||||
if (forwardHeld && !prevForwardHeld) {
|
||||
if (forwardHeld && !prevForwardHeld && minecraft->options.useSprinting) {
|
||||
// leading edge of W press
|
||||
if (sprintDoubleTapTimer > 0)
|
||||
sprinting = true;
|
||||
@@ -273,7 +534,7 @@ void LocalPlayer::move(float xa, float ya, float za) {
|
||||
|
||||
float newX = x, newZ = z;
|
||||
|
||||
if (autoJumpTime <= 0 && autoJumpEnabled)
|
||||
if (autoJumpTime <= 0 && minecraft->options.autoJump)
|
||||
{
|
||||
// auto-jump when crossing the middle of a tile, and the tile in the front is blocked
|
||||
bool jump = false;
|
||||
|
||||
Reference in New Issue
Block a user