Debian Codegraph @

include/iarchive.h

raw
1 #pragma once
2
3 /**
4 * \file iarchive.h
5 * Types relating to the use of ZIP archives (PK4 files) and their contents.
6 * \ingroup vfs
7 */
8
9 #include "igameresource.h"
10
11 #include "imodule.h"
12 #include <cstddef>
13
14 #include "itextstream.h"
15
16 #include <string>
17
18 class InputStream;
19
20 // Interface providing additional info about a given file
21 // used by the FileInfo structure to load extended info
22 // file like size, containing archive, physical path, etc.
23 class IArchiveFileInfoProvider
24 {
25 public:
26 virtual ~IArchiveFileInfoProvider() {}
27
28 // Get file size of the file given by the relative path (like "def/func.def") in bytes
29 virtual std::size_t getFileSize(const std::string& relativePath) = 0;
30
31 // Returns true if this file is an actual file on disk (as opposed to a file in a PAK)
32 virtual bool getIsPhysical(const std::string& relativePath) = 0;
33
34 // Returns the absolute file system path to the archive the given file is located in
35 virtual std::string getArchivePath(const std::string& relativePath) = 0;
36 };
37
38 /**
39 * A file opened in binary mode.
40 * \ingroup vfs
41 */
42 class ArchiveFile
43 {
44 public:
45 /// \brief destructor
46 virtual ~ArchiveFile() {}
47 /// \brief Returns the size of the file data in bytes.
48 virtual std::size_t size() const = 0;
49 /// \brief Returns the path to this file (relative to the filesystem root)
50 virtual const std::string& getName() const = 0;
51 /// \brief Returns the stream associated with this file.
52 /// Subsequent calls return the same stream.
53 /// The stream may be read forwards until it is exhausted.
54 /// The stream remains valid for the lifetime of the file.
55 virtual InputStream& getInputStream() = 0;
56 };
57 typedef std::shared_ptr<ArchiveFile> ArchiveFilePtr;
58
59 /**
60 * A file opened in text mode.
61 * \ingroup vfs
62 */
63 class ArchiveTextFile :
64 public game::IResource
65 {
66 public:
67 /// \brief Returns the path to this file (relative to the filesystem root)
68 virtual const std::string& getName() const = 0;
69
70 /// \brief Returns the stream associated with this file.
71 /// Subsequent calls return the same stream.
72 /// The stream may be read forwards until it is exhausted.
73 /// The stream remains valid for the lifetime of the file.
74 virtual TextInputStream& getInputStream() = 0;
75 };
76 typedef std::shared_ptr<ArchiveTextFile> ArchiveTextFilePtr;
77
78 /**
79 * Representation of an archive in the virtual filesystem.
80 * This might be a PK4/ZIP file or a regular mod directory.
81 *
82 * \ingroup vfs
83 */
84 class IArchive :
85 public IArchiveFileInfoProvider
86 {
87 public:
88 typedef std::shared_ptr<IArchive> Ptr;
89
90 virtual ~IArchive() {}
91
92 class Visitor
93 {
94 public:
95 virtual ~Visitor() {}
96
97 // Invoked for each file in an Archive
98 virtual void visitFile(const std::string& name, IArchiveFileInfoProvider& infoProvider) = 0;
99
100 // Invoked for each directory in an Archive. Return true to skip the directory.
101 virtual bool visitDirectory(const std::string& name, std::size_t depth) = 0;
102 };
103
104 /// \brief Returns a new object associated with the file identified by \p name, or 0 if the file cannot be opened.
105 /// Name comparisons are case-insensitive.
106 virtual ArchiveFilePtr openFile(const std::string& name) = 0;
107
108 /// \brief Returns a new object associated with the file identified by \p name, or 0 if the file cannot be opened.
109 /// Name comparisons are case-insensitive.
110 virtual ArchiveTextFilePtr openTextFile(const std::string& name) = 0;
111
112 /// Returns true if the file identified by \p name can be opened.
113 /// Name comparisons are case-insensitive.
114 virtual bool containsFile(const std::string& name) = 0;
115
116 /// \brief Performs a depth-first traversal of the archive tree starting at \p root.
117 /// Traverses the entire tree if \p root is "".
118 /// When a file is encountered, calls \c visitor.file passing the file name.
119 /// When a directory is encountered, calls \c visitor.directory passing the directory name.
120 /// Skips the directory if \c visitor.directory returned true.
121 /// Root comparisons are case-insensitive.
122 /// Names are mixed-case.
123 virtual void traverse(Visitor& visitor, const std::string& root) = 0;
124 };