speciesgen/src/starbound/metadata.cpp

117 lines
3.9 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/>.
*/
#include "starbound/metadata.h"
namespace Starbound {
//----------------------------------------------------------------------
// Metadata
//----------------------------------------------------------------------
Metadata::Metadata(const std::string name, const std::string friendlyName,
const std::string author, const std::string description,
const std::string version, const std::string link,
const std::vector<std::string> requires)
: m_name(name), m_friendlyName(friendlyName), m_author(author),
m_description(description), m_version(version), m_link(link),
m_requires(requires) {}
std::string Metadata::GetName() const { return m_name; }
std::string Metadata::GetFriendlyName() const { return m_friendlyName; }
std::string Metadata::GetDescription() const { return m_description; }
std::string Metadata::GetAuthor() const { return m_author; }
std::string Metadata::GetVersion() const { return m_version; }
std::string Metadata::GetLink() const { return m_link; }
std::vector<std::string> Metadata::GetRequires() const { return m_requires; }
void Metadata::SetName(const std::string name) { m_name = name; }
void Metadata::SetFriendlyName(const std::string friendlyName) {
m_friendlyName = friendlyName;
}
void Metadata::SetDescription(const std::string description) {
m_description = description;
}
void Metadata::SetAuthor(const std::string author) { m_author = author; }
void Metadata::SetVersion(const std::string version) { m_version = version; }
void Metadata::SetLink(const std::string link) { m_link = link; }
void Metadata::SetRequires(const std::vector<std::string> _requires) {
m_requires = _requires;
}
void Metadata::Serialize(const std::filesystem::path path) const {
std::ofstream file(path);
rapidjson::BasicOStreamWrapper wrapper(file);
rapidjson::PrettyWriter writer(wrapper);
writer.StartObject();
this->Serialize(writer);
writer.EndObject();
}
template <typename Writer> void Metadata::Serialize(Writer &writer) const {
const std::string name = m_name;
const std::string friendlyName = m_friendlyName;
const std::string description = m_description;
const std::string author = m_author;
const std::string version = m_version;
const std::string link = m_link;
const std::vector<std::string>
requires = m_requires;
writer.Key(RAPIDJSON_STRINGIFY(name));
writer.String(name.c_str(),
static_cast<rapidjson::SizeType>(name.length()));
writer.Key(RAPIDJSON_STRINGIFY(friendlyName));
writer.String(friendlyName.c_str(),
static_cast<rapidjson::SizeType>(friendlyName.length()));
writer.Key(RAPIDJSON_STRINGIFY(description));
writer.String(description.c_str(),
static_cast<rapidjson::SizeType>(description.length()));
writer.Key(RAPIDJSON_STRINGIFY(author));
writer.String(author.c_str(),
static_cast<rapidjson::SizeType>(author.length()));
writer.Key(RAPIDJSON_STRINGIFY(version));
writer.String(version.c_str(),
static_cast<rapidjson::SizeType>(version.length()));
writer.Key(RAPIDJSON_STRINGIFY(link));
writer.String(link.c_str(),
static_cast<rapidjson::SizeType>(link.length()));
writer.Key(RAPIDJSON_STRINGIFY(requires));
writer.StartArray();
for (auto &require : requires)
writer.String(require.c_str(),
static_cast<rapidjson::SizeType>(require.length()));
writer.EndArray();
}
} // namespace Starbound