MainFrame: add game status checking.

Dangerous operations are also blocked during that time.
This commit is contained in:
Guillaume Jacquemin 2020-01-10 12:23:25 +01:00
parent 83008d3b01
commit aaf38469bd
6 changed files with 244 additions and 25 deletions

View file

@ -45,4 +45,5 @@ target_link_libraries(wxMASSManager PRIVATE
Corrade::Containers
Corrade::Utility
wx_baseu-3.0
wx_mswu_core-3.0)
wx_mswu_core-3.0
wtsapi32)

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstring>
#include <algorithm>
#include <vector>
@ -24,6 +26,7 @@
#define WIN32_LEAN_AND_MEAN
#include <shlobj.h>
#include <wtsapi32.h>
#include <Corrade/Containers/Array.h>
#include <Corrade/Utility/Directory.h>
@ -46,6 +49,8 @@ EvtMainFrame::EvtMainFrame(wxWindow* parent): MainFrame(parent) {
getLocalSteamId();
initialiseListView();
isGameRunning();
_installedListView->Connect(wxEVT_LIST_ITEM_SELECTED, wxListEventHandler(EvtMainFrame::installedSelectionEvent), nullptr, this);
_installedListView->Connect(wxEVT_LIST_ITEM_DESELECTED, wxListEventHandler(EvtMainFrame::installedSelectionEvent), nullptr, this);
_installedListView->Connect(wxEVT_LIST_BEGIN_DRAG, wxListEventHandler(EvtMainFrame::listColumnDragEvent), nullptr, this);
@ -60,6 +65,8 @@ EvtMainFrame::EvtMainFrame(wxWindow* parent): MainFrame(parent) {
_watcher.Connect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler(EvtMainFrame::fileUpdateEvent), nullptr, this);
_watcher.AddTree(wxFileName(Utility::Directory::toNativeSeparators(_saveDirectory), wxPATH_WIN),
wxFSW_EVENT_CREATE|wxFSW_EVENT_DELETE|wxFSW_EVENT_MODIFY|wxFSW_EVENT_RENAME, wxString::Format("Unit??%s.sav", _localSteamId));
_gameCheckTimer.Start(3000);
}
EvtMainFrame::~EvtMainFrame() {
@ -98,6 +105,11 @@ void EvtMainFrame::importEvent(wxCommandEvent&) {
return;
}
if(_isGameRunning) {
errorMessage("The game is running. Aborting...");
return;
}
const std::string dest_file = _saveDirectory + Utility::formatString("/Unit{:.2d}{}.sav", _installedListView->GetFirstSelected(), _localSteamId);
if(Utility::Directory::exists(dest_file)) {
@ -135,6 +147,11 @@ void EvtMainFrame::moveEvent(wxCommandEvent&) {
return;
}
if(_isGameRunning) {
errorMessage("The game is running. Aborting...");
return;
}
std::string orig_file = Utility::formatString("{}/Unit{:.2d}{}.sav", _saveDirectory, source_slot, _localSteamId);
std::string dest_status = _installedListView->GetItemText(choice, 1).ToStdString();
std::string dest_file = Utility::formatString("{}/Unit{:.2d}{}.sav", _saveDirectory, choice, _localSteamId);
@ -159,6 +176,11 @@ void EvtMainFrame::deleteEvent(wxCommandEvent&) {
return;
}
if(_isGameRunning) {
errorMessage("The game is running. Aborting...");
return;
}
std::string file = Utility::formatString("{}/Unit{:.2d}{}.sav", _saveDirectory, _installedListView->GetFirstSelected(), _localSteamId);
if(Utility::Directory::exists(file)) {
@ -208,6 +230,10 @@ void EvtMainFrame::fileUpdateEvent(wxFileSystemWatcherEvent& event) {
updateCommandsState();
}
void EvtMainFrame::gameCheckTimerEvent(wxTimerEvent&) {
isGameRunning();
}
void EvtMainFrame::getSaveDirectory() {
wchar_t h[MAX_PATH];
if(!SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, h))) {
@ -256,6 +282,44 @@ void EvtMainFrame::initialiseListView() {
refreshListView();
}
void EvtMainFrame::isGameRunning() {
WTS_PROCESS_INFOW* process_infos = nullptr;
unsigned long process_count = 0;
if(WTSEnumerateProcessesW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &process_infos, &process_count)) {
for(unsigned long i = 0; i < process_count; ++i) {
if(std::wcscmp(process_infos[i].pProcessName, L"MASS_Builder-Win64-Shipping.exe") == 0) {
_isGameRunning = true;
break;
}
else {
_isGameRunning = false;
}
}
if(_isGameRunning) {
_gameStatus->SetLabel("running");
_gameStatus->SetForegroundColour(wxColour("red"));
}
else {
_gameStatus->SetLabel("not running");
_gameStatus->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_CAPTIONTEXT));
}
}
else {
_isGameRunning = false;
_gameStatus->SetLabel("unknown");
_gameStatus->SetForegroundColour(wxColour("orange"));
}
if(process_infos != nullptr) {
WTSFreeMemory(process_infos);
process_infos = nullptr;
}
updateCommandsState();
}
void EvtMainFrame::refreshListView() {
for(long i = 0; i < 32; i++) {
_installedListView->SetItem(i, 1, getSlotMassName(i));
@ -266,30 +330,14 @@ void EvtMainFrame::refreshListView() {
void EvtMainFrame::updateCommandsState() {
long selection = _installedListView->GetFirstSelected();
wxString state = "";
if(selection != -1) {
_importButton->Enable();
wxString state = _installedListView->GetItemText(selection, 1);
if(state != "<Empty>" && state != "<Invalid data>") {
_moveButton->Enable();
}
else {
_moveButton->Disable();
}
if(state != "<Empty>") {
_deleteButton->Enable();
}
else {
_deleteButton->Disable();
}
}
else {
_importButton->Disable();
_moveButton->Disable();
_deleteButton->Disable();
state = _installedListView->GetItemText(selection, 1);
}
_importButton->Enable(selection != -1 && !_isGameRunning);
_moveButton->Enable(selection != -1 && !_isGameRunning && state != "<Empty>" && state != "<Invalid data>");
_deleteButton->Enable(selection != -1 && !_isGameRunning && state != "<Empty>");
}
std::string EvtMainFrame::getSlotMassName(int index) {

View file

@ -36,11 +36,13 @@ class EvtMainFrame: public MainFrame {
void installedSelectionEvent(wxListEvent&);
void listColumnDragEvent(wxListEvent&);
void fileUpdateEvent(wxFileSystemWatcherEvent& event);
void gameCheckTimerEvent(wxTimerEvent&);
private:
void getSaveDirectory();
void getLocalSteamId();
void initialiseListView();
void isGameRunning();
void refreshListView();
void updateCommandsState();
std::string getSlotMassName(int index);
@ -52,6 +54,7 @@ class EvtMainFrame: public MainFrame {
std::string _saveDirectory;
std::string _localSteamId;
bool _isGameRunning = false;
wxFileSystemWatcher _watcher;
int _lastWatcherEventType = 0;

View file

@ -69,7 +69,24 @@ MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, co
_riskLabel->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxEmptyString ) );
_riskLabel->SetForegroundColour( wxColour( 255, 0, 0 ) );
bSizerMain->Add( _riskLabel, 0, wxALIGN_CENTER_HORIZONTAL|wxEXPAND|wxTOP|wxRIGHT|wxLEFT, 5 );
bSizerMain->Add( _riskLabel, 0, wxALIGN_CENTER_HORIZONTAL|wxTOP|wxRIGHT|wxLEFT, 5 );
wxBoxSizer* bSizerGameStatus;
bSizerGameStatus = new wxBoxSizer( wxHORIZONTAL );
_gameStatusLabel = new wxStaticText( this, wxID_ANY, wxT("Game status:"), wxDefaultPosition, wxDefaultSize, 0 );
_gameStatusLabel->Wrap( -1 );
bSizerGameStatus->Add( _gameStatusLabel, 1, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND|wxTOP|wxBOTTOM|wxLEFT, 5 );
_gameStatus = new wxStaticText( this, wxID_ANY, wxT("not running"), wxDefaultPosition, wxDefaultSize, 0 );
_gameStatus->Wrap( -1 );
_gameStatus->SetFont( wxFont( wxNORMAL_FONT->GetPointSize(), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxEmptyString ) );
_gameStatus->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_CAPTIONTEXT ) );
bSizerGameStatus->Add( _gameStatus, 0, wxALL, 5 );
bSizerMain->Add( bSizerGameStatus, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
_aboutText = new wxStaticText( this, wxID_ANY, wxT("This version of the application was tested on M.A.S.S. Builder early access version 0.2.4.\nIt may or may not work with other versions of the game.\nMade for the M.A.S.S. Builder community by Guillaume Jacquemin.\nhttps://github.com/williamjcm/wxMASSManager"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_HORIZONTAL );
_aboutText->Wrap( -1 );
@ -79,6 +96,7 @@ MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, co
this->SetSizer( bSizerMain );
this->Layout();
bSizerMain->Fit( this );
_gameCheckTimer.SetOwner( this, wxID_ANY );
this->Centre( wxBOTH );
@ -87,6 +105,7 @@ MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, co
_moveButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::moveEvent ), NULL, this );
_deleteButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::deleteEvent ), NULL, this );
_openSaveDirButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::openSaveDirEvent ), NULL, this );
this->Connect( wxID_ANY, wxEVT_TIMER, wxTimerEventHandler( MainFrame::gameCheckTimerEvent ) );
}
MainFrame::~MainFrame()
@ -96,5 +115,6 @@ MainFrame::~MainFrame()
_moveButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::moveEvent ), NULL, this );
_deleteButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::deleteEvent ), NULL, this );
_openSaveDirButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::openSaveDirEvent ), NULL, this );
this->Disconnect( wxID_ANY, wxEVT_TIMER, wxTimerEventHandler( MainFrame::gameCheckTimerEvent ) );
}

