-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasset.cpp
752 lines (646 loc) · 20.7 KB
/
asset.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#include "framework.h"
#include "asset-internal.h"
#include <algorithm>
#include <sys/types.h>
#include <sys/stat.h>
namespace Framework
{
// AssetPack implementation
AssetPack::AssetPack()
{
}
bool AssetPack::LookupFile(const char * path, const char * suffix, void ** ppDataOut, int * pSizeOut)
{
ASSERT_ERR(path);
std::string fullPath = path;
if (suffix)
fullPath += suffix;
CHECK_WARN(AssetCompiler::NormalizePath(const_cast<char *>(fullPath.data())));
auto iter = m_directory.find(fullPath);
if (iter == m_directory.end())
return false;
int iFile = iter->second;
const FileInfo & fileinfo = m_files[iFile];
if (ppDataOut)
*ppDataOut = (fileinfo.m_size > 0) ? &m_data[fileinfo.m_offset] : nullptr;
if (pSizeOut)
*pSizeOut = fileinfo.m_size;
return true;
}
bool AssetPack::HasAsset(const char * path)
{
return (m_manifest.find(std::string(path)) != m_manifest.end());
}
void AssetPack::Reset()
{
m_data.clear();
m_files.clear();
m_directory.clear();
m_manifest.clear();
m_path.clear();
}
namespace AssetCompiler
{
static const char * s_pathVersionInfo = "version";
static const char * s_pathManifest = "manifest";
}
// Prototype individual compilation functions for different asset types
bool CompileOBJMeshAsset(
const AssetCompileInfo * pACI,
mz_zip_archive * pZipOut);
bool CompileOBJMtlLibAsset(
const AssetCompileInfo * pACI,
mz_zip_archive * pZipOut);
bool CompileTextureRawAsset(
const AssetCompileInfo * pACI,
mz_zip_archive * pZipOut);
bool CompileTextureWithMipsAsset(
const AssetCompileInfo * pACI,
mz_zip_archive * pZipOut);
typedef bool (*AssetCompileFunc)(const AssetCompileInfo *, mz_zip_archive *);
static const AssetCompileFunc s_assetCompileFuncs[] =
{
&CompileOBJMeshAsset, // ACK_OBJMesh
&CompileOBJMtlLibAsset, // ACK_OBJMtlLib
&CompileTextureRawAsset, // ACK_TextureRaw
&CompileTextureWithMipsAsset, // ACK_TextureWithMips
};
cassert(dim(s_assetCompileFuncs) == ACK_Count);
static const char * s_ackNames[] =
{
"OBJ mesh", // ACK_OBJMesh
"OBJ material library", // ACK_OBJMtlLib
"raw texture", // ACK_TextureRaw
"mipmapped texture", // ACK_TextureWithMips
};
cassert(dim(s_ackNames) == ACK_Count);
// Load an asset pack file, checking that all its assets are present and up to date,
// and compiling any that aren't.
bool LoadAssetPackOrCompileIfOutOfDate(
const char * packPath,
const AssetCompileInfo * assets,
int numAssets,
AssetPack * pPackOut)
{
ASSERT_ERR(packPath);
ASSERT_ERR(assets);
ASSERT_ERR(numAssets > 0);
ASSERT_ERR(pPackOut);
using namespace AssetCompiler;
// Does the asset pack already exist?
struct _stat packStat;
if (_stat(packPath, &packStat) == 0)
{
// Check if any assets are out of date
std::vector<int> assetsToUpdate;
if (!FindOutOfDateAssets(packPath, assets, numAssets, &assetsToUpdate))
{
LOG("Asset pack %s exists but seems to be corrupt; recompiling it from sources.", packPath);
if (!CompileFullAssetPackToFile(packPath, assets, numAssets))
return false;
}
else if (assetsToUpdate.empty())
{
LOG("Asset pack %s is up to date.", packPath);
}
else
{
LOG("Asset pack %s is out of date; updating.", packPath);
if (!UpdateAssetPack(packPath, assets, numAssets, assetsToUpdate))
return false;
}
}
else
{
LOG("Asset pack %s doesn't exist; compiling it from sources.", packPath);
if (!CompileFullAssetPackToFile(packPath, assets, numAssets))
return false;
}
// It ought to exist and be up-to-date now, so load it
return LoadAssetPack(packPath, pPackOut);
}
// Just load an asset pack file.
bool LoadAssetPack(
const char * packPath,
AssetPack * pPackOut)
{
ASSERT_ERR(packPath);
ASSERT_ERR(pPackOut);
// Load the archive directory
mz_zip_archive zip = {};
if (!mz_zip_reader_init_file(&zip, packPath, 0))
{
WARN("Couldn't load asset pack %s", packPath);
return false;
}
pPackOut->m_path = packPath;
if (!AssetCompiler::LoadAssetPackFromZip(&zip, pPackOut))
{
mz_zip_reader_end(&zip);
return false;
}
mz_zip_reader_end(&zip);
LOG("Loaded asset pack %s - %dMB uncompressed", packPath, pPackOut->m_data.size() / 1048576);
return true;
}
namespace AssetCompiler
{
// Load an asset pack file from a zip stream (can be in memory or a file).
bool LoadAssetPackFromZip(
mz_zip_archive * pZip,
AssetPack * pPackOut)
{
ASSERT_ERR(pZip);
ASSERT_ERR(pPackOut);
const char * packPath = pPackOut->m_path.c_str();
int numFiles = int(mz_zip_reader_get_num_files(pZip));
pPackOut->m_files.resize(numFiles);
pPackOut->m_directory.clear();
pPackOut->m_directory.reserve(numFiles);
// Run through all the files, build the file list and directory and sum up their sizes
int bytesTotal = 0;
for (int i = 0; i < numFiles; ++i)
{
mz_zip_archive_file_stat fileStat;
if (!mz_zip_reader_file_stat(pZip, i, &fileStat))
{
WARN("Couldn't read directory entry %d of %d from asset pack %s", i, numFiles, packPath);
return false;
}
AssetPack::FileInfo * pFileInfo = &pPackOut->m_files[i];
pFileInfo->m_path = fileStat.m_filename;
pFileInfo->m_offset = bytesTotal;
pFileInfo->m_size = int(fileStat.m_uncomp_size);
pPackOut->m_directory.insert(std::make_pair(pFileInfo->m_path, i));
bytesTotal += int(fileStat.m_uncomp_size);
}
// Allocate memory to store the decompressed data
pPackOut->m_data.resize(bytesTotal);
// Decompress all the files
for (int i = 0; i < numFiles; ++i)
{
AssetPack::FileInfo * pFileInfo = &pPackOut->m_files[i];
// Skip zero size files (trailing ones will cause an std::vector assert)
if (pFileInfo->m_size == 0)
continue;
if (!mz_zip_reader_extract_to_mem(
pZip, i,
&pPackOut->m_data[pFileInfo->m_offset],
pFileInfo->m_size, 0))
{
WARN("Couldn't extract file %s (index %d of %d) from asset pack %s",
pFileInfo->m_path.c_str(), i, numFiles, packPath);
return false;
}
}
// Extract the version info
VersionInfo * pVerInfo;
int verInfoSize;
if (!pPackOut->LookupFile(s_pathVersionInfo, nullptr, (void **)&pVerInfo, &verInfoSize))
{
WARN("Couldn't find version info in asset pack %s", packPath);
return false;
}
if (verInfoSize != sizeof(VersionInfo))
{
WARN("Version info in asset pack %s is wrong size, %d bytes (expected %d)",
packPath, verInfoSize, sizeof(VersionInfo));
return false;
}
// Check that all the versions are correct
if (pVerInfo->m_packver != PACKVER_Current)
{
WARN("Asset pack %s has wrong pack version %d (expected %d)", packPath, pVerInfo->m_packver, PACKVER_Current);
return false;
}
if (pVerInfo->m_meshver != MESHVER_Current)
{
WARN("Asset pack %s has wrong mesh version %d (expected %d)", packPath, pVerInfo->m_meshver, MESHVER_Current);
return false;
}
if (pVerInfo->m_mtlver != MTLVER_Current)
{
WARN("Asset pack %s has wrong material version %d (expected %d)", packPath, pVerInfo->m_mtlver, MTLVER_Current);
return false;
}
if (pVerInfo->m_texver != TEXVER_Current)
{
WARN("Asset pack %s has wrong texture version %d (expected %d)", packPath, pVerInfo->m_texver, TEXVER_Current);
return false;
}
// Extract the manifest
const char * pManifest;
int manifestSize;
if (!pPackOut->LookupFile(s_pathManifest, nullptr, (void **)&pManifest, &manifestSize))
{
WARN("Couldn't find manifest in asset pack %s", packPath);
return false;
}
ParseManifest(pManifest, manifestSize, packPath, &pPackOut->m_manifest);
return true;
}
// Check that filenames are printable-ASCII-only, lowercase, and there are no backslashes
// (this should really be generalized to allow UTF-8 printable chars)
bool NormalizePath(char * path)
{
ASSERT_ERR(path);
for (char * pCh = path; *pCh; ++pCh)
{
if (*pCh < 32 || *pCh > 126)
{
WARN("Invalid character %c in path %s", *pCh, path);
return false;
}
else if (*pCh >= 'A' && *pCh <= 'Z')
{
*pCh += 32; // Convert to lowercase
}
else if (*pCh == '\\')
{
*pCh = '/'; // Normalize paths to forward slashes
}
}
return true;
}
// Write a memory buffer out to an asset pack .zip file.
bool WriteAssetDataToZip(
const char * assetPath,
const char * assetSuffix,
const void * pData,
size_t sizeBytes,
mz_zip_archive * pZipOut)
{
ASSERT_ERR(assetPath);
ASSERT_ERR(sizeBytes >= 0);
ASSERT_ERR(pData || sizeBytes == 0);
ASSERT_ERR(pZipOut);
if (!assetSuffix)
assetSuffix = "";
// Compose the path, but detect if it's too long
char zipPath[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE + 1] = {};
if (_snprintf_s(zipPath, _TRUNCATE, "%s%s", assetPath, assetSuffix) < 0)
{
WARN("File path %s%s is too long for .zip format", assetPath, assetSuffix);
return false;
}
CHECK_WARN(NormalizePath(zipPath));
if (!mz_zip_writer_add_mem(pZipOut, zipPath, pData, sizeBytes, MZ_NO_COMPRESSION))
{
WARN("Couldn't add file %s to archive", zipPath);
return false;
}
return true;
}
// Parse an asset pack manifest (newline-delimited list of names) into a set structure.
void ParseManifest(
const char * manifest,
int manifestSize,
const char * path,
std::unordered_set<std::string> * pManifestOut)
{
ASSERT_ERR(manifest);
ASSERT_ERR(manifestSize > 0);
ASSERT_ERR(pManifestOut);
// Make a copy so that we can parse destructively
std::vector<char> manifestCopy(manifestSize + 1);
memcpy(&manifestCopy[0], manifest, manifestSize);
TextParsingHelper tph(&manifestCopy[0], path);
while (tph.NextLine())
{
pManifestOut->insert(std::string(tph.NextToken()));
tph.ExpectEOL();
}
}
// Compile an entire asset pack from scratch, to a .zip file on disk.
bool CompileFullAssetPackToFile(
const char * packPath,
const AssetCompileInfo * assets,
int numAssets)
{
ASSERT_ERR(packPath);
ASSERT_ERR(assets);
ASSERT_ERR(numAssets > 0);
mz_zip_archive zip = {};
if (!mz_zip_writer_init_file(&zip, packPath, 0))
{
WARN("Couldn't open %s for writing", packPath);
return false;
}
bool success = CompileFullAssetPackToZip(assets, numAssets, &zip);
if (!mz_zip_writer_finalize_archive(&zip))
{
WARN("Couldn't finalize archive");
success = false;
}
mz_zip_writer_end(&zip);
return success;
}
// Compile an entire asset pack from scratch, to a zip stream (can be in memory or a file).
bool CompileFullAssetPackToZip(
const AssetCompileInfo * assets,
int numAssets,
mz_zip_archive * pZipOut)
{
ASSERT_ERR(assets);
ASSERT_ERR(numAssets > 0);
ASSERT_ERR(pZipOut);
// !!!UNDONE: not nicely generating entries in the .zip for directories in the internal paths.
// Doesn't seem to matter as .zip viewers handle it fine, but maybe we should do that anyway?
std::string manifest;
int numErrors = 0;
for (int iAsset = 0; iAsset < numAssets; ++iAsset)
{
const AssetCompileInfo * pACI = &assets[iAsset];
ACK ack = pACI->m_ack;
ASSERT_ERR(ack >= 0 && ack < ACK_Count);
LOG("[%d/%d] Compiling %s asset %s...", iAsset+1, numAssets, s_ackNames[ack], pACI->m_pathSrc);
// Compile the asset
if (s_assetCompileFuncs[ack](pACI, pZipOut))
{
// Write asset name to the manifest
manifest += pACI->m_pathSrc;
manifest += '\n';
}
else
{
WARN("Couldn't compile asset %s", pACI->m_pathSrc);
++numErrors;
}
}
if (numErrors > 0)
{
WARN("Failed to compile %d of %d assets", numErrors, numAssets);
}
// Write version info
VersionInfo version =
{
PACKVER_Current,
MESHVER_Current,
MTLVER_Current,
TEXVER_Current,
};
if (!WriteAssetDataToZip(s_pathVersionInfo, nullptr, &version, sizeof(version), pZipOut))
return false;
// Write manifest
if (!WriteAssetDataToZip(s_pathManifest, nullptr, &manifest[0], manifest.length(), pZipOut))
return false;
return (numErrors == 0);
}
// Check if any assets in a pack are out of date by version number or mod time,
// returning a list of ones that need updating.
bool FindOutOfDateAssets(
const char * packPath,
const AssetCompileInfo * assets,
int numAssets,
std::vector<int> * pAssetsToUpdateOut)
{
ASSERT_ERR(packPath);
ASSERT_ERR(assets);
ASSERT_ERR(numAssets > 0);
ASSERT_ERR(pAssetsToUpdateOut);
// Load the archive directory
mz_zip_archive zip = {};
if (!mz_zip_reader_init_file(&zip, packPath, 0))
{
WARN("Couldn't load asset pack %s", packPath);
return false;
}
// Extract the version info
VersionInfo ver;
int fileIndex = mz_zip_reader_locate_file(&zip, s_pathVersionInfo, nullptr, 0);
if (fileIndex < 0)
{
WARN("Couldn't find version info in asset pack %s", packPath);
mz_zip_reader_end(&zip);
return false;
}
if (!mz_zip_reader_extract_to_mem(&zip, fileIndex, &ver, sizeof(ver), 0))
{
WARN("Couldn't extract version info from asset pack %s", packPath);
mz_zip_reader_end(&zip);
return false;
}
// If the pack version is wrong, we have to recompile the whole thing
if (ver.m_packver != PACKVER_Current)
{
pAssetsToUpdateOut->resize(numAssets);
for (int i = 0; i < numAssets; ++i)
(*pAssetsToUpdateOut)[i] = i;
mz_zip_reader_end(&zip);
return true;
}
// Extract the manifest
fileIndex = mz_zip_reader_locate_file(&zip, s_pathManifest, nullptr, 0);
if (fileIndex < 0)
{
WARN("Couldn't find manifest in asset pack %s", packPath);
mz_zip_reader_end(&zip);
return false;
}
size_t manifestSize;
char * pManifest = (char *)mz_zip_reader_extract_to_heap(&zip, fileIndex, &manifestSize, 0);
if (!pManifest)
{
WARN("Couldn't extract manifest from asset pack %s", packPath);
mz_zip_reader_end(&zip);
return false;
}
std::unordered_set<std::string> manifest;
ParseManifest(pManifest, int(manifestSize), packPath, &manifest);
mz_free(pManifest);
mz_zip_reader_end(&zip);
// Get the mod date of the asset pack
struct _stat packStat;
CHECK_ERR(_stat(packPath, &packStat) == 0);
// Go through the assets and check their individual versions and mod dates
for (int i = 0; i < numAssets; ++i)
{
// Check the appropriate version number for the asset type
const AssetCompileInfo * pACI = &assets[i];
switch (pACI->m_ack)
{
case ACK_OBJMesh:
if (ver.m_meshver != MESHVER_Current)
{
pAssetsToUpdateOut->push_back(i);
continue;
}
break;
case ACK_OBJMtlLib:
if (ver.m_mtlver != MTLVER_Current)
{
pAssetsToUpdateOut->push_back(i);
continue;
}
break;
case ACK_TextureRaw:
case ACK_TextureWithMips:
if (ver.m_texver != TEXVER_Current)
{
pAssetsToUpdateOut->push_back(i);
continue;
}
break;
default:
ERR("Missing case for ACK %d", pACI->m_ack);
break;
}
// Check if the asset exists in the manifest. If it doesn't, needs to be compiled.
if (manifest.find(std::string(pACI->m_pathSrc)) == manifest.end())
{
pAssetsToUpdateOut->push_back(i);
continue;
}
// Check mod time of the source file against that of the pack
// If the source file doesn't exist, that's OK! Asset packs can be
// distributed in lieu of source files.
struct _stat srcStat;
if (_stat(pACI->m_pathSrc, &srcStat) == 0 &&
srcStat.st_mtime > packStat.st_mtime)
{
pAssetsToUpdateOut->push_back(i);
continue;
}
}
return true;
}
// Update an asset pack in-place by recompiling some assets,
// preserving any other data already in the pack for the others.
bool UpdateAssetPack(
const char * packPath,
const AssetCompileInfo * assets,
int numAssets,
std::vector<int> const & assetsToUpdate)
{
ASSERT_ERR(packPath);
ASSERT_ERR(assets);
ASSERT_ERR(numAssets > 0);
// Load the archive directory
mz_zip_archive zipSrc = {};
if (!mz_zip_reader_init_file(&zipSrc, packPath, 0))
{
WARN("Couldn't load asset pack %s", packPath);
return false;
}
int numSrcFiles = int(mz_zip_reader_get_num_files(&zipSrc));
// Generate a temporary filename for the new archive
char outDir[MAX_PATH] = {};
if (const char * pLastSlash = max(strrchr(packPath, '/'), strrchr(packPath, '\\')))
{
ASSERT_ERR(pLastSlash - packPath < MAX_PATH);
memcpy(outDir, packPath, pLastSlash - packPath);
}
else
{
outDir[0] = '.';
}
char tempPath[MAX_PATH];
CHECK_ERR(GetTempFileName(outDir, nullptr, 0, tempPath) != 0);
// Open the temporary file for writing
mz_zip_archive zipDest = {};
if (!mz_zip_writer_init_file(&zipDest, tempPath, 0))
{
WARN("Couldn't open temporary file %s for writing", packPath);
mz_zip_reader_end(&zipSrc);
return false;
}
std::string manifest;
int numErrors = 0;
int numAssetsToUpdate = int(assetsToUpdate.size());
// Iterate over assets, tracking position in both original asset list and
// list of assets that need updates (a sorted subset of the original ones)
for (int iAsset = 0, iAssetToUpdate = 0; iAsset < numAssets; ++iAsset)
{
const AssetCompileInfo * pACI = &assets[iAsset];
// Does this asset need recompiling?
while (iAssetToUpdate < numAssetsToUpdate && assetsToUpdate[iAssetToUpdate] < iAsset)
++iAssetToUpdate;
if (iAssetToUpdate < numAssetsToUpdate && assetsToUpdate[iAssetToUpdate] == iAsset)
{
ACK ack = pACI->m_ack;
ASSERT_ERR(ack >= 0 && ack < ACK_Count);
LOG("[%d/%d] Compiling %s asset %s...",
iAssetToUpdate+1, numAssetsToUpdate, s_ackNames[ack], pACI->m_pathSrc);
// Compile the asset
if (s_assetCompileFuncs[ack](pACI, &zipDest))
{
// Write asset name to the manifest
manifest += pACI->m_pathSrc;
manifest += '\n';
}
else
{
WARN("Couldn't compile asset %s", pACI->m_pathSrc);
++numErrors;
}
}
else
{
// Copy any files prefixed with the asset name from the old zip to the new one
// Note, this could be more efficient when there's a large number of files
for (int i = 0; i < numSrcFiles; ++i)
{
char filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
mz_zip_reader_get_filename(&zipSrc, i, filename, sizeof(filename));
if (_strnicmp(filename, pACI->m_pathSrc, strlen(pACI->m_pathSrc)) == 0)
{
if (!mz_zip_writer_add_from_zip_reader(&zipDest, &zipSrc, i))
{
WARN("Couldn't copy file %s from asset pack %s to temporary archive %s",
filename, packPath, tempPath);
mz_zip_reader_end(&zipSrc);
mz_zip_writer_end(&zipDest);
DeleteFile(tempPath);
return false;
}
}
}
// Write asset name to the manifest
manifest += pACI->m_pathSrc;
manifest += '\n';
}
}
mz_zip_reader_end(&zipSrc);
if (numErrors > 0)
{
WARN("Failed to compile %d of %d assets", numErrors, numAssetsToUpdate);
}
// Write version info
VersionInfo version =
{
PACKVER_Current,
MESHVER_Current,
MTLVER_Current,
TEXVER_Current,
};
if (!WriteAssetDataToZip(s_pathVersionInfo, nullptr, &version, sizeof(version), &zipDest))
{
mz_zip_writer_end(&zipDest);
DeleteFile(tempPath);
return false;
}
// Write manifest
if (!WriteAssetDataToZip(s_pathManifest, nullptr, &manifest[0], manifest.length(), &zipDest))
{
mz_zip_writer_end(&zipDest);
DeleteFile(tempPath);
return false;
}
if (!mz_zip_writer_finalize_archive(&zipDest))
{
WARN("Couldn't finalize temporary archive %s", tempPath);
mz_zip_writer_end(&zipDest);
DeleteFile(tempPath);
return false;
}
mz_zip_writer_end(&zipDest);
// Move the new version of the asset pack over the old one
if (!MoveFileEx(tempPath, packPath, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
{
WARN("Couldn't rename temporary file %s over asset pack %s", tempPath, packPath);
return false;
}
return (numErrors == 0);
}
}
}