Skip to content

Commit 45d24b7

Browse files
committed
Add Log system + logfile to packer
Add Exception to packer
1 parent ec870c4 commit 45d24b7

4 files changed

Lines changed: 25 additions & 11 deletions

File tree

cmake/Packer.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ set( SRC_FOLDERS
99
${CMAKE_CURRENT_SOURCE_DIR}/sources/
1010
${CMAKE_CURRENT_SOURCE_DIR}/sources/file/
1111
${CMAKE_CURRENT_SOURCE_DIR}/sources/pack/
12-
${CMAKE_CURRENT_SOURCE_DIR}/sources/Log/
12+
${CMAKE_CURRENT_SOURCE_DIR}/sources/log/
13+
${CMAKE_CURRENT_SOURCE_DIR}/sources/exception/
14+
${CMAKE_CURRENT_SOURCE_DIR}/sources/utils/
1315
)
1416
## INCLUDE FOLDERS
1517
set( INC_FOLDERS
1618
${CMAKE_CURRENT_SOURCE_DIR}/includes/
1719
${CMAKE_CURRENT_SOURCE_DIR}/includes/file/
1820
${CMAKE_CURRENT_SOURCE_DIR}/includes/pack/
19-
${CMAKE_CURRENT_SOURCE_DIR}/includes/Log/
21+
${CMAKE_CURRENT_SOURCE_DIR}/includes/log/
22+
${CMAKE_CURRENT_SOURCE_DIR}/includes/exception/
2023
)
2124

2225
## GET SOURCES

sources/file/File.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <filesystem>
88

99
#include "File.hpp"
10+
#include "FileException.hpp"
11+
#include "Log.hpp"
1012

1113
std::fstream sw::File::m_file{};
1214
sw::filePackHeader sw::File::m_header{};
@@ -30,6 +32,8 @@ void sw::File::generateFile(std::string&& fileName, std::string&& path, bool dis
3032
fileName = computeFileName(fileName, path);
3133
m_file.open(path + fileName + ".swfp", std::ios::out | std::ios::binary);
3234
}
35+
if (!m_file.is_open())
36+
throw sw::FileException("Cannot create packed file: " + path + fileName + ".swfp");
3337
m_file.write(reinterpret_cast<const char *>(&m_header), sizeof(filePackHeader));
3438
}
3539

sources/pack/Packer.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "Packer.hpp"
1111
#include "csc32.hpp"
1212
#include "File.hpp"
13+
#include "FileException.hpp"
14+
#include "utils.hpp"
15+
#include "Log.hpp"
1316

1417
std::string sw::Packer::path{};
1518

@@ -40,19 +43,19 @@ void sw::Packer::createChunkData(std::string path, sw::chunkHeader& header, sw::
4043
{
4144
std::ifstream file(path, std::ios::binary | std::ios::ate);
4245
if (!file.is_open())
43-
throw std::exception();
46+
throw sw::FileException("Cannot open target file: " + path);
4447
std::streamsize size = file.tellg();
4548
std::vector<char> buffer(size);
4649

4750
file.seekg(0, std::ios::beg);
4851
file.read(buffer.data(), size);
4952

50-
data.path = (char *)std::malloc(path.size() - sw::Packer::path.size() + 1);
53+
data.path = (char *)sw::MemAlloc(path.size() - sw::Packer::path.size() + 1);
5154
std::memset(data.path, '\0', path.size() - sw::Packer::path.size() + 1);
5255
std::memcpy(data.path, path.data() + sw::Packer::path.size(), path.size() - sw::Packer::path.size());
5356
data.pathCount = path.size() - sw::Packer::path.size();
5457
fillProps(data);
55-
data.data = std::malloc(size);
58+
data.data = sw::MemAlloc(size);
5659
std::memcpy(data.data, buffer.data(), size);
5760

5861
header.sizeBase = size;
@@ -64,11 +67,15 @@ void sw::Packer::readFile(std::string path)
6467
sw::resourceChunk chunk{};
6568
std::filesystem::path path1(path);
6669

67-
fillType("RAWD", chunk.header);
68-
chunk.header.id = sw::computeCsc32((unsigned char *)path1.filename().string().data(), path1.filename().string().size());
69-
createChunkData(path, chunk.header, chunk.data);
70-
chunk.header.crc32 = sw::computeCsc32((unsigned char *)&chunk.data, sizeof(chunk.data));
71-
sw::File::writeInFile(chunk);
70+
try {
71+
fillType("RAWD", chunk.header);
72+
chunk.header.id = sw::computeCsc32((unsigned char *) path1.filename().string().data(), path1.filename().string().size());
73+
createChunkData(path, chunk.header, chunk.data);
74+
chunk.header.crc32 = sw::computeCsc32((unsigned char *) &chunk.data, sizeof(chunk.data));
75+
sw::File::writeInFile(chunk);
76+
} catch (const std::exception& e) {
77+
sw::Log::AddLog(e.what(), sw::Log::WARNING);
78+
}
7279
}
7380

7481
void sw::Packer::fillType(std::string &&type, sw::chunkHeader& header)

sources/unpack/UnPackFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ void sw::UnPackFile::createFile(sw::chunkHeader &chunkHeader, std::string path,
6363
outFile.write((char *)buffer, chunkHeader.sizeBase);
6464
free(buffer);
6565
} catch (const sw::FileException& e) {
66-
sw::Log::AddLog("Cannot create file: " + path);
66+
sw::Log::AddLog("Cannot create file: " + path, sw::Log::WARNING);
6767
}
6868
}

0 commit comments

Comments
 (0)