forked from AtomicGameEngine/AtomicGameEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourcePackager.cpp
More file actions
213 lines (166 loc) · 6.41 KB
/
ResourcePackager.cpp
File metadata and controls
213 lines (166 loc) · 6.41 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Portions Copyright (c) 2008-2014 the Urho3D project.
//
//
// Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "Atomic/Core/StringUtils.h"
#include <Atomic/IO/Log.h>
#include <Atomic/IO/FileSystem.h>
#include <Atomic/Container/ArrayPtr.h>
#include <LZ4/lz4.h>
#include <LZ4/lz4hc.h>
#include "BuildBase.h"
#include "ResourcePackager.h"
namespace ToolCore
{
ResourcePackager::ResourcePackager(Context* context, BuildBase* buildBase) : Object(context)
, buildBase_(buildBase)
, checksum_(0)
{
}
ResourcePackager::~ResourcePackager()
{
}
bool ResourcePackager::WritePackageFile(const String& destFilePath)
{
buildBase_->BuildLog("Writing package", false);
SharedPtr<File> dest(new File(context_, destFilePath, FILE_WRITE));
if (!dest->IsOpen())
{
buildBase_->FailBuild("Could not open output file " + destFilePath);
return false;
}
// Write ID, number of files & placeholder for checksum
WriteHeader(dest);
for (unsigned i = 0; i < resourceEntries_.Size(); i++)
{
BuildResourceEntry* entry = resourceEntries_[i];
// Write entry (correct offset is still unknown, will be filled in later)
dest->WriteString(entry->packagePath_);
dest->WriteUInt(entry->offset_);
dest->WriteUInt(entry->size_);
dest->WriteUInt(entry->checksum_);
}
unsigned totalDataSize = 0;
// Write file data, calculate checksums & correct offsets
for (unsigned i = 0; i < resourceEntries_.Size(); i++)
{
BuildResourceEntry* entry = resourceEntries_[i];
entry->offset_ = dest->GetSize();
File srcFile(context_, entry->absolutePath_);
if (!srcFile.IsOpen())
{
buildBase_->FailBuild("Could not open input file " + entry->absolutePath_);
return false;
}
unsigned dataSize = entry->size_;
totalDataSize += dataSize;
SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
if (srcFile.Read(&buffer[0], dataSize) != dataSize)
{
buildBase_->FailBuild("Could not read input file " + entry->absolutePath_);
return false;
}
srcFile.Close();
for (unsigned j = 0; j < dataSize; ++j)
{
checksum_ = SDBMHash(checksum_, buffer[j]);
entry->checksum_ = SDBMHash(entry->checksum_, buffer[j]);
}
// might not want to compress for WebGL
//if (!compress_)
//{
// PrintLine(entries_[i].name_ + " size " + String(dataSize));
// dest.Write(&buffer[0], entries_[i].size_);
//}
//else
//{
unsigned compressedBlockSize_ = 32768;
SharedArrayPtr<unsigned char> compressBuffer(new unsigned char[LZ4_compressBound(compressedBlockSize_)]);
unsigned pos = 0;
unsigned totalPackedBytes = 0;
while (pos < dataSize)
{
unsigned unpackedSize = compressedBlockSize_;
if (pos + unpackedSize > dataSize)
unpackedSize = dataSize - pos;
unsigned packedSize = LZ4_compressHC((const char*)&buffer[pos], (char*)compressBuffer.Get(), unpackedSize);
if (!packedSize)
{
buildBase_->FailBuild(ToString("LZ4 compression failed for file %s at offset %u", entry->absolutePath_.CString(), pos));
return false;
}
dest->WriteUShort(unpackedSize);
dest->WriteUShort(packedSize);
dest->Write(compressBuffer.Get(), packedSize);
totalPackedBytes += 6 + packedSize;
pos += unpackedSize;
}
buildBase_->BuildLog(entry->absolutePath_ + " in " + String(dataSize) + " out " + String(totalPackedBytes), false);
}
//}
// Write package size to the end of file to allow finding it linked to an executable file
unsigned currentSize = dest->GetSize();
dest->WriteUInt(currentSize + sizeof(unsigned));
// Write header again with correct offsets & checksums
dest->Seek(0);
WriteHeader(dest);
for (unsigned i = 0; i < resourceEntries_.Size(); i++)
{
BuildResourceEntry* entry = resourceEntries_[i];
dest->WriteString(entry->packagePath_);
dest->WriteUInt(entry->offset_);
dest->WriteUInt(entry->size_);
dest->WriteUInt(entry->checksum_);
}
buildBase_->BuildLog("Resource Package:");
buildBase_->BuildLog("Number of files " + String(resourceEntries_.Size()));
buildBase_->BuildLog("File data size " + String(totalDataSize));
buildBase_->BuildLog("Package size " + String(dest->GetSize()));
return true;
}
void ResourcePackager::WriteHeader(File* dest)
{
dest->WriteFileID("ULZ4");
dest->WriteUInt(resourceEntries_.Size());
dest->WriteUInt(checksum_);
}
void ResourcePackager::GeneratePackage(const String& destFilePath)
{
for (unsigned i = 0; i < resourceEntries_.Size(); i++)
{
BuildResourceEntry* entry = resourceEntries_[i];
File file(context_);
if (!file.Open(entry->absolutePath_))
{
buildBase_->FailBuild(Atomic::ToString("Could not open resource file %s", entry->absolutePath_.CString()));
return;
}
entry->size_ = file.GetSize();
}
WritePackageFile(destFilePath);
}
void ResourcePackager::AddResourceEntry(BuildResourceEntry* entry)
{
resourceEntries_.Push(entry);
}
}