Skip to content

Commit

Permalink
[#72] feat(model_script): add mod-specific workarounds
Browse files Browse the repository at this point in the history
These workarounds are required by some mods which won't function correctly otherwise.

Related: Try/OpenGothic#483
Closes: #72
  • Loading branch information
lmichaelis committed Jul 11, 2023
1 parent 72a6c42 commit 3fe0f7b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion source/model_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace phoenix {
std::istringstream stream {frames};

int32_t fr = 0;
while (!stream.eof()) {
while (!stream.eof() && !stream.fail()) {
stream >> fr;
evt.frames.push_back(fr);
}
Expand Down
32 changes: 31 additions & 1 deletion source/model_script_dsl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ namespace phoenix {
return _m_stream.token_value();
}

std::optional<std::string> parser::maybe_keyword() {
if (this->maybe<token::keyword>())
return _m_stream.token_value();
return std::nullopt;
}

void parser::expect_keyword(std::string_view value) {
this->expect<token::keyword>();
if (!phoenix::iequals(_m_stream.token_value(), value)) {
Expand Down Expand Up @@ -185,6 +191,24 @@ namespace phoenix {
return mds::animation_flags_from_string(kw);
}

std::optional<mds::animation_flags> parser::maybe_flags() {
// NOTE: Workaround for mod-specific issues
auto kw = this->maybe_keyword();
if (!kw) {
return std::nullopt;
}

if (kw->find("ani") != std::string::npos || kw->find("model") != std::string::npos) {
this->_m_stream.backtrack();
return std::nullopt;
}

// NOTE: Quirk of original implementation: Normally, "." is used in flags to indicate
// _not set_ but sometimes ":" appears instead.
(void) this->maybe<token::colon>();
return mds::animation_flags_from_string(*kw);
}

template <token kind>
bool parser::maybe() {
if (_m_stream.next() != kind) {
Expand Down Expand Up @@ -388,6 +412,9 @@ namespace phoenix {
mds::event_sfx_ground sfx {};
sfx.frame = this->expect_int();
sfx.name = this->expect_string();

// NOTE: Fix for mod-specific issues
(void) this->maybe_keyword("EMPTY_SLOT");
return sfx;
}

Expand Down Expand Up @@ -496,6 +523,9 @@ namespace phoenix {
blend.blend_in = this->maybe_number().value_or(0);
blend.blend_out = this->maybe_number().value_or(0);

// NOTE: Fix for mod-specific issues
(void) this->maybe_flags();

// Optional events block.
if (this->maybe<token::lbrace>()) {
this->ignore_block();
Expand All @@ -520,4 +550,4 @@ namespace phoenix {
return tag;
}
} // namespace parser
} // namespace phoenix
} // namespace phoenix
2 changes: 2 additions & 0 deletions source/model_script_dsl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ namespace phoenix::parser {

[[nodiscard]] std::string expect_string();
[[nodiscard]] std::string expect_keyword();
[[nodiscard]] std::optional<std::string> maybe_keyword();
void expect_keyword(std::string_view value);
[[nodiscard]] float expect_number();
[[nodiscard]] int expect_int();
[[nodiscard]] mds::animation_flags expect_flags();
[[nodiscard]] std::optional<mds::animation_flags> maybe_flags();

template <token kind>
bool maybe();
Expand Down

0 comments on commit 3fe0f7b

Please sign in to comment.