Skip to content

Commit

Permalink
[Enhancement] - Automatically unzip world.zip in world folder if otbm…
Browse files Browse the repository at this point in the history
… file doesn't exist on server start (#205)

This change will allow the server to automatically extract the map from the zip (to the global server) in case the .otbm file is not found.

It is necessary to install "libzippp", through the command "./vcpkg install libzippp"
  • Loading branch information
omeranha authored Feb 15, 2022
1 parent 0c08898 commit 6b23e6f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
packages: >
boost-asio boost-iostreams boost-system boost-filesystem
boost-variant boost-lockfree cryptopp curl jsoncpp
luajit libmariadb pugixml spdlog
luajit libmariadb pugixml spdlog libzippp
steps:
- name: Checkout repository
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ if (MSVC)
find_package(PugiXML REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(Threads REQUIRED)
find_package(libzippp CONFIG REQUIRED)
else()
find_package(Boost REQUIRED COMPONENTS system filesystem iostreams date_time)
find_package(cryptopp CONFIG REQUIRED)
Expand All @@ -93,6 +94,7 @@ else()
find_package(spdlog CONFIG REQUIRED)
find_package(Threads REQUIRED)
find_package(unofficial-libmariadb CONFIG REQUIRED)
find_package(libzippp CONFIG REQUIRED)
endif (MSVC)

include(GNUInstallDirs)
Expand Down Expand Up @@ -265,6 +267,7 @@ target_link_libraries(${PROJECT_NAME}
${CURL_LIBRARIES}
jsoncpp_lib
spdlog::spdlog
libzippp::libzippp
)

else()
Expand All @@ -290,6 +293,7 @@ else()
spdlog::spdlog
Threads::Threads
${LUA_LIBRARIES}
libzippp::libzippp
)

endif (MSVC)
Expand Down
29 changes: 29 additions & 0 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

#include "otpch.h"

#include <boost/filesystem.hpp>
#include "libzippp.h"

#include "io/iomap.h"
#include "io/iomapserialize.h"
#include "creatures/combat/combat.h"
#include "creatures/creature.h"
#include "game/game.h"
#include "creatures/monsters/monster.h"
#include "creatures/npcs/npc.h"
#include "utils/tools.h"

extern Game g_game;

Expand All @@ -38,8 +42,33 @@ bool Map::load(const std::string& identifier) {
return true;
}

bool Map::extractMap(const std::string& identifier) const {
if (boost::filesystem::exists(identifier)) {
return true;
}

using namespace libzippp;
std::string mapName = g_configManager().getString(MAP_NAME) + ".otbm";
SPDLOG_INFO("Unzipping " + mapName + " to world folder");
ZipArchive zf("data/world/world.zip");

if (!zf.open(ZipArchive::ReadOnly)) {
SPDLOG_ERROR("[Map::extractMap] - Failed to unzip world.zip, file doesn't exist");
consoleHandlerExit();
return false;
}

std::ofstream unzippedFile("data/world/" + mapName, std::ofstream::binary);
zf.getEntry(mapName).readContent(unzippedFile, ZipArchive::Current);
zf.close();
return true;
}

bool Map::loadMap(const std::string& identifier, bool loadHouses, bool loadMonsters, bool loadNpcs)
{
// Extract the map
this->extractMap(identifier);

// Load the map
this->load(identifier);

Expand Down
5 changes: 5 additions & 0 deletions src/map/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ class Map
*/
bool load(const std::string& identifier);
/**
* Extract the map.
* \returns true if the map was extracted successfully
*/
bool extractMap(const std::string& identifier) const;
/**
* Load the main map
* \param identifier Is the main map name (name of file .otbm)
* \param loadHouses if true, the main map houses is loaded
Expand Down
13 changes: 12 additions & 1 deletion src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "utils/tools.h"


void printXMLError(const std::string& where, const std::string& fileName, const pugi::xml_parse_result& result)
{
SPDLOG_ERROR("[{}] Failed to load {}: {}", where, fileName, result.description());
Expand Down Expand Up @@ -1410,6 +1409,18 @@ void capitalizeWords(std::string& source)
}
}

/**
* @details
* Prevents the console from closing so there is time to read the error information
* Then can press any key to close
*/
void consoleHandlerExit()
{
SPDLOG_ERROR("The program will close after pressing the enter key...");
getchar();
return;
}

NameEval_t validateName(const std::string &name)
{

Expand Down
1 change: 1 addition & 0 deletions src/utils/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ ItemAttrTypes stringToItemAttribute(const std::string& str);
const char* getReturnMessage(ReturnValue value);

void capitalizeWords(std::string &source);
void consoleHandlerExit();
NameEval_t validateName(const std::string &name);

bool isCaskItem(uint16_t itemId);
Expand Down
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"curl",
"jsoncpp",
"cryptopp",
"libzippp",
{
"name": "luajit",
"platform": "windows"
Expand Down

3 comments on commit 6b23e6f

@augustorajas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uma sugestão, em vez de ser ("data/world/world.zip") o diretório direto poderia mudar o nome do arquivo via config.lua

@omeranha
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uma sugestão, em vez de ser ("data/world/world.zip") o diretório direto poderia mudar o nome do arquivo via config.lua

não há necessidade, o diretório e nome padrão do zip sempre será esse, o processo de extrair o otbm só acontecerá uma vez em uma situação normal(caso você não delete o .otbm da pasta, claro)
caso seu mapa for custom, o processo não irá acontecer.

@beats-dh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uma sugestão, em vez de ser ("data/world/world.zip") o diretório direto poderia mudar o nome do arquivo via config.lua

Não entendi bem sua sugestão mais já pega o nome do mapa pelo config.lua, esse diretório que vc citou é o mapa zipado ele sempre vai chamar "world.zip" e sempre vai ficar em "data/world/", o mapa zipado vc pode escolher qualquer nome para ele. Ex "beats.otbm" vc seta no config.lua e pode zipar esse beats.otbm como world.zip.

Please sign in to comment.