Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
archive.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * interface for reading from and creating archives.
25 */
26
27#ifndef INCLUDED_ARCHIVE
28#define INCLUDED_ARCHIVE
29
31#include "lib/file/file_system.h" // FileInfo
33#include "lib/os_path.h"
34#include "lib/status.h"
35
36#include <cstdint>
37#include <ctime>
38#include <memory>
39
40class Path;
41
42// rationale: this module doesn't build a directory tree of the entries
43// within an archive. that task is left to the VFS; here, we are only
44// concerned with enumerating all archive entries.
45
46namespace ERR
47{
50}
51
53{
54};
55
56typedef std::shared_ptr<IArchiveFile> PIArchiveFile;
57
59{
60 virtual ~IArchiveReader();
61
62 /**
63 * called for each archive entry.
64 * @param pathname full pathname of entry; only valid during the callback.
65 **/
66 typedef void (*ArchiveEntryCallback)(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile, uintptr_t cbData);
67 virtual Status ReadEntries(ArchiveEntryCallback cb, uintptr_t cbData) = 0;
68};
69
70typedef std::shared_ptr<IArchiveReader> PIArchiveReader;
71
72// note: when creating an archive, any existing file with the given pathname
73// will be overwritten.
74
75// rationale: don't support partial adding, i.e. updating archive with
76// only one file. this would require overwriting parts of the Zip archive,
77// which is annoying and slow. also, archives are usually built in
78// seek-optimal order, which would break if we start inserting files.
79// while testing, loose files can be used, so there's no loss.
80
82{
83 /**
84 * write out the archive to disk; only hereafter is it valid.
85 **/
86 virtual ~IArchiveWriter();
87
88 /**
89 * add a file to the archive.
90 *
91 * rationale: passing in a filename instead of the compressed file
92 * contents makes for better encapsulation because callers don't need
93 * to know about the codec. one disadvantage is that loading the file
94 * contents can no longer take advantage of the VFS cache nor previously
95 * archived versions. however, the archive builder usually adds files
96 * precisely because they aren't in archives, and the cache would
97 * thrash anyway, so this is deemed acceptable.
98 *
99 * @param pathname the actual file to add
100 * @param pathnameInArchive the name to store in the archive
101 **/
102 virtual Status AddFile(const OsPath& pathname, const Path& pathnameInArchive) = 0;
103
104 /**
105 * add a file to the archive, when it is already in memory and not on disk.
106 *
107 * @param data the uncompressed file contents to add
108 * @param size the length of data
109 * @param mtime the last-modified-time to be stored in the archive
110 * @param pathnameInArchive the name to store in the archive
111 **/
112 virtual Status AddMemory(const std::uint8_t* data, size_t size, time_t mtime,
113 const OsPath& pathnameInArchive) = 0;
114};
115
116typedef std::shared_ptr<IArchiveWriter> PIArchiveWriter;
117
118#endif // #ifndef INCLUDED_ARCHIVE
std::shared_ptr< IArchiveWriter > PIArchiveWriter
Definition: archive.h:116
std::shared_ptr< IArchiveReader > PIArchiveReader
Definition: archive.h:70
std::shared_ptr< IArchiveFile > PIArchiveFile
Definition: archive.h:56
Definition: file_system.h:41
Definition: path.h:84
Definition: debug.h:340
const Status ARCHIVE_UNKNOWN_METHOD
Definition: archive.h:49
const Status ARCHIVE_UNKNOWN_FORMAT
Definition: archive.h:48
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: archive.h:53
Definition: archive.h:59
void(* ArchiveEntryCallback)(const VfsPath &pathname, const CFileInfo &fileInfo, PIArchiveFile archiveFile, uintptr_t cbData)
called for each archive entry.
Definition: archive.h:66
virtual ~IArchiveReader()
Definition: archive.cpp:39
virtual Status ReadEntries(ArchiveEntryCallback cb, uintptr_t cbData)=0
Definition: archive.h:82
virtual Status AddMemory(const std::uint8_t *data, size_t size, time_t mtime, const OsPath &pathnameInArchive)=0
add a file to the archive, when it is already in memory and not on disk.
virtual ~IArchiveWriter()
write out the archive to disk; only hereafter is it valid.
Definition: archive.cpp:43
virtual Status AddFile(const OsPath &pathname, const Path &pathnameInArchive)=0
add a file to the archive.
Definition: file_loader.h:34