| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "icommandsystem.h" |
| 4 |
#include "iscenegraph.h" |
| 5 |
#include "ui/ieventmanager.h" |
| 6 |
|
| 7 |
#include "MapObserver.h" |
| 8 |
|
| 9 |
#include <sigc++/connection.h> |
| 10 |
#include <wx/timer.h> |
| 11 |
|
| 12 |
namespace gameconn |
| 13 |
{ |
| 14 |
|
| 15 |
class MessageTcp; |
| 16 |
class AutomationEngine; |
| 17 |
|
| 18 |
/** |
| 19 |
* stgatilov: This is TheDarkMod-only system for connecting to game process via socket. |
| 20 |
* It allows features like: |
| 21 |
* - immediate camera synchronization |
| 22 |
* - updating edited entities in game immediately (aka "hot reload") |
| 23 |
*/ |
| 24 |
class GameConnection : |
| 25 |
public wxEvtHandler, //note: must be inherited first, according to wxWidgets docs |
| 26 |
public RegisterableModule |
| 27 |
{ |
| 28 |
public: |
| 29 |
GameConnection(); |
| 30 |
~GameConnection(); |
| 31 |
|
| 32 |
// Connect to TDM instance if not connected yet. |
| 33 |
// Returns false if failed to connect. |
| 34 |
bool connect(); |
| 35 |
// Disconnect from TDM instance if connected. |
| 36 |
// If force = true, then it blocks until pending requests are finished. |
| 37 |
// If force = false, then all pending requests are dropped (no blocking for sure). |
| 38 |
void disconnect(bool force = false); |
| 39 |
// Returns false if connection is not yet established or has been closed recently. |
| 40 |
bool isAlive() const; |
| 41 |
|
| 42 |
// Starts async procedure including: |
| 43 |
// * connect to existing game instance or start a new one |
| 44 |
// * set current mod/mission and map |
| 45 |
// * optionally dmap it |
| 46 |
// * make sure game is started afresh |
| 47 |
void restartGame(bool dmap); |
| 48 |
// Returns true iff restartGame sequence is currently in progress. |
| 49 |
bool isGameRestarting() const; |
| 50 |
|
| 51 |
// Enable/disable continuous sync of camera: update in-game player to DarkRadiant camera. |
| 52 |
void setCameraSyncEnabled(bool enable); |
| 53 |
// Returns true iff continuous camera sync is enabled right now. |
| 54 |
bool isCameraSyncEnabled() const; |
| 55 |
// Trigger one-off sync of game position back to DarkRadiant camera. |
| 56 |
void backSyncCamera(); |
| 57 |
|
| 58 |
// Ask game to reload .map file from disk (right now, once). |
| 59 |
void reloadMap(); |
| 60 |
// Enable/disable mode: force game to reload .map from disk every time DarkRadiant saves it. |
| 61 |
void setAutoReloadMapEnabled(bool enable); |
| 62 |
// Returns true iff .map reload mode is enabled. |
| 63 |
bool isAutoReloadMapEnabled() const; |
| 64 |
|
| 65 |
// Enable/disable listening for all entity changes for "update map" feature. |
| 66 |
// See doUpdateMap for more details. |
| 67 |
void setUpdateMapObserverEnabled(bool on); |
| 68 |
// Returns true iff observer for "update map" is enabled. |
| 69 |
bool isUpdateMapObserverEnabled() const; |
| 70 |
// Send pending changes of map entities to game (right now, once). |
| 71 |
// All changes a) since last successful call of this method, |
| 72 |
// or b) since the observer was enabled; are sent as a diff. |
| 73 |
// The game applies the diff on top of its current map state and hot reloads entities. |
| 74 |
void doUpdateMap(); |
| 75 |
// Enable/disable mode: doUpdateMap after every entity change. |
| 76 |
// Note: the update is postponed to next think, so that mass changes go as one diff. |
| 77 |
void setAlwaysUpdateMapEnabled(bool on); |
| 78 |
// Returns true iff the mode for update map after every change is enabled. |
| 79 |
bool isAlwaysUpdateMapEnabled() const; |
| 80 |
|
| 81 |
// Toggle game pause status: pause if it is live / unpause if it is paused. |
| 82 |
// Note: there is no way to learn if game is paused right now. |
| 83 |
void togglePauseGame(); |
| 84 |
// Respawn all entities in the current selection set. |
| 85 |
void respawnSelectedEntities(); |
| 86 |
|
| 87 |
// This signal is emitted when status changes: |
| 88 |
// * connected/disconnected |
| 89 |
// * restart game starts/ends |
| 90 |
// * any mode starts/ends |
| 91 |
// GUI should update itself every time it is triggered. |
| 92 |
sigc::signal<void, int> signal_StatusChanged; |
| 93 |
|
| 94 |
//RegisterableModule implementation |
| 95 |
const std::string& getName() const override; |
| 96 |
const StringSet& getDependencies() const override; |
| 97 |
void initialiseModule(const IApplicationContext& ctx) override; |
| 98 |
void shutdownModule() override; |
| 99 |
|
| 100 |
private: |
| 101 |
|
| 102 |
// Underlying engine for connection to TDM game. |
| 103 |
std::unique_ptr<AutomationEngine> _engine; |
| 104 |
// When connected, this timer calls "think" method periodically. |
| 105 |
std::unique_ptr<wxTimer> _thinkTimer; |
| 106 |
// Flag is used to block new timer events if an old timer event has not finished yet. |
| 107 |
bool _timerInProgress = false; |
| 108 |
|
| 109 |
// Signal subscription for when map is saved, loaded, unloaded, etc. |
| 110 |
sigc::connection _mapEventListener; |
| 111 |
|
| 112 |
// True iff _cameraOutData holds new camera position, which should be sent to game soon. |
| 113 |
bool _cameraOutPending = false; |
| 114 |
// Data for camera position (setviewpos format: X Y Z -pitch yaw roll) |
| 115 |
Vector3 _cameraOutData[2]; |
| 116 |
// Signal subscription for when camera of DarkRadiant view changes. |
| 117 |
sigc::connection _cameraChangedSignal; |
| 118 |
|
| 119 |
// Observes over changes to map data (mainly spawnargs of entities). |
| 120 |
MapObserver _mapObserver; |
| 121 |
// True when "setAutoReloadMapEnabled" is enabled. |
| 122 |
bool _autoReloadMap = false; |
| 123 |
// True when "setAlwaysUpdateMapEnabled" is enabled. |
| 124 |
bool _updateMapAlways = false; |
| 125 |
|
| 126 |
// True when restartGame procedure is executed. |
| 127 |
bool _restartInProgress = false; |
| 128 |
|
| 129 |
// IEventPtrs corresponding to activatable menu options. |
| 130 |
IEventPtr _event_toggleCameraSync; |
| 131 |
IEventPtr _event_backSyncCamera; |
| 132 |
|
| 133 |
private: |
| 134 |
|
| 135 |
// Add any required items to the application toolbars. |
| 136 |
void addToolbarItems(); |
| 137 |
|
| 138 |
// Enable/disable timer calling think method regularly. |
| 139 |
void setThinkLoop(bool enable); |
| 140 |
// Callback for _thinkTimer. |
| 141 |
void onTimerEvent(wxTimerEvent& ev); |
| 142 |
// Check how socket is doing, accept responses and send pending async requests. |
| 143 |
// This should be done regularly: in fact, timer calls it often. |
| 144 |
void think(); |
| 145 |
// If there are any pending async commands (e.g. camera update), send one now. |
| 146 |
// Returns true iff anything was sent to game. |
| 147 |
bool sendAnyPendingAsync(); |
| 148 |
|
| 149 |
// Given a command to be executed in game console (no EOLs), returns its full request text. |
| 150 |
// The result is ready to be sent over to AutomationEngine, which will prepend seqno automatically. |
| 151 |
static std::string composeConExecRequest(std::string consoleLine); |
| 152 |
|
| 153 |
// Waits for previous TAG_GENERIC requests to finish, then executes request as TAG_GENERIC (blocking). |
| 154 |
std::string executeGenericRequest(const std::string& request); |
| 155 |
// Set noclip or god or notarget to specific state (blocking). |
| 156 |
// toggleCommand is the command which toggles state. |
| 157 |
// offKeyword is the part of phrase printed to game console when the state becomes disabled. |
| 158 |
void executeSetTogglableFlag(const std::string &toggleCommand, bool enable, const std::string &offKeyword); |
| 159 |
// Learn state of the specified cvar (blocking). |
| 160 |
std::string executeGetCvarValue(const std::string &cvarName, std::string *defaultValue = nullptr); |
| 161 |
// Learn current status: installed mod/map, active gui, etc. (blocking). |
| 162 |
std::map<std::string, std::string> executeQueryStatus(); |
| 163 |
|
| 164 |
// Called from camera modification callback: schedules async "setviewpos" action for future. |
| 165 |
void updateCamera(); |
| 166 |
// Send request for camera update, which is pending yet. |
| 167 |
bool sendPendingCameraUpdate(); |
| 168 |
// Enable notarget/god/noclip to allow player to fly around without problems. |
| 169 |
void enableGhostMode(); |
| 170 |
|
| 171 |
// Save map using DarkRadiant command if there are any pending modifications. |
| 172 |
void saveMapIfNeeded(); |
| 173 |
// Callback called on map saving, loading and unloading. |
| 174 |
void onMapEvent(IMap::MapEvent ev); |
| 175 |
}; |
| 176 |
|
| 177 |
} |