162 lines
4.8 KiB
C++
162 lines
4.8 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: frames.cpp
|
|
// Purpose: Implementation of Frames and related
|
|
// Author: prisixia
|
|
// Created: 2022-10-03
|
|
// Copyright: (C) 2022-2023 prisixia
|
|
// Licence: GNU General Public License version 3
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef SPECIESGEN_STARBOUND_FRAMES_H
|
|
#define SPECIESGEN_STARBOUND_FRAMES_H
|
|
|
|
#include <filesystem>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <tuple>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#include "cereal/types/string.hpp"
|
|
#include "cereal/types/vector.hpp"
|
|
|
|
namespace Starbound::Frames {
|
|
class Grid {
|
|
public:
|
|
Grid() = delete;
|
|
Grid(std::vector<std::vector<std::optional<std::string>>>,
|
|
const std::tuple<uint32_t, uint32_t> = {129, 129},
|
|
const std::tuple<uint32_t, uint32_t> = {9, 6});
|
|
|
|
[[nodiscard]] std::tuple<uint32_t, uint32_t> GetSize() const;
|
|
[[nodiscard]] std::tuple<uint32_t, uint32_t> GetDimensions() const;
|
|
[[nodiscard]] std::vector<std::vector<std::optional<std::string>>>
|
|
GetNames() const;
|
|
|
|
void SetSize(const std::tuple<uint32_t, uint32_t>);
|
|
void SetDimensions(const std::tuple<uint32_t, uint32_t>);
|
|
void SetNames(const std::vector<std::vector<std::optional<std::string>>> &);
|
|
|
|
private:
|
|
std::tuple<uint32_t, uint32_t> m_size;
|
|
std::tuple<uint32_t, uint32_t> m_dimensions;
|
|
std::vector<std::vector<std::optional<std::string>>> m_names;
|
|
};
|
|
|
|
class Frames {
|
|
public:
|
|
Frames() = delete;
|
|
Frames(std::unordered_map<std::string, std::string>, Grid);
|
|
|
|
[[nodiscard]] std::unordered_map<std::string, std::string>
|
|
GetAliases() const;
|
|
|
|
[[nodiscard]] Grid GetGrid() const;
|
|
|
|
void SetAliases(const std::unordered_map<std::string, std::string> &);
|
|
void SetGrid(const Grid &);
|
|
|
|
private:
|
|
std::unordered_map<std::string, std::string> m_aliases;
|
|
Grid m_grid;
|
|
};
|
|
} // namespace Starbound::Frames
|
|
|
|
//----------------------------------------------------------------------
|
|
// Serialisation
|
|
//----------------------------------------------------------------------
|
|
|
|
namespace cereal {
|
|
template <class Archive>
|
|
void Save(Archive &archive,
|
|
std::unordered_map<std::string, std::string> const &m) {
|
|
for (const std::pair<const std::string, std::string> &pair : m) {
|
|
archive(CEREAL_NVP_(pair.first.c_str(), pair.second));
|
|
}
|
|
}
|
|
|
|
template <class Archive>
|
|
void Save(Archive &archive, std::tuple<uint32_t, uint32_t> const &m) {
|
|
archive(make_size_tag(static_cast<size_type>(
|
|
std::tuple_size_v<std::tuple<uint32_t, uint32_t>>)));
|
|
archive(std::get<0>(m), std::get<1>(m));
|
|
}
|
|
|
|
template <class Archive>
|
|
void Save(Archive &archive, std::vector<std::optional<std::string>> const &m) {
|
|
archive(make_size_tag(static_cast<size_type>(m.size())));
|
|
|
|
for (const auto &opt : m) {
|
|
if (opt.has_value()) {
|
|
archive(opt.value());
|
|
} else {
|
|
archive(nullptr);
|
|
}
|
|
}
|
|
}
|
|
|
|
template <class Archive>
|
|
static void load_and_construct(Archive &archive,
|
|
construct<Starbound::Frames::Grid> &construct) {
|
|
std::tuple<uint32_t, uint32_t> size;
|
|
std::tuple<uint32_t, uint32_t> dimensions;
|
|
std::vector<std::vector<std::optional<std::string>>> names;
|
|
|
|
archive(CEREAL_NVP(size), CEREAL_NVP(dimensions), CEREAL_NVP(names));
|
|
|
|
construct(names, size, dimensions);
|
|
}
|
|
|
|
template <class Archive>
|
|
void Save(Archive &archive, Starbound::Frames::Grid const &m) {
|
|
const std::tuple<uint32_t, uint32_t> size = m.GetSize();
|
|
const std::tuple<uint32_t, uint32_t> dimensions = m.GetDimensions();
|
|
const std::vector<std::vector<std::optional<std::string>>> names =
|
|
m.GetNames();
|
|
|
|
archive(CEREAL_NVP(size), CEREAL_NVP(dimensions), CEREAL_NVP(names));
|
|
}
|
|
|
|
template <class Archive>
|
|
static void
|
|
load_and_construct(Archive &archive,
|
|
construct<Starbound::Frames::Frames> &construct) {
|
|
std::unordered_map<std::string, std::string> aliases;
|
|
Starbound::Frames::Grid grid{{}};
|
|
|
|
archive(CEREAL_NVP(aliases), CEREAL_NVP(grid));
|
|
|
|
construct(aliases, grid);
|
|
}
|
|
|
|
template <class Archive>
|
|
void Save(Archive &archive, Starbound::Frames::Frames const &m) {
|
|
const std::unordered_map<std::string, std::string> aliases = m.GetAliases();
|
|
const Starbound::Frames::Grid grid = m.GetGrid();
|
|
|
|
archive(CEREAL_NVP(aliases), CEREAL_NVP(grid));
|
|
}
|
|
} // namespace cereal
|
|
|
|
#endif // SPECIESGEN_STARBOUND_FRAMES_H
|