2021-06-11 19:24:52 +02:00
// MassBuilderSaveTool
2022-01-30 11:36:56 +01:00
// Copyright (C) 2021-2022 Guillaume Jacquemin
2021-06-11 19:24:52 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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 "SaveTool.h"
# include <cstring>
2022-03-09 11:04:19 +01:00
# include <Corrade/Containers/Pair.h>
2021-07-21 14:36:49 +02:00
# include <Corrade/Containers/ScopeGuard.h>
2022-03-09 13:27:45 +01:00
# include <Corrade/Utility/Format.h>
2022-03-09 11:04:19 +01:00
# include <Corrade/Utility/Path.h>
2021-06-11 19:24:52 +02:00
# include <Corrade/Utility/String.h>
# include <Corrade/Utility/Unicode.h>
2021-08-18 17:16:10 +02:00
# include <Magnum/GL/DebugOutput.h>
2021-06-11 19:24:52 +02:00
# include <Magnum/GL/DefaultFramebuffer.h>
2021-08-18 17:16:10 +02:00
# include <Magnum/GL/Extensions.h>
2021-06-11 19:24:52 +02:00
# include <Magnum/GL/Renderer.h>
# include <Magnum/ImGuiIntegration/Integration.h>
# include <Magnum/ImGuiIntegration/Context.hpp>
2021-07-22 11:39:53 +02:00
# include <cpr/cpr.h>
2021-06-11 19:24:52 +02:00
# include <windef.h>
# include <winuser.h>
# include <processthreadsapi.h>
# include <shellapi.h>
2021-07-21 14:36:49 +02:00
# include <shlobj.h>
# include <wtsapi32.h>
2021-06-11 19:24:52 +02:00
# include "../FontAwesome/IconsFontAwesome5.h"
# include "../FontAwesome/IconsFontAwesome5Brands.h"
2022-03-04 21:18:55 +01:00
using namespace Containers : : Literals ;
2021-06-11 19:24:52 +02:00
extern const ImVec2 center_pivot = { 0.5f , 0.5f } ;
2021-07-13 18:35:23 +02:00
# ifdef SAVETOOL_DEBUG_BUILD
2021-06-11 19:24:52 +02:00
Utility : : Tweakable tweak ;
# endif
SaveTool : : SaveTool ( const Arguments & arguments ) :
Platform : : Sdl2Application { arguments ,
2021-07-13 18:46:21 +02:00
Configuration { } . setTitle ( " M.A.S.S. Builder Save Tool " SAVETOOL_VERSION " ( \" " SAVETOOL_CODENAME " \" ) " )
2021-06-11 19:24:52 +02:00
. setSize ( { 960 , 720 } ) }
{
2021-07-13 18:35:23 +02:00
# ifdef SAVETOOL_DEBUG_BUILD
2022-03-04 21:18:55 +01:00
tweak . enable ( " " _s , " ../../ " _s ) ;
2021-06-11 19:24:52 +02:00
# endif
GL : : Renderer : : enable ( GL : : Renderer : : Feature : : Blending ) ;
GL : : Renderer : : enable ( GL : : Renderer : : Feature : : ScissorTest ) ;
GL : : Renderer : : disable ( GL : : Renderer : : Feature : : FaceCulling ) ;
GL : : Renderer : : disable ( GL : : Renderer : : Feature : : DepthTest ) ;
GL : : Renderer : : setBlendFunction ( GL : : Renderer : : BlendFunction : : SourceAlpha ,
GL : : Renderer : : BlendFunction : : OneMinusSourceAlpha ) ;
GL : : Renderer : : setBlendEquation ( GL : : Renderer : : BlendEquation : : Add ,
GL : : Renderer : : BlendEquation : : Add ) ;
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Renderer initialisation successful. " ;
Utility : : Debug { } < < " ===Configuring SDL2=== " ;
{
Utility : : Debug d { } ;
d < < " Enabling clickthrough... " ;
if ( SDL_VERSION_ATLEAST ( 2 , 0 , 5 ) ) {
if ( SDL_SetHintWithPriority ( SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH , " 1 " , SDL_HINT_OVERRIDE ) = = SDL_TRUE ) {
d < < " success! " _s ;
} else {
d < < " error: hint couldn't be set. " _s ;
}
} else {
d < < " error: SDL2 is too old (version < 2.0.5). " _s ;
}
}
2021-06-11 19:24:52 +02:00
2022-03-09 13:27:45 +01:00
if ( ( _initEventId = SDL_RegisterEvents ( 3 ) ) = = UnsignedInt ( - 1 ) ) {
2021-06-11 19:24:52 +02:00
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Error " ,
2022-03-09 13:27:45 +01:00
" SDL_RegisterEvents() failed in SaveTool::SaveTool(). Exiting... " , window ( ) ) ;
2021-07-21 14:36:49 +02:00
exit ( EXIT_FAILURE ) ;
2021-07-22 11:39:53 +02:00
return ;
2021-07-21 14:36:49 +02:00
}
2021-07-22 11:39:53 +02:00
_updateEventId = _initEventId + 1 ;
2022-03-09 13:27:45 +01:00
_fileEventId = _initEventId + 2 ;
2021-07-22 11:39:53 +02:00
2022-03-30 14:18:08 +02:00
if ( SDL_InitSubSystem ( SDL_INIT_TIMER ) ! = 0 ) {
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Error initialising the app " , SDL_GetError ( ) , window ( ) ) ;
exit ( EXIT_FAILURE ) ;
return ;
}
Utility : : Debug { } < < " SDL2 configuration successful. " ;
2022-01-15 11:31:33 +01:00
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " ===Initialising the Save Tool=== " ;
initialiseGui ( ) ;
if ( ! initialiseToolDirectories ( ) ) {
2022-03-04 21:18:55 +01:00
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Error initialising the app " , _lastError . data ( ) , window ( ) ) ;
2021-07-21 14:36:49 +02:00
exit ( EXIT_FAILURE ) ;
2021-07-22 11:39:53 +02:00
return ;
2021-07-21 14:36:49 +02:00
}
2022-03-30 14:18:08 +02:00
if ( ! findGameDataDirectory ( ) ) {
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Error initialising the app " , _lastError . data ( ) , window ( ) ) ;
2021-07-21 14:36:49 +02:00
exit ( EXIT_FAILURE ) ;
2021-07-22 11:39:53 +02:00
return ;
2021-07-21 14:36:49 +02:00
}
checkGameState ( ) ;
_gameCheckTimerId = SDL_AddTimer ( 2000 ,
[ ] ( UnsignedInt interval , void * param ) - > UnsignedInt {
static_cast < SaveTool * > ( param ) - > checkGameState ( ) ;
return interval ;
2022-03-04 21:18:55 +01:00
} , this ) ;
2021-07-21 14:36:49 +02:00
if ( _gameCheckTimerId = = 0 ) {
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Error " , SDL_GetError ( ) , window ( ) ) ;
2021-06-11 19:24:52 +02:00
exit ( EXIT_FAILURE ) ;
2021-07-22 11:39:53 +02:00
return ;
2021-06-11 19:24:52 +02:00
}
2021-07-23 14:17:42 +02:00
initialiseConfiguration ( ) ;
2021-07-28 14:17:21 +02:00
2021-08-18 16:53:29 +02:00
switch ( _framelimit ) {
case Framelimit : : Vsync :
setSwapInterval ( 1 ) ;
break ;
case Framelimit : : HalfVsync :
setSwapInterval ( 2 ) ;
break ;
case Framelimit : : FpsCap :
setSwapInterval ( 0 ) ;
setMinimalLoopPeriod ( 1000 / _fpsCap ) ;
break ;
}
2021-07-28 14:17:21 +02:00
if ( _checkUpdatesOnStartup ) {
2021-08-18 20:09:22 +02:00
_updateThread = std : : thread { [ this ] { checkForUpdates ( ) ; } } ;
2022-03-04 21:18:55 +01:00
_queue . addToast ( Toast : : Type : : Default , " Checking for updates... " _s ) ;
2021-07-28 14:17:21 +02:00
}
2021-08-18 17:16:10 +02:00
if ( GL : : Context : : current ( ) . isExtensionSupported < GL : : Extensions : : KHR : : debug > ( ) & &
GL : : Context : : current ( ) . detectedDriver ( ) = = GL : : Context : : DetectedDriver : : NVidia )
{
GL : : DebugOutput : : setEnabled ( GL : : DebugOutput : : Source : : Api , GL : : DebugOutput : : Type : : Other , { 131185 } , false ) ;
}
2021-08-18 20:09:22 +02:00
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Initialisation successful. " ;
Utility : : Debug { } < < " ===Running main loop=== " ;
2021-08-18 20:09:22 +02:00
if ( _skipDisclaimer ) {
_uiState = UiState : : Initialising ;
_initThread = std : : thread { [ this ] { initialiseManager ( ) ; } } ;
}
2021-06-11 19:24:52 +02:00
}
SaveTool : : ~ SaveTool ( ) {
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " ===Perfoming cleanup=== " ;
2021-06-11 19:24:52 +02:00
SDL_RemoveTimer ( _gameCheckTimerId ) ;
2021-07-23 14:17:42 +02:00
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Saving the configuration... " ;
2022-03-04 21:18:55 +01:00
_conf . setValue ( " cheat_mode " _s , _cheatMode ) ;
_conf . setValue ( " unsafe_mode " _s , _unsafeMode ) ;
_conf . setValue ( " startup_update_check " _s , _checkUpdatesOnStartup ) ;
_conf . setValue ( " skip_disclaimer " _s , _skipDisclaimer ) ;
2021-08-18 16:53:29 +02:00
switch ( _framelimit ) {
case Framelimit : : Vsync :
2022-03-04 21:18:55 +01:00
_conf . setValue ( " frame_limit " _s , " vsync " _s ) ;
2021-08-18 16:53:29 +02:00
break ;
case Framelimit : : HalfVsync :
2022-03-04 21:18:55 +01:00
_conf . setValue ( " frame_limit " _s , " half_vsync " _s ) ;
2021-08-18 16:53:29 +02:00
break ;
case Framelimit : : FpsCap :
2022-03-04 21:18:55 +01:00
_conf . setValue < UnsignedInt > ( " frame_limit " _s , _fpsCap ) ;
2021-08-18 16:53:29 +02:00
break ;
}
2021-07-23 14:17:42 +02:00
_conf . save ( ) ;
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Exiting... " ;
2021-06-11 19:24:52 +02:00
}
void SaveTool : : handleFileAction ( efsw : : WatchID watch_id ,
2021-06-20 14:14:10 +02:00
const std : : string & ,
2021-06-11 19:24:52 +02:00
const std : : string & filename ,
efsw : : Action action ,
std : : string old_filename )
{
2022-03-09 13:27:45 +01:00
SDL_Event event ;
SDL_zero ( event ) ;
event . type = _fileEventId ;
2021-06-11 19:24:52 +02:00
if ( watch_id = = _watchIDs [ StagingDir ] & & Utility : : String : : endsWith ( filename , " .sav " ) ) {
2022-03-09 13:27:45 +01:00
event . user . code = StagedUpdate ;
SDL_PushEvent ( & event ) ;
2021-06-11 19:24:52 +02:00
return ;
}
if ( Utility : : String : : endsWith ( filename , " Config.sav " ) ) {
return ;
2022-03-09 13:27:45 +01:00
} // TODO: actually do something when config files will finally be handled
2021-09-27 17:54:30 +02:00
2022-03-09 13:27:45 +01:00
if ( ! Utility : : String : : endsWith ( filename , _currentProfile - > account ( ) + " .sav " ) ) {
return ;
}
2021-09-27 17:54:30 +02:00
2022-03-09 13:27:45 +01:00
event . user . code = action ;
2022-03-21 16:31:29 +01:00
event . user . data1 = Containers : : String { Containers : : AllocatedInit , filename . c_str ( ) } . release ( ) ;
2022-03-09 13:27:45 +01:00
if ( action = = efsw : : Actions : : Moved ) {
2022-03-28 09:46:29 +02:00
event . user . data2 = Containers : : String { Containers : : AllocatedInit , old_filename . c_str ( ) } . release ( ) ;
2021-06-11 19:24:52 +02:00
}
2022-03-09 13:27:45 +01:00
SDL_PushEvent ( & event ) ;
2021-06-11 19:24:52 +02:00
}
void SaveTool : : drawEvent ( ) {
2021-07-13 18:35:23 +02:00
# ifdef SAVETOOL_DEBUG_BUILD
2021-06-11 19:24:52 +02:00
tweak . update ( ) ;
# endif
GL : : defaultFramebuffer . clear ( GL : : FramebufferClear : : Color ) ;
drawImGui ( ) ;
swapBuffers ( ) ;
redraw ( ) ;
}
void SaveTool : : viewportEvent ( ViewportEvent & event ) {
GL : : defaultFramebuffer . setViewport ( { { } , event . framebufferSize ( ) } ) ;
_imgui . relayout ( event . windowSize ( ) ) ;
}
void SaveTool : : keyPressEvent ( KeyEvent & event ) {
if ( _imgui . handleKeyPressEvent ( event ) ) return ;
}
void SaveTool : : keyReleaseEvent ( KeyEvent & event ) {
if ( _imgui . handleKeyReleaseEvent ( event ) ) return ;
}
void SaveTool : : mousePressEvent ( MouseEvent & event ) {
if ( _imgui . handleMousePressEvent ( event ) ) return ;
}
void SaveTool : : mouseReleaseEvent ( MouseEvent & event ) {
if ( _imgui . handleMouseReleaseEvent ( event ) ) return ;
}
void SaveTool : : mouseMoveEvent ( MouseMoveEvent & event ) {
if ( _imgui . handleMouseMoveEvent ( event ) ) return ;
}
void SaveTool : : mouseScrollEvent ( MouseScrollEvent & event ) {
if ( _imgui . handleMouseScrollEvent ( event ) ) {
event . setAccepted ( ) ;
return ;
}
}
void SaveTool : : textInputEvent ( TextInputEvent & event ) {
if ( _imgui . handleTextInputEvent ( event ) ) return ;
}
void SaveTool : : anyEvent ( SDL_Event & event ) {
if ( event . type = = _initEventId ) {
initEvent ( event ) ;
}
2021-07-22 11:39:53 +02:00
else if ( event . type = = _updateEventId ) {
updateCheckEvent ( event ) ;
}
2022-03-09 13:27:45 +01:00
else if ( event . type = = _fileEventId ) {
fileUpdateEvent ( event ) ;
}
2021-06-11 19:24:52 +02:00
}
void SaveTool : : initEvent ( SDL_Event & event ) {
2021-08-18 20:09:22 +02:00
_initThread . join ( ) ;
2021-06-11 19:24:52 +02:00
switch ( event . user . code ) {
case InitSuccess :
_uiState = UiState : : ProfileManager ;
ImGui : : CloseCurrentPopup ( ) ;
break ;
case ProfileManagerFailure :
2022-03-30 14:18:08 +02:00
Utility : : Error { } < < " Error initialising ProfileManager: " < < _profileManager - > lastError ( ) ;
2022-03-04 21:18:55 +01:00
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Error initialising ProfileManager " , _profileManager - > lastError ( ) . data ( ) , window ( ) ) ;
2021-06-11 19:24:52 +02:00
exit ( EXIT_FAILURE ) ;
break ;
default :
break ;
}
}
2021-07-22 11:39:53 +02:00
void SaveTool : : updateCheckEvent ( SDL_Event & event ) {
2021-08-18 20:09:22 +02:00
_updateThread . join ( ) ;
2021-07-22 11:39:53 +02:00
2021-07-28 14:17:21 +02:00
cpr : : Response r { std : : move ( * static_cast < cpr : : Response * > ( event . user . data1 ) ) } ;
delete static_cast < cpr : : Response * > ( event . user . data1 ) ;
if ( r . elapsed > 10.0 ) {
2022-03-04 21:18:55 +01:00
_queue . addToast ( Toast : : Type : : Error , " The request timed out. " _s ) ;
2021-07-28 14:17:21 +02:00
return ;
}
2022-03-31 09:00:57 +02:00
if ( r . status_code = = 0 ) {
_queue . addToast ( Toast : : Type : : Error , " Seems like the connection was blocked. \n Please check your firewall's settings. " ) ;
}
2021-07-28 14:17:21 +02:00
if ( r . status_code ! = 200 ) {
2022-03-31 09:00:57 +02:00
_queue . addToast ( Toast : : Type : : Error , Utility : : format ( " The request failed with error code {}: \n {} " , r . status_code , r . reason . c_str ( ) ) ) ;
2021-07-28 14:17:21 +02:00
return ;
}
2021-07-28 14:18:39 +02:00
struct Version {
2022-03-04 21:18:55 +01:00
explicit Version ( Containers : : StringView str ) {
2021-07-28 14:18:39 +02:00
std : : size_t start_point = 0 ;
if ( str [ 0 ] = = ' v ' ) {
start_point + + ;
}
2022-03-21 16:47:29 +01:00
auto components = Containers : : StringView { str . data ( ) + start_point } . split ( ' . ' ) ;
2022-03-04 21:18:55 +01:00
major = std : : strtol ( components [ 0 ] . data ( ) , nullptr , 10 ) ;
minor = std : : strtol ( components [ 1 ] . data ( ) , nullptr , 10 ) ;
patch = std : : strtol ( components [ 2 ] . data ( ) , nullptr , 10 ) ;
2022-03-30 15:15:54 +02:00
fullVersion = major * 10000 + minor * 100 + patch ;
if ( str . hasSuffix ( " -pre " ) ) {
prerelease = true ;
}
2021-07-28 14:18:39 +02:00
}
2022-03-30 15:15:54 +02:00
Int fullVersion ;
Int major = 0 ;
Int minor = 0 ;
Int patch = 0 ;
bool prerelease = false ;
2021-07-28 14:18:39 +02:00
bool operator = = ( const Version & other ) const {
2022-03-30 15:15:54 +02:00
return fullVersion = = other . fullVersion ;
2021-07-28 14:18:39 +02:00
}
bool operator > ( const Version & other ) const {
2022-03-30 15:15:54 +02:00
if ( ( fullVersion > other . fullVersion ) | |
( fullVersion = = other . fullVersion & & prerelease = = false & & other . prerelease = = true ) )
{
return true ;
}
else {
return false ;
}
2021-07-28 14:18:39 +02:00
}
2022-03-04 21:18:55 +01:00
operator Containers : : String ( ) const {
return Utility : : format ( " {}.{}.{} " , major , minor , patch ) ;
2022-01-03 12:10:20 +01:00
}
2021-07-28 14:18:39 +02:00
} ;
2021-07-28 14:27:19 +02:00
static const Version current_ver { SAVETOOL_VERSION } ;
2021-07-28 14:18:39 +02:00
2022-03-30 15:15:54 +02:00
Containers : : String response = r . text . c_str ( ) ;
auto components = response . split ( ' \n ' ) ;
2021-12-02 20:37:47 +01:00
2022-03-30 15:15:54 +02:00
Version latest_ver { components . front ( ) } ;
2021-12-02 20:37:47 +01:00
2022-03-30 15:15:54 +02:00
if ( latest_ver > current_ver ) {
_queue . addToast ( Toast : : Type : : Warning , " Your version is out of date. \n Check the settings for more information. " _s ,
std : : chrono : : milliseconds { 5000 } ) ;
_updateAvailable = true ;
_latestVersion = latest_ver ;
_releaseLink = Utility : : format ( " https://williamjcm.ovh/git/williamjcm/MassBuilderSaveTool/releases/tag/v{} " , components . front ( ) ) ;
_downloadLink = components . back ( ) ;
}
else if ( latest_ver = = current_ver | | ( current_ver > latest_ver & & current_ver . prerelease = = true ) ) {
_queue . addToast ( Toast : : Type : : Success , " The application is already up to date. " _s ) ;
}
else if ( current_ver > latest_ver & & current_ver . prerelease = = false ) {
_queue . addToast ( Toast : : Type : : Warning , " Your version is more recent than the latest one in the repo. How??? " _s ) ;
2021-07-28 14:18:39 +02:00
}
2021-07-22 11:39:53 +02:00
}
2022-03-09 13:27:45 +01:00
void SaveTool : : fileUpdateEvent ( SDL_Event & event ) {
2022-03-09 14:02:56 +01:00
if ( event . user . code = = StagedUpdate ) {
_massManager - > refreshStagedMasses ( ) ;
return ;
}
2022-03-09 13:27:45 +01:00
Containers : : String filename { static_cast < char * > ( event . user . data1 ) , std : : strlen ( static_cast < char * > ( event . user . data1 ) ) , nullptr } ;
Containers : : String old_filename ;
Int index = 0 ;
Int old_index = 0 ;
bool is_current_profile = filename = = _currentProfile - > filename ( ) ;
bool is_unit = filename . hasPrefix ( _currentProfile - > isDemo ( ) ? " DemoUnit " _s : " Unit " _s ) ;
if ( is_unit ) {
index = ( ( filename [ _currentProfile - > isDemo ( ) ? 8 : 4 ] - 0x30 ) * 10 ) +
( filename [ _currentProfile - > isDemo ( ) ? 9 : 5 ] - 0x30 ) ;
}
2022-03-22 09:59:30 +01:00
2022-03-09 13:27:45 +01:00
if ( event . user . code = = FileMoved ) {
old_filename = Containers : : String { static_cast < char * > ( event . user . data2 ) , std : : strlen ( static_cast < char * > ( event . user . data2 ) ) , nullptr } ;
old_index = ( ( old_filename [ _currentProfile - > isDemo ( ) ? 8 : 4 ] - 0x30 ) * 10 ) +
( old_filename [ _currentProfile - > isDemo ( ) ? 9 : 5 ] - 0x30 ) ;
}
switch ( event . user . code ) {
case FileAdded :
if ( is_unit ) {
if ( ! _currentMass | | _currentMass ! = & ( _massManager - > hangar ( index ) ) ) {
_massManager - > refreshHangar ( index ) ;
}
else {
_currentMass - > setDirty ( ) ;
}
}
break ;
case FileDeleted :
if ( is_current_profile ) {
_currentProfile = nullptr ;
_uiState = UiState : : ProfileManager ;
_profileManager - > refreshProfiles ( ) ;
}
else if ( is_unit ) {
if ( ! _currentMass | | _currentMass ! = & ( _massManager - > hangar ( index ) ) ) {
_massManager - > refreshHangar ( index ) ;
}
}
break ;
case FileModified :
if ( is_current_profile ) {
_currentProfile - > refreshValues ( ) ;
}
else if ( is_unit ) {
if ( ! _currentMass | | _currentMass ! = & ( _massManager - > hangar ( index ) ) ) {
_massManager - > refreshHangar ( index ) ;
}
else {
2022-03-22 09:59:30 +01:00
if ( _modifiedBySaveTool & & _currentMass - > filename ( ) = = filename ) {
auto handle = CreateFileW ( Utility : : Unicode : : widen ( Containers : : StringView { filename } ) . data ( ) , GENERIC_READ , 0 ,
nullptr , OPEN_EXISTING , 0 , nullptr ) ;
if ( handle & & handle ! = INVALID_HANDLE_VALUE ) {
CloseHandle ( handle ) ;
_modifiedBySaveTool = false ;
}
}
else {
2022-03-09 13:27:45 +01:00
_currentMass - > setDirty ( ) ;
}
}
}
break ;
case FileMoved :
if ( is_unit ) {
if ( old_filename . hasSuffix ( " .sav " _s ) ) {
_massManager - > refreshHangar ( index ) ;
_massManager - > refreshHangar ( old_index ) ;
}
}
break ;
default :
_queue . addToast ( Toast : : Type : : Warning , " Unknown file action type " _s ) ;
}
}
2021-07-23 14:17:42 +02:00
void SaveTool : : initialiseConfiguration ( ) {
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Reading configuration file... " ;
2022-03-04 21:18:55 +01:00
if ( _conf . hasValue ( " cheat_mode " _s ) ) {
_cheatMode = _conf . value < bool > ( " cheat_mode " _s ) ;
2021-07-28 15:00:15 +02:00
}
else {
2022-03-04 21:18:55 +01:00
_conf . setValue ( " cheat_mode " _s , _cheatMode ) ;
2021-07-28 15:00:15 +02:00
}
2022-03-04 21:18:55 +01:00
if ( _conf . hasValue ( " unsafe_mode " _s ) ) {
_unsafeMode = _conf . value < bool > ( " unsafe_mode " _s ) ;
2021-07-23 14:17:42 +02:00
}
else {
2022-03-04 21:18:55 +01:00
_conf . setValue ( " unsafe_mode " _s , _unsafeMode ) ;
2021-07-23 14:17:42 +02:00
}
2022-03-04 21:18:55 +01:00
if ( _conf . hasValue ( " startup_update_check " _s ) ) {
_checkUpdatesOnStartup = _conf . value < bool > ( " startup_update_check " _s ) ;
2021-07-23 14:17:42 +02:00
}
else {
2022-03-04 21:18:55 +01:00
_conf . setValue ( " startup_update_check " _s , _checkUpdatesOnStartup ) ;
2021-07-23 14:17:42 +02:00
}
2022-03-04 21:18:55 +01:00
if ( _conf . hasValue ( " skip_disclaimer " _s ) ) {
_skipDisclaimer = _conf . value < bool > ( " skip_disclaimer " _s ) ;
2021-08-18 20:09:22 +02:00
}
else {
2022-03-04 21:18:55 +01:00
_conf . setValue ( " skip_disclaimer " _s , _skipDisclaimer ) ;
2021-08-18 20:09:22 +02:00
}
2022-03-04 21:18:55 +01:00
if ( _conf . hasValue ( " frame_limit " _s ) ) {
std : : string frame_limit = _conf . value ( " frame_limit " _s ) ;
if ( frame_limit = = " vsync " _s ) {
2021-08-18 16:53:29 +02:00
_framelimit = Framelimit : : Vsync ;
}
2022-03-04 21:18:55 +01:00
else if ( frame_limit = = " half_vsync " _s ) {
2021-08-18 16:53:29 +02:00
_framelimit = Framelimit : : HalfVsync ;
}
else {
_framelimit = Framelimit : : FpsCap ;
_fpsCap = std : : stoul ( frame_limit ) ;
}
}
else {
2022-03-04 21:18:55 +01:00
_conf . setValue ( " frame_limit " _s , " vsync " _s ) ;
2021-08-18 16:53:29 +02:00
}
2021-07-23 14:17:42 +02:00
_conf . save ( ) ;
}
2021-06-11 19:24:52 +02:00
void SaveTool : : initialiseGui ( ) {
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Initialising ImGui... " ;
2021-06-11 19:24:52 +02:00
ImGui : : CreateContext ( ) ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
2022-03-04 21:18:55 +01:00
auto reg_font = _rs . getRaw ( " SourceSansPro-Regular.ttf " _s ) ;
2021-06-11 19:24:52 +02:00
ImFontConfig font_config ;
font_config . FontDataOwnedByAtlas = false ;
std : : strcpy ( font_config . Name , " Source Sans Pro " ) ;
io . Fonts - > AddFontFromMemoryTTF ( const_cast < char * > ( reg_font . data ( ) ) , reg_font . size ( ) , 20.0f , & font_config ) ;
auto icon_font = _rs . getRaw ( FONT_ICON_FILE_NAME_FAS ) ;
static const ImWchar icon_range [ ] = { ICON_MIN_FA , ICON_MAX_FA , 0 } ;
ImFontConfig icon_config ;
icon_config . FontDataOwnedByAtlas = false ;
icon_config . MergeMode = true ;
icon_config . PixelSnapH = true ;
icon_config . OversampleH = icon_config . OversampleV = 1 ;
icon_config . GlyphMinAdvanceX = 18.0f ;
io . Fonts - > AddFontFromMemoryTTF ( const_cast < char * > ( icon_font . data ( ) ) , icon_font . size ( ) , 16.0f , & icon_config , icon_range ) ;
auto brand_font = _rs . getRaw ( FONT_ICON_FILE_NAME_FAB ) ;
static const ImWchar brand_range [ ] = { ICON_MIN_FAB , ICON_MAX_FAB , 0 } ;
io . Fonts - > AddFontFromMemoryTTF ( const_cast < char * > ( brand_font . data ( ) ) , brand_font . size ( ) , 16.0f , & icon_config , brand_range ) ;
2022-03-04 21:18:55 +01:00
auto mono_font = _rs . getRaw ( " SourceCodePro-Regular.ttf " _s ) ;
2021-06-11 19:24:52 +02:00
ImVector < ImWchar > range ;
ImFontGlyphRangesBuilder builder ;
builder . AddRanges ( io . Fonts - > GetGlyphRangesDefault ( ) ) ;
builder . AddChar ( u ' š ' ) ; // This allows displaying Vladimír Vondruš' name in Corrade's and Magnum's licences.
builder . BuildRanges ( & range ) ;
io . Fonts - > AddFontFromMemoryTTF ( const_cast < char * > ( mono_font . data ( ) ) , mono_font . size ( ) , 18.0f , & font_config , range . Data ) ;
_imgui = ImGuiIntegration : : Context ( * ImGui : : GetCurrentContext ( ) , windowSize ( ) ) ;
io . IniFilename = nullptr ;
ImGuiStyle & style = ImGui : : GetStyle ( ) ;
style . WindowTitleAlign = { 0.5f , 0.5f } ;
style . FrameRounding = 3.2f ;
style . Colors [ ImGuiCol_WindowBg ] = ImColor ( 0xff1f1f1f ) ;
}
void SaveTool : : initialiseManager ( ) {
SDL_Event event ;
SDL_zero ( event ) ;
event . type = _initEventId ;
2021-07-21 20:38:33 +02:00
_profileManager . emplace ( _saveDir , _backupsDir ) ;
2021-06-11 19:24:52 +02:00
if ( ! _profileManager - > ready ( ) ) {
event . user . code = ProfileManagerFailure ;
SDL_PushEvent ( & event ) ;
return ;
}
event . user . code = InitSuccess ;
SDL_PushEvent ( & event ) ;
}
2022-03-30 14:18:08 +02:00
auto SaveTool : : initialiseToolDirectories ( ) - > bool {
Utility : : Debug { } < < " Initialising Save Tool directories... " ;
2022-03-09 11:04:19 +01:00
_backupsDir = Utility : : Path : : join ( Utility : : Path : : split ( * Utility : : Path : : executableLocation ( ) ) . first ( ) , " backups " ) ;
_stagingDir = Utility : : Path : : join ( Utility : : Path : : split ( * Utility : : Path : : executableLocation ( ) ) . first ( ) , " staging " ) ;
2022-03-06 09:12:40 +01:00
//_armouryDir = Utility::Directory::join(Utility::Directory::path(Utility::Directory::executableLocation()), "armoury");
//_armoursDir = Utility::Directory::join(_armouryDir, "armours");
//_weaponsDir = Utility::Directory::join(_armouryDir, "weapons");
//_stylesDir = Utility::Directory::join(_armouryDir, "styles");
2022-03-09 11:04:19 +01:00
if ( ! Utility : : Path : : exists ( _backupsDir ) ) {
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Backups directory not found, creating... " ;
if ( ! Utility : : Path : : make ( _backupsDir ) ) {
Utility : : Error { } < < ( _lastError = " Couldn't create the backups directory. " ) ;
return false ;
}
2022-03-06 09:12:40 +01:00
}
2022-03-09 11:04:19 +01:00
if ( ! Utility : : Path : : exists ( _stagingDir ) ) {
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Staging directory not found, creating... " ;
if ( ! Utility : : Path : : make ( _stagingDir ) ) {
Utility : : Error { } < < ( _lastError = " Couldn't create the backups directory. " ) ;
return false ;
}
2022-03-06 09:12:40 +01:00
}
//if(!Utility::Directory::exists(_armouryDir)) {
2022-03-30 14:18:08 +02:00
// Utility::Debug{} << "Armoury directory not found, creating...";
// if(!Utility::Path::make(_armouryDir)) {
// Utility::Error{} << (_lastError = "Couldn't create the armoury directory.");
// return false;
// }
2022-03-06 09:12:40 +01:00
//}
//if(!Utility::Directory::exists(_armoursDir)) {
2022-03-30 14:18:08 +02:00
// Utility::Debug{} << "Armours directory not found, creating...";
// if(!Utility::Path::make(_armoursDir)) {
// Utility::Error{} << (_lastError = "Couldn't create the armours directory.");
// return false;
// }
2022-03-06 09:12:40 +01:00
//}
//if(!Utility::Directory::exists(_weaponsDir)) {
2022-03-30 14:18:08 +02:00
// Utility::Debug{} << "Weapons directory not found, creating...";
// if(!Utility::Path::make(_weaponsDir)) {
// Utility::Error{} << (_lastError = "Couldn't create the weapons directory.");
// return false;
// }
2022-03-06 09:12:40 +01:00
//}
//if(!Utility::Directory::exists(_stylesDir)) {
2022-03-30 14:18:08 +02:00
// Utility::Debug{} << "Styles directory not found, creating...";
// if(!Utility::Path::make(_stylesDir)) {
// Utility::Error{} << (_lastError = "Couldn't create the styles directory.");
// return false;
// }
2022-03-06 09:12:40 +01:00
//}
2022-03-30 14:18:08 +02:00
return true ;
2022-03-06 09:12:40 +01:00
}
2021-07-21 14:36:49 +02:00
auto SaveTool : : findGameDataDirectory ( ) - > bool {
2022-03-30 14:18:08 +02:00
Utility : : Debug { } < < " Searching for the game's save directory... " ;
2021-07-21 14:36:49 +02:00
wchar_t * localappdata_path = nullptr ;
Containers : : ScopeGuard guard { localappdata_path , CoTaskMemFree } ;
if ( SHGetKnownFolderPath ( FOLDERID_LocalAppData , KF_FLAG_NO_APPCONTAINER_REDIRECTION , nullptr , & localappdata_path ) ! = S_OK )
{
2022-03-30 14:18:08 +02:00
Utility : : Error { } < < ( _lastError = " SHGetKnownFolderPath() failed in SaveTool::findGameDataDirectory() " _s ) ;
2021-07-21 14:36:49 +02:00
return false ;
}
2022-03-09 11:04:19 +01:00
_gameDataDir = Utility : : Path : : join ( Utility : : Path : : fromNativeSeparators ( Utility : : Unicode : : narrow ( localappdata_path ) ) , " MASS_Builder " _s ) ;
2021-07-21 14:36:49 +02:00
2022-03-09 11:04:19 +01:00
if ( ! Utility : : Path : : exists ( _gameDataDir ) ) {
2022-03-30 14:18:08 +02:00
Utility : : Error { } < < ( _lastError = _gameDataDir + " wasn't found. Make sure to play the game at least once. " _s ) ;
2021-07-21 14:36:49 +02:00
return false ;
}
2022-03-30 14:18:08 +02:00
_configDir = Utility : : Path : : join ( _gameDataDir , " Saved/Config/WindowsNoEditor " _s ) ;
_saveDir = Utility : : Path : : join ( _gameDataDir , " Saved/SaveGames " _s ) ;
_screenshotsDir = Utility : : Path : : join ( _gameDataDir , " Saved/Screenshots/WindowsNoEditor " _s ) ;
2021-07-21 14:36:49 +02:00
return true ;
}
2021-06-11 19:24:52 +02:00
void SaveTool : : initialiseMassManager ( ) {
2022-03-09 13:27:45 +01:00
_massManager . emplace ( _saveDir , _currentProfile - > account ( ) , _currentProfile - > isDemo ( ) , _stagingDir ) ;
2021-06-11 19:24:52 +02:00
}
void SaveTool : : initialiseFileWatcher ( ) {
_fileWatcher . emplace ( ) ;
2021-07-21 20:38:33 +02:00
_watchIDs [ SaveDir ] = _fileWatcher - > addWatch ( _saveDir , this , false ) ;
2021-07-21 20:57:39 +02:00
_watchIDs [ StagingDir ] = _fileWatcher - > addWatch ( _stagingDir , this , false ) ;
2021-06-11 19:24:52 +02:00
_fileWatcher - > watch ( ) ;
}
void SaveTool : : drawImGui ( ) {
_imgui . newFrame ( ) ;
if ( ImGui : : GetIO ( ) . WantTextInput & & ! isTextInputActive ( ) ) {
startTextInput ( ) ;
}
else if ( ! ImGui : : GetIO ( ) . WantTextInput & & isTextInputActive ( ) ) {
stopTextInput ( ) ;
}
drawGui ( ) ;
_imgui . updateApplicationCursor ( * this ) ;
_imgui . drawFrame ( ) ;
}
void SaveTool : : drawGui ( ) {
drawMainMenu ( ) ;
2021-06-20 23:49:30 +02:00
switch ( _uiState ) {
case UiState : : Disclaimer :
drawDisclaimer ( ) ;
break ;
case UiState : : Initialising :
drawInitialisation ( ) ;
break ;
case UiState : : ProfileManager :
drawProfileManager ( ) ;
break ;
case UiState : : MainManager :
drawManager ( ) ;
break ;
2021-08-19 20:35:00 +02:00
case UiState : : MassViewer :
drawMassViewer ( ) ;
break ;
2021-06-11 19:24:52 +02:00
}
if ( _aboutPopup ) {
drawAbout ( ) ;
}
2021-07-13 18:35:23 +02:00
# ifdef SAVETOOL_DEBUG_BUILD
2021-06-11 19:24:52 +02:00
if ( _demoWindow ) {
ImGui : : ShowDemoWindow ( & _demoWindow ) ;
}
if ( _styleEditor ) {
ImGui : : ShowStyleEditor ( & ImGui : : GetStyle ( ) ) ;
}
if ( _metricsWindow ) {
ImGui : : ShowMetricsWindow ( & _metricsWindow ) ;
}
# endif
2021-07-25 10:52:54 +02:00
_queue . draw ( windowSize ( ) ) ;
2021-06-11 19:24:52 +02:00
}
void SaveTool : : drawDisclaimer ( ) {
ImGui : : SetNextWindowPos ( ImVec2 { Vector2 { windowSize ( ) / 2.0f } } , ImGuiCond_Always , center_pivot ) ;
if ( ImGui : : Begin ( " Disclaimer##DisclaimerWindow " , nullptr ,
2021-07-25 10:52:54 +02:00
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus |
2021-06-11 19:24:52 +02:00
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_MenuBar ) )
{
if ( ImGui : : BeginMenuBar ( ) ) {
ImGui : : TextUnformatted ( " Disclaimer " ) ;
ImGui : : EndMenuBar ( ) ;
}
ImGui : : TextUnformatted ( " Before you start using the app, there are a few things you should know: " ) ;
ImGui : : PushTextWrapPos ( windowSize ( ) . x ( ) * 0.67f ) ;
ImGui : : Bullet ( ) ;
ImGui : : SameLine ( ) ;
ImGui : : TextUnformatted ( " For this application to work properly, it is recommended to disable Steam Cloud syncing for the game. To disable it, right-click the game in your Steam library, click \" Properties \" , go to the \" General \" tab, and uncheck \" Keep game saves in the Steam Cloud for M.A.S.S. Builder \" . " ) ;
ImGui : : Bullet ( ) ;
ImGui : : SameLine ( ) ;
ImGui : : TextUnformatted ( " The developer of this application (Guillaume Jacquemin) isn't associated with Vermillion Digital, and both parties cannot be held responsible for data loss or corruption this app might cause. PLEASE USE AT YOUR OWN RISK! " ) ;
ImGui : : Bullet ( ) ;
ImGui : : SameLine ( ) ;
ImGui : : TextUnformatted ( " This application is released under the terms of the GNU General Public Licence version 3. Please see the COPYING file for more details, or the About screen if you somehow didn't get that file with your download of the program. " ) ;
ImGui : : Bullet ( ) ;
ImGui : : SameLine ( ) ;
2021-07-13 18:46:21 +02:00
ImGui : : TextUnformatted ( " This version of the application was tested on M.A.S.S. Builder early access version " SUPPORTED_GAME_VERSION " . It may or may not work with other versions of the game. " ) ;
2021-06-11 19:24:52 +02:00
ImGui : : PopTextWrapPos ( ) ;
if ( ImGui : : BeginTable ( " ##DisclaimerLayoutTable " , 3 ) ) {
ImGui : : TableSetupColumn ( " ##Empty1 " , ImGuiTableColumnFlags_WidthStretch ) ;
ImGui : : TableSetupColumn ( " ##Button " , ImGuiTableColumnFlags_WidthFixed ) ;
ImGui : : TableSetupColumn ( " ##Empty2 " , ImGuiTableColumnFlags_WidthStretch ) ;
ImGui : : TableNextRow ( ) ;
2021-08-18 20:09:22 +02:00
ImGui : : TableSetColumnIndex ( 0 ) ;
ImGui : : Dummy ( { 0.0f , 5.0f } ) ;
ImGui : : Dummy ( { 4.0f , 0.0f } ) ;
ImGui : : SameLine ( ) ;
ImGui : : Checkbox ( " Don't show next time " , & _skipDisclaimer ) ;
2021-06-11 19:24:52 +02:00
ImGui : : TableSetColumnIndex ( 1 ) ;
ImGui : : PushStyleVar ( ImGuiStyleVar_FramePadding , { 24.0f , 12.0f } ) ;
if ( ImGui : : Button ( " I understand the risks " ) ) {
_uiState = UiState : : Initialising ;
2021-08-18 20:09:22 +02:00
_initThread = std : : thread { [ this ] { initialiseManager ( ) ; } } ;
2021-06-11 19:24:52 +02:00
}
ImGui : : PopStyleVar ( ) ;
ImGui : : EndTable ( ) ;
}
}
ImGui : : End ( ) ;
}
void SaveTool : : drawInitialisation ( ) {
ImGui : : SetNextWindowPos ( ImVec2 { Vector2 { windowSize ( ) / 2.0f } } , ImGuiCond_Always , center_pivot ) ;
if ( ImGui : : BeginPopupModal ( " ##InitPopup " , nullptr ,
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar ) )
{
ImGui : : TextUnformatted ( " Initialising the manager. Please wait... " ) ;
ImGui : : EndPopup ( ) ;
}
ImGui : : OpenPopup ( " ##InitPopup " ) ;
}
void SaveTool : : drawGameState ( ) {
ImGui : : TextUnformatted ( " Game state: " ) ;
ImGui : : SameLine ( ) ;
{
2021-07-21 14:36:49 +02:00
switch ( _gameState ) {
2021-06-11 19:24:52 +02:00
case GameState : : Unknown :
2021-06-20 19:36:42 +02:00
ImGui : : TextColored ( ImColor { 0xff00a5ff } , ICON_FA_CIRCLE ) ;
drawTooltip ( " unknown " ) ;
2021-06-11 19:24:52 +02:00
break ;
case GameState : : NotRunning :
2021-06-20 19:36:42 +02:00
ImGui : : TextColored ( ImColor { 0xff32cd32 } , ICON_FA_CIRCLE ) ;
drawTooltip ( " not running " ) ;
2021-06-11 19:24:52 +02:00
break ;
case GameState : : Running :
2021-06-20 19:36:42 +02:00
ImGui : : TextColored ( ImColor { 0xff0000ff } , ICON_FA_CIRCLE ) ;
drawTooltip ( " running " ) ;
2021-06-11 19:24:52 +02:00
break ;
}
}
}
2022-03-04 21:18:55 +01:00
void SaveTool : : drawHelpMarker ( Containers : : StringView text , Float wrap_pos ) {
2021-06-11 19:24:52 +02:00
ImGui : : TextUnformatted ( ICON_FA_QUESTION_CIRCLE ) ;
drawTooltip ( text , wrap_pos ) ;
}
2022-03-04 21:18:55 +01:00
void SaveTool : : drawTooltip ( Containers : : StringView text , Float wrap_pos ) {
2021-06-11 19:24:52 +02:00
if ( ImGui : : IsItemHovered ( ) ) {
ImGui : : BeginTooltip ( ) ;
if ( wrap_pos > 0.0f ) {
ImGui : : PushTextWrapPos ( wrap_pos ) ;
}
2022-03-04 21:18:55 +01:00
ImGui : : TextUnformatted ( text . data ( ) ) ;
2021-06-11 19:24:52 +02:00
if ( wrap_pos > 0.0f ) {
ImGui : : PopTextWrapPos ( ) ;
}
ImGui : : EndTooltip ( ) ;
}
}
2022-03-04 21:18:55 +01:00
void SaveTool : : openUri ( Containers : : StringView uri ) {
2022-03-09 11:04:19 +01:00
ShellExecuteW ( nullptr , nullptr , Utility : : Unicode : : widen ( uri . data ( ) ) , nullptr , nullptr , SW_SHOWDEFAULT ) ;
2021-06-11 19:24:52 +02:00
}
2021-07-21 14:36:49 +02:00
void SaveTool : : checkGameState ( ) {
WTS_PROCESS_INFOW * process_infos = nullptr ;
unsigned long process_count = 0 ;
if ( WTSEnumerateProcessesW ( WTS_CURRENT_SERVER_HANDLE , 0 , 1 , & process_infos , & process_count ) ) {
Containers : : ScopeGuard guard { process_infos , WTSFreeMemory } ;
for ( unsigned long i = 0 ; i < process_count ; + + i ) {
if ( std : : wcscmp ( process_infos [ i ] . pProcessName , L " MASS_Builder-Win64-Shipping.exe " ) = = 0 ) {
_gameState = GameState : : Running ;
break ;
}
else {
_gameState = GameState : : NotRunning ;
}
}
}
else {
_gameState = GameState : : Unknown ;
}
}
2021-07-22 11:39:53 +02:00
void SaveTool : : checkForUpdates ( ) {
2022-03-30 15:15:54 +02:00
cpr : : Response r = cpr : : Get ( cpr : : Url { " https://williamjcm.ovh/mbst/version " } , cpr : : Timeout { 10000 } ) ;
2021-07-22 11:39:53 +02:00
SDL_Event event ;
SDL_zero ( event ) ;
event . type = _updateEventId ;
2021-07-28 14:15:39 +02:00
event . user . data1 = new cpr : : Response { std : : move ( r ) } ;
2021-07-22 11:39:53 +02:00
SDL_PushEvent ( & event ) ;
}