10#include "absl/strings/str_cat.h"
12#include "google/protobuf/io/coded_stream.h"
13#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
21template <
typename Table>
22absl::Status Read(
const std::string& folder,
const char* file,
23 absl::StatusOr<Table> (*parse)(std::string_view),
25 const std::string path = absl::StrCat(folder,
"/", file);
26 const absl::StatusOr<std::string> text =
ReadFile(path);
30 absl::StatusOr<Table> parsed = parse(*text);
32 return absl::InvalidArgumentError(
33 absl::StrCat(path,
", ", parsed.status().message()));
35 table = *std::move(parsed);
36 return absl::OkStatus();
41constexpr uint64_t kOffsetBasis = 14695981039346656037u;
42constexpr uint64_t kPrime = 1099511628211u;
44void Add(uint64_t& hash,
const google::protobuf::Message& message) {
47 google::protobuf::io::StringOutputStream stream(&bytes);
48 google::protobuf::io::CodedOutputStream coded(&stream);
51 coded.SetSerializationDeterministic(
true);
54 static_cast<void>(message.SerializeToCodedStream(&coded));
56 for (
const char byte : bytes) {
57 hash ^=
static_cast<unsigned char>(byte);
64absl::StatusOr<Content>
ReadContent(
const std::string& folder) {
87 read = Read(folder,
"rig.txtpb",
ParseRig, content.
rig);
97 return absl::InvalidArgumentError(
98 absl::StrCat(folder,
", ", checked.message()));
105 std::set<std::string> joints;
106 for (
const rig::Joint& joint :
Skeleton(content.
rig, -1)) {
107 joints.insert(joint.name());
109 const auto on_the_rig =
110 [&joints](
const google::protobuf::RepeatedPtrField<figures::Part>& parts,
111 std::string_view whose) {
112 for (
const figures::Part& part :
Unfold(parts)) {
113 for (
const std::string& joint :
114 {part.bone(), part.from(), part.to()}) {
115 if (!joint.empty() && !joints.contains(joint)) {
116 return absl::InvalidArgumentError(
117 absl::StrCat(whose,
" has a part on a joint ", joint,
118 " that isn't in the rig"));
122 return absl::OkStatus();
124 absl::Status rigged = on_the_rig(content.
figures.body(),
"the body");
125 for (
int i = 0; rigged.ok() && i < content.
figures.hairs_size(); ++i) {
126 rigged = on_the_rig(content.
figures.hairs(i).parts(),
"a way to wear hair");
128 for (
const auto& [
id, item] : content.
items.
by_id) {
131 on_the_rig(item.look().parts(), absl::StrCat(
"the ", item.name()));
138 for (
const monsters::Drop& drop : kind.loot()) {
139 if (!content.
items.
by_id.contains(drop.item())) {
140 return absl::InvalidArgumentError(
141 absl::StrCat(
"the ", kind.name(),
" drops an item ", drop.item(),
142 " that isn't in the items file"));
146 for (
const auto& [
id, kind] : content.
npcs.
kinds) {
147 const npcs::Look& look = kind.look();
148 if (std::cmp_greater_equal(look.hair(), content.
figures.hairs_size())) {
149 return absl::InvalidArgumentError(
150 absl::StrCat(
"the ", kind.name(),
" has a hair ", look.hair(),
151 " that isn't in the figures file"));
154 std::vector<uint32_t> worn(look.wearing().begin(), look.wearing().end());
155 if (look.weapon() != 0) {
156 worn.push_back(look.weapon());
158 for (
const uint32_t item : worn) {
160 return absl::InvalidArgumentError(
161 absl::StrCat(
"the ", kind.name(),
" wears an item ", item,
162 " that isn't in the items file"));
165 for (
const uint32_t skill : kind.teaches()) {
167 return absl::InvalidArgumentError(
168 absl::StrCat(
"the ", kind.name(),
" teaches a skill ", skill,
169 " that isn't in the skills file"));
173 for (
const auto& [
id, quest] : content.
quests) {
174 if (!content.
npcs.
kinds.contains(quest.giver())) {
175 return absl::InvalidArgumentError(
176 absl::StrCat(
"the ", quest.name(),
" has a giver ", quest.giver(),
177 " that isn't in the NPCs file"));
179 if (!content.
monsters.
kinds.contains(quest.kill().monster())) {
180 return absl::InvalidArgumentError(absl::StrCat(
181 "the ", quest.name(),
" asks for a monster ", quest.kill().monster(),
182 " that isn't in the monsters file"));
184 for (
const quests::Prize& prize : quest.prizes()) {
185 if (!content.
items.
by_id.contains(prize.item())) {
186 return absl::InvalidArgumentError(
187 absl::StrCat(
"the ", quest.name(),
" gives an item ", prize.item(),
188 " that isn't in the items file"));
194 const auto in_a_slot = [&content](
const recipes::Recipe& recipe,
195 std::string_view verb,
196 const recipes::ItemCount& some) {
197 const auto item = content.
items.
by_id.find(some.item());
199 return absl::InvalidArgumentError(
200 absl::StrCat(recipe.name(),
" ", verb,
" an item ", some.item(),
201 " that isn't in the items file"));
203 if (some.count() >
StackSize(item->second)) {
204 return absl::InvalidArgumentError(
205 absl::StrCat(recipe.name(),
" ", verb,
" ", some.count(),
" of the ",
206 item->second.name(),
", which don't fit in a slot"));
208 return absl::OkStatus();
210 for (
const auto& [
id, recipe] : content.
recipes) {
212 for (
int i = 0; fits.ok() && i < recipe.inputs_size(); ++i) {
213 fits = in_a_slot(recipe,
"takes", recipe.inputs(i));
216 fits = in_a_slot(recipe,
"makes", recipe.output());
222 for (
const auto& [
id, faction] : content.
factions) {
223 if (!content.
npcs.
kinds.contains(faction.herald())) {
224 return absl::InvalidArgumentError(
225 absl::StrCat(faction.name(),
" has a herald ", faction.herald(),
226 " that isn't in the NPCs file"));
229 return absl::OkStatus();
233 uint64_t hash = kOffsetBasis;
236 Add(hash, content.
rig);
239 for (
const auto& [
id, item] : content.
items.
by_id) {
240 items::Item seen = item;
247 seen.set_name(kind.name());
248 *seen.mutable_look() = kind.look();
252 for (monsters::Skill skill : kind.skills()) {
253 skill.clear_damage();
254 skill.clear_cooldown_ms();
256 *seen.add_skills() = std::move(skill);
260 for (
const monsters::Phase& phase : kind.phases()) {
261 seen.add_phases()->set_health(phase.health());
265 for (
const auto& [
id, kind] : content.
npcs.
kinds) {
268 seen.set_name(kind.name());
269 seen.set_talks(kind.talks());
270 *seen.mutable_look() = kind.look();
271 *seen.mutable_teaches() = kind.teaches();
275 for (
const auto& [
id, quest] : content.
quests) {
278 for (
const auto& [
id, recipe] : content.
recipes) {
283 for (
const auto& [
id, faction] : content.
factions) {
288 for (
const auto& [
id, skill] : content.
skills.
by_id) {
289 skills::Skill seen = skill;
absl::StatusOr< std::map< uint32_t, quests::Quest > > ParseQuests(std::string_view text)
absl::StatusOr< ItemTable > ParseItems(std::string_view text)
absl::StatusOr< rig::Rig > ParseRig(std::string_view text)
uint64_t ClientHash(const Content &content)
absl::StatusOr< figures::Figures > ParseFigures(std::string_view text)
absl::StatusOr< std::map< uint32_t, factions::Faction > > ParseFactions(std::string_view text)
std::vector< rig::Joint > Skeleton(const rig::Rig &rig, int build)
absl::StatusOr< MonsterTable > ParseMonsters(std::string_view text)
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
absl::StatusOr< std::string > ReadFile(const std::string &path)
absl::StatusOr< NpcTable > ParseNpcs(std::string_view text)
std::vector< figures::Part > Unfold(const google::protobuf::RepeatedPtrField< figures::Part > &parts)
absl::StatusOr< SkillTable > ParseSkills(std::string_view text)
absl::Status CheckContent(const Content &content)
absl::StatusOr< std::map< uint32_t, recipes::Recipe > > ParseRecipes(std::string_view text)
absl::StatusOr< Content > ReadContent(const std::string &folder)
Reads each file in the folder, or says which file is wrong, and where.
std::map< uint32_t, factions::Faction > factions
By id.
std::map< uint32_t, quests::Quest > quests
By id.
std::map< uint32_t, recipes::Recipe > recipes
By id.
rig::Rig rig
The skeleton every person is built on, and how it moves.
std::map< uint32_t, items::Item > by_id
std::map< uint32_t, monsters::Kind > kinds
std::map< uint32_t, npcs::Kind > kinds
std::map< uint32_t, skills::Skill > by_id