| 185 | } |
| 186 | |
| 187 | std::list<Logic::Console::Command>::iterator Console::determineCommand(const std::vector<std::string>& tokens) |
| 188 | { |
| 189 | bool worldAvailable = m_BaseEngine.getMainWorld().isValid(); |
| 190 | std::vector<std::size_t> numMatchingTokens(m_Commands.size(), 0); |
| 191 | size_t commandIndex = 0; |
| 192 | for (auto it = m_Commands.begin(); it != m_Commands.end(); ++it, ++commandIndex) |
| 193 | { |
| 194 | const auto& command = *it; |
| 195 | if (!worldAvailable && command.requiresWorld) |
| 196 | continue; |
| 197 | if (command.numFixTokens > tokens.size()) |
| 198 | continue; |
| 199 | bool allStagesMatched = true; |
| 200 | for (std::size_t tokenID = 0; tokenID < command.numFixTokens; tokenID++) |
| 201 | { |
| 202 | auto suggestion = findSuggestion(command.generators.at(tokenID)(), tokens.at(tokenID)); |
| 203 | if (suggestion == nullptr) |
| 204 | allStagesMatched = false; |
| 205 | } |
| 206 | if (allStagesMatched) |
| 207 | { |
| 208 | numMatchingTokens[commandIndex] = command.numFixTokens; |
| 209 | } |
| 210 | } |
| 211 | auto itMaxelement = std::max_element(numMatchingTokens.begin(), numMatchingTokens.end()); |
| 212 | if (itMaxelement != numMatchingTokens.end() && *itMaxelement != 0) |
| 213 | { |
| 214 | // case: at least one command matched with command.numFixTokens |
| 215 | auto commandIndex = itMaxelement - numMatchingTokens.begin(); |
| 216 | return std::next(m_Commands.begin(), commandIndex); |
| 217 | } |
| 218 | return m_Commands.end(); |
| 219 | } |
| 220 | |
| 221 | void Console::generateSuggestions(const std::string& input, bool limitToFixed) |
| 222 | { |