-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackFile.cpp
More file actions
131 lines (107 loc) · 3.58 KB
/
PackFile.cpp
File metadata and controls
131 lines (107 loc) · 3.58 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Copyright (c) 2020-present Caps Collective & contributors
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
//
// This code is released under an unmodified zlib license.
// For conditions of distribution and use, please see:
// https://opensource.org/licenses/Zlib
//
#include "PackFile.h"
#include <utils/FileSystem.h>
#include <utils/Logging.h>
#include <fstream>
#include <iostream>
#include <map>
namespace Siege
{
PackFile::PackFile(const String& filepath)
{
LoadFromPath(filepath);
}
bool PackFile::LoadFromPath(const String& filepath)
{
std::ifstream inputFileStream;
inputFileStream.open(filepath, std::ios::in | std::ios::binary);
inputFileStream.read(reinterpret_cast<char*>(&header), sizeof(Header));
body = reinterpret_cast<char*>(malloc(header.bodySize));
inputFileStream.read(body, (uint32_t) header.bodySize);
inputFileStream.close();
char* tocStart = body + header.tocOffset;
CC_ASSERT(memcmp(tocStart, &PACKER_MAGIC_NUMBER_TOC, 4) == 0, "Failed to find magic number!")
char* tocCurr = tocStart + PACKER_MAGIC_NUMBER_SIZE;
char* tocEnd = body + header.bodySize;
while (tocCurr < tocEnd)
{
TocEntry* toc = reinterpret_cast<TocEntry*>(tocCurr);
if (!toc)
{
break;
}
tocCurr += toc->GetDataSize();
entries.emplace(toc->name, toc);
}
return true;
}
std::shared_ptr<PackFileData> PackFile::FindData(const String& filepath)
{
const TocEntry* toc = entries[filepath];
if (!toc)
{
return nullptr;
}
uLongf bodyDataSizeUncompressed = toc->dataSize;
uLongf bodyDataSizeCompressed = toc->dataSizeCompressed;
PackFileData* packFileData = new (malloc(bodyDataSizeUncompressed)) PackFileData();
int result = uncompress(reinterpret_cast<Bytef*>(packFileData),
&bodyDataSizeUncompressed,
reinterpret_cast<Bytef*>(body + toc->dataOffset),
bodyDataSizeCompressed);
CC_ASSERT(result == Z_OK, "Decompression failed for filepath: " + filepath);
return {packFileData, free};
}
const std::map<String, PackFile::TocEntry*>& PackFile::GetEntries()
{
return entries;
}
const PackFile::Header& PackFile::GetHeader()
{
return header;
}
PackFile::TocEntry* PackFile::TocEntry::Create(const String& name,
uint32_t dataOffset,
uint32_t dataSize)
{
uint32_t nameDataSize = name.Size() + 1;
void* mem = malloc(sizeof(TocEntry) + nameDataSize);
TocEntry* tocEntry = new (mem) TocEntry();
tocEntry->dataOffset = dataOffset;
tocEntry->dataSize = dataSize;
strcpy(&tocEntry->name[0], name.Str());
return tocEntry;
}
} // namespace Siege
namespace std
{
std::ostream& operator<<(std::ostream& os, const Siege::PackFile::MagicNumber& magicNumber)
{
os << magicNumber.string;
return os;
}
std::ostream& operator<<(std::ostream& os, const Siege::PackFile::Header& header)
{
os << "PackFile::Header: {";
os << "magic: " << header.magic << ", ";
os << "version: " << header.version << ", ";
os << "bodySize: " << header.bodySize << ", ";
os << "tocOffset: " << header.tocOffset << "}";
return os;
}
std::ostream& operator<<(std::ostream& os, const Siege::PackFile::TocEntry& tocEntry)
{
os << "PackFile::TocEntry: {";
os << "dataOffset: " << tocEntry.dataOffset << ", ";
os << "dataSize: " << tocEntry.dataSize << ", ";
os << "name: " << tocEntry.name << "}";
return os;
}
} // namespace std