载入中...
搜索中...
未找到
LevelFormat.cpp
浏览该文件的文档.
65eve::Value::Object extraFields(const eve::Value& value, std::initializer_list<const char*> known) {
78void readProperties(eve::json::Value v, const eve::Value* owned, std::unordered_map<std::string, std::string>& out,
94 if (p.getString("type", "string") == "string" && p.get("value").isString() && p.keys().size() == 3)
103eve::Value propertyValue(const std::unordered_map<std::string, std::string>& properties, const Object& extras,
129 for (const auto& [key, value] : std::map<std::string, std::string>(properties.begin(), properties.end()))
160 eve::Result<std::unique_ptr<LevelDocument>> read(const std::string& s) const override { return readJson(s, false); }
161 eve::Result<std::string> write(const LevelDocument& d) const override { return writeJson(d, false); }
170 const auto fail = [](const std::string& message, eve::DiagnosticCode code = eve::DiagnosticCode::Unsupported) {
178 (r.getString("format") != "eve.level" || (version && (!version->isInt64() || version->asInt() != 1))))
181 if (r.getBool("infinite", false)) return fail("Infinite maps are not yet editable by LevelDocument");
190 if (w < 1 || h < 1 || int64_t(w) * h > 16 * 1024 * 1024 || !std::isfinite(tw) || !std::isfinite(th) ||
194 "map dimensions must be positive, finite and within the 16M-cell budget", {}, {}, "editor.level"));
204 {"format", "version", "type", "width", "height", "tilewidth", "tileheight", "orientation", "layers"});
216 if (!validId(owned.value().find("layers")->at(i), tiled) || !validProperties(v.get("properties")) ||
219 return fail("Invalid layer identity, properties or presentation", eve::DiagnosticCode::ParseError);
221 return fail("Chunked or encoded tile layers require the runtime importer; editing is not supported");
224 int li = obj ? out->addObjectLayer(v.getString("name")) : out->addTileLayer(v.getString("name"));
228 if (!layerIds.insert(l->id).second) return fail("Duplicate layer id", eve::DiagnosticCode::ParseError);
237 readProperties(v.get("properties"), owned.value().find("layers")->at(i).find("properties"), l->properties,
244 if (!q.isObject()) return fail("Map object must be an object", eve::DiagnosticCode::ParseError);
266 readProperties(q.get("properties"), ownedObject.find("properties"), ob->properties, ob->extensions);
271 return fail("Tile data must contain exactly width * height GIDs", eve::DiagnosticCode::ParseError);
302 for (const auto& object : layer.objects) nextObject = std::max(nextObject, numericId(object.id) + 1);
308 tiled ? eve::Value(numericId(layer.id) ? numericId(layer.id) : nextLayer++) : eve::Value(layer.id);
379 eve::DiagnosticCode::InvalidArgument, "level format must not be null", {}, {}, "editor.level"));
412eve::Result<std::string> LevelFormatRegistry::encode(const std::string& id, const LevelDocument& d) const {
424 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "cannot open level", p, {}, "editor.level"));
431eve::Result<void> LevelFormatRegistry::save(const std::string& p, const LevelDocument& d, const std::string& id) const {
444 staging += ".save-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + "-" +
464 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "cannot write level", p, {}, "editor.level"));
470 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "failed while writing level", p, {}, "editor.level"));
472 if (!MoveFileExW(temporary.c_str(), destination.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
474 eve::Diagnostic::error(eve::DiagnosticCode::Failed, "cannot replace level file", p, {}, "editor.level"));
479 eve::DiagnosticCode::Failed, "cannot replace level file: " + error.message(), p, {}, "editor.level"));
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
void expect(std::string_view message) const
Require success for a void operation.
Definition Result.h:548
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:57
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:180
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:124
static Document parse(const std::string &text, std::string *error=nullptr)
Parse.
Definition Json.cpp:581
Format-neutral, editable top-down level model.
Definition LevelDocument.h:60
static const Object & extensions(const LevelDocument &document)
Definition LevelFormat.cpp:148
static Object & extensions(LevelDocument &document)
Definition LevelFormat.cpp:147
eve::Result< std::unique_ptr< LevelDocument > > load(const std::string &path, const std::string &format={}) const
Loads .
Definition LevelFormat.cpp:419
eve::Result< void > registerFormat(std::unique_ptr< LevelFormat > format)
Registers format.
Definition LevelFormat.cpp:376
eve::Result< void > save(const std::string &path, const LevelDocument &level, const std::string &format={}) const
Encode then atomically replace a file; failures preserve an existing destination.
Definition LevelFormat.cpp:431
std::string detect(const std::string &path, const std::string &text) const
Detect.
Definition LevelFormat.cpp:396
eve::Result< std::string > encode(const std::string &format, const LevelDocument &level) const
Encode.
Definition LevelFormat.cpp:412
eve::Result< std::unique_ptr< LevelDocument > > decode(const std::string &format, const std::string &text) const
Decode.
Definition LevelFormat.cpp:404
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:26
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
Definition Json.cpp:563
Definition Brush.cpp:10
@ InvalidArgument
@ NotFound
@ Unsupported
@ Failed
@ ParseError
@ Applied
@ Object
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Definition Runtime.h:120
Definition Container.h:602