Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Filesystem.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_PS_FILESYSTEM
19#define INCLUDED_PS_FILESYSTEM
20
22#include "lib/file/vfs/vfs.h"
24#include "lib/status.h"
25#include "ps/Errors.h"
26
27#include <cstddef>
28#include <memory>
29
30class CStr8;
31
32extern PIVFS g_VFS;
33
34extern bool VfsFileExists(const VfsPath& pathname);
35
36extern bool VfsDirectoryExists(const VfsPath& pathname);
37
38/**
39 * callback function type for file change notifications
40 */
41typedef Status (*FileReloadFunc)(void* param, const VfsPath& path);
42
43/**
44 * register a callback function to be called by ReloadChangedFiles
45 */
46void RegisterFileReloadFunc(FileReloadFunc func, void* obj);
47
48/**
49 * delete a callback function registered with RegisterFileReloadFunc
50 * (removes any with the same func and obj)
51 */
52void UnregisterFileReloadFunc(FileReloadFunc func, void* obj);
53
54/**
55 * poll for directory change notifications and reload all affected files.
56 * must be called regularly (e.g. once a frame), else notifications
57 * may be lost.
58 * note: polling is much simpler than asynchronous notifications.
59 **/
61
63ERROR_TYPE(CVFSFile, LoadFailed);
64ERROR_TYPE(CVFSFile, AlreadyLoaded);
65
66/**
67 * Reads a file, then gives read-only access to the contents
68 */
70{
71public:
72 CVFSFile();
73 ~CVFSFile();
74
75 /**
76 * Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed
77 * @note Dies if the file has already been successfully loaded
78 * @param log Whether to log a failure to load a file
79 */
80 PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, bool log = true);
81
82 /**
83 * Returns buffer of this file as a stream of bytes
84 * @note file must have been successfully loaded
85 */
86 const std::uint8_t* GetBuffer() const;
87 size_t GetBufferSize() const;
88
89 /**
90 * Returns contents of file as a string
91 * @note file must have been successfully loaded
92 */
93 CStr8 GetAsString() const;
94
95 /**
96 * Returns contents of a UTF-8 encoded file as a string with optional BOM removed
97 * @note file must have been successfully loaded
98 */
99 CStr8 DecodeUTF8() const;
100
101private:
102 std::unique_ptr<std::uint8_t[], AlignedDeleter> m_Buffer;
104};
105
106#endif // #ifndef INCLUDED_PS_FILESYSTEM
std::uint32_t PSRETURN
Definition: Errors.h:75
void RegisterFileReloadFunc(FileReloadFunc func, void *obj)
register a callback function to be called by ReloadChangedFiles
Definition: Filesystem.cpp:51
ERROR_GROUP(CVFSFile)
ERROR_TYPE(CVFSFile, LoadFailed)
bool VfsDirectoryExists(const VfsPath &pathname)
Definition: Filesystem.cpp:45
void UnregisterFileReloadFunc(FileReloadFunc func, void *obj)
delete a callback function registered with RegisterFileReloadFunc (removes any with the same func and...
Definition: Filesystem.cpp:56
PIVFS g_VFS
Definition: Filesystem.cpp:36
bool VfsFileExists(const VfsPath &pathname)
Definition: Filesystem.cpp:40
Status ReloadChangedFiles()
poll for directory change notifications and reload all affected files.
Definition: Filesystem.cpp:82
Status(* FileReloadFunc)(void *param, const VfsPath &path)
callback function type for file change notifications
Definition: Filesystem.h:41
Reads a file, then gives read-only access to the contents.
Definition: Filesystem.h:70
~CVFSFile()
Definition: Filesystem.cpp:116
CStr8 GetAsString() const
Returns contents of file as a string.
Definition: Filesystem.cpp:152
std::unique_ptr< std::uint8_t[], AlignedDeleter > m_Buffer
Definition: Filesystem.h:102
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename, bool log=true)
Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed.
Definition: Filesystem.cpp:120
CVFSFile()
Definition: Filesystem.cpp:111
CStr8 DecodeUTF8() const
Returns contents of a UTF-8 encoded file as a string with optional BOM removed.
Definition: Filesystem.cpp:157
size_t m_BufferSize
Definition: Filesystem.h:103
const std::uint8_t * GetBuffer() const
Returns buffer of this file as a stream of bytes.
Definition: Filesystem.cpp:142
size_t GetBufferSize() const
Definition: Filesystem.cpp:147
Definition: path.h:84
def log(severity, message)
Definition: tests.py:27
Definition: vfs_util.cpp:42
std::int64_t Status
Error handling system.
Definition: status.h:174
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:240