载入中...
搜索中...
未找到
WarshipRegistry.cpp
浏览该文件的文档.
2
3#include "common/Diagnostic.h"
4#include "common/Value.h"
5#include "common/ValueRead.h"
6
7#include <cmath>
8#include <limits>
9#include <utility>
10
11namespace eve::warship {
12namespace {
13
14template <typename T>
15Result<T> fail(const Status& status) {
17}
18
19const Value* field(const Value::Object& object, std::string_view name) {
20 const auto it = object.find(std::string(name));
21 return it == object.end() ? nullptr : &it->second;
22}
23
24Result<std::string> optionalString(const Value::Object& object, std::string_view name, std::string fallback) {
25 if (!field(object, name)) return Result<std::string>::success(std::move(fallback));
26 return readStringMember(object, name);
27}
28
29Result<float> optionalFloat(const Value::Object& object, std::string_view name, float fallback) {
30 if (!field(object, name)) return Result<float>::success(fallback);
31 auto parsed = readFiniteFloatValue(*field(object, name));
32 if (!parsed) return fail<float>(parsed.status());
33 return Result<float>::success(parsed.value());
34}
35
36Result<int> optionalInt(const Value::Object& object, std::string_view name, int fallback) {
37 if (!field(object, name)) return Result<int>::success(fallback);
38 auto parsed = readNumericDoubleValue(*field(object, name));
39 if (!parsed) return fail<int>(parsed.status());
40 const double number = parsed.value();
41 if (!std::isfinite(number) || number < static_cast<double>(std::numeric_limits<int>::min()) ||
42 number > static_cast<double>(std::numeric_limits<int>::max())) {
44 Diagnostic::error(DiagnosticCode::InvalidArgument, "integer field out of range", "warship.registry"));
45 }
46 return Result<int>::success(static_cast<int>(number));
47}
48
49Result<std::vector<std::string>> optionalStringArray(const Value::Object& object, std::string_view name) {
50 std::vector<std::string> out;
51 const Value* value = field(object, name);
52 if (!value) return Result<std::vector<std::string>>::success(std::move(out));
53 const auto* array = value->getIf<Value::Array>();
54 if (!array) {
55 return Result<std::vector<std::string>>::failure(
56 Diagnostic::error(DiagnosticCode::InvalidArgument, "tags must be a string array", "warship.registry"));
57 }
58 for (const Value& entry : *array) {
59 const auto* text = entry.getIf<std::string>();
60 if (!text) {
61 return Result<std::vector<std::string>>::failure(
62 Diagnostic::error(DiagnosticCode::InvalidArgument, "tag entries must be strings", "warship.registry"));
63 }
64 out.push_back(*text);
65 }
66 return Result<std::vector<std::string>>::success(std::move(out));
67}
68
69Result<CompartmentFunctionDef> parseFunction(const Value::Object& object) {
70 CompartmentFunctionDef def;
71 auto id = readStringMember(object, "id");
72 if (!id) return fail<CompartmentFunctionDef>(id.status());
73 if (id.value().empty()) {
75 Diagnostic::error(DiagnosticCode::InvalidArgument, "function id must be non-empty", "warship.function"));
76 }
77 def.id = std::move(id).takeValue();
78
79 auto display = optionalString(object, "name", def.id);
80 if (!display) return fail<CompartmentFunctionDef>(display.status());
81 def.displayName = std::move(display).takeValue();
82
83 auto powerDraw = optionalFloat(object, "powerDraw", 0.f);
84 if (!powerDraw) return fail<CompartmentFunctionDef>(powerDraw.status());
85 def.powerDraw = powerDraw.value();
86
87 auto powerProvide = optionalFloat(object, "powerProvide", 0.f);
88 if (!powerProvide) return fail<CompartmentFunctionDef>(powerProvide.status());
89 def.powerProvide = powerProvide.value();
90
91 auto thrustBonus = optionalFloat(object, "thrustBonus", 0.f);
92 if (!thrustBonus) return fail<CompartmentFunctionDef>(thrustBonus.status());
93 def.thrustBonus = thrustBonus.value();
94
95 auto hangar = optionalInt(object, "hangarCapacity", 0);
96 if (!hangar) return fail<CompartmentFunctionDef>(hangar.status());
97 def.hangarCapacity = hangar.value();
98
99 auto weapons = optionalInt(object, "weaponBayCount", 0);
100 if (!weapons) return fail<CompartmentFunctionDef>(weapons.status());
101 def.weaponBayCount = weapons.value();
102
103 auto crew = optionalInt(object, "crewCapacity", 0);
104 if (!crew) return fail<CompartmentFunctionDef>(crew.status());
105 def.crewCapacity = crew.value();
106
107 auto sail = optionalFloat(object, "sailPower", 0.f);
108 if (!sail) return fail<CompartmentFunctionDef>(sail.status());
109 def.sailPower = sail.value();
110
111 auto tags = optionalStringArray(object, "tags");
112 if (!tags) return fail<CompartmentFunctionDef>(tags.status());
113 def.tags = std::move(tags).takeValue();
114 return Result<CompartmentFunctionDef>::success(std::move(def));
115}
116
117Result<LayoutSeed> parseSeed(const Value::Object& object) {
118 LayoutSeed seed;
119 auto deck = optionalInt(object, "deck", 0);
120 if (!deck) return fail<LayoutSeed>(deck.status());
121 seed.deck = deck.value();
122 auto row = optionalInt(object, "row", 0);
123 if (!row) return fail<LayoutSeed>(row.status());
124 seed.row = row.value();
125 auto col = optionalInt(object, "col", 0);
126 if (!col) return fail<LayoutSeed>(col.status());
127 seed.col = col.value();
128 auto functionId = readStringMember(object, "function");
129 if (!functionId) {
130 functionId = readStringMember(object, "functionId");
131 }
132 if (!functionId) return fail<LayoutSeed>(functionId.status());
133 seed.functionId = std::move(functionId).takeValue();
134 if (seed.functionId.empty()) {
136 "layout function id must be non-empty", "warship.layout"));
137 }
138 return Result<LayoutSeed>::success(std::move(seed));
139}
140
141Result<WarshipDefinition> parseHull(const Value::Object& object) {
142 WarshipDefinition def;
143 auto id = readStringMember(object, "id");
144 if (!id) return fail<WarshipDefinition>(id.status());
145 if (id.value().empty()) {
147 Diagnostic::error(DiagnosticCode::InvalidArgument, "hull id must be non-empty", "warship.hull"));
148 }
149 def.id = std::move(id).takeValue();
150
151 auto category = optionalString(object, "category", kCategoryWarship);
152 if (!category) return fail<WarshipDefinition>(category.status());
153 def.category = std::move(category).takeValue();
154
155 auto mobility = optionalString(object, "mobility", kMobilitySurface);
156 if (!mobility) return fail<WarshipDefinition>(mobility.status());
157 def.mobility = std::move(mobility).takeValue();
158
159 auto length = optionalFloat(object, "length", def.length);
160 if (!length) return fail<WarshipDefinition>(length.status());
161 def.length = length.value();
162 auto beam = optionalFloat(object, "beam", def.beam);
163 if (!beam) return fail<WarshipDefinition>(beam.status());
164 def.beam = beam.value();
165 auto maxSpeed = optionalFloat(object, "maxSpeed", def.maxSpeed);
166 if (!maxSpeed) return fail<WarshipDefinition>(maxSpeed.status());
167 def.maxSpeed = maxSpeed.value();
168 auto accel = optionalFloat(object, "accel", def.accel);
169 if (!accel) return fail<WarshipDefinition>(accel.status());
170 def.accel = accel.value();
171 auto turnRate = optionalFloat(object, "turnRate", def.turnRate);
172 if (!turnRate) return fail<WarshipDefinition>(turnRate.status());
173 def.turnRate = turnRate.value();
174 auto maxHealth = optionalFloat(object, "maxHealth", def.maxHealth);
175 if (!maxHealth) return fail<WarshipDefinition>(maxHealth.status());
176 def.maxHealth = maxHealth.value();
177
178 if (def.length <= 0.f || def.beam <= 0.f || def.maxSpeed < 0.f || def.accel < 0.f || def.turnRate < 0.f ||
179 def.maxHealth < 0.f) {
181 DiagnosticCode::InvalidArgument, "hull numeric fields must be non-negative (size > 0)", "warship.hull"));
182 }
183
184 if (const Value* gridValue = field(object, "grid")) {
185 const auto* gridObject = gridValue->getIf<Value::Object>();
186 if (!gridObject) {
188 Diagnostic::error(DiagnosticCode::InvalidArgument, "grid must be an object", "warship.hull"));
189 }
190 auto cols = optionalInt(*gridObject, "cols", 4);
191 if (!cols) return fail<WarshipDefinition>(cols.status());
192 auto rows = optionalInt(*gridObject, "rows", 2);
193 if (!rows) return fail<WarshipDefinition>(rows.status());
194 auto decks = optionalInt(*gridObject, "decks", 1);
195 if (!decks) return fail<WarshipDefinition>(decks.status());
196 if (cols.value() <= 0 || rows.value() <= 0 || decks.value() <= 0) {
198 DiagnosticCode::InvalidArgument, "grid cols/rows/decks must be positive", "warship.hull"));
199 }
200 def.grid.cols = cols.value();
201 def.grid.rows = rows.value();
202 def.grid.decks = decks.value();
203 } else {
204 def.grid.cols = 4;
205 def.grid.rows = 2;
206 def.grid.decks = 1;
207 }
208
209 if (const Value* layoutValue = field(object, "defaultLayout")) {
210 const auto* array = layoutValue->getIf<Value::Array>();
211 if (!array) {
213 Diagnostic::error(DiagnosticCode::InvalidArgument, "defaultLayout must be an array", "warship.hull"));
214 }
215 for (const Value& entry : *array) {
216 const auto* seedObject = entry.getIf<Value::Object>();
217 if (!seedObject) {
219 DiagnosticCode::InvalidArgument, "defaultLayout entries must be objects", "warship.hull"));
220 }
221 auto seed = parseSeed(*seedObject);
222 if (!seed) return fail<WarshipDefinition>(seed.status());
223 if (def.grid.indexOf(seed.value().deck, seed.value().row, seed.value().col) < 0) {
225 DiagnosticCode::InvalidArgument, "defaultLayout cell is outside the hull grid", "warship.hull"));
226 }
227 def.defaultLayout.push_back(std::move(seed).takeValue());
228 }
229 }
230
231 auto tags = optionalStringArray(object, "tags");
232 if (!tags) return fail<WarshipDefinition>(tags.status());
233 def.tags = std::move(tags).takeValue();
234 return Result<WarshipDefinition>::success(std::move(def));
235}
236
237} // namespace
238
239std::unordered_map<std::string, WarshipDefinition>& WarshipRegistry::hullTable() {
240 static std::unordered_map<std::string, WarshipDefinition> table;
241 return table;
242}
243
244std::unordered_map<std::string, CompartmentFunctionDef>& WarshipRegistry::functionTable() {
245 static std::unordered_map<std::string, CompartmentFunctionDef> table;
246 return table;
247}
248
250 auto parsed = Value::fromJson(json);
251 if (!parsed) return fail<int>(parsed.status());
252 Value root = std::move(parsed).takeValue();
253
254 int registered = 0;
255 auto accept = [&](const Value::Object& object) -> Result<void> {
256 auto def = parseHull(object);
257 if (!def) return Result<void>::failure(def.status());
258 WarshipDefinition parsed = std::move(def).takeValue();
259 hullTable()[parsed.id] = std::move(parsed);
260 ++registered;
261 return Result<void>::success();
262 };
263
264 if (const auto* array = root.getIf<Value::Array>()) {
265 for (const Value& entry : *array) {
266 const auto* object = entry.getIf<Value::Object>();
267 if (!object) {
269 "hull array entries must be objects", "warship.hull"));
270 }
271 if (auto ok = accept(*object); !ok) return fail<int>(ok.status());
272 }
273 return Result<int>::success(registered);
274 }
275 const auto* object = root.getIf<Value::Object>();
276 if (!object) {
278 Diagnostic::error(DiagnosticCode::InvalidArgument, "hull JSON must be an object or array", "warship.hull"));
279 }
280 if (auto ok = accept(*object); !ok) return fail<int>(ok.status());
281 return Result<int>::success(registered);
282}
283
285 auto parsed = Value::fromJson(json);
286 if (!parsed) return fail<int>(parsed.status());
287 Value root = std::move(parsed).takeValue();
288
289 int registered = 0;
290 auto accept = [&](const Value::Object& object) -> Result<void> {
291 auto def = parseFunction(object);
292 if (!def) return Result<void>::failure(def.status());
293 CompartmentFunctionDef parsed = std::move(def).takeValue();
294 functionTable()[parsed.id] = std::move(parsed);
295 ++registered;
296 return Result<void>::success();
297 };
298
299 if (const auto* array = root.getIf<Value::Array>()) {
300 for (const Value& entry : *array) {
301 const auto* object = entry.getIf<Value::Object>();
302 if (!object) {
304 DiagnosticCode::InvalidArgument, "function array entries must be objects", "warship.function"));
305 }
306 if (auto ok = accept(*object); !ok) return fail<int>(ok.status());
307 }
308 return Result<int>::success(registered);
309 }
310 const auto* object = root.getIf<Value::Object>();
311 if (!object) {
313 "function JSON must be an object or array", "warship.function"));
314 }
315 if (auto ok = accept(*object); !ok) return fail<int>(ok.status());
316 return Result<int>::success(registered);
317}
318
319void WarshipRegistry::clearHulls() { hullTable().clear(); }
320
321void WarshipRegistry::clearFunctions() { functionTable().clear(); }
322
323bool WarshipRegistry::hasHull(const std::string& id) { return findHull(id) != nullptr; }
324
325bool WarshipRegistry::hasFunction(const std::string& id) { return findFunction(id) != nullptr; }
326
327int WarshipRegistry::hullCount() { return static_cast<int>(hullTable().size()); }
328
329int WarshipRegistry::functionCount() { return static_cast<int>(functionTable().size()); }
330
331const WarshipDefinition* WarshipRegistry::findHull(const std::string& id) {
332 auto it = hullTable().find(id);
333 if (it == hullTable().end()) return nullptr;
334 return &it->second;
335}
336
338 auto it = functionTable().find(id);
339 if (it == functionTable().end()) return nullptr;
340 return &it->second;
341}
342
343} // namespace eve::warship
int root
Definition AnimSmr.cpp:119
Stable, structured diagnostics shared by engine modules.
std::string name
Definition ECS.cpp:154
int rows
int cols
wgpu::PopErrorScopeStatus status
float length
std::unordered_set< std::string > tags
Object::Ptr value
double number
std::string text
ScriptStructure out
size_t end
Json object
uint32_t seed
Definition TreeMesh.cpp:276
const char * category
Owning, renderer-independent dynamic values.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
static const CompartmentFunctionDef * findFunction(const std::string &id)
Lookup a compartment function. @ownership Borrowed; valid until the next clear / replace of that id....
static Result< int > loadHullsFromJson(std::string_view json)
static bool hasHull(const std::string &id)
static bool hasFunction(const std::string &id)
static const WarshipDefinition * findHull(const std::string &id)
Lookup a hull template. @ownership Borrowed; valid until the next clear / replace of that id....
static Result< int > loadFunctionsFromJson(std::string_view json)
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
Warship module entry (eve.Warship): hull/function catalogues, spawn, compartment remodel,...
Definition Warship.cpp:17
constexpr const char * kCategoryWarship
Documented hull category conventions (free string; not an enum).
constexpr const char * kMobilitySurface
Built-in mobility model names used by hull definitions.
Result< float > readFiniteFloatValue(const Value &value)
Convert an Integer or Double to a finite Float after validating its source range.
Definition Value.cpp:81
Result< std::string > readStringMember(const Value::Object &object, std::string_view name)
Read a required string field, including an empty string, as an owning value.
Definition Value.cpp:25
Result< double > readNumericDoubleValue(const Value &value)
Read an Int64 or Double as a double without imposing a finite-value policy.
Definition Value.cpp:74
Compartment function catalogue entry.
Hull template registered through Warship::registerHullsFromJson.