FIX: i fucking wrote profiler just to find that sleep function takes SECONDS arg 😭😭😭😭😭😭😭😭😭😭

This commit is contained in:
Kolyah35
2026-03-15 20:35:02 +03:00
parent b0fa2b820c
commit fadcf3a7d0
9 changed files with 133 additions and 172 deletions

View File

@@ -9,7 +9,9 @@ set(CMAKE_POLICY_VERSION_MINIMUM 3.10)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "-Wno-c++11-narrowing -Wno-narrowing -Wno-invalid-source-encoding -Wno-reserved-user-defined-literal") set(CMAKE_CXX_FLAGS "-Wno-c++11-narrowing -Wno-narrowing -Wno-invalid-source-encoding -Wno-reserved-user-defined-literal")
endif() endif()
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
CPMAddPackage("gh:madler/zlib@1.3.2") CPMAddPackage("gh:madler/zlib@1.3.2")
CPMAddPackage( CPMAddPackage(
NAME "libpng" NAME "libpng"
@@ -44,8 +46,10 @@ CPMAddPackage(
"GLFW_BUILD_DOCS OFF" "GLFW_BUILD_DOCS OFF"
"BUILD_SHARED_LIBS ON" "BUILD_SHARED_LIBS ON"
) )
# TODO: Clear this paths with * # TODO: Clear this paths with *
file(GLOB SERVER_SOURCES file(GLOB SERVER_SOURCES
"project/lib_projects/raknet/jni/RaknetSources/*.cpp"
"src/NinecraftApp.cpp" "src/NinecraftApp.cpp"
"src/Performance.cpp" "src/Performance.cpp"
"src/SharedConstants.cpp" "src/SharedConstants.cpp"
@@ -53,8 +57,10 @@ file(GLOB SERVER_SOURCES
"src/client/IConfigListener.cpp" "src/client/IConfigListener.cpp"
"src/client/Minecraft.cpp" "src/client/Minecraft.cpp"
"src/client/OptionStrings.cpp" "src/client/OptionStrings.cpp"
"src/client/Option.cpp"
"src/client/Options.cpp" "src/client/Options.cpp"
"src/client/OptionsFile.cpp" "src/client/OptionsFile.cpp"
"src/client/ServerProfiler.cpp"
"src/client/gamemode/CreativeMode.cpp" "src/client/gamemode/CreativeMode.cpp"
"src/client/gamemode/GameMode.cpp" "src/client/gamemode/GameMode.cpp"
@@ -99,38 +105,15 @@ file(GLOB SERVER_SOURCES
"src/world/Direction.cpp" "src/world/Direction.cpp"
"src/world/entity/AgableMob.cpp" "src/world/entity/*.cpp"
"src/world/entity/Entity.cpp"
"src/world/entity/EntityFactory.cpp"
"src/world/entity/FlyingMob.cpp"
"src/world/entity/HangingEntity.cpp"
"src/world/entity/Mob.cpp"
"src/world/entity/MobCategory.cpp"
"src/world/entity/Motive.cpp"
"src/world/entity/Painting.cpp"
"src/world/entity/PathfinderMob.cpp"
"src/world/entity/SynchedEntityData.cpp"
"src/world/entity/ai/control/MoveControl.cpp" "src/world/entity/ai/control/MoveControl.cpp"
"src/world/entity/animal/Animal.cpp" "src/world/entity/animal/*.cpp"
"src/world/entity/animal/Chicken.cpp"
"src/world/entity/animal/Cow.cpp"
"src/world/entity/animal/Pig.cpp"
"src/world/entity/animal/Sheep.cpp"
"src/world/entity/animal/WaterAnimal.cpp"
"src/world/entity/item/FallingTile.cpp" "src/world/entity/item/*.cpp"
"src/world/entity/item/ItemEntity.cpp"
"src/world/entity/item/PrimedTnt.cpp"
"src/world/entity/item/TripodCamera.cpp"
"src/world/entity/monster/Creeper.cpp" "src/world/entity/monster/*.cpp"
"src/world/entity/monster/Monster.cpp"
"src/world/entity/monster/PigZombie.cpp"
"src/world/entity/monster/Skeleton.cpp"
"src/world/entity/monster/Spider.cpp"
"src/world/entity/monster/Zombie.cpp"
"src/world/entity/player/Inventory.cpp" "src/world/entity/player/Inventory.cpp"
"src/world/entity/player/Player.cpp" "src/world/entity/player/Player.cpp"
@@ -140,38 +123,10 @@ file(GLOB SERVER_SOURCES
"src/world/food/SimpleFoodData.cpp" "src/world/food/SimpleFoodData.cpp"
"src/world/inventory/BaseContainerMenu.cpp" "src/world/inventory/*.cpp"
"src/world/inventory/ContainerMenu.cpp" "src/world/item/*.cpp"
"src/world/inventory/FillingContainer.cpp" "src/world/item/crafting/*.cpp"
"src/world/inventory/FurnaceMenu.cpp" "src/world/level/*.cpp"
"src/world/item/ArmorItem.cpp"
"src/world/item/BedItem.cpp"
"src/world/item/DyePowderItem.cpp"
"src/world/item/HangingEntityItem.cpp"
"src/world/item/HatchetItem.cpp"
"src/world/item/HoeItem.cpp"
"src/world/item/Item.cpp"
"src/world/item/ItemInstance.cpp"
"src/world/item/PickaxeItem.cpp"
"src/world/item/ShovelItem.cpp"
"src/world/item/crafting/ArmorRecipes.cpp"
"src/world/item/crafting/FurnaceRecipes.cpp"
"src/world/item/crafting/OreRecipes.cpp"
"src/world/item/crafting/Recipe.cpp"
"src/world/item/crafting/Recipes.cpp"
"src/world/item/crafting/StructureRecipes.cpp"
"src/world/item/crafting/ToolRecipes.cpp"
"src/world/item/crafting/WeaponRecipes.cpp"
"src/world/level/Explosion.cpp"
"src/world/level/Level.cpp"
"src/world/level/LightLayer.cpp"
"src/world/level/LightUpdate.cpp"
"src/world/level/MobSpawner.cpp"
"src/world/level/Region.cpp"
"src/world/level/TickNextTickData.cpp"
"src/world/level/biome/Biome.cpp" "src/world/level/biome/Biome.cpp"
"src/world/level/biome/BiomeSource.cpp" "src/world/level/biome/BiomeSource.cpp"
@@ -180,54 +135,21 @@ file(GLOB SERVER_SOURCES
"src/world/level/dimension/Dimension.cpp" "src/world/level/dimension/Dimension.cpp"
"src/world/level/levelgen/CanyonFeature.cpp" "src/world/level/levelgen/*.cpp"
"src/world/level/levelgen/DungeonFeature.cpp"
"src/world/level/levelgen/LargeCaveFeature.cpp"
"src/world/level/levelgen/LargeFeature.cpp"
"src/world/level/levelgen/RandomLevelSource.cpp"
"src/world/level/levelgen/feature/Feature.cpp" "src/world/level/levelgen/feature/Feature.cpp"
"src/world/level/levelgen/synth/ImprovedNoise.cpp" "src/world/level/levelgen/synth/*.cpp"
"src/world/level/levelgen/synth/PerlinNoise.cpp"
"src/world/level/levelgen/synth/Synth.cpp"
"src/world/level/material/Material.cpp" "src/world/level/material/Material.cpp"
"src/world/level/pathfinder/Path.cpp" "src/world/level/pathfinder/Path.cpp"
"src/world/level/storage/ExternalFileLevelStorage.cpp" "src/world/level/storage/*.cpp"
"src/world/level/storage/ExternalFileLevelStorageSource.cpp" "src/world/level/tile/*.cpp"
"src/world/level/storage/FolderMethods.cpp" "src/world/level/tile/entity/*.cpp"
"src/world/level/storage/LevelData.cpp"
"src/world/level/storage/LevelStorageSource.cpp"
"src/world/level/storage/RegionFile.cpp"
"src/world/level/tile/BedTile.cpp"
"src/world/level/tile/ChestTile.cpp"
"src/world/level/tile/CropTile.cpp"
"src/world/level/tile/DoorTile.cpp"
"src/world/level/tile/EntityTile.cpp"
"src/world/level/tile/FurnaceTile.cpp"
"src/world/level/tile/GrassTile.cpp"
"src/world/level/tile/HeavyTile.cpp"
"src/world/level/tile/LightGemTile.cpp"
"src/world/level/tile/MelonTile.cpp"
"src/world/level/tile/Mushroom.cpp"
"src/world/level/tile/NetherReactor.cpp"
"src/world/level/tile/NetherReactorPattern.cpp"
"src/world/level/tile/StairTile.cpp"
"src/world/level/tile/StemTile.cpp"
"src/world/level/tile/StoneSlabTile.cpp"
"src/world/level/tile/TallGrass.cpp"
"src/world/level/tile/Tile.cpp"
"src/world/level/tile/TrapDoorTile.cpp"
"src/world/level/tile/entity/ChestTileEntity.cpp"
"src/world/level/tile/entity/FurnaceTileEntity.cpp"
"src/world/level/tile/entity/NetherReactorTileEntity.cpp"
"src/world/level/tile/entity/SignTileEntity.cpp"
"src/world/level/tile/entity/TileEntity.cpp"
"src/world/phys/HitResult.cpp" "src/world/phys/HitResult.cpp"
) )
file(GLOB CLIENT_SOURCES file(GLOB CLIENT_SOURCES
"src/client/*.cpp" "src/client/*.cpp"
@@ -324,10 +246,6 @@ add_executable(${PROJECT_NAME}
"glad/src/glad.c" "glad/src/glad.c"
) )
#add_executable("${PROJECT_NAME}-server"
# ${SERVER_SOURCES}
#)
if(WIN32) if(WIN32)
set(EXTRA_LIBS "ws2_32") set(EXTRA_LIBS "ws2_32")
target_compile_definitions(${PROJECT_NAME} PUBLIC "_CRT_SECURE_NO_WARNINGS") target_compile_definitions(${PROJECT_NAME} PUBLIC "_CRT_SECURE_NO_WARNINGS")
@@ -347,13 +265,16 @@ target_include_directories(${PROJECT_NAME} PUBLIC
) )
# Server # Server
#target_link_libraries("${PROJECT_NAME}-server" PRIVATE add_executable("${PROJECT_NAME}-server" ${SERVER_SOURCES})
# raknet ${CMAKE_THREAD_LIBS_INIT})
#target_compile_definitions("${PROJECT_NAME}-server" PUBLIC "STANDALONE_SERVER")
#target_include_directories("${PROJECT_NAME}-server" PUBLIC target_compile_definitions("${PROJECT_NAME}-server" PUBLIC "STANDALONE_SERVER" "SERVER_PROFILER")
# "${CMAKE_SOURCE_DIR}/src/"
#) target_include_directories("${PROJECT_NAME}-server" PUBLIC
"${CMAKE_SOURCE_DIR}/src/"
"project/lib_projects/raknet/jni/RaknetSources"
)
target_link_libraries("${PROJECT_NAME}-server" ${CMAKE_THREAD_LIBS_INIT} png_shared)
# Client # Client
target_compile_definitions(${PROJECT_NAME} PUBLIC "OPENGL_ES" "NO_EGL" ${PLATFORM}) target_compile_definitions(${PROJECT_NAME} PUBLIC "OPENGL_ES" "NO_EGL" ${PLATFORM})

View File

@@ -19,6 +19,7 @@ set(CompileFiles ../../src/main.cpp
../../src/client/IConfigListener.cpp ../../src/client/IConfigListener.cpp
../../src/client/Minecraft.cpp ../../src/client/Minecraft.cpp
../../src/client/Options.cpp ../../src/client/Options.cpp
../../src/client/Option.cpp
../../src/client/OptionsFile.cpp ../../src/client/OptionsFile.cpp
../../src/client/OptionStrings.cpp ../../src/client/OptionStrings.cpp
../../src/client/gamemode/GameMode.cpp ../../src/client/gamemode/GameMode.cpp

View File

@@ -18,19 +18,14 @@
#include "../network/ServerSideNetworkHandler.h" #include "../network/ServerSideNetworkHandler.h"
//#include "../network/Packet.h" //#include "../network/Packet.h"
#include "../world/entity/player/Inventory.h" #include "../world/entity/player/Inventory.h"
#include "../world/level/chunk/ChunkCache.h"
#include "../world/level/tile/Tile.h" #include "../world/level/tile/Tile.h"
#include "../world/level/storage/LevelStorageSource.h" #include "../world/level/storage/LevelStorageSource.h"
#include "../world/level/storage/LevelStorage.h" #include "../world/level/storage/LevelStorage.h"
#include "player/input/KeyboardInput.h" #include "player/input/KeyboardInput.h"
#include "world/level/chunk/ChunkSource.h"
#ifndef STANDALONE_SERVER #ifndef STANDALONE_SERVER
#include "player/input/touchscreen/TouchInputHolder.h" #include "player/input/touchscreen/TouchInputHolder.h"
#endif
#include "player/LocalPlayer.h"
#include "gamemode/CreativeMode.h"
#include "gamemode/SurvivalMode.h"
#include "player/LocalPlayer.h"
#ifndef STANDALONE_SERVER
#include "particle/ParticleEngine.h" #include "particle/ParticleEngine.h"
#include "gui/Screen.h" #include "gui/Screen.h"
#include "gui/Font.h" #include "gui/Font.h"
@@ -38,65 +33,14 @@
#include "gui/screens/ConsoleScreen.h" #include "gui/screens/ConsoleScreen.h"
#include "gui/screens/ChatScreen.h" #include "gui/screens/ChatScreen.h"
#include "sound/SoundEngine.h" #include "sound/SoundEngine.h"
#endif
#include "../platform/CThread.h"
#include "../platform/input/Mouse.h"
#include "../AppPlatform.h"
#include "../Performance.h"
#include "../LicenseCodes.h"
#include "../util/PerfTimer.h"
#include "../util/PerfRenderer.h"
#include "player/input/MouseBuildInput.h"
#include "../world/Facing.h"
#include "../network/packet/PlaceBlockPacket.h"
#include "player/input/IInputHolder.h"
#ifndef STANDALONE_SERVER
#include "player/input/touchscreen/TouchscreenInput.h" #include "player/input/touchscreen/TouchscreenInput.h"
#include "player/input/ControllerTurnInput.h"
#include "player/input/XperiaPlayInput.h"
#endif
#include "renderer/Chunk.h" #include "renderer/Chunk.h"
#include "player/input/MouseTurnInput.h"
#include "../world/entity/MobFactory.h"
#include "../world/level/MobSpawner.h"
#include "../util/Mth.h"
#include "../network/packet/InteractPacket.h"
#ifndef STANDALONE_SERVER
#include "gui/screens/PrerenderTilesScreen.h" #include "gui/screens/PrerenderTilesScreen.h"
#include "renderer/Textures.h" #include "renderer/Textures.h"
#include "gui/screens/DeathScreen.h" #include "gui/screens/DeathScreen.h"
#endif
#include "../network/packet/RespawnPacket.h"
#include "IConfigListener.h"
#include "../world/entity/MobCategory.h"
#ifndef STANDALONE_SERVER
#include "gui/screens/FurnaceScreen.h" #include "gui/screens/FurnaceScreen.h"
#endif
#include "../world/Difficulty.h"
#include "../server/ServerLevel.h"
#ifdef CREATORMODE
#include "../server/CreatorLevel.h"
#endif
#include "../network/packet/AdventureSettingsPacket.h"
#include "../network/packet/SetSpawnPositionPacket.h"
#include "../network/command/CommandServer.h"
#include "gamemode/CreatorMode.h"
#ifndef STANDALONE_SERVER
#include "gui/screens/ArmorScreen.h" #include "gui/screens/ArmorScreen.h"
#endif
#include "../world/level/levelgen/synth/ImprovedNoise.h"
#ifndef STANDALONE_SERVER
#include "renderer/tileentity/TileEntityRenderDispatcher.h" #include "renderer/tileentity/TileEntityRenderDispatcher.h"
#endif
#ifndef STANDALONE_SERVER
#include "renderer/ptexture/DynamicTexture.h" #include "renderer/ptexture/DynamicTexture.h"
#include "renderer/GameRenderer.h" #include "renderer/GameRenderer.h"
#include "renderer/ItemInHandRenderer.h" #include "renderer/ItemInHandRenderer.h"
@@ -106,8 +50,40 @@
#include "gui/Font.h" #include "gui/Font.h"
#include "gui/screens/RenameMPLevelScreen.h" #include "gui/screens/RenameMPLevelScreen.h"
#include "sound/SoundEngine.h" #include "sound/SoundEngine.h"
#endif // STANDALONE_SERVER
#include "player/LocalPlayer.h"
#include "gamemode/CreativeMode.h"
#include "gamemode/SurvivalMode.h"
#include "player/LocalPlayer.h"
#include "../platform/CThread.h"
#include "../platform/input/Mouse.h"
#include "../AppPlatform.h"
#include "../LicenseCodes.h"
#include "../util/PerfTimer.h"
#include "../util/PerfRenderer.h"
#include "player/input/MouseBuildInput.h"
#include "player/input/IInputHolder.h"
#include "player/input/MouseTurnInput.h"
#include "../world/entity/MobFactory.h"
#include "../world/level/MobSpawner.h"
#include "../util/Mth.h"
#include "../network/packet/InteractPacket.h"
#include "../network/packet/RespawnPacket.h"
#include "IConfigListener.h"
#include "../world/entity/MobCategory.h"
#include "../world/Difficulty.h"
#include "../server/ServerLevel.h"
#ifdef CREATORMODE
#include "../server/CreatorLevel.h"
#endif #endif
#include "../network/command/CommandServer.h"
#include "gamemode/CreatorMode.h"
static void checkGlError(const char* tag) { static void checkGlError(const char* tag) {
#ifdef GLDEBUG #ifdef GLDEBUG
while (1) { while (1) {
@@ -118,7 +94,7 @@ static void checkGlError(const char* tag) {
} }
#endif /*GLDEBUG*/ #endif /*GLDEBUG*/
} }
#include <fstream>
/*static*/ /*static*/
const char* Minecraft::progressMessages[] = { const char* Minecraft::progressMessages[] = {
"Locating server", "Locating server",

View File

@@ -4,13 +4,12 @@
#include "Options.h" #include "Options.h"
#ifndef STANDALONE_SERVER #ifndef STANDALONE_SERVER
#include "MouseHandler.h" #include "MouseHandler.h"
#endif
#include "Timer.h"
#include "player/input/ITurnInput.h"
#ifndef STANDALONE_SERVER
#include "gui/Gui.h" #include "gui/Gui.h"
#include "gui/screens/ScreenChooser.h" #include "gui/screens/ScreenChooser.h"
#endif #endif
#include "Timer.h"
//#include "../network/RakNetInstance.h" //#include "../network/RakNetInstance.h"
#include "../world/phys/HitResult.h" #include "../world/phys/HitResult.h"

View File

@@ -47,6 +47,7 @@
#include "../../network/packet/PlayerArmorEquipmentPacket.h" #include "../../network/packet/PlayerArmorEquipmentPacket.h"
namespace { namespace {
#ifndef STANDALONE_SERVER
static bool isBase64(unsigned char c) { static bool isBase64(unsigned char c) {
return (std::isalnum(c) || (c == '+') || (c == '/')); return (std::isalnum(c) || (c == '+') || (c == '/'));
@@ -177,6 +178,8 @@ static std::string getCapeUrlForUsername(const std::string& username) {
return getTextureUrlForUsername(username, "CAPE"); return getTextureUrlForUsername(username, "CAPE");
} }
#endif
static bool ensureDirectoryExists(const std::string& path) { static bool ensureDirectoryExists(const std::string& path) {
#if defined(_WIN32) #if defined(_WIN32)
return _mkdir(path.c_str()) == 0 || errno == EEXIST; return _mkdir(path.c_str()) == 0 || errno == EEXIST;
@@ -200,6 +203,8 @@ static bool fileExists(const std::string& path) {
#endif #endif
} }
#ifndef STANDALONE_SERVER
static void* fetchSkinForPlayer(void* param) { static void* fetchSkinForPlayer(void* param) {
LocalPlayer* player = (LocalPlayer*)param; LocalPlayer* player = (LocalPlayer*)param;
if (!player) return NULL; if (!player) return NULL;
@@ -290,6 +295,8 @@ static void* fetchCapeForPlayer(void* param) {
return NULL; return NULL;
} }
#endif
//@note: doesn't work completely, since it doesn't care about stairs rotation //@note: doesn't work completely, since it doesn't care about stairs rotation
static bool isJumpable(int tileId) { static bool isJumpable(int tileId) {
return tileId != Tile::fence->id return tileId != Tile::fence->id
@@ -318,12 +325,14 @@ LocalPlayer::LocalPlayer(Minecraft* minecraft, Level* level, User* user, int dim
this->dimension = dimension; this->dimension = dimension;
_init(); _init();
#ifndef STANDALONE_SERVER
if (user != NULL && !user->name.empty()) { if (user != NULL && !user->name.empty()) {
this->name = user->name; this->name = user->name;
// Fetch user skin and cape from Mojang servers in the background (avoids blocking the main thread) // Fetch user skin and cape from Mojang servers in the background (avoids blocking the main thread)
new CThread(fetchSkinForPlayer, this); new CThread(fetchSkinForPlayer, this);
new CThread(fetchCapeForPlayer, this); new CThread(fetchCapeForPlayer, this);
} }
#endif
} }
LocalPlayer::~LocalPlayer() { LocalPlayer::~LocalPlayer() {

View File

@@ -61,15 +61,17 @@ int main(int numArguments, char* pszArgs[]) {
std::cout << "Level has been generated in " << getTimeS() - startTime << std::endl; std::cout << "Level has been generated in " << getTimeS() - startTime << std::endl;
((MAIN_CLASS*)g_app)->level->saveLevelData(); ((MAIN_CLASS*)g_app)->level->saveLevelData();
std::cout << "Level has been saved!" << std::endl; std::cout << "Level has been saved!" << std::endl;
while(!app->wantToQuit()) { while(!app->wantToQuit()) {
app->update(); app->update();
//pthread_yield(); //pthread_yield();
sleep(20); sleepMs(20);
} }
((MAIN_CLASS*)g_app)->level->saveLevelData(); ((MAIN_CLASS*)g_app)->level->saveLevelData();
delete app; delete app;
appContext.platform->finish(); appContext.platform->finish();
delete appContext.platform; delete appContext.platform;
std::cout << "Quit correctly" << std::endl; std::cout << "Quit correctly" << std::endl;
return g_exitCode; return g_exitCode;
} }

View File

@@ -10,6 +10,12 @@
#define TIMER_PUSH(x) PerfTimer::push(x) #define TIMER_PUSH(x) PerfTimer::push(x)
#define TIMER_POP() PerfTimer::pop() #define TIMER_POP() PerfTimer::pop()
#define TIMER_POP_PUSH(x) PerfTimer::popPush(x) #define TIMER_POP_PUSH(x) PerfTimer::popPush(x)
#elif defined(SERVER_PROFILER)
#include "ServerProfiler.h"
#define TIMER_PUSH(x) ServerProfiler::push(x)
#define TIMER_POP() ServerProfiler::pop()
#define TIMER_POP_PUSH(x) ServerProfiler::popPush(x)
#else #else
#define TIMER_PUSH(x) ((void*)0) #define TIMER_PUSH(x) ((void*)0)
#define TIMER_POP() ((void*)0) #define TIMER_POP() ((void*)0)

View File

@@ -0,0 +1,4 @@
#include "ServerProfiler.h"
std::stack<ServerProfiler::Entry> ServerProfiler::stack;
std::unordered_map<std::string, double> ServerProfiler::times;

43
src/util/ServerProfiler.h Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include <chrono>
#include <ratio>
#include <iostream>
#include <string>
#include <unordered_map>
#include <stack>
namespace ServerProfiler {
using clock = std::chrono::high_resolution_clock;
struct Entry {
std::string name;
clock::time_point start;
};
static std::stack<Entry> stack;
static std::unordered_map<std::string, double> times;
inline void push(const std::string& name) {
stack.push({name, clock::now()});
}
inline void pop() {
auto end = clock::now();
auto e = stack.top();
stack.pop();
double dt = std::chrono::duration<double, std::micro>(end - e.start).count();
times[e.name] += dt;
}
inline void popPush(const std::string& name) {
pop(); push(name);
}
inline void report() {
for (auto& it : times) {
std::cout << "[PROFILER] " << it.first << ": " << it.second << std::endl;
}
}
};