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:
@@ -73,6 +73,8 @@
|
||||
#include "../util/Mth.h"
|
||||
#include "../network/packet/InteractPacket.h"
|
||||
#include "../network/packet/RespawnPacket.h"
|
||||
#include "../network/packet/AdventureSettingsPacket.h"
|
||||
#include "../network/packet/SetSpawnPositionPacket.h"
|
||||
#include "IConfigListener.h"
|
||||
#include "../world/entity/MobCategory.h"
|
||||
#include "../world/Difficulty.h"
|
||||
@@ -333,6 +335,7 @@ void Minecraft::leaveGame(bool renameLevel /*=false*/)
|
||||
|
||||
_running = false;
|
||||
#ifndef STANDALONE_SERVER
|
||||
gui.clearMessages();
|
||||
if (renameLevel) {
|
||||
setScreen(new RenameMPLevelScreen(LevelStorageSource::TempLevelId));
|
||||
}
|
||||
@@ -725,10 +728,6 @@ void Minecraft::tickInput() {
|
||||
setScreen(new ConsoleScreen());
|
||||
}
|
||||
|
||||
if (!screen && key == Keyboard::KEY_O || key == 250) {
|
||||
releaseMouse();
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_F3) {
|
||||
options.toggle(OPTIONS_RENDER_DEBUG);
|
||||
}
|
||||
@@ -742,92 +741,96 @@ void Minecraft::tickInput() {
|
||||
*/
|
||||
}
|
||||
|
||||
if (!screen && key == Keyboard::KEY_O || key == 250) {
|
||||
releaseMouse();
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_L) {
|
||||
if (key == Keyboard::KEY_F) {
|
||||
int dst = options.getIntValue(OPTIONS_VIEW_DISTANCE);
|
||||
options.set(OPTIONS_VIEW_DISTANCE, (dst + 1) % 4);
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_U) {
|
||||
onGraphicsReset();
|
||||
player->heal(100);
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_B || key == 108) // Toggle the game mode
|
||||
setIsCreativeMode(!isCreativeMode());
|
||||
|
||||
if (key == Keyboard::KEY_P) // Step forward in time
|
||||
level->setTime( level->getTime() + 1000);
|
||||
|
||||
if (key == Keyboard::KEY_G) {
|
||||
setScreen(new ArmorScreen());
|
||||
/*
|
||||
std::vector<AABB>& boxs = level->getCubes(NULL, AABB(128.1f, 73, 128.1f, 128.9f, 74.9f, 128.9f));
|
||||
LOGI("boxes: %d\n", (int)boxs.size());
|
||||
*/
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_Y) {
|
||||
textures->reloadAll();
|
||||
player->hurtTo(2);
|
||||
}
|
||||
if (key == Keyboard::KEY_Z || key == 108) {
|
||||
for (int i = 0; i < 1; ++i) {
|
||||
Mob* mob = NULL;
|
||||
int forceId = 0;//MobTypes::Sheep;
|
||||
|
||||
int types[] = {
|
||||
MobTypes::Sheep,
|
||||
MobTypes::Pig,
|
||||
MobTypes::Chicken,
|
||||
MobTypes::Cow,
|
||||
};
|
||||
|
||||
int mobType = (forceId > 0)? forceId : types[Mth::random(sizeof(types) / sizeof(int))];
|
||||
mob = MobFactory::CreateMob(mobType, level);
|
||||
|
||||
//((Animal*)mob)->setAge(-1000);
|
||||
float dx = 4 - 8 * Mth::random() + 4 * Mth::sin(Mth::DEGRAD * player->yRot);
|
||||
float dz = 4 - 8 * Mth::random() + 4 * Mth::cos(Mth::DEGRAD * player->yRot);
|
||||
if (mob && !MobSpawner::addMob(level, mob, player->x + dx, player->y, player->z + dz, Mth::random()*360, 0, true))
|
||||
delete mob;
|
||||
#ifdef CHEATS
|
||||
if (key == Keyboard::KEY_U) {
|
||||
onGraphicsReset();
|
||||
player->heal(100);
|
||||
}
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_X) {
|
||||
const EntityList& entities = level->getAllEntities();
|
||||
for (int i = entities.size()-1; i >= 0; --i) {
|
||||
Entity* e = entities[i];
|
||||
if (!e->isPlayer())
|
||||
level->removeEntity(e);
|
||||
if (key == Keyboard::KEY_B || key == 108) // Toggle the game mode
|
||||
setIsCreativeMode(!isCreativeMode());
|
||||
|
||||
if (key == Keyboard::KEY_P) // Step forward in time
|
||||
level->setTime( level->getTime() + 1000);
|
||||
|
||||
if (key == Keyboard::KEY_G) {
|
||||
setScreen(new ArmorScreen());
|
||||
/*
|
||||
std::vector<AABB>& boxs = level->getCubes(NULL, AABB(128.1f, 73, 128.1f, 128.9f, 74.9f, 128.9f));
|
||||
LOGI("boxes: %d\n", (int)boxs.size());
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_C /*|| key == 4*/) {
|
||||
player->inventory->clearInventoryWithDefault();
|
||||
// @todo: Add saving here for benchmarking
|
||||
}
|
||||
if (key == Keyboard::KEY_H) {
|
||||
setScreen( new PrerenderTilesScreen() );
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_O) {
|
||||
for (int i = Inventory::MAX_SELECTION_SIZE; i < player->inventory->getContainerSize(); ++i)
|
||||
if (player->inventory->getItem(i))
|
||||
player->inventory->dropSlot(i, false);
|
||||
}
|
||||
if (key == Keyboard::KEY_M) {
|
||||
Difficulty difficulty = (Difficulty)options.getIntValue(OPTIONS_DIFFICULTY);
|
||||
options.set(OPTIONS_DIFFICULTY, (difficulty == Difficulty::PEACEFUL)?
|
||||
Difficulty::NORMAL : Difficulty::PEACEFUL);
|
||||
//setIsCreativeMode( !isCreativeMode() );
|
||||
}
|
||||
|
||||
if (options.getBooleanValue(OPTIONS_RENDER_DEBUG)) {
|
||||
if (key >= '0' && key <= '9') {
|
||||
_perfRenderer->debugFpsMeterKeyPress(key - '0');
|
||||
if (key == Keyboard::KEY_Y) {
|
||||
textures->reloadAll();
|
||||
player->hurtTo(2);
|
||||
}
|
||||
}
|
||||
if (key == Keyboard::KEY_Z || key == 108) {
|
||||
for (int i = 0; i < 1; ++i) {
|
||||
Mob* mob = NULL;
|
||||
int forceId = 0;//MobTypes::Sheep;
|
||||
|
||||
int types[] = {
|
||||
MobTypes::Sheep,
|
||||
MobTypes::Pig,
|
||||
MobTypes::Chicken,
|
||||
MobTypes::Cow,
|
||||
};
|
||||
|
||||
int mobType = (forceId > 0)? forceId : types[Mth::random(sizeof(types) / sizeof(int))];
|
||||
mob = MobFactory::CreateMob(mobType, level);
|
||||
|
||||
//((Animal*)mob)->setAge(-1000);
|
||||
float dx = 4 - 8 * Mth::random() + 4 * Mth::sin(Mth::DEGRAD * player->yRot);
|
||||
float dz = 4 - 8 * Mth::random() + 4 * Mth::cos(Mth::DEGRAD * player->yRot);
|
||||
if (mob && !MobSpawner::addMob(level, mob, player->x + dx, player->y, player->z + dz, Mth::random()*360, 0, true))
|
||||
delete mob;
|
||||
}
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_X) {
|
||||
const EntityList& entities = level->getAllEntities();
|
||||
for (int i = entities.size()-1; i >= 0; --i) {
|
||||
Entity* e = entities[i];
|
||||
if (!e->isPlayer())
|
||||
level->removeEntity(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_C /*|| key == 4*/) {
|
||||
player->inventory->clearInventoryWithDefault();
|
||||
// @todo: Add saving here for benchmarking
|
||||
}
|
||||
if (key == Keyboard::KEY_H) {
|
||||
setScreen( new PrerenderTilesScreen() );
|
||||
}
|
||||
|
||||
if (key == Keyboard::KEY_O) {
|
||||
for (int i = Inventory::MAX_SELECTION_SIZE; i < player->inventory->getContainerSize(); ++i)
|
||||
if (player->inventory->getItem(i))
|
||||
player->inventory->dropSlot(i, false);
|
||||
}
|
||||
if (key == Keyboard::KEY_M) {
|
||||
Difficulty difficulty = (Difficulty)options.getIntValue(OPTIONS_DIFFICULTY);
|
||||
options.set(OPTIONS_DIFFICULTY, (difficulty == Difficulty::PEACEFUL)?
|
||||
Difficulty::NORMAL : Difficulty::PEACEFUL);
|
||||
//setIsCreativeMode( !isCreativeMode() );
|
||||
}
|
||||
|
||||
if (options.getBooleanValue(OPTIONS_RENDER_DEBUG)) {
|
||||
if (key >= '0' && key <= '9') {
|
||||
_perfRenderer->debugFpsMeterKeyPress(key - '0');
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef PLATFORM_DESKTOP
|
||||
|
||||
@@ -97,6 +97,8 @@ public:
|
||||
|
||||
Options(Minecraft* minecraft, const std::string& workingDirectory = "")
|
||||
: minecraft(minecraft) {
|
||||
// elements werent initialized so i was getting a garbage pointer and a crash
|
||||
m_options.fill(nullptr);
|
||||
initTable();
|
||||
load();
|
||||
}
|
||||
|
||||
@@ -135,10 +135,14 @@ void Gui::render(float a, bool mouseFree, int xMouse, int yMouse) {
|
||||
#endif
|
||||
#if defined(RPI)
|
||||
renderDebugInfo();
|
||||
#elif defined(PLATFORM_DESKTOP)
|
||||
#endif
|
||||
|
||||
if (Keyboard::isKeyDown(Keyboard::KEY_TAB)) {
|
||||
renderPlayerList(font, screenWidth, screenHeight);
|
||||
}
|
||||
|
||||
if (minecraft->options.getBooleanValue(OPTIONS_RENDER_DEBUG))
|
||||
renderDebugInfo();
|
||||
#endif
|
||||
}
|
||||
|
||||
glDisable(GL_BLEND);
|
||||
@@ -330,6 +334,11 @@ void Gui::addMessage(const std::string& _string) {
|
||||
}
|
||||
}
|
||||
|
||||
void Gui::clearMessages() {
|
||||
guiMessages.clear();
|
||||
chatScrollOffset = 0;
|
||||
}
|
||||
|
||||
void Gui::setNowPlaying(const std::string& string) {
|
||||
overlayMessageString = "Now playing: " + string;
|
||||
overlayMessageTime = 20 * 3;
|
||||
@@ -799,6 +808,83 @@ void Gui::renderDebugInfo() {
|
||||
t.endOverrideAndDraw();
|
||||
}
|
||||
|
||||
void Gui::renderPlayerList(Font* font, int screenWidth, int screenHeight) {
|
||||
// only show when in game, no other screen
|
||||
// if (!minecraft->level) return;
|
||||
|
||||
// only show the overlay while connected to a multiplayer server
|
||||
Level* level = minecraft->level;
|
||||
if (!level) return;
|
||||
if (!level->isClientSide) return;
|
||||
|
||||
std::vector<std::string> playerNames;
|
||||
playerNames.reserve(level->players.size());
|
||||
|
||||
for (Player* player : level->players) {
|
||||
if (!player) continue;
|
||||
playerNames.push_back(player->name);
|
||||
}
|
||||
|
||||
// is this check needed? if there are no players, the box won't render at all since height will be 0,
|
||||
// but maybe we want to skip rendering entirely in that case
|
||||
// if (playerNames.empty())
|
||||
// return;
|
||||
|
||||
std::sort(playerNames.begin(), playerNames.end());
|
||||
|
||||
float maxNameWidth = 0.0f;
|
||||
// find the longest name so we can size the box accordingly
|
||||
for (const std::string& name : playerNames) {
|
||||
float nameWidth = font->width(name);
|
||||
if (nameWidth > maxNameWidth)
|
||||
maxNameWidth = nameWidth;
|
||||
}
|
||||
|
||||
// player count title
|
||||
std::string titleText = "Players (" + std::to_string(playerNames.size()) + ")";
|
||||
float titleWidth = font->width(titleText);
|
||||
|
||||
if (titleWidth > maxNameWidth)
|
||||
maxNameWidth = titleWidth;
|
||||
|
||||
const float padding = 4.0f;
|
||||
const float lineHeight = (float)Font::DefaultLineHeight;
|
||||
|
||||
const float boxWidth = maxNameWidth + padding * 2;
|
||||
const float boxHeight = (playerNames.size() + 1) * lineHeight + padding * 2;
|
||||
|
||||
const float boxLeft = (screenWidth - boxWidth) / 2.0f;
|
||||
const float boxTop = 10.0f;
|
||||
const float boxRight = boxLeft + boxWidth;
|
||||
const float boxBottom = boxTop + boxHeight;
|
||||
|
||||
fill(boxLeft, boxTop, boxRight, boxBottom, 0x90000000);
|
||||
|
||||
float titleX = (screenWidth - titleWidth) / 2.0f;
|
||||
float titleY = boxTop + padding;
|
||||
|
||||
// scale the text down slightly
|
||||
// i think the gl scaling is the best for this
|
||||
// oh my god this looks really bad OH GOD
|
||||
//const float textScale = 0.8f;
|
||||
//const float invTextScale = 1.0f / textScale;
|
||||
//glPushMatrix2();
|
||||
//glScalef2(textScale, textScale, 1);
|
||||
|
||||
// draw title
|
||||
//font->draw(titleText, titleX * invTextScale, titleY * invTextScale, 0xFFFFFFFF);
|
||||
font->draw(titleText, titleX, titleY, 0xFFFFFFFF);
|
||||
|
||||
// draw player names
|
||||
// we should add ping icons here eventually, but for now just show names
|
||||
float currentY = boxTop + padding + lineHeight;
|
||||
for (const std::string& name : playerNames) {
|
||||
font->draw(name, (boxLeft + padding), currentY, 0xFFDDDDDD);
|
||||
currentY += lineHeight;
|
||||
}
|
||||
//glPopMatrix2();
|
||||
}
|
||||
|
||||
void Gui::renderSleepAnimation( const int screenWidth, const int screenHeight ) {
|
||||
int timer = minecraft->player->getSleepTimer();
|
||||
float amount = (float) timer / (float) Player::SLEEP_DURATION;
|
||||
|
||||
@@ -61,10 +61,12 @@ public:
|
||||
void renderBubbles();
|
||||
void renderHearts();
|
||||
void renderDebugInfo();
|
||||
void renderPlayerList(Font* font, int screenWidth, int screenHeight);
|
||||
|
||||
void renderProgressIndicator( const bool isTouchInterface, const int screenWidth, const int screenHeight, float a );
|
||||
|
||||
void addMessage(const std::string& string);
|
||||
void clearMessages();
|
||||
void postError(int errCode);
|
||||
|
||||
void onGraphicsReset();
|
||||
|
||||
@@ -170,22 +170,31 @@ void TouchscreenInput_TestFps::onConfigChanged(const Config& c) {
|
||||
//rebuild();
|
||||
}
|
||||
|
||||
void TouchscreenInput_TestFps::setKey( int key, bool state )
|
||||
void TouchscreenInput_TestFps::setKey(int key, bool state)
|
||||
{
|
||||
#ifdef WIN32
|
||||
//LOGI("key: %d, %d\n", key, state);
|
||||
//LOGI("key: %d, %d\n", key, state);
|
||||
|
||||
int id = -1;
|
||||
if (key == _options->keyUp.key) id = KEY_UP;
|
||||
if (key == _options->keyDown.key) id = KEY_DOWN;
|
||||
if (key == _options->keyLeft.key) id = KEY_LEFT;
|
||||
if (key == _options->keyRight.key) id = KEY_RIGHT;
|
||||
if (key == _options->keyJump.key) id = KEY_JUMP;
|
||||
if (key == _options->keySneak.key) id = KEY_SNEAK;
|
||||
if (key == _options->keyCraft.key) id = KEY_CRAFT;
|
||||
if (id >= 0) {
|
||||
_keys[id] = state;
|
||||
}
|
||||
// theres no keyUp etc???
|
||||
//if (key == _options->keyUp.key) id = KEY_UP;
|
||||
//if (key == _options->keyDown.key) id = KEY_DOWN;
|
||||
//if (key == _options->keyLeft.key) id = KEY_LEFT;
|
||||
//if (key == _options->keyRight.key) id = KEY_RIGHT;
|
||||
//if (key == _options->keyJump.key) id = KEY_JUMP;
|
||||
//if (key == _options->keySneak.key) id = KEY_SNEAK;
|
||||
//if (key == _options->keyCraft.key) id = KEY_CRAFT;
|
||||
//if (id >= 0) {
|
||||
// _keys[id] = state;
|
||||
//}
|
||||
|
||||
if (key == _options->getIntValue(OPTIONS_KEY_FORWARD)) id = KEY_UP;
|
||||
if (key == _options->getIntValue(OPTIONS_KEY_BACK)) id = KEY_DOWN;
|
||||
if (key == _options->getIntValue(OPTIONS_KEY_LEFT)) id = KEY_LEFT;
|
||||
if (key == _options->getIntValue(OPTIONS_KEY_RIGHT)) id = KEY_RIGHT;
|
||||
if (key == _options->getIntValue(OPTIONS_KEY_JUMP)) id = KEY_JUMP;
|
||||
if (key == _options->getIntValue(OPTIONS_KEY_SNEAK)) id = KEY_SNEAK;
|
||||
//if (key == _options->getIntValue(OPTIONS_KEY_CRAFT)) id = KEY_CRAFT;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -167,16 +167,16 @@ void HumanoidMobRenderer::additionalRendering(Mob* mob, float a) {
|
||||
const float depth = 1.0f;
|
||||
|
||||
// Front
|
||||
t.tex(u0, vTop); t.vertex(-halfW, 0.0f, 0.0f);
|
||||
t.tex(u1, vTop); t.vertex(halfW, 0.0f, 0.0f);
|
||||
t.tex(u1, vBottom); t.vertex(halfW, height, 0.0f);
|
||||
t.tex(u0, vBottom); t.vertex(-halfW, height, 0.0f);
|
||||
t.tex(u2, vTop); t.vertex(-halfW, 0.0f, 0.0f);
|
||||
t.tex(u3, vTop); t.vertex(halfW, 0.0f, 0.0f);
|
||||
t.tex(u3, vBottom); t.vertex(halfW, height, 0.0f);
|
||||
t.tex(u2, vBottom); t.vertex(-halfW, height, 0.0f);
|
||||
|
||||
// Back
|
||||
t.tex(u2, vTop); t.vertex(halfW, 0.0f, depth);
|
||||
t.tex(u3, vTop); t.vertex(-halfW, 0.0f, depth);
|
||||
t.tex(u3, vBottom); t.vertex(-halfW, height, depth);
|
||||
t.tex(u2, vBottom); t.vertex(halfW, height, depth);
|
||||
// Back
|
||||
t.tex(u0, vTop); t.vertex(halfW, 0.0f, depth);
|
||||
t.tex(u1, vTop); t.vertex(-halfW, 0.0f, depth);
|
||||
t.tex(u1, vBottom); t.vertex(-halfW, height, depth);
|
||||
t.tex(u0, vBottom); t.vertex(halfW, height, depth);
|
||||
|
||||
// Left
|
||||
t.tex(uL0, vTop); t.vertex(-halfW, 0.0f, depth);
|
||||
|
||||
@@ -1,68 +1,68 @@
|
||||
#ifndef NET_MINECRAFT_CLIENT_SOUND__SoundEngine_H__
|
||||
#define NET_MINECRAFT_CLIENT_SOUND__SoundEngine_H__
|
||||
|
||||
//package net.minecraft.client.sound;
|
||||
|
||||
#if defined(ANDROID) && !defined(PRE_ANDROID23)
|
||||
#include "../../platform/audio/SoundSystemSL.h"
|
||||
#elif defined(__APPLE__) || defined(PLATFORM_DESKTOP)
|
||||
#include "../../platform/audio/SoundSystemAL.h"
|
||||
#else
|
||||
#include "../../platform/audio/SoundSystem.h"
|
||||
#endif
|
||||
#include "SoundRepository.h"
|
||||
#include "../../util/Random.h"
|
||||
|
||||
class Minecraft;
|
||||
class Mob;
|
||||
class Options;
|
||||
|
||||
class SoundEngine
|
||||
{
|
||||
static const int SOUND_DISTANCE = 16;
|
||||
|
||||
#if defined(ANDROID) && !defined(PRE_ANDROID23) && !defined(RPI)
|
||||
SoundSystemSL soundSystem;
|
||||
#elif defined(__APPLE__) || defined(PLATFORM_DESKTOP)
|
||||
SoundSystemAL soundSystem;
|
||||
#else
|
||||
SoundSystem soundSystem;
|
||||
#endif
|
||||
|
||||
Options* options;
|
||||
int idCounter;
|
||||
//static bool loaded;
|
||||
Random random;
|
||||
//int noMusicDelay = random.nextInt(20 * 60 * 10);
|
||||
float _x;
|
||||
float _y;
|
||||
float _z;
|
||||
float _yRot;
|
||||
float _invMaxDistance;
|
||||
|
||||
public:
|
||||
SoundEngine(float maxDistance);
|
||||
|
||||
~SoundEngine();
|
||||
|
||||
void init(Minecraft* mc, Options* options);
|
||||
void destroy();
|
||||
|
||||
void enable(bool status);
|
||||
|
||||
void updateOptions();
|
||||
void update(Mob* player, float a);
|
||||
|
||||
void play(const std::string& name, float x, float y, float z, float volume, float pitch);
|
||||
void playUI(const std::string& name, float volume, float pitch);
|
||||
|
||||
float _getVolumeMult(float x, float y, float z);
|
||||
private:
|
||||
void loadLibrary() {}
|
||||
SoundDesc _pp(const std::string& fn);
|
||||
|
||||
SoundRepository sounds;
|
||||
Minecraft* mc;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_CLIENT_SOUND__SoundEngine_H__*/
|
||||
#ifndef NET_MINECRAFT_CLIENT_SOUND__SoundEngine_H__
|
||||
#define NET_MINECRAFT_CLIENT_SOUND__SoundEngine_H__
|
||||
|
||||
//package net.minecraft.client.sound;
|
||||
|
||||
#if defined(ANDROID) && !defined(PRE_ANDROID23)
|
||||
#include "../../platform/audio/SoundSystemSL.h"
|
||||
#elif (defined(__APPLE__) || defined(PLATFORM_DESKTOP)) && !defined(NO_SOUND)
|
||||
#include "../../platform/audio/SoundSystemAL.h"
|
||||
#else
|
||||
#include "../../platform/audio/SoundSystem.h"
|
||||
#endif
|
||||
#include "SoundRepository.h"
|
||||
#include "../../util/Random.h"
|
||||
|
||||
class Minecraft;
|
||||
class Mob;
|
||||
class Options;
|
||||
|
||||
class SoundEngine
|
||||
{
|
||||
static const int SOUND_DISTANCE = 16;
|
||||
|
||||
#if defined(ANDROID) && !defined(PRE_ANDROID23) && !defined(RPI)
|
||||
SoundSystemSL soundSystem;
|
||||
#elif (defined(__APPLE__) || defined(PLATFORM_DESKTOP)) && !defined(NO_SOUND)
|
||||
SoundSystemAL soundSystem;
|
||||
#else
|
||||
SoundSystem soundSystem;
|
||||
#endif
|
||||
|
||||
Options* options;
|
||||
int idCounter;
|
||||
//static bool loaded;
|
||||
Random random;
|
||||
//int noMusicDelay = random.nextInt(20 * 60 * 10);
|
||||
float _x;
|
||||
float _y;
|
||||
float _z;
|
||||
float _yRot;
|
||||
float _invMaxDistance;
|
||||
|
||||
public:
|
||||
SoundEngine(float maxDistance);
|
||||
|
||||
~SoundEngine();
|
||||
|
||||
void init(Minecraft* mc, Options* options);
|
||||
void destroy();
|
||||
|
||||
void enable(bool status);
|
||||
|
||||
void updateOptions();
|
||||
void update(Mob* player, float a);
|
||||
|
||||
void play(const std::string& name, float x, float y, float z, float volume, float pitch);
|
||||
void playUI(const std::string& name, float volume, float pitch);
|
||||
|
||||
float _getVolumeMult(float x, float y, float z);
|
||||
private:
|
||||
void loadLibrary() {}
|
||||
SoundDesc _pp(const std::string& fn);
|
||||
|
||||
SoundRepository sounds;
|
||||
Minecraft* mc;
|
||||
};
|
||||
|
||||
#endif /*NET_MINECRAFT_CLIENT_SOUND__SoundEngine_H__*/
|
||||
|
||||
@@ -24,6 +24,7 @@ int transformKey(int glfwkey) {
|
||||
|
||||
switch (glfwkey) {
|
||||
case GLFW_KEY_ESCAPE: return Keyboard::KEY_ESCAPE;
|
||||
case GLFW_KEY_TAB: return Keyboard::KEY_TAB;
|
||||
case GLFW_KEY_BACKSPACE: return Keyboard::KEY_BACKSPACE;
|
||||
case GLFW_KEY_LEFT_SHIFT: return Keyboard::KEY_LSHIFT;
|
||||
case GLFW_KEY_ENTER: return Keyboard::KEY_RETURN;
|
||||
|
||||
@@ -23,39 +23,57 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// if cmake detected opensll, we define HTTPCLIENT_USE_OPENSSL and include the openssl headers
|
||||
// most linux distros should have openssl right
|
||||
#if defined(HTTPCLIENT_USE_OPENSSL)
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
bool startsWith(const std::string& s, const std::string& prefix) {
|
||||
return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
|
||||
bool stringStartsWith(const std::string& str, const std::string& prefix) {
|
||||
return str.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), str.begin());
|
||||
}
|
||||
|
||||
bool parseUrl(const std::string& url, std::string& scheme, std::string& host, int& port, std::string& path);
|
||||
bool parseUrl(const std::string& url, std::string& schemeOut, std::string& hostOut, int& portOut, std::string& pathOut);
|
||||
|
||||
// forward declarations for helper functions used by https implementation
|
||||
bool resolveAndConnect(const std::string& host, int port, int& outSocket);
|
||||
bool extractStatusCode(const std::string& headers, int& outStatus);
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
bool downloadHttpsWinHttp(const std::string& url, std::vector<unsigned char>& outBody) {
|
||||
outBody.clear();
|
||||
// download an https url using windows winhttp
|
||||
// this is only used on windows because the rest of the code is a simple raw tcp http client
|
||||
bool downloadHttpsWinHttp(const std::string& url, std::vector<unsigned char>& outputBody) {
|
||||
// gotta start clear
|
||||
outputBody.clear();
|
||||
|
||||
std::string scheme, host, path;
|
||||
std::string scheme;
|
||||
std::string host;
|
||||
std::string path;
|
||||
int port = 0;
|
||||
if (!parseUrl(url, scheme, host, port, path))
|
||||
return false;
|
||||
|
||||
// WinHTTP expects the path to include the leading '/'.
|
||||
HINTERNET hSession = WinHttpOpen(L"MinecraftPE/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||
if (!hSession)
|
||||
return false;
|
||||
|
||||
HINTERNET hConnect = WinHttpConnect(hSession, std::wstring(host.begin(), host.end()).c_str(), port, 0);
|
||||
if (!hConnect) {
|
||||
WinHttpCloseHandle(hSession);
|
||||
// split into scheme/host/port/path
|
||||
if (!parseUrl(url, scheme, host, port, path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
HINTERNET hRequest = WinHttpOpenRequest(
|
||||
hConnect,
|
||||
// creating an http session
|
||||
HINTERNET session = WinHttpOpen(L"MinecraftPE/0.6.1", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||
if (!session) {
|
||||
return false;
|
||||
}
|
||||
HINTERNET connectHandle = WinHttpConnect(session, std::wstring(host.begin(), host.end()).c_str(), port, 0);
|
||||
if (!connectHandle) {
|
||||
WinHttpCloseHandle(session);
|
||||
return false;
|
||||
}
|
||||
|
||||
HINTERNET requestHandle = WinHttpOpenRequest(
|
||||
connectHandle,
|
||||
L"GET",
|
||||
std::wstring(path.begin(), path.end()).c_str(),
|
||||
NULL,
|
||||
@@ -64,109 +82,245 @@ bool downloadHttpsWinHttp(const std::string& url, std::vector<unsigned char>& ou
|
||||
WINHTTP_FLAG_SECURE
|
||||
);
|
||||
|
||||
if (!hRequest) {
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
if (!requestHandle) {
|
||||
WinHttpCloseHandle(connectHandle);
|
||||
WinHttpCloseHandle(session);
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD redirectPolicy = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS;
|
||||
WinHttpSetOption(hRequest, WINHTTP_OPTION_REDIRECT_POLICY, &redirectPolicy, sizeof(redirectPolicy));
|
||||
WinHttpSetOption(requestHandle, WINHTTP_OPTION_REDIRECT_POLICY, &redirectPolicy, sizeof(redirectPolicy));
|
||||
|
||||
BOOL result = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
|
||||
if (!result) {
|
||||
WinHttpCloseHandle(hRequest);
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
BOOL sendResult = WinHttpSendRequest(requestHandle, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
|
||||
if (!sendResult) {
|
||||
WinHttpCloseHandle(requestHandle);
|
||||
WinHttpCloseHandle(connectHandle);
|
||||
WinHttpCloseHandle(session);
|
||||
return false;
|
||||
}
|
||||
|
||||
result = WinHttpReceiveResponse(hRequest, NULL);
|
||||
if (!result) {
|
||||
WinHttpCloseHandle(hRequest);
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
BOOL receiveResult = WinHttpReceiveResponse(requestHandle, NULL);
|
||||
if (!receiveResult) {
|
||||
WinHttpCloseHandle(requestHandle);
|
||||
WinHttpCloseHandle(connectHandle);
|
||||
WinHttpCloseHandle(session);
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD bytesAvailable = 0;
|
||||
while (WinHttpQueryDataAvailable(hRequest, &bytesAvailable) && bytesAvailable > 0) {
|
||||
while (WinHttpQueryDataAvailable(requestHandle, &bytesAvailable) && bytesAvailable > 0) {
|
||||
std::vector<unsigned char> buffer(bytesAvailable);
|
||||
DWORD bytesRead = 0;
|
||||
if (!WinHttpReadData(hRequest, buffer.data(), bytesAvailable, &bytesRead) || bytesRead == 0)
|
||||
if (!WinHttpReadData(requestHandle, buffer.data(), bytesAvailable, &bytesRead) || bytesRead == 0)
|
||||
break;
|
||||
outBody.insert(outBody.end(), buffer.begin(), buffer.begin() + bytesRead);
|
||||
outputBody.insert(outputBody.end(), buffer.begin(), buffer.begin() + bytesRead);
|
||||
}
|
||||
|
||||
WinHttpCloseHandle(hRequest);
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
WinHttpCloseHandle(requestHandle);
|
||||
WinHttpCloseHandle(connectHandle);
|
||||
WinHttpCloseHandle(session);
|
||||
|
||||
return !outBody.empty();
|
||||
return !outputBody.empty();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
std::string toLower(const std::string& s) {
|
||||
std::string out = s;
|
||||
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
|
||||
return out;
|
||||
#if defined(HTTPCLIENT_USE_OPENSSL) && !defined(_WIN32)
|
||||
bool downloadHttpsOpenSSL(const std::string& url, std::vector<unsigned char>& outputBody) {
|
||||
outputBody.clear();
|
||||
|
||||
std::string scheme;
|
||||
std::string host;
|
||||
std::string path;
|
||||
int port = 0;
|
||||
|
||||
// split into scheme/host/port/path
|
||||
if (!parseUrl(url, scheme, host, port, path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int socketFd = -1;
|
||||
if (!resolveAndConnect(host, port, socketFd)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SSL_library_init();
|
||||
SSL_load_error_strings();
|
||||
OpenSSL_add_all_algorithms();
|
||||
|
||||
SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
|
||||
if (!ctx) {
|
||||
close(socketFd);
|
||||
return false;
|
||||
}
|
||||
|
||||
// do not validate certificates we donst ship ca roots.
|
||||
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr);
|
||||
|
||||
SSL* ssl = SSL_new(ctx);
|
||||
if (!ssl) {
|
||||
SSL_CTX_free(ctx);
|
||||
close(socketFd);
|
||||
return false;
|
||||
}
|
||||
|
||||
SSL_set_fd(ssl, socketFd);
|
||||
SSL_set_tlsext_host_name(ssl, host.c_str());
|
||||
|
||||
if (SSL_connect(ssl) != 1) {
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ctx);
|
||||
close(socketFd);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string httpRequest;
|
||||
httpRequest += "GET ";
|
||||
httpRequest += path;
|
||||
httpRequest += " HTTP/1.1\r\n";
|
||||
httpRequest += "Host: ";
|
||||
httpRequest += host;
|
||||
httpRequest += "\r\n";
|
||||
httpRequest += "User-Agent: MinecraftPE\r\n";
|
||||
httpRequest += "Connection: close\r\n";
|
||||
httpRequest += "\r\n";
|
||||
|
||||
if (SSL_write(ssl, httpRequest.data(), (int)httpRequest.size()) <= 0) {
|
||||
SSL_shutdown(ssl);
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ctx);
|
||||
close(socketFd);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> rawResponse;
|
||||
|
||||
const int BUFFER_SIZE = 4096;
|
||||
unsigned char buffer[BUFFER_SIZE];
|
||||
while (true) {
|
||||
int bytesRead = SSL_read(ssl, buffer, BUFFER_SIZE);
|
||||
if (bytesRead <= 0)
|
||||
break;
|
||||
rawResponse.insert(rawResponse.end(), buffer, buffer + bytesRead);
|
||||
}
|
||||
|
||||
SSL_shutdown(ssl);
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ctx);
|
||||
close(socketFd);
|
||||
|
||||
if (rawResponse.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string headerDelimiter = "\r\n\r\n";
|
||||
auto headerEndIt = std::search(rawResponse.begin(), rawResponse.end(), headerDelimiter.begin(), headerDelimiter.end());
|
||||
if (headerEndIt == rawResponse.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t headerLength = headerEndIt - rawResponse.begin();
|
||||
std::string headers(reinterpret_cast<const char*>(rawResponse.data()), headerLength);
|
||||
size_t bodyStartIndex = headerLength + headerDelimiter.size();
|
||||
|
||||
int statusCode = 0;
|
||||
if (!extractStatusCode(headers, statusCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (statusCode != 200) {
|
||||
std::string bodySnippet;
|
||||
size_t len = rawResponse.size() < 1024 ? rawResponse.size() : 1024;
|
||||
bodySnippet.assign(rawResponse.begin(), rawResponse.begin() + len);
|
||||
LOGW("[HttpClient] HTTP status %d for %s\n", statusCode, url.c_str());
|
||||
LOGW("[HttpClient] Headers:\n%s\n", headers.c_str());
|
||||
LOGW("[HttpClient] Body (up to 1024 bytes):\n%s\n", bodySnippet.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
outputBody.assign(rawResponse.begin() + bodyStartIndex, rawResponse.end());
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string toLower(const std::string& input) {
|
||||
std::string output = input;
|
||||
std::transform(output.begin(), output.end(), output.begin(), ::tolower);
|
||||
return output;
|
||||
}
|
||||
|
||||
bool parseUrl(const std::string& url, std::string& scheme, std::string& host, int& port, std::string& path) {
|
||||
scheme.clear();
|
||||
host.clear();
|
||||
path.clear();
|
||||
port = 0;
|
||||
bool parseUrl(const std::string& url, std::string& schemeOut, std::string& hostOut, int& portOut, std::string& pathOut) {
|
||||
schemeOut.clear();
|
||||
hostOut.clear();
|
||||
pathOut.clear();
|
||||
portOut = 0;
|
||||
|
||||
// Very simple URL parser.
|
||||
// url format: scheme://host[:port]/path
|
||||
auto pos = url.find("://");
|
||||
if (pos == std::string::npos) return false;
|
||||
scheme = toLower(url.substr(0, pos));
|
||||
size_t start = pos + 3;
|
||||
size_t schemeSep = url.find("://");
|
||||
if (schemeSep == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t slash = url.find('/', start);
|
||||
std::string hostPort = (slash == std::string::npos) ? url.substr(start) : url.substr(start, slash - start);
|
||||
path = (slash == std::string::npos) ? "/" : url.substr(slash);
|
||||
schemeOut = toLower(url.substr(0, schemeSep));
|
||||
size_t hostStart = schemeSep + 3;
|
||||
|
||||
size_t colon = hostPort.find(':');
|
||||
if (colon != std::string::npos) {
|
||||
host = hostPort.substr(0, colon);
|
||||
port = atoi(hostPort.c_str() + colon + 1);
|
||||
// split host/port from the path
|
||||
size_t pathStart = url.find('/', hostStart);
|
||||
std::string hostPort;
|
||||
if (pathStart == std::string::npos) {
|
||||
// no path part, so just use / as the default
|
||||
hostPort = url.substr(hostStart);
|
||||
pathOut = "/";
|
||||
} else {
|
||||
host = hostPort;
|
||||
hostPort = url.substr(hostStart, pathStart - hostStart);
|
||||
pathOut = url.substr(pathStart);
|
||||
}
|
||||
|
||||
if (scheme == "http") {
|
||||
if (port == 0) port = 80;
|
||||
} else if (scheme == "https") {
|
||||
if (port == 0) port = 443;
|
||||
// if the host includes a ":port", split it out
|
||||
size_t portSep = hostPort.find(':');
|
||||
if (portSep != std::string::npos) {
|
||||
hostOut = hostPort.substr(0, portSep);
|
||||
portOut = atoi(hostPort.c_str() + portSep + 1);
|
||||
} else {
|
||||
hostOut = hostPort;
|
||||
}
|
||||
|
||||
return !host.empty() && !scheme.empty();
|
||||
// fill in default ports for known schemes
|
||||
if (schemeOut == "http") {
|
||||
if (portOut == 0) portOut = 80;
|
||||
} else if (schemeOut == "https") {
|
||||
if (portOut == 0) portOut = 443;
|
||||
}
|
||||
|
||||
// return success only if we got at a scheme and host
|
||||
return !hostOut.empty() && !schemeOut.empty();
|
||||
}
|
||||
|
||||
bool readAll(int sockfd, std::vector<unsigned char>& out) {
|
||||
const int BUF_SIZE = 4096;
|
||||
unsigned char buffer[BUF_SIZE];
|
||||
// read all available data from a tcp socket until the connection is closed
|
||||
// data is appended to outData
|
||||
bool readAll(int socketFd, std::vector<unsigned char>& outData) {
|
||||
const int BUFFER_SIZE = 4096;
|
||||
unsigned char buffer[BUFFER_SIZE];
|
||||
|
||||
while (true) {
|
||||
int received = recv(sockfd, (char*)buffer, BUF_SIZE, 0);
|
||||
if (received <= 0)
|
||||
int bytesRead = recv(socketFd, (char*)buffer, BUFFER_SIZE, 0);
|
||||
if (bytesRead <= 0)
|
||||
break;
|
||||
out.insert(out.end(), buffer, buffer + received);
|
||||
outData.insert(outData.end(), buffer, buffer + bytesRead);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool resolveAndConnect(const std::string& host, int port, int& outSock) {
|
||||
// resolve a hostname and connect a tcp socket to the given host:port
|
||||
// on windows this also makes sure winsock is initialized
|
||||
// if successful, outSocket will contain a connected socket descriptor
|
||||
bool resolveAndConnect(const std::string& host, int port, int& outSocket) {
|
||||
#if defined(_WIN32)
|
||||
static bool initialized = false;
|
||||
if (!initialized) {
|
||||
static bool wsaStarted = false;
|
||||
if (!wsaStarted) {
|
||||
WSADATA wsaData;
|
||||
WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||
initialized = true;
|
||||
wsaStarted = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -174,60 +328,95 @@ bool resolveAndConnect(const std::string& host, int port, int& outSock) {
|
||||
struct addrinfo* result = NULL;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_family = AF_UNSPEC; // ipv4 ipv6
|
||||
hints.ai_socktype = SOCK_STREAM; // tcp
|
||||
|
||||
// getaddrinfo expects strings for port and host
|
||||
std::ostringstream portStream;
|
||||
portStream << port;
|
||||
const std::string portStr = portStream.str();
|
||||
if (getaddrinfo(host.c_str(), portStr.c_str(), &hints, &result) != 0)
|
||||
const std::string portString = portStream.str();
|
||||
|
||||
if (getaddrinfo(host.c_str(), portString.c_str(), &hints, &result) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int sock = -1;
|
||||
for (struct addrinfo* rp = result; rp != NULL; rp = rp->ai_next) {
|
||||
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if (sock < 0) continue;
|
||||
if (connect(sock, rp->ai_addr, (int)rp->ai_addrlen) == 0)
|
||||
break; // success
|
||||
int socketFd = -1;
|
||||
|
||||
// try each resolved address until we successfully connect
|
||||
for (struct addrinfo* addr = result; addr != NULL; addr = addr->ai_next) {
|
||||
socketFd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||
if (socketFd < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connect(socketFd, addr->ai_addr, (int)addr->ai_addrlen) == 0) {
|
||||
// connected! yay!
|
||||
break;
|
||||
}
|
||||
|
||||
// failed to connect, try next
|
||||
#if defined(_WIN32)
|
||||
closesocket(sock);
|
||||
closesocket(socketFd);
|
||||
#else
|
||||
close(sock);
|
||||
close(socketFd);
|
||||
#endif
|
||||
sock = -1;
|
||||
socketFd = -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
if (sock < 0)
|
||||
if (socketFd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outSock = sock;
|
||||
outSocket = socketFd;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string getHeaderValue(const std::string& headers, const std::string& key) {
|
||||
std::string lower = toLower(headers);
|
||||
std::string lowerKey = toLower(key);
|
||||
size_t pos = lower.find(lowerKey);
|
||||
if (pos == std::string::npos) return "";
|
||||
size_t colon = lower.find(':', pos + lowerKey.size());
|
||||
if (colon == std::string::npos) return "";
|
||||
size_t start = colon + 1;
|
||||
while (start < lower.size() && (lower[start] == ' ' || lower[start] == '\t')) start++;
|
||||
size_t end = lower.find('\r', start);
|
||||
if (end == std::string::npos) end = lower.find('\n', start);
|
||||
if (end == std::string::npos) end = lower.size();
|
||||
return headers.substr(start, end - start);
|
||||
std::string headersLower = toLower(headers);
|
||||
std::string keyLower = toLower(key);
|
||||
|
||||
size_t pos = headersLower.find(keyLower);
|
||||
if (pos == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
size_t colonPos = headersLower.find(':', pos + keyLower.size());
|
||||
if (colonPos == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
|
||||
size_t valueStart = colonPos + 1;
|
||||
while (valueStart < headersLower.size() && (headersLower[valueStart] == ' ' || headersLower[valueStart] == '\t')) {
|
||||
valueStart++;
|
||||
}
|
||||
|
||||
size_t valueEnd = headersLower.find('\r', valueStart);
|
||||
if (valueEnd == std::string::npos) {
|
||||
valueEnd = headersLower.find('\n', valueStart);
|
||||
}
|
||||
if (valueEnd == std::string::npos) {
|
||||
valueEnd = headersLower.size();
|
||||
}
|
||||
|
||||
return headers.substr(valueStart, valueEnd - valueStart);
|
||||
}
|
||||
|
||||
// wxtract the http status code from the first response line (foex example "HTTP/1.1 200 OK")
|
||||
// returns false on malformed responses.
|
||||
bool extractStatusCode(const std::string& headers, int& outStatus) {
|
||||
size_t pos = headers.find(" ");
|
||||
if (pos == std::string::npos) return false;
|
||||
size_t pos2 = headers.find(" ", pos + 1);
|
||||
if (pos2 == std::string::npos) return false;
|
||||
std::string code = headers.substr(pos + 1, pos2 - pos - 1);
|
||||
size_t firstSpace = headers.find(' ');
|
||||
if (firstSpace == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t secondSpace = headers.find(' ', firstSpace + 1);
|
||||
if (secondSpace == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string code = headers.substr(firstSpace + 1, secondSpace - firstSpace - 1);
|
||||
outStatus = atoi(code.c_str());
|
||||
return true;
|
||||
}
|
||||
@@ -239,96 +428,121 @@ namespace HttpClient {
|
||||
bool download(const std::string& url, std::vector<unsigned char>& outBody) {
|
||||
outBody.clear();
|
||||
|
||||
std::string currentUrl = url;
|
||||
for (int redirect = 0; redirect < 3; ++redirect) {
|
||||
std::string scheme, host, path;
|
||||
int port = 0;
|
||||
if (!parseUrl(currentUrl, scheme, host, port, path)) {
|
||||
LOGW("[HttpClient] parseUrl failed for '%s'\n", currentUrl.c_str());
|
||||
// keep a copy of the current url so we can follow redirects
|
||||
std::string urlToDownload = url;
|
||||
|
||||
// follow up to 3 redirects (301/302/307/308)
|
||||
// if a redirect is encountered we loop again with the new loaction url
|
||||
for (int redirectCount = 0; redirectCount < 3; ++redirectCount) {
|
||||
std::string urlScheme;
|
||||
std::string urlHost;
|
||||
std::string urlPath;
|
||||
int urlPort = 0;
|
||||
|
||||
// parse the url into its components so we can open a socket/start an https request
|
||||
if (!parseUrl(urlToDownload, urlScheme, urlHost, urlPort, urlPath)) {
|
||||
LOGW("[HttpClient] parseUrl failed for '%s'\n", urlToDownload.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (scheme == "https") {
|
||||
// for https we delegate to winhttp on windows, since this simple client
|
||||
// only supports plain http over raw sockets
|
||||
if (urlScheme == "https") {
|
||||
#if defined(_WIN32)
|
||||
LOGI("[HttpClient] using WinHTTP for HTTPS URL %s\n", currentUrl.c_str());
|
||||
return downloadHttpsWinHttp(currentUrl, outBody);
|
||||
LOGI("[HttpClient] using WinHTTP for HTTPS URL %s\n", urlToDownload.c_str());
|
||||
return downloadHttpsWinHttp(urlToDownload, outBody);
|
||||
#elif defined(HTTPCLIENT_USE_OPENSSL)
|
||||
LOGI("[HttpClient] using OpenSSL for HTTPS URL %s\n", urlToDownload.c_str());
|
||||
return downloadHttpsOpenSSL(urlToDownload, outBody);
|
||||
#else
|
||||
LOGW("[HttpClient] HTTPS not supported on this platform: %s\n", currentUrl.c_str());
|
||||
LOGW("[HttpClient] HTTPS not supported on this platform: %s\n", urlToDownload.c_str());
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (scheme != "http") {
|
||||
LOGW("[HttpClient] unsupported scheme '%s' for URL '%s'\n", scheme.c_str(), currentUrl.c_str());
|
||||
// we only support plain http for all non-windows platforms for nw
|
||||
if (urlScheme != "http") {
|
||||
LOGW("[HttpClient] unsupported scheme '%s' for URL '%s'\n", urlScheme.c_str(), urlToDownload.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
int sock = -1;
|
||||
if (!resolveAndConnect(host, port, sock)) {
|
||||
LOGW("[HttpClient] resolve/connect failed for %s:%d\n", host.c_str(), port);
|
||||
int socketFd = -1;
|
||||
if (!resolveAndConnect(urlHost, urlPort, socketFd)) {
|
||||
LOGW("[HttpClient] resolve/connect failed for %s:%d\n", urlHost.c_str(), urlPort);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string request = "GET " + path + " HTTP/1.1\r\n";
|
||||
request += "Host: " + host + "\r\n";
|
||||
request += "User-Agent: MinecraftPE\r\n";
|
||||
request += "Connection: close\r\n";
|
||||
request += "\r\n";
|
||||
std::string httpRequest;
|
||||
httpRequest += "GET ";
|
||||
httpRequest += urlPath;
|
||||
httpRequest += " HTTP/1.1\r\n";
|
||||
httpRequest += "Host: ";
|
||||
httpRequest += urlHost;
|
||||
httpRequest += "\r\n";
|
||||
httpRequest += "User-Agent: MinecraftPE\r\n";
|
||||
httpRequest += "Connection: close\r\n";
|
||||
httpRequest += "\r\n";
|
||||
|
||||
send(sock, request.c_str(), (int)request.size(), 0);
|
||||
send(socketFd, httpRequest.c_str(), (int)httpRequest.size(), 0);
|
||||
|
||||
std::vector<unsigned char> raw;
|
||||
readAll(sock, raw);
|
||||
std::vector<unsigned char> rawResponse;
|
||||
readAll(socketFd, rawResponse);
|
||||
|
||||
#if defined(_WIN32)
|
||||
closesocket(sock);
|
||||
closesocket(socketFd);
|
||||
#else
|
||||
close(sock);
|
||||
close(socketFd);
|
||||
#endif
|
||||
|
||||
if (raw.empty()) {
|
||||
LOGW("[HttpClient] no response data from %s\n", currentUrl.c_str());
|
||||
if (rawResponse.empty()) {
|
||||
LOGW("[HttpClient] no response data from %s\n", urlToDownload.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// split headers and body
|
||||
const std::string delim = "\r\n\r\n";
|
||||
auto it = std::search(raw.begin(), raw.end(), delim.begin(), delim.end());
|
||||
if (it == raw.end())
|
||||
// find the end of the headers (\r\n\r\n) so we can split headers and body
|
||||
const std::string headerDelimiter = "\r\n\r\n";
|
||||
auto headerEndIt = std::search(rawResponse.begin(), rawResponse.end(), headerDelimiter.begin(), headerDelimiter.end());
|
||||
if (headerEndIt == rawResponse.end()) {
|
||||
// we didn't find the end of headers :(
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t headerLen = it - raw.begin();
|
||||
std::string headers(reinterpret_cast<const char*>(raw.data()), headerLen);
|
||||
size_t bodyStart = headerLen + delim.size();
|
||||
// extract the header block as a string so we can inspect it
|
||||
size_t headerLength = headerEndIt - rawResponse.begin();
|
||||
std::string headers(reinterpret_cast<const char*>(rawResponse.data()), headerLength);
|
||||
size_t bodyStartIndex = headerLength + headerDelimiter.size();
|
||||
|
||||
int status = 0;
|
||||
if (!extractStatusCode(headers, status))
|
||||
int statusCode = 0;
|
||||
if (!extractStatusCode(headers, statusCode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status == 301 || status == 302 || status == 307 || status == 308) {
|
||||
if (statusCode == 301 || statusCode == 302 || statusCode == 307 || statusCode == 308) {
|
||||
std::string location = getHeaderValue(headers, "Location");
|
||||
if (location.empty()) {
|
||||
LOGW("[HttpClient] redirect without Location header for %s\n", currentUrl.c_str());
|
||||
LOGW("[HttpClient] redirect without Location header for %s\n", urlToDownload.c_str());
|
||||
return false;
|
||||
}
|
||||
LOGI("[HttpClient] redirect %s -> %s\n", currentUrl.c_str(), location.c_str());
|
||||
currentUrl = location;
|
||||
LOGI("[HttpClient] redirect %s -> %s\n", urlToDownload.c_str(), location.c_str());
|
||||
urlToDownload = location;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (status != 200) {
|
||||
if (statusCode != 200) {
|
||||
// if we got any status other than 200 OK, log what happened
|
||||
std::string bodySnippet;
|
||||
if (!outBody.empty()) {
|
||||
size_t len = outBody.size() < 1024 ? outBody.size() : 1024;
|
||||
bodySnippet.assign(outBody.begin(), outBody.begin() + len);
|
||||
}
|
||||
LOGW("[HttpClient] HTTP status %d for %s\n", status, currentUrl.c_str());
|
||||
LOGW("[HttpClient] HTTP status %d for %s\n", statusCode, urlToDownload.c_str());
|
||||
LOGW("[HttpClient] Headers:\n%s\n", headers.c_str());
|
||||
LOGW("[HttpClient] Body (up to 1024 bytes):\n%s\n", bodySnippet.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
outBody.assign(raw.begin() + bodyStart, raw.end());
|
||||
// everything looks good! copy just the body bytes (after the headers) into outBody.
|
||||
outBody.assign(rawResponse.begin() + bodyStartIndex, rawResponse.end());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,6 @@
|
||||
|
||||
namespace HttpClient {
|
||||
|
||||
/// Download the given URL into "outBody".
|
||||
/// Returns true if the download completed successfully (HTTP 200) and the body is in outBody.
|
||||
/// This function supports plain HTTP only (no TLS). It will follow up to 3 redirects.
|
||||
bool download(const std::string& url, std::vector<unsigned char>& outBody);
|
||||
|
||||
} // namespace HttpClient
|
||||
|
||||
@@ -56,6 +56,7 @@ public:
|
||||
static const int KEY_Z = 90;
|
||||
|
||||
static const int KEY_BACKSPACE = 8;
|
||||
static const int KEY_TAB = 9;
|
||||
static const int KEY_RETURN = 13;
|
||||
|
||||
static const int KEY_F1 = 112;
|
||||
|
||||
Reference in New Issue
Block a user