| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* |
| 10 |
* Some simple physfs extensions |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#pragma once |
| 15 |
|
| 16 |
#include <cstddef> |
| 17 |
#include <cstring> |
| 18 |
#include <memory> |
| 19 |
#include <ranges> |
| 20 |
#include <span> |
| 21 |
#include <stdarg.h> |
| 22 |
#include <type_traits> |
| 23 |
|
| 24 |
// When PhysicsFS can *easily* be built as a framework on Mac OS X, |
| 25 |
// the framework form will be supported again -kreatordxx |
| 26 |
#if 1 //!(defined(__APPLE__) && defined(__MACH__)) |
| 27 |
#include <physfs.h> |
| 28 |
#else |
| 29 |
#include <physfs/physfs.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "fmtcheck.h" |
| 33 |
#include "dxxsconf.h" |
| 34 |
#include "dsx-ns.h" |
| 35 |
#include "dxxerror.h" |
| 36 |
#include "vecmat.h" |
| 37 |
#include "byteutil.h" |
| 38 |
|
| 39 |
#include <stdexcept> |
| 40 |
#include "u_mem.h" |
| 41 |
#include "pack.h" |
| 42 |
#include "ntstring.h" |
| 43 |
#include "fwd-partial_range.h" |
| 44 |
#include <array> |
| 45 |
#include <memory> |
| 46 |
|
| 47 |
namespace dcx { |
| 48 |
|
| 49 |
template <typename V> |
| 50 |
dxx_compiler_attribute_always_inline() |
| 51 |
static inline PHYSFS_sint64 PHYSFSX_check_readBytes(PHYSFS_File *const file, V *const buffer, const PHYSFS_uint64 len) |
| 52 |
{ |
| 53 |
static_assert(std::is_standard_layout<V>::value && std::is_trivial<V>::value, "non-POD value read"); |
| 54 |
#if defined(DXX_HAVE_BUILTIN_OBJECT_SIZE) && defined(DXX_CONSTANT_TRUE) |
| 55 |
if (const size_t compiler_determined_buffer_size{__builtin_object_size(buffer, 1)}; compiler_determined_buffer_size != static_cast<size_t>(-1) && DXX_CONSTANT_TRUE(len > compiler_determined_buffer_size)) |
| 56 |
DXX_ALWAYS_ERROR_FUNCTION("read size exceeds element size"); |
| 57 |
#endif |
| 58 |
return {PHYSFS_readBytes(file, buffer, {len})}; |
| 59 |
} |
| 60 |
|
| 61 |
template <typename V, std::size_t N> |
| 62 |
dxx_compiler_attribute_always_inline() |
| 63 |
static inline PHYSFS_sint64 PHYSFSX_check_readBytes(PHYSFS_File *const file, std::array<V, N> &buffer, const PHYSFS_uint64 len) |
| 64 |
{ |
| 65 |
static_assert(std::is_standard_layout<V>::value && std::is_trivial<V>::value, "C++ array of non-POD elements read"); |
| 66 |
#ifdef DXX_CONSTANT_TRUE |
| 67 |
if (constexpr size_t compiler_determined_buffer_size{sizeof(V) * N}; DXX_CONSTANT_TRUE(len > compiler_determined_buffer_size)) |
| 68 |
DXX_ALWAYS_ERROR_FUNCTION("read size exceeds array size"); |
| 69 |
#endif |
| 70 |
return {PHYSFSX_check_readBytes(file, std::data(buffer), {len})}; |
| 71 |
} |
| 72 |
|
| 73 |
template <typename V, typename D> |
| 74 |
dxx_compiler_attribute_always_inline() |
| 75 |
static inline PHYSFS_sint64 PHYSFSX_check_readBytes(PHYSFS_File *const file, const std::unique_ptr<V, D> &v, const PHYSFS_uint64 len) |
| 76 |
{ |
| 77 |
return {PHYSFSX_check_readBytes(file, v.get(), {len})}; |
| 78 |
} |
| 79 |
|
| 80 |
template <typename V> |
| 81 |
dxx_compiler_attribute_always_inline() |
| 82 |
static inline PHYSFS_sint64 PHYSFSX_check_writeBytes(PHYSFS_File *file, const V *const buffer, const PHYSFS_uint64 len) |
| 83 |
{ |
| 84 |
static_assert(std::is_standard_layout<V>::value && std::is_trivial<V>::value, "non-POD value written"); |
| 85 |
#if defined(DXX_HAVE_BUILTIN_OBJECT_SIZE) && defined(DXX_CONSTANT_TRUE) |
| 86 |
if (const size_t compiler_determined_buffer_size{__builtin_object_size(buffer, 1)}; compiler_determined_buffer_size != static_cast<size_t>(-1) && DXX_CONSTANT_TRUE(len > compiler_determined_buffer_size)) |
| 87 |
DXX_ALWAYS_ERROR_FUNCTION("write size exceeds element size"); |
| 88 |
#endif |
| 89 |
return {PHYSFS_writeBytes(file, buffer, len)}; |
| 90 |
} |
| 91 |
|
| 92 |
template <typename V, std::size_t N> |
| 93 |
dxx_compiler_attribute_always_inline() |
| 94 |
static inline PHYSFS_sint64 PHYSFSX_check_writeBytes(PHYSFS_File *file, const std::array<V, N> &buffer, const PHYSFS_uint64 len) |
| 95 |
{ |
| 96 |
static_assert(std::is_standard_layout<V>::value && std::is_trivial<V>::value, "C++ array of non-POD elements written"); |
| 97 |
#ifdef DXX_CONSTANT_TRUE |
| 98 |
if (constexpr size_t compiler_determined_buffer_size{sizeof(V) * N}; DXX_CONSTANT_TRUE(len > compiler_determined_buffer_size)) |
| 99 |
DXX_ALWAYS_ERROR_FUNCTION("write size exceeds array size"); |
| 100 |
#endif |
| 101 |
return {PHYSFSX_check_writeBytes(file, buffer.data(), len)}; |
| 102 |
} |
| 103 |
|
| 104 |
template <typename T, typename D> |
| 105 |
dxx_compiler_attribute_always_inline() |
| 106 |
static inline PHYSFS_sint64 PHYSFSX_check_writeBytes(PHYSFS_File *file, const std::unique_ptr<T, D> &p, const PHYSFS_uint64 len) |
| 107 |
{ |
| 108 |
return {PHYSFSX_check_writeBytes(file, p.get(), len)}; |
| 109 |
} |
| 110 |
|
| 111 |
template <typename V> |
| 112 |
PHYSFS_sint64 PHYSFSX_check_readBytes(PHYSFS_File *file, exact_type<V> v, PHYSFS_uint64 C) = delete; |
| 113 |
template <typename V> |
| 114 |
PHYSFS_sint64 PHYSFSX_check_writeBytes(PHYSFS_File *file, exact_type<V> v, PHYSFS_uint32 S, PHYSFS_uint32 C) = delete; |
| 115 |
|
| 116 |
PHYSFS_sint64 PHYSFSX_check_readBytes(PHYSFS_File *file, auto **v, PHYSFS_uint64 C) = delete; |
| 117 |
PHYSFS_sint64 PHYSFSX_check_writeBytes(PHYSFS_File *file, auto **v, PHYSFS_uint32 S, PHYSFS_uint32 C) = delete; |
| 118 |
#define PHYSFSX_readBytes(HANDLE, BUFFER, LEN) PHYSFSX_check_readBytes(HANDLE, BUFFER, LEN) |
| 119 |
#define PHYSFSX_writeBytes(HANDLE, BUFFER, LEN) PHYSFSX_check_writeBytes(HANDLE, BUFFER, LEN) |
| 120 |
|
| 121 |
enum class physfsx_endian : bool |
| 122 |
{ |
| 123 |
native, |
| 124 |
foreign, |
| 125 |
}; |
| 126 |
|
| 127 |
static inline PHYSFS_sint64 PHYSFSX_writeU8(PHYSFS_File *file, PHYSFS_uint8 val) |
| 128 |
{ |
| 129 |
return {PHYSFS_writeBytes(file, &val, 1)}; |
| 130 |
} |
| 131 |
|
| 132 |
#ifdef DXX_BUILD_DESCENT |
| 133 |
#if DXX_BUILD_DESCENT == 2 |
| 134 |
static inline PHYSFS_sint64 PHYSFSX_writeString(PHYSFS_File *file, const char *s) |
| 135 |
{ |
| 136 |
return {PHYSFS_writeBytes(file, s, strlen(s) + 1)}; |
| 137 |
} |
| 138 |
#endif |
| 139 |
#endif |
| 140 |
|
| 141 |
static inline auto PHYSFSX_puts(PHYSFS_File *file, const std::span<const char> s) |
| 142 |
{ |
| 143 |
return PHYSFS_writeBytes(file, s.data(), s.size()); |
| 144 |
} |
| 145 |
|
| 146 |
static inline auto PHYSFSX_puts_literal(PHYSFS_File *file, const std::span<const char> s) |
| 147 |
{ |
| 148 |
return PHYSFS_writeBytes(file, s.data(), s.size() - 1); |
| 149 |
} |
| 150 |
|
| 151 |
static inline int PHYSFSX_fgetc(PHYSFS_File *const fp) |
| 152 |
{ |
| 153 |
unsigned char c; |
| 154 |
|
| 155 |
if (PHYSFSX_readBytes(fp, &c, 1) != 1) |
| 156 |
return EOF; |
| 157 |
|
| 158 |
return c; |
| 159 |
} |
| 160 |
|
| 161 |
static inline int PHYSFSX_fseek(PHYSFS_File *fp, long int offset, int where) |
| 162 |
{ |
| 163 |
int c, goal_position; |
| 164 |
|
| 165 |
switch(where) |
| 166 |
{ |
| 167 |
case SEEK_SET: |
| 168 |
goal_position = offset; |
| 169 |
break; |
| 170 |
case SEEK_CUR: |
| 171 |
goal_position = PHYSFS_tell(fp) + offset; |
| 172 |
break; |
| 173 |
case SEEK_END: |
| 174 |
goal_position = PHYSFS_fileLength(fp) + offset; |
| 175 |
break; |
| 176 |
default: |
| 177 |
return 1; |
| 178 |
} |
| 179 |
c = PHYSFS_seek(fp, goal_position); |
| 180 |
return !c; |
| 181 |
} |
| 182 |
|
| 183 |
template <std::size_t N> |
| 184 |
struct PHYSFSX_gets_line_t : |
| 185 |
#if DXX_HAVE_POISON |
| 186 |
/* Force onto heap to improve checker accuracy */ |
| 187 |
private std::unique_ptr<std::array<char, N>> |
| 188 |
#else |
| 189 |
private std::array<char, N> |
| 190 |
#endif |
| 191 |
{ |
| 192 |
PHYSFSX_gets_line_t() = default; |
| 193 |
PHYSFSX_gets_line_t(const PHYSFSX_gets_line_t &) = delete; |
| 194 |
PHYSFSX_gets_line_t &operator=(const PHYSFSX_gets_line_t &) = delete; |
| 195 |
using line_t = std::array<char, N>; |
| 196 |
#if DXX_HAVE_POISON |
| 197 |
using base_type = std::unique_ptr<line_t>; |
| 198 |
const line_t &line() const { return *this->base_type::get(); } |
| 199 |
line_t &line() { return *this->base_type::get(); } |
| 200 |
std::span<char, N> next() |
| 201 |
{ |
| 202 |
static_cast<base_type &>(*this) = std::make_unique_for_overwrite<line_t>(); |
| 203 |
return line(); |
| 204 |
} |
| 205 |
typename line_t::reference operator[](const typename line_t::size_type i) { return line()[i]; } |
| 206 |
typename line_t::const_reference operator[](const typename line_t::size_type i) const { return line()[i]; } |
| 207 |
#else |
| 208 |
const line_t &line() const { return *this; } |
| 209 |
line_t &line() { return *this; } |
| 210 |
std::span<char, N> next() { return line(); } |
| 211 |
using line_t::operator[]; |
| 212 |
#endif |
| 213 |
operator char *() { return line().data(); } |
| 214 |
operator const char *() const { return line().data(); } |
| 215 |
static constexpr std::size_t size() { return N; } |
| 216 |
typename line_t::const_iterator begin() const { return line().begin(); } |
| 217 |
typename line_t::const_iterator end() const { return line().end(); } |
| 218 |
}; |
| 219 |
|
| 220 |
template <> |
| 221 |
struct PHYSFSX_gets_line_t<0> |
| 222 |
{ |
| 223 |
#define DXX_ALLOCATE_PHYSFS_LINE(n) std::make_unique_for_overwrite<char[]>(n) |
| 224 |
#if !DXX_HAVE_POISON |
| 225 |
const |
| 226 |
#endif |
| 227 |
std::unique_ptr<char[]> m_line; |
| 228 |
const std::size_t m_length; |
| 229 |
PHYSFSX_gets_line_t(const std::size_t n) : |
| 230 |
#if !DXX_HAVE_POISON |
| 231 |
m_line{DXX_ALLOCATE_PHYSFS_LINE(n)}, |
| 232 |
#endif |
| 233 |
m_length{n} |
| 234 |
{ |
| 235 |
} |
| 236 |
char *line() { return m_line.get(); } |
| 237 |
const char *line() const { return m_line.get(); } |
| 238 |
std::span<char> next() |
| 239 |
{ |
| 240 |
#if DXX_HAVE_POISON |
| 241 |
/* Reallocate to tell checker to undefine the buffer */ |
| 242 |
m_line = DXX_ALLOCATE_PHYSFS_LINE(m_length); |
| 243 |
#endif |
| 244 |
return std::span<char>(m_line.get(), m_length); |
| 245 |
} |
| 246 |
std::size_t size() const { return m_length; } |
| 247 |
operator const char *() const { return m_line.get(); } |
| 248 |
const char *begin() const { return *this; } |
| 249 |
const char *end() const { return std::next(begin(), m_length); } |
| 250 |
operator const void *() const = delete; |
| 251 |
#undef DXX_ALLOCATE_PHYSFS_LINE |
| 252 |
}; |
| 253 |
|
| 254 |
class PHYSFSX_fgets_t |
| 255 |
{ |
| 256 |
template <std::size_t Extent> |
| 257 |
[[nodiscard]] |
| 258 |
static auto get(const std::span<char, Extent> buf, const std::size_t offset, PHYSFS_File *const fp) |
| 259 |
{ |
| 260 |
if (offset > buf.size()) |
| 261 |
throw std::invalid_argument("offset too large"); |
| 262 |
return get(buf.subspan(offset), fp); |
| 263 |
} |
| 264 |
public: |
| 265 |
template <std::size_t n> |
| 266 |
[[nodiscard]] |
| 267 |
dxx_compiler_attribute_nonnull() |
| 268 |
auto operator()(PHYSFSX_gets_line_t<n> &buf, PHYSFS_File *const fp, const std::size_t offset = 0) const |
| 269 |
{ |
| 270 |
return get(buf.next(), offset, fp); |
| 271 |
} |
| 272 |
template <std::size_t n> |
| 273 |
[[nodiscard]] |
| 274 |
dxx_compiler_attribute_nonnull() |
| 275 |
auto operator()(ntstring<n> &buf, PHYSFS_File *const fp, const std::size_t offset = 0) const |
| 276 |
{ |
| 277 |
auto r = get(std::span(buf), offset, fp); |
| 278 |
buf.back() = 0; |
| 279 |
return r; |
| 280 |
} |
| 281 |
struct result : std::ranges::subrange<std::span<char>::iterator> |
| 282 |
{ |
| 283 |
using std::ranges::subrange<std::span<char>::iterator>::subrange; |
| 284 |
explicit operator bool() const |
| 285 |
{ |
| 286 |
/* get() indicates a failure to read data (including failure due to |
| 287 |
* end-of-file) by returning a value-initialized `result`. Treat a |
| 288 |
* value-initialized iterator as false, so that callers can readily |
| 289 |
* detect this case. |
| 290 |
*/ |
| 291 |
return begin() != std::span<char>::iterator{}; |
| 292 |
} |
| 293 |
}; |
| 294 |
private: |
| 295 |
[[nodiscard]] |
| 296 |
static result get(std::span<char> buf, PHYSFS_File *const fp); |
| 297 |
}; |
| 298 |
|
| 299 |
constexpr PHYSFSX_fgets_t PHYSFSX_fgets{}; |
| 300 |
|
| 301 |
int PHYSFSX_printf(PHYSFS_File *file, const char *format) = delete; |
| 302 |
|
| 303 |
dxx_compiler_attribute_format_printf(2, 3) |
| 304 |
static inline int PHYSFSX_printf(PHYSFS_File *file, const char *format, ...) |
| 305 |
{ |
| 306 |
char buffer[1024]; |
| 307 |
va_list args; |
| 308 |
|
| 309 |
va_start(args, format); |
| 310 |
const std::size_t len = std::max(vsnprintf(buffer, sizeof(buffer), format, args), 0); |
| 311 |
va_end(args); |
| 312 |
|
| 313 |
return PHYSFSX_puts(file, {buffer, len}); |
| 314 |
} |
| 315 |
|
| 316 |
#define PHYSFSX_writeFix PHYSFS_writeSLE32 |
| 317 |
#define PHYSFSX_writeFixAng PHYSFS_writeSLE16 |
| 318 |
|
| 319 |
static inline int PHYSFSX_writeVector(PHYSFS_File *file, const vms_vector &v) |
| 320 |
{ |
| 321 |
if (PHYSFSX_writeFix(file, v.x) < 1 || |
| 322 |
PHYSFSX_writeFix(file, v.y) < 1 || |
| 323 |
PHYSFSX_writeFix(file, v.z) < 1) |
| 324 |
return 0; |
| 325 |
|
| 326 |
return 1; |
| 327 |
} |
| 328 |
|
| 329 |
struct NamedPHYSFS_File |
| 330 |
{ |
| 331 |
PHYSFS_File *const fp; |
| 332 |
const char *const filename; |
| 333 |
operator PHYSFS_File *() const |
| 334 |
{ |
| 335 |
return fp; |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
[[noreturn]] |
| 340 |
dxx_compiler_attribute_cold |
| 341 |
void PHYSFSX_read_helper_report_error(const char *filename, unsigned line, const char *func, NamedPHYSFS_File file); |
| 342 |
|
| 343 |
template <typename T, auto F> |
| 344 |
struct PHYSFSX_read_helper |
| 345 |
{ |
| 346 |
T operator()(NamedPHYSFS_File file, const char *filename = __builtin_FILE(), unsigned line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) const; |
| 347 |
}; |
| 348 |
|
| 349 |
template <typename T, auto F> |
| 350 |
[[nodiscard]] |
| 351 |
T PHYSFSX_read_helper<T, F>::operator()(const NamedPHYSFS_File file, const char *const filename, const unsigned line, const char *const func) const |
| 352 |
{ |
| 353 |
T i; |
| 354 |
if (!F(file.fp, &i)) |
| 355 |
PHYSFSX_read_helper_report_error(filename, line, func, file); |
| 356 |
return i; |
| 357 |
} |
| 358 |
|
| 359 |
template <typename T, T (*swap_value)(const T &)> |
| 360 |
struct PHYSFSX_read_swap_helper |
| 361 |
{ |
| 362 |
[[nodiscard]] |
| 363 |
T operator()(PHYSFS_File *const file, physfsx_endian swap) const |
| 364 |
{ |
| 365 |
T val{}; |
| 366 |
PHYSFSX_readBytes(file, &val, sizeof(val)); |
| 367 |
return swap != physfsx_endian::native ? swap_value(val) : val; |
| 368 |
} |
| 369 |
}; |
| 370 |
|
| 371 |
template <typename T1, int (*F)(PHYSFS_File *, T1 *), typename T2, T1 T2::* ... m> |
| 372 |
struct PHYSFSX_read_sequence_helper |
| 373 |
{ |
| 374 |
void operator()(const NamedPHYSFS_File file, T2 &i, const char *filename = __builtin_FILE(), unsigned line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) const; |
| 375 |
}; |
| 376 |
|
| 377 |
template <typename T1, int (*F)(PHYSFS_File *, T1 *), typename T2, T1 T2::* ... m> |
| 378 |
void PHYSFSX_read_sequence_helper<T1, F, T2, m...>::operator()(const NamedPHYSFS_File file, T2 &i, const char *const filename, const unsigned line, const char *const func) const |
| 379 |
{ |
| 380 |
if (unlikely(!F(file.fp, &(i.*m)) || ...)) |
| 381 |
PHYSFSX_read_helper_report_error(filename, line, func, file); |
| 382 |
} |
| 383 |
|
| 384 |
static inline int PHYSFSX_readS8(PHYSFS_File *const file, int8_t *const b) |
| 385 |
{ |
| 386 |
return PHYSFSX_readBytes(file, b, sizeof(*b)) == sizeof(*b); |
| 387 |
} |
| 388 |
|
| 389 |
static constexpr PHYSFSX_read_helper<int8_t, PHYSFSX_readS8> PHYSFSX_readByte{}; |
| 390 |
static constexpr PHYSFSX_read_helper<int16_t, PHYSFS_readSLE16> PHYSFSX_readSLE16{}; |
| 391 |
static constexpr PHYSFSX_read_helper<uint16_t, PHYSFS_readULE16> PHYSFSX_readULE16{}; |
| 392 |
static constexpr PHYSFSX_read_helper<int32_t, PHYSFS_readSLE32> PHYSFSX_readInt{}; |
| 393 |
static constexpr PHYSFSX_read_helper<uint32_t, PHYSFS_readULE32> PHYSFSX_readULE32{}; |
| 394 |
static constexpr PHYSFSX_read_helper<fix, PHYSFS_readSLE32> PHYSFSX_readFix{}; |
| 395 |
static constexpr PHYSFSX_read_helper<fixang, PHYSFS_readSLE16> PHYSFSX_readFixAng{}; |
| 396 |
|
| 397 |
static constexpr PHYSFSX_read_swap_helper<PHYSFS_sint16, SWAPSHORT> PHYSFSX_readSXE16{}; |
| 398 |
static constexpr PHYSFSX_read_swap_helper<PHYSFS_uint16, SWAPSHORT> PHYSFSX_readUXE16{}; |
| 399 |
static constexpr PHYSFSX_read_swap_helper<PHYSFS_sint32, SWAPINT> PHYSFSX_readSXE32{}; |
| 400 |
static constexpr PHYSFSX_read_swap_helper<PHYSFS_uint32, SWAPINT> PHYSFSX_readUXE32{}; |
| 401 |
|
| 402 |
static constexpr PHYSFSX_read_sequence_helper<fix, PHYSFS_readSLE32, vms_vector, &vms_vector::x, &vms_vector::y, &vms_vector::z> PHYSFSX_readVector{}; |
| 403 |
static constexpr PHYSFSX_read_sequence_helper<fixang, PHYSFS_readSLE16, vms_angvec, &vms_angvec::p, &vms_angvec::b, &vms_angvec::h> PHYSFSX_readAngleVec{}; |
| 404 |
|
| 405 |
template <std::size_t N> |
| 406 |
static auto PHYSFSX_skipBytes(PHYSFS_File *const fp) |
| 407 |
{ |
| 408 |
std::array<uint8_t, N> skip; |
| 409 |
return PHYSFS_readBytes(fp, std::data(skip), std::size(skip)); |
| 410 |
} |
| 411 |
|
| 412 |
class PHYSFS_File_deleter |
| 413 |
{ |
| 414 |
public: |
| 415 |
int operator()(PHYSFS_File *fp) const |
| 416 |
{ |
| 417 |
return PHYSFS_close(fp); |
| 418 |
} |
| 419 |
}; |
| 420 |
|
| 421 |
class RAIIPHYSFS_File : public std::unique_ptr<PHYSFS_File, PHYSFS_File_deleter> |
| 422 |
{ |
| 423 |
typedef std::unique_ptr<PHYSFS_File, PHYSFS_File_deleter> base_t; |
| 424 |
public: |
| 425 |
using base_t::base_t; |
| 426 |
using base_t::operator bool; |
| 427 |
operator PHYSFS_File *() const && = delete; |
| 428 |
operator PHYSFS_File *() const & |
| 429 |
{ |
| 430 |
return get(); |
| 431 |
} |
| 432 |
int close() |
| 433 |
{ |
| 434 |
/* Like reset(), but returns result */ |
| 435 |
int r = get_deleter()(get()); |
| 436 |
if (r) |
| 437 |
release(); |
| 438 |
return r; |
| 439 |
} |
| 440 |
bool operator==(auto &&) const = delete; |
| 441 |
bool operator!=(auto &&) const = delete; |
| 442 |
}; |
| 443 |
|
| 444 |
class RAIINamedPHYSFS_File : public RAIIPHYSFS_File |
| 445 |
{ |
| 446 |
public: |
| 447 |
const char *filename{}; |
| 448 |
RAIINamedPHYSFS_File() = default; |
| 449 |
RAIINamedPHYSFS_File(RAIIPHYSFS_File fp, const char *filename) : |
| 450 |
RAIIPHYSFS_File{std::move(fp)}, filename{filename} |
| 451 |
{ |
| 452 |
} |
| 453 |
RAIINamedPHYSFS_File(PHYSFS_File *fp, const char *filename) : |
| 454 |
RAIIPHYSFS_File{fp}, filename{filename} |
| 455 |
{ |
| 456 |
} |
| 457 |
operator NamedPHYSFS_File() const |
| 458 |
{ |
| 459 |
return {this->operator PHYSFS_File *(), filename}; |
| 460 |
} |
| 461 |
}; |
| 462 |
|
| 463 |
typedef char file_extension_t[5]; |
| 464 |
|
| 465 |
[[nodiscard]] |
| 466 |
dxx_compiler_attribute_nonnull() |
| 467 |
const file_extension_t *PHYSFSX_checkMatchingExtension(const char *filename, const std::ranges::subrange<const file_extension_t *> range); |
| 468 |
|
| 469 |
enum class physfs_search_path : bool |
| 470 |
{ |
| 471 |
prepend, |
| 472 |
append, |
| 473 |
}; |
| 474 |
|
| 475 |
PHYSFS_ErrorCode PHYSFSX_addRelToSearchPath(char *relname, std::array<char, PATH_MAX> &realPath, physfs_search_path); |
| 476 |
|
| 477 |
/* Callee may change the case of the characters in the supplied buffer, but |
| 478 |
* will not make the filename longer or shorter. */ |
| 479 |
void PHYSFSX_removeRelFromSearchPath(char *relname); |
| 480 |
[[nodiscard]] |
| 481 |
int PHYSFSX_fsize(char *hogname); |
| 482 |
[[nodiscard]] |
| 483 |
int PHYSFSX_exists_ignorecase(char *filename); |
| 484 |
|
| 485 |
extern void PHYSFSX_listSearchPathContent(); |
| 486 |
[[nodiscard]] |
| 487 |
int PHYSFSX_getRealPath(const char *stdPath, std::array<char, PATH_MAX> &realPath); |
| 488 |
|
| 489 |
class PHYSFS_unowned_storage_mount_deleter |
| 490 |
{ |
| 491 |
public: |
| 492 |
void operator()(const char *const p) const noexcept |
| 493 |
{ |
| 494 |
PHYSFS_unmount(p); |
| 495 |
} |
| 496 |
}; |
| 497 |
|
| 498 |
class PHYSFS_computed_path_mount_deleter : PHYSFS_unowned_storage_mount_deleter, std::default_delete<std::array<char, PATH_MAX>> |
| 499 |
{ |
| 500 |
public: |
| 501 |
using element_type = std::array<char, PATH_MAX>; |
| 502 |
PHYSFS_computed_path_mount_deleter() = default; |
| 503 |
PHYSFS_computed_path_mount_deleter(const PHYSFS_computed_path_mount_deleter &) = default; |
| 504 |
PHYSFS_computed_path_mount_deleter(PHYSFS_computed_path_mount_deleter &&) = default; |
| 505 |
PHYSFS_computed_path_mount_deleter(std::default_delete<element_type> &&d) : |
| 506 |
std::default_delete<element_type>{std::move(d)} |
| 507 |
{ |
| 508 |
} |
| 509 |
PHYSFS_computed_path_mount_deleter &operator=(PHYSFS_computed_path_mount_deleter &&) = default; |
| 510 |
void operator()(element_type *const p) const noexcept |
| 511 |
{ |
| 512 |
PHYSFS_unowned_storage_mount_deleter::operator()(p->data()); |
| 513 |
std::default_delete<element_type>::operator()(p); |
| 514 |
} |
| 515 |
}; |
| 516 |
|
| 517 |
/* RAIIPHYSFS_LiteralMount takes a pointer to storage, but does not take |
| 518 |
* ownership of the underlying storage. On destruction, it will pass |
| 519 |
* that pointer to PHYSFS_unmount. The pointer must remain valid until |
| 520 |
* RAIIPHYSFS_LiteralMount is destroyed. |
| 521 |
*/ |
| 522 |
using RAIIPHYSFS_LiteralMount = std::unique_ptr<const char, PHYSFS_unowned_storage_mount_deleter>; |
| 523 |
/* RAIIPHYSFS_ComputedPathMount owns a pointer to allocated storage. On |
| 524 |
* destruction, it will pass that pointer to PHYSFS_unmount, then free |
| 525 |
* the pointer. |
| 526 |
*/ |
| 527 |
using RAIIPHYSFS_ComputedPathMount = std::unique_ptr<typename PHYSFS_computed_path_mount_deleter::element_type, PHYSFS_computed_path_mount_deleter>; |
| 528 |
|
| 529 |
[[nodiscard]] |
| 530 |
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name, physfs_search_path position); |
| 531 |
|
| 532 |
extern int PHYSFSX_rename(const char *oldpath, const char *newpath); |
| 533 |
|
| 534 |
[[nodiscard]] |
| 535 |
std::pair<RAIINamedPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openReadBuffered(const char *filename); |
| 536 |
|
| 537 |
[[nodiscard]] |
| 538 |
std::pair<RAIINamedPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openReadBuffered_updateCase(char *filename); |
| 539 |
|
| 540 |
std::pair<RAIIPHYSFS_File, PHYSFS_ErrorCode> PHYSFSX_openWriteBuffered(const char *filename); |
| 541 |
extern void PHYSFSX_addArchiveContent(); |
| 542 |
extern void PHYSFSX_removeArchiveContent(); |
| 543 |
} |
| 544 |
|
| 545 |
#ifdef DXX_BUILD_DESCENT |
| 546 |
namespace dsx { |
| 547 |
|
| 548 |
bool PHYSFSX_init(int argc, char *argv[]); |
| 549 |
int PHYSFSX_checkSupportedArchiveTypes(); |
| 550 |
#if DXX_BUILD_DESCENT == 2 |
| 551 |
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *name1, char *name2, physfs_search_path); |
| 552 |
#endif |
| 553 |
|
| 554 |
} |
| 555 |
#endif |