coastline 0.0.4
Loading...
Searching...
No Matches
savefile.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <filesystem>
6#include <functional>
7#include <memory>
8#include <optional>
9#include <span>
10#include <string_view>
11#include <vector>
12
13namespace coastline {
14
20
29 std::size_t limit);
30
33class SaveFile {
34public:
42
48 SaveFile(SaveFile &&) noexcept;
53 SaveFile &operator=(SaveFile &&) noexcept;
54
61 std::optional<std::vector<std::uint8_t>> Read(std::size_t limit) const;
62
70 bool Write(std::span<const std::uint8_t> bytes) const;
71
77 bool Remove() const;
78
79private:
80 struct State;
81 explicit SaveFile(std::unique_ptr<State> state);
82 std::unique_ptr<State> m_state;
83};
84
93bool WriteSaveFile(const std::filesystem::path &path, std::span<const std::uint8_t> bytes);
94
103bool WriteSaveFile(const std::filesystem::path &path, std::string_view text);
104
117std::optional<std::vector<std::uint8_t>>
118ReadVerifiedSaveFile(const std::filesystem::path &path,
119 std::size_t limit,
120 bool resetInvalid = true,
121 const SavePayloadValidator &validate = {});
122
132
133} // namespace coastline
SaveFile(SaveFile &&) noexcept
Transfer the source save's process lock without releasing it.
std::optional< std::vector< std::uint8_t > > Read(std::size_t limit) const
Read the locked save with bounded allocation.
~SaveFile()
Release the process lock and its native handle.
bool Remove() const
Delete the locked save while excluding cooperating writers.
static std::optional< SaveFile > Open(const std::filesystem::path &path)
Lock a save for a complete read-modify-write transaction, creating parent directories.
bool Write(std::span< const std::uint8_t > bytes) const
Replace the save atomically after writing and synchronising a unique sibling file.
Definition assetimport.h:11
std::optional< std::vector< std::uint8_t > > ReadSaveFile(const std::filesystem::path &path, std::size_t limit)
Read a complete regular file without allocating beyond a caller-supplied limit.
std::optional< std::vector< std::uint8_t > > ReadVerifiedSaveFile(const std::filesystem::path &path, std::size_t limit, bool resetInvalid=true, const SavePayloadValidator &validate={})
Read an internal binary save with its format signature, payload length, and SHA-1 checksum.
bool WriteVerifiedSaveFile(const std::filesystem::path &path, std::span< const std::uint8_t > bytes)
Atomically write an internal binary save with a format signature, length, and SHA-1 checksum.
bool WriteSaveFile(const std::filesystem::path &path, std::span< const std::uint8_t > bytes)
Lock and atomically replace a binary save.
std::function< bool(std::span< const std::uint8_t >)> SavePayloadValidator
Validate format-specific fields of an internal save's payload under its exclusive process lock.
Definition savefile.h:19