Skip to content

Commit 225d454

Browse files
committed
fix: extractZipFile is not part of Client but more generic.
This solve a crash from mainmenu while extracting the zip
1 parent ba40b39 commit 225d454

File tree

5 files changed

+72
-69
lines changed

5 files changed

+72
-69
lines changed

src/client/client.cpp

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -725,72 +725,6 @@ bool Client::loadMedia(const std::string &data, const std::string &filename,
725725
return false;
726726
}
727727

728-
bool Client::extractZipFile(const char *filename, const std::string &destination)
729-
{
730-
auto fs = m_rendering_engine->get_filesystem();
731-
732-
if (!fs->addFileArchive(filename, false, false, io::EFAT_ZIP)) {
733-
return false;
734-
}
735-
736-
sanity_check(fs->getFileArchiveCount() > 0);
737-
738-
/**********************************************************************/
739-
/* WARNING this is not threadsafe!! */
740-
/**********************************************************************/
741-
io::IFileArchive* opened_zip = fs->getFileArchive(fs->getFileArchiveCount() - 1);
742-
743-
const io::IFileList* files_in_zip = opened_zip->getFileList();
744-
745-
unsigned int number_of_files = files_in_zip->getFileCount();
746-
747-
for (unsigned int i=0; i < number_of_files; i++) {
748-
std::string fullpath = destination;
749-
fullpath += DIR_DELIM;
750-
fullpath += files_in_zip->getFullFileName(i).c_str();
751-
std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
752-
753-
if (!files_in_zip->isDirectory(i)) {
754-
if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
755-
fs->removeFileArchive(fs->getFileArchiveCount()-1);
756-
return false;
757-
}
758-
759-
io::IReadFile* toread = opened_zip->createAndOpenFile(i);
760-
761-
FILE *targetfile = fopen(fullpath.c_str(),"wb");
762-
763-
if (targetfile == NULL) {
764-
fs->removeFileArchive(fs->getFileArchiveCount()-1);
765-
return false;
766-
}
767-
768-
char read_buffer[1024];
769-
long total_read = 0;
770-
771-
while (total_read < toread->getSize()) {
772-
773-
unsigned int bytes_read =
774-
toread->read(read_buffer,sizeof(read_buffer));
775-
if ((bytes_read == 0 ) ||
776-
(fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
777-
{
778-
fclose(targetfile);
779-
fs->removeFileArchive(fs->getFileArchiveCount() - 1);
780-
return false;
781-
}
782-
total_read += bytes_read;
783-
}
784-
785-
fclose(targetfile);
786-
}
787-
788-
}
789-
790-
fs->removeFileArchive(fs->getFileArchiveCount() - 1);
791-
return true;
792-
}
793-
794728
// Virtual methods from con::PeerHandler
795729
void Client::peerAdded(con::Peer *peer)
796730
{

src/client/client.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
384384
bool loadMedia(const std::string &data, const std::string &filename,
385385
bool from_media_push = false);
386386

387-
bool extractZipFile(const char *filename, const std::string &destination);
388-
389387
// Send a request for conventional media transfer
390388
void request_media(const std::vector<std::string> &file_requests);
391389

src/filesys.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,70 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
727727
return true;
728728
}
729729

730+
bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination)
731+
{
732+
if (!fs->addFileArchive(filename, false, false, io::EFAT_ZIP)) {
733+
return false;
734+
}
735+
736+
sanity_check(fs->getFileArchiveCount() > 0);
737+
738+
/**********************************************************************/
739+
/* WARNING this is not threadsafe!! */
740+
/**********************************************************************/
741+
io::IFileArchive* opened_zip = fs->getFileArchive(fs->getFileArchiveCount() - 1);
742+
743+
const io::IFileList* files_in_zip = opened_zip->getFileList();
744+
745+
unsigned int number_of_files = files_in_zip->getFileCount();
746+
747+
for (unsigned int i=0; i < number_of_files; i++) {
748+
std::string fullpath = destination;
749+
fullpath += DIR_DELIM;
750+
fullpath += files_in_zip->getFullFileName(i).c_str();
751+
std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
752+
753+
if (!files_in_zip->isDirectory(i)) {
754+
if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
755+
fs->removeFileArchive(fs->getFileArchiveCount()-1);
756+
return false;
757+
}
758+
759+
io::IReadFile* toread = opened_zip->createAndOpenFile(i);
760+
761+
FILE *targetfile = fopen(fullpath.c_str(),"wb");
762+
763+
if (targetfile == NULL) {
764+
fs->removeFileArchive(fs->getFileArchiveCount()-1);
765+
return false;
766+
}
767+
768+
char read_buffer[1024];
769+
long total_read = 0;
770+
771+
while (total_read < toread->getSize()) {
772+
773+
unsigned int bytes_read =
774+
toread->read(read_buffer,sizeof(read_buffer));
775+
if ((bytes_read == 0 ) ||
776+
(fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
777+
{
778+
fclose(targetfile);
779+
fs->removeFileArchive(fs->getFileArchiveCount() - 1);
780+
return false;
781+
}
782+
total_read += bytes_read;
783+
}
784+
785+
fclose(targetfile);
786+
}
787+
788+
}
789+
790+
fs->removeFileArchive(fs->getFileArchiveCount() - 1);
791+
return true;
792+
}
793+
730794
bool ReadFile(const std::string &path, std::string &out)
731795
{
732796
std::ifstream is(path, std::ios::binary | std::ios::ate);

src/filesys.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3636
#define PATH_DELIM ":"
3737
#endif
3838

39+
namespace irr { namespace io {
40+
class IFileSystem;
41+
}}
42+
3943
namespace fs
4044
{
4145

@@ -125,6 +129,8 @@ const char *GetFilenameFromPath(const char *path);
125129

126130
bool safeWriteToFile(const std::string &path, const std::string &content);
127131

132+
bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
133+
128134
bool ReadFile(const std::string &path, std::string &out);
129135

130136
bool Rename(const std::string &from, const std::string &to);

src/script/lua_api/l_mainmenu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,9 @@ int ModApiMainMenu::l_extract_zip(lua_State *L)
628628
std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
629629

630630
if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
631+
auto rendering_engine = getGuiEngine(L)->m_rendering_engine;
631632
fs::CreateAllDirs(absolute_destination);
632-
lua_pushboolean(L, getClient(L)->extractZipFile(zipfile, destination));
633+
lua_pushboolean(L, fs::extractZipFile(rendering_engine->get_filesystem(), zipfile, destination));
633634
return 1;
634635
}
635636

0 commit comments

Comments
 (0)