File: VFSHandler.h

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (107 lines) | stat: -rw-r--r-- 3,062 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _VFS_HANDLER_H
#define _VFS_HANDLER_H

#include <array>
#include <map>
#include <string>
#include <vector>
#include <cinttypes>

class IArchive;

/**
 * Main API for accessing the Virtual File System (VFS).
 * This only allows accessing the VFS (stuff within archives registered with the
 * VFS), NOT the real file system.
 */
class CVFSHandler
{
public:
	CVFSHandler();
	~CVFSHandler();

	enum Section {
		Mod,
		Map,
		Base,
		Menu,
		Count,
		Error
	};

	static Section GetModeSection(char mode);

	static Section GetModTypeSection(int modtype);

	/**
	 * Checks whether a file exists in the VFS (does not work for dirs).
	 * This is cheaper then calling LoadFile, if you do not require the contents
	 * of the file.
	 * @param filePath raw file path, for example "maps/myMap.smf",
	 *   case-insensitive
	 * @return true if the file exists in the VFS, false otherwise
	 */
	bool FileExists(const std::string& filePath, Section section);
	/**
	 * Reads the contents of a file from within the VFS.
	 * @param filePath raw file path, for example "maps/myMap.smf",
	 *   case-insensitive
	 * @return true if the file exists in the VFS and was successfully read
	 */
	bool LoadFile(const std::string& filePath, std::vector<std::uint8_t>& buffer, Section section);

	/**
	 * Returns all the files in the given (virtual) directory without the
	 * preceeding pathname.
	 * @param dir raw directory path, for example "maps/" or "maps",
	 *   case-insensitive
	 */
	std::vector<std::string> GetFilesInDir(const std::string& dir, Section section);
	/**
	 * Returns all the sub-directories in the given (virtual) directory without
	 * the preceeding pathname.
	 * @param dir raw directory path, for example "maps/" or "maps",
	 *   case-insensitive
	 */
	std::vector<std::string> GetDirsInDir(const std::string& dir, Section section);

	/**
	 * Adds an archive to the VFS.
	 * @param override determines whether in case of a  conflict, the existing
	 *   entry in the VFS is overwritten or not.
	 */
	bool AddArchive(const std::string& archiveName, bool overwrite);
	/**
	 * Adds an archive and all of its dependencies to the VFS.
	 * @param override determines whether in case of a  conflict, the existing
	 *   entry in the VFS is overwritten or not.
	 */
	bool AddArchiveWithDeps(const std::string& archiveName, bool overwrite);

	/**
	 * Removes an archive from the VFS.
	 * @return true if the archive is not loaded anymore; it was not loaded
	 *   in the first place, or was unloaded successfully.
	 */
	bool RemoveArchive(const std::string& archiveName);

	void DeleteArchives();

protected:
	struct FileData {
		IArchive* ar;
		int size;
	};
	std::array<std::map<std::string, FileData>, Section::Count> files;
	std::map<std::string, IArchive*> archives;

private:
	std::string GetNormalizedPath(const std::string& rawPath);
	const FileData* GetFileData(const std::string& normalizedFilePath, Section section);
};

extern CVFSHandler* vfsHandler;

#endif // _VFS_HANDLER_H