2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
#pragma once
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "common/byte_stream.h"
|
|
|
|
#include "common/fifo_queue.h"
|
|
|
|
#include "common/heap_array.h"
|
|
|
|
#include "common/types.h"
|
2019-09-09 07:01:26 +00:00
|
|
|
#include <cstring>
|
2019-09-14 10:28:47 +00:00
|
|
|
#include <deque>
|
2019-09-09 07:01:26 +00:00
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class String;
|
|
|
|
|
|
|
|
class StateWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Mode
|
|
|
|
{
|
|
|
|
Read,
|
|
|
|
Write
|
|
|
|
};
|
|
|
|
|
2020-10-12 10:46:59 +00:00
|
|
|
StateWrapper(ByteStream* stream, Mode mode, u32 version);
|
2019-09-09 07:01:26 +00:00
|
|
|
StateWrapper(const StateWrapper&) = delete;
|
|
|
|
~StateWrapper();
|
|
|
|
|
|
|
|
ByteStream* GetStream() const { return m_stream; }
|
|
|
|
bool HasError() const { return m_error; }
|
|
|
|
bool IsReading() const { return (m_mode == Mode::Read); }
|
|
|
|
bool IsWriting() const { return (m_mode == Mode::Write); }
|
|
|
|
Mode GetMode() const { return m_mode; }
|
2019-10-25 13:30:44 +00:00
|
|
|
void SetMode(Mode mode) { m_mode = mode; }
|
2020-10-12 10:46:59 +00:00
|
|
|
u32 GetVersion() const { return m_version; }
|
2019-09-09 07:01:26 +00:00
|
|
|
|
|
|
|
/// Overload for integral or floating-point types. Writes bytes as-is.
|
|
|
|
template<typename T, std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>, int> = 0>
|
|
|
|
void Do(T* value_ptr)
|
|
|
|
{
|
|
|
|
if (m_mode == Mode::Read)
|
|
|
|
{
|
|
|
|
if (m_error || (m_error |= !m_stream->Read2(value_ptr, sizeof(T))) == true)
|
|
|
|
*value_ptr = static_cast<T>(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_error)
|
|
|
|
m_error |= !m_stream->Write2(value_ptr, sizeof(T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload for enum types. Uses the underlying type.
|
|
|
|
template<typename T, std::enable_if_t<std::is_enum_v<T>, int> = 0>
|
|
|
|
void Do(T* value_ptr)
|
|
|
|
{
|
|
|
|
using TType = std::underlying_type_t<T>;
|
|
|
|
if (m_mode == Mode::Read)
|
|
|
|
{
|
|
|
|
TType temp;
|
|
|
|
if (m_error || (m_error |= !m_stream->Read2(&temp, sizeof(TType))) == true)
|
|
|
|
temp = static_cast<TType>(0);
|
|
|
|
|
|
|
|
*value_ptr = static_cast<T>(temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TType temp;
|
|
|
|
std::memcpy(&temp, value_ptr, sizeof(TType));
|
|
|
|
if (!m_error)
|
|
|
|
m_error |= !m_stream->Write2(&temp, sizeof(TType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload for POD types, such as structs.
|
|
|
|
template<typename T, std::enable_if_t<std::is_pod_v<T>, int> = 0>
|
|
|
|
void DoPOD(T* value_ptr)
|
|
|
|
{
|
|
|
|
if (m_mode == Mode::Read)
|
|
|
|
{
|
|
|
|
if (m_error || (m_error |= !m_stream->Read2(value_ptr, sizeof(T))) == true)
|
2019-10-22 13:07:51 +00:00
|
|
|
std::memset(value_ptr, 0, sizeof(*value_ptr));
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_error)
|
|
|
|
m_error |= !m_stream->Write2(value_ptr, sizeof(T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void DoArray(T* values, size_t count)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
Do(&values[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void DoPODArray(T* values, size_t count)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
DoPOD(&values[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoBytes(void* data, size_t length);
|
2023-03-16 09:33:59 +00:00
|
|
|
void DoBytesEx(void* data, size_t length, u32 version_introduced, const void* default_value);
|
2019-09-09 07:01:26 +00:00
|
|
|
|
|
|
|
void Do(bool* value_ptr);
|
|
|
|
void Do(std::string* value_ptr);
|
|
|
|
void Do(String* value_ptr);
|
|
|
|
|
2019-09-29 08:05:20 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
void Do(std::array<T, N>* data)
|
|
|
|
{
|
|
|
|
DoArray(data->data(), data->size());
|
|
|
|
}
|
|
|
|
|
2019-10-27 03:34:13 +00:00
|
|
|
template<typename T, size_t N>
|
2023-08-19 03:52:51 +00:00
|
|
|
void Do(FixedHeapArray<T, N>* data)
|
2019-10-27 03:34:13 +00:00
|
|
|
{
|
|
|
|
DoArray(data->data(), data->size());
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
template<typename T>
|
|
|
|
void Do(std::vector<T>* data)
|
|
|
|
{
|
|
|
|
u32 length = static_cast<u32>(data->size());
|
|
|
|
Do(&length);
|
|
|
|
if (m_mode == Mode::Read)
|
|
|
|
data->resize(length);
|
|
|
|
DoArray(data->data(), data->size());
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:28:47 +00:00
|
|
|
template<typename T>
|
|
|
|
void Do(std::deque<T>* data)
|
|
|
|
{
|
|
|
|
u32 length = static_cast<u32>(data->size());
|
|
|
|
Do(&length);
|
|
|
|
if (m_mode == Mode::Read)
|
|
|
|
{
|
|
|
|
data->clear();
|
|
|
|
for (u32 i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
Do(&value);
|
|
|
|
data->push_back(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < length; i++)
|
|
|
|
Do(&data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:22:23 +00:00
|
|
|
template<typename T, u32 CAPACITY>
|
|
|
|
void Do(FIFOQueue<T, CAPACITY>* data)
|
|
|
|
{
|
|
|
|
u32 size = data->GetSize();
|
|
|
|
Do(&size);
|
|
|
|
|
|
|
|
if (m_mode == Mode::Read)
|
|
|
|
{
|
|
|
|
T* temp = new T[size];
|
|
|
|
DoArray(temp, size);
|
2019-09-28 06:21:09 +00:00
|
|
|
data->Clear();
|
2019-09-17 09:22:23 +00:00
|
|
|
data->PushRange(temp, size);
|
|
|
|
delete[] temp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
T temp(data->Peek(i));
|
|
|
|
Do(&temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
bool DoMarker(const char* marker);
|
|
|
|
|
2020-10-12 10:46:59 +00:00
|
|
|
template<typename T>
|
|
|
|
void DoEx(T* data, u32 version_introduced, T default_value)
|
|
|
|
{
|
2023-03-16 09:33:59 +00:00
|
|
|
if (m_mode == Mode::Read && m_version < version_introduced)
|
2020-10-12 10:46:59 +00:00
|
|
|
{
|
|
|
|
*data = std::move(default_value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Do(data);
|
|
|
|
}
|
|
|
|
|
2022-03-26 14:52:11 +00:00
|
|
|
void SkipBytes(size_t count)
|
|
|
|
{
|
|
|
|
if (m_mode != Mode::Read)
|
|
|
|
{
|
|
|
|
m_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_error)
|
|
|
|
m_error = !m_stream->SeekRelative(static_cast<s64>(count));
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
private:
|
|
|
|
ByteStream* m_stream;
|
|
|
|
Mode m_mode;
|
2020-10-12 10:46:59 +00:00
|
|
|
u32 m_version;
|
2019-09-09 07:01:26 +00:00
|
|
|
bool m_error = false;
|
|
|
|
};
|