Skip to content

Commit c0807ef

Browse files
committed
Add unpack whole file
Update path value for pack
1 parent 8fbd36e commit c0807ef

5 files changed

Lines changed: 54 additions & 33 deletions

File tree

includes/unpack/UnPackFile.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88

99
#include <string>
1010

11+
#include "Chunk.hpp"
12+
1113
namespace sw {
1214

1315
class UnPackFile {
1416
public:
15-
explicit UnPackFile(std::string path);
17+
explicit UnPackFile(std::string path, std::string outputPath = "./");
1618
private:
19+
std::fstream m_file;
1720

21+
void readChunk(std::string& outputPath);
22+
void createFile(sw::chunkHeader &chunkHeader, std::string path, std::string& outputPath);
1823
};
1924

2025
} // sw

sources/file/File.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ sw::filePackHeader sw::File::m_header{};
1313

1414
void sw::File::createHeader()
1515
{
16-
m_header.id[0] = 's';
17-
m_header.id[1] = 'w';
18-
m_header.id[2] = 'f';
19-
m_header.id[3] = 'p';
16+
m_header.id[0] = 'S';
17+
m_header.id[1] = 'W';
18+
m_header.id[2] = 'F';
19+
m_header.id[3] = 'P';
2020
m_header.version = 100;
2121
m_header.fileType = DEVPACK;
2222
}

sources/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
int main(int argc, char** argv)
1717
{
18-
if (argc != 2) {
18+
if (argc < 2) {
1919
std::cerr << "ERROR: Missing arguments" << std::endl;
2020
return 1;
2121
}
@@ -27,7 +27,7 @@ int main(int argc, char** argv)
2727
sw::Packer::startPackaging(argv[1]);
2828
sw::File::saveFile();
2929
#elif SWFP_UNPACKER
30-
sw::UnPackFile file{argv[1]};
30+
sw::UnPackFile file{argv[1], (argc >= 3 ? argv[2] : "./")};
3131
#endif
3232
return 0;
3333
}

sources/pack/Packer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ void sw::Packer::createChunkData(std::string path, sw::chunkHeader& header, sw::
4747
file.seekg(0, std::ios::beg);
4848
file.read(buffer.data(), size);
4949

50-
data.path = (char *)std::malloc(path.size());
51-
std::memset(data.path, '\0', path.size());
52-
std::memcpy(data.path, path.data(), path.size());
53-
data.pathCount = path.size();
50+
data.path = (char *)std::malloc(path.size() - sw::Packer::path.size() + 1);
51+
std::memset(data.path, '\0', path.size() - sw::Packer::path.size() + 1);
52+
std::memcpy(data.path, path.data() + sw::Packer::path.size(), path.size() - sw::Packer::path.size());
53+
data.pathCount = path.size() - sw::Packer::path.size();
5454
fillProps(data);
5555
data.data = std::malloc(size);
5656
std::memcpy(data.data, buffer.data(), size);

sources/unpack/UnPackFile.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,57 @@
55

66
#include <fstream>
77
#include <iostream>
8+
#include <filesystem>
89

910
#include "UnPackFile.hpp"
1011
#include "File.hpp"
11-
#include "Chunk.hpp"
1212

13-
sw::UnPackFile::UnPackFile(std::string path)
13+
sw::UnPackFile::UnPackFile(std::string path, std::string outputPath)
1414
{
15-
std::fstream file;
16-
17-
file.open(path, std::ios::binary | std::ios::in);
15+
m_file.open(path, std::ios::binary | std::ios::in);
1816
char *buf = (char *)malloc(sizeof(filePackHeader));
1917

20-
file.read(buf, sizeof(filePackHeader));
18+
m_file.read(buf, sizeof(filePackHeader));
19+
20+
std::string id = "SWFP";
21+
for (int i = 0; i < 4; i++)
22+
if (((filePackHeader*)buf)->id[i] != id[i]) {
23+
std::cerr << "Incompatible/Corrupted file" << std::endl;
24+
return;
25+
}
2126

22-
std::cout << "Id: " << ((filePackHeader*)buf)->id << std::endl;
23-
std::cout << "Version: " << ((filePackHeader*)buf)->version << std::endl;
24-
std::cout << "Count: " << ((filePackHeader*)buf)->resourcesCount << std::endl << std::endl;
27+
if (((filePackHeader*)buf)->version != 100) {
28+
std::cerr << "Incompatible version " << ((filePackHeader*)buf)->version << " supported version: 100" << std::endl;
29+
return;
30+
}
2531

32+
for (int i = 0 ; i < ((filePackHeader*)buf)->resourcesCount; i++)
33+
readChunk(outputPath);
34+
}
35+
36+
void sw::UnPackFile::readChunk(std::string& outputPath)
37+
{
2638
auto *chunkHeader = (char *)malloc(sizeof(sw::chunkHeader));
2739
auto *chunkData = (char *)malloc(sizeof(unsigned int) * 2);
2840

29-
file.read(chunkHeader, sizeof(sw::chunkHeader));
30-
file.read(chunkData, sizeof(unsigned int) * 2);
31-
std::cout << "chunk size: " << ((sw::chunkHeader*)chunkHeader)->sizePack << std::endl;
41+
m_file.read(chunkHeader, sizeof(sw::chunkHeader));
42+
m_file.read(chunkData, sizeof(unsigned int) * 2);
3243
char *pathM = (char *)malloc(((sw::chunkData *)chunkData)->pathCount + 1);
3344
memset(pathM, '\0', ((sw::chunkData *)chunkData)->pathCount + 1);
34-
file.read(pathM, ((sw::chunkData *)chunkData)->pathCount);
35-
std::cout << "path: " << pathM << std::endl;
36-
37-
std::fstream outFile("./out.png", std::ios::out | std::ios::binary);
38-
39-
auto *buffer = (unsigned char*)malloc(((sw::chunkHeader*)chunkHeader)->sizeBase + 4);
40-
memset(buffer, '\0', ((sw::chunkHeader*)chunkHeader)->sizeBase + 4);
41-
file.read((char *)buffer, ((sw::chunkHeader*)chunkHeader)->sizeBase + 4);
42-
outFile.write((char *)buffer, ((sw::chunkHeader*)chunkHeader)->sizeBase + 4);
43-
45+
m_file.read(pathM, ((sw::chunkData *)chunkData)->pathCount);
46+
createFile(*(sw::chunkHeader*)chunkHeader, pathM, outputPath);
47+
free(chunkHeader);
48+
free(chunkData);
49+
free(pathM);
50+
}
4451

52+
void sw::UnPackFile::createFile(sw::chunkHeader &chunkHeader, std::string path, std::string& outputPath)
53+
{
54+
std::filesystem::path p(outputPath + path);
55+
std::filesystem::create_directories(p.parent_path());
56+
auto *buffer = (unsigned char*)malloc(chunkHeader.sizeBase);
57+
memset(buffer, '\0', chunkHeader.sizeBase);
58+
m_file.read((char *)buffer, chunkHeader.sizeBase);
59+
std::fstream outFile(outputPath + path, std::ios::out | std::ios::binary);
60+
outFile.write((char *)buffer, chunkHeader.sizeBase);
4561
}

0 commit comments

Comments
 (0)