18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

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.
This commit is contained in:
jerrrrycho
2026-11-17 10:06:41 +01:00
parent 8d2d9badb5
commit aa2200418a
2 changed files with 53 additions and 2 deletions

View File

@@ -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<std::string> 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<std::string>{"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<std::string>{"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<std::string>{"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());

View File

@@ -137,8 +137,17 @@ inline std::string parse_spec(std::vector<std::string> 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<char>(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 <typename Parser_>
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<std::string> 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);