This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundlefilescheme.cpp
198 lines (154 loc) · 5.85 KB
/
bundlefilescheme.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "bundlefilescheme.h"
#include "bundlefileblockreader.h"
#include "filereadsource.h"
#include "memoryreadsource.h"
#include "bundlereader.h"
#include "vecstreambuf.h"
#include "endianreader.h"
#include "lz4.h"
#include "lz4hc.h"
#include "lzma.h"
#include <fstream>
using namespace VRCE::BundleFiles;
VRCE::BundleFileScheme::BundleFileScheme(std::filesystem::path filePath)
: FileSchemeList(filePath)
, m_header()
, m_metadata()
{
}
VRCE::BundleFileScheme VRCE::BundleFileScheme::ReadScheme(std::filesystem::path filePath)
{
BundleFileScheme scheme(filePath);
VRCE::BinaryReader reader(filePath);
if (!reader.isValid()) {
throw ""; // File wont open or some shit
}
scheme.readScheme(reader);
return scheme;
}
void VRCE::BundleFileScheme::readScheme(VRCE::BinaryReader& reader)
{
std::int64_t basePosition = reader.position();
readHeader(reader);
switch (m_header.signature().value()) {
case BundleType::UnityRaw:
case BundleType::UnityWeb:
{
std::shared_ptr<std::vector<std::uint8_t>> data;
std::int64_t metadataOffset = 0;
readRawWebMetaData(reader, data, metadataOffset);
VRCE::BinaryReader dataReader(data);
readRawWebData(dataReader, metadataOffset);
break;
}
case BundleType::UnityFS:
{
std::int64_t headerSize = reader.position() - basePosition;
readFileStreamMetaData(reader, basePosition);
readFileStreamData(reader, basePosition, headerSize);
break;
}
default:
throw "";// "Unknown bundle signature '{Header.Signature}'"
}
}
void VRCE::BundleFileScheme::readHeader(VRCE::BinaryReader& reader)
{
std::int64_t headerPosition = reader.position();
VRCE::BundleFiles::BundleReader bundleReader(reader, VRCE::EndianType::BigEndian);
m_header.read(bundleReader);
if (m_header.signature().isRawWeb()) {
if (reader.position() - headerPosition != m_header.rawWeb()->headerSize())
{
throw ""; // "Read {reader.Position - headerPosition} but expected {Header.RawWeb.HeaderSize}"
}
}
}
void VRCE::BundleFileScheme::readRawWebMetaData(VRCE::BinaryReader& reader, std::shared_ptr<std::vector<std::uint8_t>>& dataBuf, std::int64_t& metadataOffset)
{
auto header = m_header.rawWeb();
std::int32_t metadataSize = 0;
if (BundleRawWebHeader::HasUncompressedBlocksInfoSize(m_header.version())) {
metadataSize = header->uncompressedBlocksInfoSize();
}
switch (m_header.signature().value()) {
case BundleType::UnityRaw:
break;
case BundleType::UnityWeb:
break;
default:
throw ""; // Exception($"Unsupported bundle signature '{Header.Signature}'");
}
}
void VRCE::BundleFileScheme::readFileStreamMetaData(VRCE::BinaryReader& reader, std::int64_t basePosition)
{
auto header = m_header.fileStream();
if (header->flags().isBlocksInfoAtTheEnd())
{
reader.seekBeg(basePosition + (header->size() - header->compressedBlocksInfoSize()));
}
CompressionType metaCompression = header->flags().compression();
switch (metaCompression) {
case CompressionType::None:
{
readMetaData(reader, header->uncompressedBlocksInfoSize());
break;
}
case CompressionType::Lzma:
{
std::vector<std::uint8_t> compressedData(header->compressedBlocksInfoSize());
auto uncompressedData = std::make_shared<std::vector<std::uint8_t>>(header->uncompressedBlocksInfoSize());
reader.readInto(compressedData);
// TODO: lzma decompression
// Parse it
VRCE::BinaryReader uncompressedDataReader(uncompressedData);
readMetaData(uncompressedDataReader, header->uncompressedBlocksInfoSize());
break;
}
case CompressionType::Lz4:
case CompressionType::Lz4HC:
{
std::vector<std::uint8_t> compressedData(header->compressedBlocksInfoSize());
auto uncompressedData = std::make_shared<std::vector<std::uint8_t>>(header->uncompressedBlocksInfoSize());
reader.readInto(compressedData);
// DeCompress (TODO: loop until all is read) ((POSSIBLE BUG))
int result = LZ4_decompress_safe((char*)compressedData.data(), (char*)uncompressedData->data(), compressedData.size(), header->uncompressedBlocksInfoSize());
if (result != header->uncompressedBlocksInfoSize()) {
throw ""; // Decompression failed...
}
// Parse it
VRCE::BinaryReader uncompressedDataReader(uncompressedData);
readMetaData(uncompressedDataReader, header->uncompressedBlocksInfoSize());
break;
}
default:
throw ""; // NotSupportedException($"Bundle compression '{metaCompression}' isn't supported");
}
}
void VRCE::BundleFileScheme::readMetaData(VRCE::BinaryReader& reader, std::int32_t metadataSize)
{
std::int64_t metadataPosition = reader.position();
BundleReader bundleReader(reader, EndianType::BigEndian, m_header.signature(), m_header.version(), m_header.flags());
m_metadata.read(bundleReader);
if (metadataSize > 0)
{
if (reader.position() - metadataPosition != metadataSize)
{
throw "";// new Exception($"Read {stream.Position - metadataPosition} but expected {metadataSize}");
}
}
}
void VRCE::BundleFileScheme::readRawWebData(VRCE::BinaryReader& reader, std::int64_t metadataOffset)
{
}
void VRCE::BundleFileScheme::readFileStreamData(VRCE::BinaryReader& reader, std::int64_t basePosition, std::int64_t headerSize)
{
if (m_header.fileStream()->flags().isBlocksInfoAtTheEnd()) {
reader.seekBeg(basePosition + headerSize);
}
BundleFileBlockReader blockReader(reader, m_metadata.blocksInfo());
for (const auto& entry : m_metadata.directoryInfo().nodes()) {
VRCE::BinaryReader entryReader = blockReader.readEntry(entry);
//VRCE::FileScheme scheme = G
}
}