| 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 |
#pragma once |
| 8 |
|
| 9 |
#include <stdexcept> |
| 10 |
#include "physfsx.h" |
| 11 |
#include "serial.h" |
| 12 |
|
| 13 |
namespace dcx { |
| 14 |
|
| 15 |
class PHYSFSX_short_read : public std::runtime_error |
| 16 |
{ |
| 17 |
public: |
| 18 |
PHYSFSX_short_read(PHYSFS_File *) : |
| 19 |
runtime_error("short read in PHYSFS file") |
| 20 |
{ |
| 21 |
} |
| 22 |
}; |
| 23 |
|
| 24 |
class PHYSFSX_short_write : public std::runtime_error |
| 25 |
{ |
| 26 |
public: |
| 27 |
PHYSFSX_short_write(PHYSFS_File *) : |
| 28 |
runtime_error("short write in PHYSFS file") |
| 29 |
{ |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
template <typename T, typename E = PHYSFSX_short_read> |
| 34 |
void PHYSFSX_serialize_read(const NamedPHYSFS_File fp, T &t) |
| 35 |
{ |
| 36 |
const std::size_t maximum_size{serial::message_type<T>::maximum_size}; |
| 37 |
uint8_t buf[maximum_size]; |
| 38 |
if (PHYSFSX_readBytes(fp, buf, maximum_size) != maximum_size) |
| 39 |
throw E(fp); |
| 40 |
serial::reader::le_bytebuffer b{buf}; |
| 41 |
serial::process_buffer(b, t); |
| 42 |
} |
| 43 |
|
| 44 |
template <typename T, typename E = PHYSFSX_short_write> |
| 45 |
void PHYSFSX_serialize_write(PHYSFS_File *fp, const T &t) |
| 46 |
{ |
| 47 |
const size_t maximum_size = serial::message_type<T>::maximum_size; |
| 48 |
uint8_t buf[maximum_size]; |
| 49 |
serial::writer::le_bytebuffer b{buf}; |
| 50 |
serial::process_buffer(b, t); |
| 51 |
if (PHYSFS_writeBytes(fp, buf, maximum_size) != maximum_size) |
| 52 |
throw E(fp); |
| 53 |
} |
| 54 |
|
| 55 |
} |