From fadcf3a7d0a40fde225773bde3f0af9f38cb9b4f Mon Sep 17 00:00:00 2001 From: Kolyah35 Date: Sun, 15 Mar 2026 20:35:02 +0300 Subject: [PATCH] FIX: i fucking wrote profiler just to find that sleep function takes SECONDS arg :sob::sob::sob::sob::sob::sob::sob::sob::sob::sob: --- CMakeLists.txt | 137 +++++------------------- project/dedicated_server/CMakeLists.txt | 1 + src/client/Minecraft.cpp | 94 ++++++---------- src/client/Minecraft.h | 7 +- src/client/player/LocalPlayer.cpp | 9 ++ src/main_dedicated.cpp | 4 +- src/util/PerfTimer.h | 6 ++ src/util/ServerProfiler.cpp | 4 + src/util/ServerProfiler.h | 43 ++++++++ 9 files changed, 133 insertions(+), 172 deletions(-) create mode 100644 src/util/ServerProfiler.cpp create mode 100644 src/util/ServerProfiler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c89e29e..e04f344 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,9 @@ set(CMAKE_POLICY_VERSION_MINIMUM 3.10) 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") endif() + find_package(Threads REQUIRED) + CPMAddPackage("gh:madler/zlib@1.3.2") CPMAddPackage( NAME "libpng" @@ -44,8 +46,10 @@ CPMAddPackage( "GLFW_BUILD_DOCS OFF" "BUILD_SHARED_LIBS ON" ) + # TODO: Clear this paths with * file(GLOB SERVER_SOURCES + "project/lib_projects/raknet/jni/RaknetSources/*.cpp" "src/NinecraftApp.cpp" "src/Performance.cpp" "src/SharedConstants.cpp" @@ -53,8 +57,10 @@ file(GLOB SERVER_SOURCES "src/client/IConfigListener.cpp" "src/client/Minecraft.cpp" "src/client/OptionStrings.cpp" + "src/client/Option.cpp" "src/client/Options.cpp" "src/client/OptionsFile.cpp" + "src/client/ServerProfiler.cpp" "src/client/gamemode/CreativeMode.cpp" "src/client/gamemode/GameMode.cpp" @@ -99,38 +105,15 @@ file(GLOB SERVER_SOURCES "src/world/Direction.cpp" - "src/world/entity/AgableMob.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/*.cpp" "src/world/entity/ai/control/MoveControl.cpp" - "src/world/entity/animal/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/animal/*.cpp" - "src/world/entity/item/FallingTile.cpp" - "src/world/entity/item/ItemEntity.cpp" - "src/world/entity/item/PrimedTnt.cpp" - "src/world/entity/item/TripodCamera.cpp" + "src/world/entity/item/*.cpp" - "src/world/entity/monster/Creeper.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/monster/*.cpp" "src/world/entity/player/Inventory.cpp" "src/world/entity/player/Player.cpp" @@ -140,39 +123,11 @@ file(GLOB SERVER_SOURCES "src/world/food/SimpleFoodData.cpp" - "src/world/inventory/BaseContainerMenu.cpp" - "src/world/inventory/ContainerMenu.cpp" - "src/world/inventory/FillingContainer.cpp" - "src/world/inventory/FurnaceMenu.cpp" + "src/world/inventory/*.cpp" + "src/world/item/*.cpp" + "src/world/item/crafting/*.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/BiomeSource.cpp" @@ -180,54 +135,21 @@ file(GLOB SERVER_SOURCES "src/world/level/dimension/Dimension.cpp" - "src/world/level/levelgen/CanyonFeature.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/*.cpp" "src/world/level/levelgen/feature/Feature.cpp" - "src/world/level/levelgen/synth/ImprovedNoise.cpp" - "src/world/level/levelgen/synth/PerlinNoise.cpp" - "src/world/level/levelgen/synth/Synth.cpp" + "src/world/level/levelgen/synth/*.cpp" "src/world/level/material/Material.cpp" "src/world/level/pathfinder/Path.cpp" - "src/world/level/storage/ExternalFileLevelStorage.cpp" - "src/world/level/storage/ExternalFileLevelStorageSource.cpp" - "src/world/level/storage/FolderMethods.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/level/storage/*.cpp" + "src/world/level/tile/*.cpp" + "src/world/level/tile/entity/*.cpp" "src/world/phys/HitResult.cpp" ) + file(GLOB CLIENT_SOURCES "src/client/*.cpp" @@ -324,10 +246,6 @@ add_executable(${PROJECT_NAME} "glad/src/glad.c" ) -#add_executable("${PROJECT_NAME}-server" -# ${SERVER_SOURCES} -#) - if(WIN32) set(EXTRA_LIBS "ws2_32") target_compile_definitions(${PROJECT_NAME} PUBLIC "_CRT_SECURE_NO_WARNINGS") @@ -347,13 +265,16 @@ target_include_directories(${PROJECT_NAME} PUBLIC ) # Server -#target_link_libraries("${PROJECT_NAME}-server" PRIVATE -# raknet ${CMAKE_THREAD_LIBS_INIT}) -#target_compile_definitions("${PROJECT_NAME}-server" PUBLIC "STANDALONE_SERVER") +add_executable("${PROJECT_NAME}-server" ${SERVER_SOURCES}) -#target_include_directories("${PROJECT_NAME}-server" PUBLIC -# "${CMAKE_SOURCE_DIR}/src/" -#) +target_compile_definitions("${PROJECT_NAME}-server" PUBLIC "STANDALONE_SERVER" "SERVER_PROFILER") + +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 target_compile_definitions(${PROJECT_NAME} PUBLIC "OPENGL_ES" "NO_EGL" ${PLATFORM}) diff --git a/project/dedicated_server/CMakeLists.txt b/project/dedicated_server/CMakeLists.txt index 96ea1a7..a2bf40b 100755 --- a/project/dedicated_server/CMakeLists.txt +++ b/project/dedicated_server/CMakeLists.txt @@ -19,6 +19,7 @@ set(CompileFiles ../../src/main.cpp ../../src/client/IConfigListener.cpp ../../src/client/Minecraft.cpp ../../src/client/Options.cpp +../../src/client/Option.cpp ../../src/client/OptionsFile.cpp ../../src/client/OptionStrings.cpp ../../src/client/gamemode/GameMode.cpp diff --git a/src/client/Minecraft.cpp b/src/client/Minecraft.cpp index 06341b9..77b3e11 100755 --- a/src/client/Minecraft.cpp +++ b/src/client/Minecraft.cpp @@ -18,19 +18,14 @@ #include "../network/ServerSideNetworkHandler.h" //#include "../network/Packet.h" #include "../world/entity/player/Inventory.h" -#include "../world/level/chunk/ChunkCache.h" #include "../world/level/tile/Tile.h" #include "../world/level/storage/LevelStorageSource.h" #include "../world/level/storage/LevelStorage.h" #include "player/input/KeyboardInput.h" +#include "world/level/chunk/ChunkSource.h" + #ifndef STANDALONE_SERVER #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 "gui/Screen.h" #include "gui/Font.h" @@ -38,65 +33,14 @@ #include "gui/screens/ConsoleScreen.h" #include "gui/screens/ChatScreen.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/ControllerTurnInput.h" -#include "player/input/XperiaPlayInput.h" - -#endif - #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 "renderer/Textures.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" -#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" -#endif -#include "../world/level/levelgen/synth/ImprovedNoise.h" -#ifndef STANDALONE_SERVER #include "renderer/tileentity/TileEntityRenderDispatcher.h" -#endif - -#ifndef STANDALONE_SERVER #include "renderer/ptexture/DynamicTexture.h" #include "renderer/GameRenderer.h" #include "renderer/ItemInHandRenderer.h" @@ -106,8 +50,40 @@ #include "gui/Font.h" #include "gui/screens/RenameMPLevelScreen.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 +#include "../network/command/CommandServer.h" +#include "gamemode/CreatorMode.h" + static void checkGlError(const char* tag) { #ifdef GLDEBUG while (1) { @@ -118,7 +94,7 @@ static void checkGlError(const char* tag) { } #endif /*GLDEBUG*/ } -#include + /*static*/ const char* Minecraft::progressMessages[] = { "Locating server", diff --git a/src/client/Minecraft.h b/src/client/Minecraft.h index 35f3a22..6e44939 100755 --- a/src/client/Minecraft.h +++ b/src/client/Minecraft.h @@ -4,13 +4,12 @@ #include "Options.h" #ifndef STANDALONE_SERVER #include "MouseHandler.h" -#endif -#include "Timer.h" -#include "player/input/ITurnInput.h" -#ifndef STANDALONE_SERVER #include "gui/Gui.h" #include "gui/screens/ScreenChooser.h" #endif + +#include "Timer.h" + //#include "../network/RakNetInstance.h" #include "../world/phys/HitResult.h" diff --git a/src/client/player/LocalPlayer.cpp b/src/client/player/LocalPlayer.cpp index ebd8296..90d06a9 100755 --- a/src/client/player/LocalPlayer.cpp +++ b/src/client/player/LocalPlayer.cpp @@ -47,6 +47,7 @@ #include "../../network/packet/PlayerArmorEquipmentPacket.h" namespace { +#ifndef STANDALONE_SERVER static bool isBase64(unsigned char c) { return (std::isalnum(c) || (c == '+') || (c == '/')); @@ -177,6 +178,8 @@ static std::string getCapeUrlForUsername(const std::string& username) { return getTextureUrlForUsername(username, "CAPE"); } +#endif + static bool ensureDirectoryExists(const std::string& path) { #if defined(_WIN32) return _mkdir(path.c_str()) == 0 || errno == EEXIST; @@ -200,6 +203,8 @@ static bool fileExists(const std::string& path) { #endif } +#ifndef STANDALONE_SERVER + static void* fetchSkinForPlayer(void* param) { LocalPlayer* player = (LocalPlayer*)param; if (!player) return NULL; @@ -290,6 +295,8 @@ static void* fetchCapeForPlayer(void* param) { return NULL; } +#endif + //@note: doesn't work completely, since it doesn't care about stairs rotation static bool isJumpable(int tileId) { return tileId != Tile::fence->id @@ -318,12 +325,14 @@ LocalPlayer::LocalPlayer(Minecraft* minecraft, Level* level, User* user, int dim this->dimension = dimension; _init(); +#ifndef STANDALONE_SERVER 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); } +#endif } LocalPlayer::~LocalPlayer() { diff --git a/src/main_dedicated.cpp b/src/main_dedicated.cpp index 2d6eb33..8aa14cb 100755 --- a/src/main_dedicated.cpp +++ b/src/main_dedicated.cpp @@ -61,15 +61,17 @@ int main(int numArguments, char* pszArgs[]) { std::cout << "Level has been generated in " << getTimeS() - startTime << std::endl; ((MAIN_CLASS*)g_app)->level->saveLevelData(); std::cout << "Level has been saved!" << std::endl; + while(!app->wantToQuit()) { app->update(); //pthread_yield(); - sleep(20); + sleepMs(20); } ((MAIN_CLASS*)g_app)->level->saveLevelData(); delete app; appContext.platform->finish(); delete appContext.platform; + std::cout << "Quit correctly" << std::endl; return g_exitCode; } \ No newline at end of file diff --git a/src/util/PerfTimer.h b/src/util/PerfTimer.h index ba96b08..9b00ae1 100755 --- a/src/util/PerfTimer.h +++ b/src/util/PerfTimer.h @@ -10,6 +10,12 @@ #define TIMER_PUSH(x) PerfTimer::push(x) #define TIMER_POP() PerfTimer::pop() #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 #define TIMER_PUSH(x) ((void*)0) #define TIMER_POP() ((void*)0) diff --git a/src/util/ServerProfiler.cpp b/src/util/ServerProfiler.cpp new file mode 100644 index 0000000..35b1250 --- /dev/null +++ b/src/util/ServerProfiler.cpp @@ -0,0 +1,4 @@ +#include "ServerProfiler.h" + +std::stack ServerProfiler::stack; +std::unordered_map ServerProfiler::times; diff --git a/src/util/ServerProfiler.h b/src/util/ServerProfiler.h new file mode 100644 index 0000000..0a8347f --- /dev/null +++ b/src/util/ServerProfiler.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace ServerProfiler { + using clock = std::chrono::high_resolution_clock; + + struct Entry { + std::string name; + clock::time_point start; + }; + + static std::stack stack; + static std::unordered_map 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(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; + } + } +}; \ No newline at end of file