View file

@ -482,7 +482,7 @@
</object>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxALIGN_CENTER_HORIZONTAL|wxEXPAND|wxTOP|wxRIGHT|wxLEFT</property>
<property name="flag">wxALIGN_CENTER_HORIZONTAL|wxTOP|wxRIGHT|wxLEFT</property>
<property name="proportion">0</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
@ -541,6 +541,139 @@
<property name="wrap">-1</property>
</object>
</object>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxALIGN_CENTER_HORIZONTAL</property>
<property name="proportion">0</property>
<object class="wxBoxSizer" expanded="1">
<property name="minimum_size"></property>
<property name="name">bSizerGameStatus</property>
<property name="orient">wxHORIZONTAL</property>
<property name="permission">none</property>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND|wxTOP|wxBOTTOM|wxLEFT</property>
<property name="proportion">1</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
<property name="LeftDockable">1</property>
<property name="RightDockable">1</property>
<property name="TopDockable">1</property>
<property name="aui_layer"></property>
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="context_help"></property>
<property name="context_menu">1</property>
<property name="default_pane">0</property>
<property name="dock">Dock</property>
<property name="dock_fixed">0</property>
<property name="docking">Left</property>
<property name="enabled">1</property>
<property name="fg"></property>
<property name="floatable">1</property>
<property name="font"></property>
<property name="gripper">0</property>
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label">Game status:</property>
<property name="markup">0</property>
<property name="max_size"></property>
<property name="maximize_button">0</property>
<property name="maximum_size"></property>
<property name="min_size"></property>
<property name="minimize_button">0</property>
<property name="minimum_size"></property>
<property name="moveable">1</property>
<property name="name">_gameStatusLabel</property>
<property name="pane_border">1</property>
<property name="pane_position"></property>
<property name="pane_size"></property>
<property name="permission">protected</property>
<property name="pin_button">1</property>
<property name="pos"></property>
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style"></property>
<property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<property name="wrap">-1</property>
</object>
</object>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxALL</property>
<property name="proportion">0</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
<property name="LeftDockable">1</property>
<property name="RightDockable">1</property>
<property name="TopDockable">1</property>
<property name="aui_layer"></property>
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="context_help"></property>
<property name="context_menu">1</property>
<property name="default_pane">0</property>
<property name="dock">Dock</property>
<property name="dock_fixed">0</property>
<property name="docking">Left</property>
<property name="enabled">1</property>
<property name="fg">wxSYS_COLOUR_CAPTIONTEXT</property>
<property name="floatable">1</property>
<property name="font">,90,92,-1,70,0</property>
<property name="gripper">0</property>
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label">not running</property>
<property name="markup">0</property>
<property name="max_size"></property>
<property name="maximize_button">0</property>
<property name="maximum_size"></property>
<property name="min_size"></property>
<property name="minimize_button">0</property>
<property name="minimum_size"></property>
<property name="moveable">1</property>
<property name="name">_gameStatus</property>
<property name="pane_border">1</property>
<property name="pane_position"></property>
<property name="pane_size"></property>
<property name="permission">protected</property>
<property name="pin_button">1</property>
<property name="pos"></property>
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style"></property>
<property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<property name="wrap">-1</property>
</object>
</object>
</object>
</object>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT</property>
@ -603,6 +736,15 @@
</object>
</object>
</object>
<object class="wxTimer" expanded="1">
<property name="enabled">0</property>
<property name="id">wxID_ANY</property>
<property name="name">_gameCheckTimer</property>
<property name="oneshot">0</property>
<property name="period">5000</property>
<property name="permission">protected</property>
<event name="OnTimer">gameCheckTimerEvent</event>
</object>
</object>
</object>
</wxFormBuilder_Project>

View file

@ -22,6 +22,7 @@
#include <wx/sizer.h>
#include <wx/statbox.h>
#include <wx/stattext.h>
#include <wx/timer.h>
#include <wx/frame.h>
///////////////////////////////////////////////////////////////////////////
@ -41,13 +42,17 @@ class MainFrame : public wxFrame
wxButton* _deleteButton;
wxButton* _openSaveDirButton;
wxStaticText* _riskLabel;
wxStaticText* _gameStatusLabel;
wxStaticText* _gameStatus;
wxStaticText* _aboutText;
wxTimer _gameCheckTimer;
// Virtual event handlers, overide them in your derived class
virtual void importEvent( wxCommandEvent& event ) { event.Skip(); }
virtual void moveEvent( wxCommandEvent& event ) { event.Skip(); }
virtual void deleteEvent( wxCommandEvent& event ) { event.Skip(); }
virtual void openSaveDirEvent( wxCommandEvent& event ) { event.Skip(); }
virtual void gameCheckTimerEvent( wxTimerEvent& event ) { event.Skip(); }
public: