-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,168 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef CONSTANTS_HPP | ||
#define CONSTANTS_HPP | ||
|
||
#define TEXTURE_SIZE 32 | ||
#define MAX_MAP_SIZE 30 | ||
|
||
#endif //CONSTANTS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "inmemoryzip.hpp" | ||
|
||
|
||
InMemoryZipFileWriter::InMemoryZipFileWriter(const QString &fileName) { | ||
file = zipOpen(qPrintable(fileName), APPEND_STATUS_CREATE); | ||
if (file == nullptr) { | ||
qDebug() << "ERROR: InMemoryZipFileWriter: could not open or create file" << fileName; | ||
} | ||
} | ||
|
||
InMemoryZipFileWriter::~InMemoryZipFileWriter() { | ||
zipClose(file, nullptr); | ||
} | ||
|
||
bool InMemoryZipFileWriter::addFileToZip(const QString &fileName, const QByteArray &data) const { | ||
if (!isValid()) { | ||
qDebug() << "ERROR: InMemoryZipFileWriter: zip file is not open"; | ||
return false; | ||
} | ||
|
||
constexpr zip_fileinfo zipInfo = {}; | ||
int err = zipOpenNewFileInZip(file, qPrintable(fileName), &zipInfo, | ||
nullptr, 0, nullptr, 0, nullptr, | ||
Z_DEFLATED, -1); | ||
|
||
if (err != ZIP_OK) { | ||
qDebug() << "ERROR: InMemoryZipFileWriter: could not open file in zip" << fileName; | ||
return false; | ||
} | ||
|
||
qDebug() << "InMemoryZipFileWriter: inserting" << fileName << "buffer size" << data.size(); | ||
|
||
err = zipWriteInFileInZip(file, data.data(), data.size()); | ||
if (err < 0) { | ||
zipCloseFileInZip(file); | ||
qDebug() << "ERROR: InMemoryZipFileWriter: could not write file in zip" << fileName; | ||
return false; | ||
} | ||
|
||
err = zipCloseFileInZip(file); | ||
if (err != ZIP_OK) { | ||
qDebug() << "ERROR: InMemoryZipFileWriter: could not close file in zip" << fileName; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
InMemoryZipFileReader::InMemoryZipFileReader(const QString &fileName) { | ||
file = unzOpen64(qPrintable(fileName)); | ||
if (file == nullptr) { | ||
qDebug() << "ERROR: InMemoryZipFileReader: could not open file" << fileName; | ||
} | ||
} | ||
|
||
InMemoryZipFileReader::~InMemoryZipFileReader() { | ||
unzClose(file); | ||
} | ||
|
||
bool InMemoryZipFileReader::extractFile(const QString &fileName, QByteArray &data) const { | ||
if (!isValid()) { | ||
qDebug() << "ERROR: InMemoryZipFileReader: zip file is not open"; | ||
return false; | ||
} | ||
|
||
if (unzLocateFile(file, qPrintable(fileName), 0) != UNZ_OK) { | ||
qDebug() << "ERROR: InMemoryZipFileReader: file not found in the zipfile: " << fileName; | ||
return false; | ||
} | ||
|
||
unz_file_info64 file_info; | ||
|
||
if (unzGetCurrentFileInfo64(file, &file_info, nullptr, 0, nullptr, 0, nullptr, 0) != UNZ_OK) { | ||
qDebug() << "ERROR: InMemoryZipFileReader: could not read file info: " << fileName; | ||
return false; | ||
} | ||
|
||
if (unzOpenCurrentFile(file) != UNZ_OK) { | ||
qDebug() << "ERROR: InMemoryZipFileReader: could not open in zip file: " << fileName; | ||
return false; | ||
} | ||
|
||
data.fill(0, static_cast<int>(file_info.uncompressed_size + 1)); | ||
|
||
qDebug() << "InMemoryZipFileReader: extracting" << fileName << "buffer size" << data.size(); | ||
|
||
const int read = unzReadCurrentFile(file, data.data(), data.size()); | ||
|
||
unzCloseCurrentFile(file); | ||
|
||
if (read < 0) { | ||
qDebug() << "ERROR: InMemoryZipFileReader: could not read file in zip" << fileName; | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#ifndef IN_MEMORY_ZIP_FILE_HANDLER_H | ||
#define IN_MEMORY_ZIP_FILE_HANDLER_H | ||
|
||
#include <QDebug> | ||
|
||
#include "unzip.h" | ||
#include "zip.h" | ||
|
||
|
||
class QByteArray; | ||
class QString; | ||
class QTextStream; | ||
|
||
|
||
/** | ||
* Handle writing contents of a ZIP file | ||
* Used to write map data and resources to .skm files | ||
*/ | ||
class InMemoryZipFileWriter { | ||
zipFile file; | ||
|
||
public: | ||
explicit InMemoryZipFileWriter(const QString &fileName); | ||
|
||
~InMemoryZipFileWriter(); | ||
|
||
bool addFileToZip(const QString &fileName, const QByteArray &data) const; | ||
|
||
/** | ||
* @brief isValid | ||
* @return true if the ZIP file was opened successfully | ||
*/ | ||
bool isValid() const { return file != nullptr; } | ||
}; | ||
|
||
/** | ||
* Handle reading contents of a ZIP file without extracting it to disk | ||
* Used to read map data and resources from .skm files | ||
* Based on https://asmaloney.com/2011/12/code/in-memory-zip-file-access-using-qt/ | ||
*/ | ||
class InMemoryZipFileReader { | ||
unzFile file; | ||
|
||
public: | ||
explicit InMemoryZipFileReader(const QString &inFileName); | ||
|
||
~InMemoryZipFileReader(); | ||
|
||
/** | ||
* @brief isValid | ||
* @return true if the ZIP file was opened successfully | ||
*/ | ||
bool isValid() const { return file != nullptr; } | ||
|
||
/** | ||
* Extract a file from the ZIP file and put the contents into a QByteArray | ||
* @param fileName name of the file in the zip to extract | ||
* @param data QByteArray to put the file contents into | ||
* @return error code | ||
*/ | ||
bool extractFile(const QString &fileName, QByteArray &data) const; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include "snakewindow.hpp" | ||
#include "screens/snakewindow.hpp" | ||
|
||
using namespace std; | ||
|
||
|
Oops, something went wrong.