-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnPackFile.cpp
More file actions
83 lines (70 loc) · 2.94 KB
/
UnPackFile.cpp
File metadata and controls
83 lines (70 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
** ShipWreck Engine , 2023
** unPackFile.cpp
*/
#include <fstream>
#include <filesystem>
#include <iostream>
#include "UnPackFile.hpp"
#include "File.hpp"
#include "FileException.hpp"
#include "utils.hpp"
#include "Log.hpp"
#include "zstd.hpp"
sw::UnPackFile::UnPackFile(std::string path, std::string outputPath)
{
m_file.open(path, std::ios::binary | std::ios::in);
char *buf = (char *)sw::MemAlloc(sizeof(filePackHeader));
m_file.read(buf, sizeof(filePackHeader));
for (int i = 0; i < 4; i++)
if (((filePackHeader*)buf)->id[i] != FILE_TYPE[i])
throw sw::FileException("Incompatible/Corrupted file");
if (((filePackHeader*)buf)->version != FILE_VERSION)
throw sw::FileException("Incompatible version " + std::to_string(((filePackHeader*)buf)->version) + " supported version: " + std::to_string(FILE_VERSION));
for (int i = 0 ; i < ((filePackHeader*)buf)->resourcesCount; i++)
readChunk(outputPath);
m_file.close();
free(buf);
}
void sw::UnPackFile::readChunk(std::string& outputPath)
{
auto *chunkHeader = (char *)sw::MemAlloc(sizeof(sw::chunkHeader));
auto *chunkData = (char *)sw::MemAlloc(sizeof(unsigned int) * 2);
m_file.read(chunkHeader, sizeof(sw::chunkHeader));
m_file.read(chunkData, sizeof(unsigned int) * 2);
char *pathM = (char *)sw::MemAlloc(((sw::chunkData *)chunkData)->pathCount + 1);
memset(pathM, '\0', ((sw::chunkData *)chunkData)->pathCount + 1);
m_file.read(pathM, ((sw::chunkData *)chunkData)->pathCount);
createFile(*(sw::chunkHeader*)chunkHeader, pathM, outputPath);
free(chunkHeader);
free(chunkData);
free(pathM);
}
void sw::UnPackFile::createFile(sw::chunkHeader &chunkHeader, std::string path, std::string& outputPath)
{
std::filesystem::path p(outputPath + path);
std::filesystem::create_directories(p.parent_path());
#ifdef SWFP_COMP
auto *bufferComp = (unsigned char*)sw::MemAlloc(chunkHeader.sizePack);
auto *buffer = (unsigned char*)sw::MemAlloc(ZSTD_compressBound(chunkHeader.sizeBase));
memset(bufferComp, '\0', chunkHeader.sizePack);
m_file.read((char *)bufferComp, chunkHeader.sizePack);
size_t sizeComp = ZSTD_decompress(buffer, ZSTD_compressBound(chunkHeader.sizeBase), bufferComp, chunkHeader.sizePack);
if (sizeComp != chunkHeader.sizeBase)
throw sw::FileException("File size id different");
#else
auto *buffer = (unsigned char*)sw::MemAlloc(chunkHeader.sizeBase);
memset(buffer, '\0', chunkHeader.sizePack);
m_file.read((char *)buffer, chunkHeader.sizePack);
#endif
try {
std::fstream outFile(outputPath + path, std::ios::out | std::ios::binary);
if (!outFile.is_open())
throw sw::FileException("File cannot be created");
outFile.write((char *)buffer, chunkHeader.sizeBase);
outFile.close();
free(buffer);
} catch (const sw::FileException& e) {
sw::Log::AddLog("Cannot create file: " + path, sw::Log::WARNING);
}
}