speciesgen/include/starbound/patch.h

111 lines
3.2 KiB
C++

/*
speciesgen
Copyright (C) 2022-2023 prisixia
This file is part of speciesgen.
speciesgen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
speciesgen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with speciesgen. If not, see <https://www.gnu.org/licenses/>.
*/
/////////////////////////////////////////////////////////////////////////////
// Name: patch.cpp
// Purpose: Implementation of Patch and related
// Author: prisixia
// Created: 2022-10-02
// Copyright: (C) 2022-2023 prisixia
// Licence: GNU General Public License version 3
/////////////////////////////////////////////////////////////////////////////
#ifndef SPECIESGEN_STARBOUND_PATCH_H
#define SPECIESGEN_STARBOUND_PATCH_H
#include <algorithm>
#include <filesystem>
#include <string>
#include "cereal/cereal.hpp"
#include "magic_enum.hpp"
namespace Starbound::Patch {
enum class Operation {
Test = 0,
Add = 1,
Remove = 2,
Copy = 3,
Move = 4,
Replace = 5
};
template <typename Value = std::string> class Patch {
public:
Patch() = delete;
Patch(std::filesystem::path path, Operation op = Operation::Add)
: m_op(op), m_path(std::move(path)) {}
Patch(std::filesystem::path path, Value value,
Operation op = Operation::Add)
: m_op(op), m_path(std::move(path)), m_value(value) {}
[[nodiscard]] Operation GetOperation() const { return m_op; }
[[nodiscard]] std::filesystem::path GetPath() const { return m_path; }
[[nodiscard]] Value GetValue() const { return m_value; }
void SetOperation(const Operation op) { m_op = op; }
void SetPath(const std::filesystem::path &path) { m_path = path; }
void SetValue(const Value value) { m_value = value; }
private:
Operation m_op;
std::filesystem::path m_path;
Value m_value;
};
} // namespace Starbound::Patch
//----------------------------------------------------------------------
// Serialisation
//----------------------------------------------------------------------
namespace cereal {
template <class Archive, typename Value>
static void
load_and_construct(Archive &archive,
construct<Starbound::Patch::Patch<Value>> &construct) {
std::string _op;
std::filesystem::path path;
Value value;
archive(CEREAL_NVP_("op", _op), CEREAL_NVP(path), CEREAL_NVP(value));
const auto op =
magic_enum::enum_cast<Starbound::Patch::Operation>(_op).value_or(
Starbound::Patch::Operation::Add);
construct(path, value, op);
}
template <class Archive, typename Value>
void Save(Archive &archive, Starbound::Patch::Patch<Value> const &m) {
std::string op =
static_cast<std::string>(magic_enum::enum_name(m.GetOperation()))
.c_str();
const std::string path = m.GetPath().string();
const Value value = m.GetValue();
std::for_each(op.begin(), op.end(), [](char &c) { c = ::tolower(c); });
archive(CEREAL_NVP(op), CEREAL_NVP(path), CEREAL_NVP(value));
}
} // namespace cereal
#endif // SPECIESGEN_STARBOUND_PATCH_HPP