Skip to content

Commit

Permalink
Add a proper interface for IGameConnection
Browse files Browse the repository at this point in the history
Previously the IGameConnection interface was empty, but the code which used the
module actually did a dynamic_cast down to the GameConnection implementing
class and used its methods directly, thereby breaking the module abstraction.

Now, the methods used by the UI code have been added as pure virtuals in the
IGameConnection interface, eliminating the need for the dynamic downcast.
  • Loading branch information
Matthew Mott committed Sep 1, 2020
1 parent 83e4028 commit 94de3c7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
9 changes: 8 additions & 1 deletion include/igameconnection.h
Expand Up @@ -6,7 +6,14 @@
class IGameConnection : public RegisterableModule
{
public:
//TODO: to be determined...
virtual void setCameraSyncEnabled(bool enable) = 0;
virtual void backSyncCamera() = 0;
virtual void reloadMap() = 0;
virtual void setAutoReloadMapEnabled(bool enable) = 0;
virtual void setUpdateMapLevel(bool on, bool always) = 0;
virtual void doUpdateMap() = 0;
virtual void togglePauseGame() = 0;
virtual void respawnSelectedEntities() = 0;
};

const char* const MODULE_GAMECONNECTION = "GameConnection";
Expand Down
7 changes: 4 additions & 3 deletions radiant/gameconnection/GameConnection.cpp
Expand Up @@ -24,9 +24,11 @@ static std::string messagePreamble(std::string type) {
static std::string actionPreamble(std::string type) {
return messagePreamble("action") + fmt::format("action \"{}\"\n", type);
}
#if 0
static std::string queryPreamble(std::string type) {
return messagePreamble("query") + fmt::format("query \"{}\"\n", type);
}
#endif


int GameConnection::newSeqno() {
Expand Down Expand Up @@ -62,7 +64,7 @@ void GameConnection::think() {
//validate and remove preamble
int responseSeqno, lineLen;
int ret = sscanf(_response.data(), "response %d\n%n", &responseSeqno, &lineLen);
assert(ret == 1); ret;
assert(ret == 1);
assert(responseSeqno == _seqnoInProgress);
_response.erase(_response.begin(), _response.begin() + lineLen);
//mark request as "no longer in progress"
Expand All @@ -72,8 +74,7 @@ void GameConnection::think() {
}
else {
//doing nothing now: send async command if present
bool sentAsync = sendAnyPendingAsync();
sentAsync = false; //unused
sendAnyPendingAsync();
}
_connection->think();
if (!_connection->isAlive()) {
Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -162,7 +162,7 @@ void UserInterfaceModule::registerUICommands()

// FIXME: should not dynamic_cast, required methods should be in the
// IGameConnection interface
if (GameConnection *gameconn = dynamic_cast<GameConnection*>(GlobalGameConnection())) {
if (IGameConnection *gameconn = GlobalGameConnection()) {
GlobalCommandSystem().addCommand("GameConnectionCameraSyncEnable",
[gameconn](const cmd::ArgumentList&) { gameconn->setCameraSyncEnabled(true); });
GlobalCommandSystem().addCommand("GameConnectionCameraSyncDisable",
Expand Down

0 comments on commit 94de3c7

Please sign in to comment.