From aa2200418a2f5f8b33c33db99e167e4ca374a9ca Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 17 Nov 2026 10:06:41 +0100 Subject: [PATCH] fix(doors): an animdoor entry without its } leaves the rest of the .mmd to be read read_spec() took every token up to the end of the file into the block when the closing brace was missing, so endmodels, sounds:, the lights and the rest were lost. The block now ends at the first token that is neither a key nor the value right after one (or at endmodels), and that token is put back for the caller. --- tests/unit/test_door_wings.cpp | 38 +++++++++++++++++++++++++++++++++- vehicle/doorwings.h | 17 ++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_door_wings.cpp b/tests/unit/test_door_wings.cpp index e049d6c61..9eff62441 100644 --- a/tests/unit/test_door_wings.cpp +++ b/tests/unit/test_door_wings.cpp @@ -125,11 +125,47 @@ TEST_CASE("door wings: a faulty animdoor entry says what is wrong with it") { CHECK(read("3 { type: Roll range: 0.65 }", spec) == "unknown door type \"Roll\""); CHECK(read("3 { type: Shift range: 0,65 }", spec) == "bad number \"0,65\" for \"range:\""); CHECK(read("3 { type: Shift range: wide }", spec) == "bad number \"wide\" for \"range:\""); - CHECK(read("3 { kind: Shift }", spec) == "unknown key \"kind:\""); + CHECK(read("3 { kind: Shift }", spec) == "no } after the keys, \"kind:\" is none of them"); CHECK(read("3 { type: Shift range: }", spec) == "no value for \"range:\""); CHECK(read("3 { type: Shift range: 0.65", spec) == "no } after the keys"); } +TEST_CASE("door wings: an animdoor entry without its } leaves the entries after it to the .mmd") { + // the first token that is neither a key nor the value of one ends the entry, and is read next by the caller + auto const next{[](std::string const &Text) { + door_wings::wing_spec spec{fizdoors}; + cParser parser(Text, cParser::buffer_TEXT); + auto const error{door_wings::read_spec(parser, spec)}; + std::vector rest; + std::string token; + do { + token.clear(); + parser.getTokens(1, false); + parser >> token; + if (false == token.empty()) { + rest.emplace_back(token); + } + } while (false == token.empty()); + return std::pair{error, rest}; + }}; + { + auto const [error, rest]{next("3 { type: Plug range: 0.9\nsounds: horn.wav\nlights: 1\nendmodels")}; + CHECK(error == "no } after the keys, \"sounds:\" is none of them"); + CHECK(rest == std::vector{"sounds:", "horn.wav", "lights:", "1", "endmodels"}); + } + { + auto const [error, rest]{next("3 { type: Plug range: endmodels")}; + CHECK(error == "no } after the keys, \"endmodels\" is none of them"); + CHECK(rest == std::vector{"endmodels"}); + } + { + // a whole entry leaves the rest as it is + auto const [error, rest]{next("3 { type: Plug range: 0.9 } sounds: horn.wav endmodels")}; + CHECK(error.empty()); + CHECK(rest == std::vector{"sounds:", "horn.wav", "endmodels"}); + } +} + TEST_CASE("door wings: the parsed keys go into the spec, pairs of a key and its value") { door_wings::wing_spec spec{fizdoors}; CHECK(door_wings::parse_spec({"type:", "Shift", "range:", "0.65"}, spec).empty()); diff --git a/vehicle/doorwings.h b/vehicle/doorwings.h index 1266dafec..db05b03fc 100644 --- a/vehicle/doorwings.h +++ b/vehicle/doorwings.h @@ -137,8 +137,17 @@ inline std::string parse_spec(std::vector const &Tokens, wing_spec return {}; } +// whether the token is one of the keys of an animdoor block, in any letter case +inline bool is_spec_key(std::string const &Token) { + std::string key{Token}; + std::transform(key.begin(), key.end(), key.begin(), [](unsigned char const Character) { return static_cast(std::tolower(Character)); }); + return key == "type:" || key == "range:" || key == "rangeout:"; +} + // reads an animdoor entry after its key: the wing number and the block in braces, into the spec filled with the values -// of the .fiz. returns what is wrong with the entry, or an empty text when nothing is. the parser is a cParser +// of the .fiz. returns what is wrong with the entry, or an empty text when nothing is. the parser is a cParser. a block +// without its } ends at the first token that is neither a key nor the value right after one (or at endmodels): that +// token is left for the caller, so the entries after a faulty one are read as ever template std::string read_spec(Parser_ &Parser, wing_spec &Spec) { Parser.getTokens(1, false); @@ -150,6 +159,7 @@ std::string read_spec(Parser_ &Parser, wing_spec &Spec) { return "no { after the wing number"; } std::vector block; + auto value{false}; // the token is the value of the key before it while (true) { token.clear(); Parser.getTokens(1, false); @@ -160,6 +170,11 @@ std::string read_spec(Parser_ &Parser, wing_spec &Spec) { if (token == "}") { break; } + if (token == "endmodels" || (false == value && false == is_spec_key(token))) { + Parser.injectString(token); + return "no } after the keys, \"" + token + "\" is none of them"; + } + value = false == value; block.emplace_back(token); } return parse_spec(block, Spec);