Skip to content

Commit 6b23e6f

Browse files
authored
[Enhancement] - Automatically unzip world.zip in world folder if otbm 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"
1 parent 0c08898 commit 6b23e6f

File tree

7 files changed

+53
-2
lines changed

7 files changed

+53
-2
lines changed

.github/workflows/build-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
packages: >
3030
boost-asio boost-iostreams boost-system boost-filesystem
3131
boost-variant boost-lockfree cryptopp curl jsoncpp
32-
luajit libmariadb pugixml spdlog
32+
luajit libmariadb pugixml spdlog libzippp
3333
3434
steps:
3535
- name: Checkout repository

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ if (MSVC)
8383
find_package(PugiXML REQUIRED)
8484
find_package(spdlog CONFIG REQUIRED)
8585
find_package(Threads REQUIRED)
86+
find_package(libzippp CONFIG REQUIRED)
8687
else()
8788
find_package(Boost REQUIRED COMPONENTS system filesystem iostreams date_time)
8889
find_package(cryptopp CONFIG REQUIRED)
@@ -93,6 +94,7 @@ else()
9394
find_package(spdlog CONFIG REQUIRED)
9495
find_package(Threads REQUIRED)
9596
find_package(unofficial-libmariadb CONFIG REQUIRED)
97+
find_package(libzippp CONFIG REQUIRED)
9698
endif (MSVC)
9799

98100
include(GNUInstallDirs)
@@ -265,6 +267,7 @@ target_link_libraries(${PROJECT_NAME}
265267
${CURL_LIBRARIES}
266268
jsoncpp_lib
267269
spdlog::spdlog
270+
libzippp::libzippp
268271
)
269272

270273
else()
@@ -290,6 +293,7 @@ else()
290293
spdlog::spdlog
291294
Threads::Threads
292295
${LUA_LIBRARIES}
296+
libzippp::libzippp
293297
)
294298

295299
endif (MSVC)

src/map/map.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919

2020
#include "otpch.h"
2121

22+
#include <boost/filesystem.hpp>
23+
#include "libzippp.h"
24+
2225
#include "io/iomap.h"
2326
#include "io/iomapserialize.h"
2427
#include "creatures/combat/combat.h"
2528
#include "creatures/creature.h"
2629
#include "game/game.h"
2730
#include "creatures/monsters/monster.h"
2831
#include "creatures/npcs/npc.h"
32+
#include "utils/tools.h"
2933

3034
extern Game g_game;
3135

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

45+
bool Map::extractMap(const std::string& identifier) const {
46+
if (boost::filesystem::exists(identifier)) {
47+
return true;
48+
}
49+
50+
using namespace libzippp;
51+
std::string mapName = g_configManager().getString(MAP_NAME) + ".otbm";
52+
SPDLOG_INFO("Unzipping " + mapName + " to world folder");
53+
ZipArchive zf("data/world/world.zip");
54+
55+
if (!zf.open(ZipArchive::ReadOnly)) {
56+
SPDLOG_ERROR("[Map::extractMap] - Failed to unzip world.zip, file doesn't exist");
57+
consoleHandlerExit();
58+
return false;
59+
}
60+
61+
std::ofstream unzippedFile("data/world/" + mapName, std::ofstream::binary);
62+
zf.getEntry(mapName).readContent(unzippedFile, ZipArchive::Current);
63+
zf.close();
64+
return true;
65+
}
66+
4167
bool Map::loadMap(const std::string& identifier, bool loadHouses, bool loadMonsters, bool loadNpcs)
4268
{
69+
// Extract the map
70+
this->extractMap(identifier);
71+
4372
// Load the map
4473
this->load(identifier);
4574

src/map/map.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ class Map
187187
*/
188188
bool load(const std::string& identifier);
189189
/**
190+
* Extract the map.
191+
* \returns true if the map was extracted successfully
192+
*/
193+
bool extractMap(const std::string& identifier) const;
194+
/**
190195
* Load the main map
191196
* \param identifier Is the main map name (name of file .otbm)
192197
* \param loadHouses if true, the main map houses is loaded

src/utils/tools.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "utils/tools.h"
2323

24-
2524
void printXMLError(const std::string& where, const std::string& fileName, const pugi::xml_parse_result& result)
2625
{
2726
SPDLOG_ERROR("[{}] Failed to load {}: {}", where, fileName, result.description());
@@ -1410,6 +1409,18 @@ void capitalizeWords(std::string& source)
14101409
}
14111410
}
14121411

1412+
/**
1413+
* @details
1414+
* Prevents the console from closing so there is time to read the error information
1415+
* Then can press any key to close
1416+
*/
1417+
void consoleHandlerExit()
1418+
{
1419+
SPDLOG_ERROR("The program will close after pressing the enter key...");
1420+
getchar();
1421+
return;
1422+
}
1423+
14131424
NameEval_t validateName(const std::string &name)
14141425
{
14151426

src/utils/tools.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ ItemAttrTypes stringToItemAttribute(const std::string& str);
105105
const char* getReturnMessage(ReturnValue value);
106106

107107
void capitalizeWords(std::string &source);
108+
void consoleHandlerExit();
108109
NameEval_t validateName(const std::string &name);
109110

110111
bool isCaskItem(uint16_t itemId);

vcpkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"curl",
1717
"jsoncpp",
1818
"cryptopp",
19+
"libzippp",
1920
{
2021
"name": "luajit",
2122
"platform": "windows"

0 commit comments

Comments
 (0)