Skip to content

Commit

Permalink
Fix executable directory path function
Browse files Browse the repository at this point in the history
  • Loading branch information
RadWolfie committed Jan 18, 2019
1 parent 318d017 commit 4edfebb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion resource/Cxbx.rc
Expand Up @@ -576,7 +576,7 @@ BEGIN
POPUP "Config &Data Location...", 65535,MFT_STRING,MFS_ENABLED
BEGIN
MENUITEM "Store in AppData", ID_SETTINGS_CONFIG_DLOCAPPDATA,MFT_STRING,MFS_ENABLED
MENUITEM "Store with Executable", ID_SETTINGS_CONFIG_DLOCCURDIR,MFT_STRING,MFS_ENABLED
MENUITEM "Store with Executable", ID_SETTINGS_CONFIG_DLOCEXECDIR,MFT_STRING,MFS_ENABLED
MENUITEM "Custom", ID_SETTINGS_CONFIG_DLOCCUSTOM,MFT_STRING,MFS_ENABLED
END
POPUP "&Symbol Cache", 65535,MFT_STRING,MFS_ENABLED
Expand Down
23 changes: 12 additions & 11 deletions src/common/Settings.cpp
Expand Up @@ -49,7 +49,9 @@ static_assert(false, "Please implement support for cross-platform's user profile
#include <QDir> // for create directory
#include <QFile> // for check file existance
#include <QStandardPaths> // for cross-platform's user profile support
#endif
#endif

std::string g_exec_filepath;

// NOTE: Update settings_version when add/edit/delete setting's structure.
const uint settings_version = 3;
Expand Down Expand Up @@ -127,10 +129,9 @@ static struct {
const char* DirectHostBackBufferAccess = "DirectHostBackBufferAccess";
} sect_hack_keys;

std::string GenerateCurrentDirectoryStr()
{
// NOTE: There is no cross-platform support for getting file's current directory.
return std::experimental::filesystem::current_path().generic_string();
std::string GenerateExecDirectoryStr()
{
return g_exec_filepath.substr(0, g_exec_filepath.find_last_of("\\/"));
}

// NOTE: This function will be only have Qt support, std::experimental::filesystem doesn't have generic support.
Expand Down Expand Up @@ -192,13 +193,13 @@ bool Settings::Init()
std::string saveFile;
#ifdef RETRO_API_VERSION // TODO: Change me to #ifndef QT_VERSION
// Can only have one option without Qt.
saveFile = GenerateCurrentDirectoryStr();
saveFile = GenerateExecDirectoryStr();

#else // Only support for Qt compile build.
int iRet = MessageBox(nullptr, szSettings_save_user_option_message, "Cxbx-Reloaded", MB_YESNOCANCEL | MB_ICONQUESTION);

if (iRet == IDYES) {
saveFile = GenerateCurrentDirectoryStr();
saveFile = GenerateExecDirectoryStr();
}
else if (iRet == IDNO){
saveFile = GenerateUserProfileDirectoryStr();
Expand Down Expand Up @@ -238,7 +239,7 @@ bool Settings::Init()

bool Settings::LoadUserConfig()
{
std::string fileSearch = GenerateCurrentDirectoryStr();
std::string fileSearch = GenerateExecDirectoryStr();

fileSearch.append(szSettings_settings_file);

Expand Down Expand Up @@ -727,7 +728,7 @@ std::string Settings::GetDataLocation()
default:
#ifdef RETRO_API_VERSION // TODO: Change me to #ifndef QT_VERSION

m_gui.DataStorageToggle = CXBX_DATA_CURDIR;
m_gui.DataStorageToggle = CXBX_DATA_EXECDIR;

#else // Only support for Qt compile build.

Expand All @@ -739,8 +740,8 @@ std::string Settings::GetDataLocation()
break;
#endif

case CXBX_DATA_CURDIR:
m_current_data_location = GenerateCurrentDirectoryStr();
case CXBX_DATA_EXECDIR:
m_current_data_location = GenerateExecDirectoryStr();
break;

case CXBX_DATA_CUSTOM:
Expand Down
6 changes: 4 additions & 2 deletions src/common/Settings.hpp
Expand Up @@ -37,14 +37,16 @@
#include "Cxbx.h"

#include "SimpleIni.h"
#include <string>
#include <string>

extern std::string g_exec_filepath;

#define szSettings_alloc_error "ERROR: Unable to allocate Settings class."

// Cxbx-Reloaded's data storage location.
typedef enum _CXBX_DATA {
CXBX_DATA_APPDATA = 0,
CXBX_DATA_CURDIR = 1,
CXBX_DATA_EXECDIR = 1,
CXBX_DATA_CUSTOM = 2,
} CXBX_DATA;

Expand Down
2 changes: 1 addition & 1 deletion src/gui/ResCxbx.h
Expand Up @@ -351,7 +351,7 @@
#define ID_HACKS_SPEEDHACKS 40103
#define ID_SETTINGS_CONFIG_DLOCCUSTOM 40104
#define ID_SETTINGS_CONFIG_DLOCAPPDATA 40105
#define ID_SETTINGS_CONFIG_DLOCCURDIR 40106
#define ID_SETTINGS_CONFIG_DLOCEXECDIR 40106
#define ID_SETTINGS_ALLOWADMINPRIVILEGE 40107
#define ID_SETTINGS_CONFIG_LOGGING 40108
#define ID_SYNC_CONFIG_LOGGING 40109
Expand Down
2 changes: 2 additions & 0 deletions src/gui/WinMain.cpp
Expand Up @@ -80,6 +80,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
HWND hWnd = nullptr;
DWORD guiProcessID = 0;

// TODO: Convert ALL __argc & __argv to use main(int argc, char** argv) method.
if (__argc >= 2 && std::strcmp(__argv[1], "/load") == 0 && std::strlen(__argv[2]) > 0) {
bKernel = true;

Expand All @@ -98,6 +99,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
bKernel = false;
guiProcessID = GetCurrentProcessId();
}
g_exec_filepath = __argv[0]; // NOTE: Workaround solution until simulated "main" function is made.

/*! initialize shared memory */
EmuShared::Init(guiProcessID);
Expand Down
12 changes: 6 additions & 6 deletions src/gui/WndMain.cpp
Expand Up @@ -1040,9 +1040,9 @@ LRESULT CALLBACK WndMain::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lP
}
break;

case ID_SETTINGS_CONFIG_DLOCCURDIR:
case ID_SETTINGS_CONFIG_DLOCEXECDIR:
{
g_Settings->m_gui.DataStorageToggle = CXBX_DATA_CURDIR;
g_Settings->m_gui.DataStorageToggle = CXBX_DATA_EXECDIR;
RefreshMenus();
}
break;
Expand Down Expand Up @@ -1734,19 +1734,19 @@ void WndMain::RefreshMenus()
switch (g_Settings->m_gui.DataStorageToggle) {
case CXBX_DATA_APPDATA:
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCAPPDATA, MF_CHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCCURDIR, MF_UNCHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCEXECDIR, MF_UNCHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCCUSTOM, MF_UNCHECKED);
break;

case CXBX_DATA_CURDIR:
case CXBX_DATA_EXECDIR:
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCAPPDATA, MF_UNCHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCCURDIR, MF_CHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCEXECDIR, MF_CHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCCUSTOM, MF_UNCHECKED);
break;

case CXBX_DATA_CUSTOM:
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCAPPDATA, MF_UNCHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCCURDIR, MF_UNCHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCEXECDIR, MF_UNCHECKED);
CheckMenuItem(settings_menu, ID_SETTINGS_CONFIG_DLOCCUSTOM, MF_CHECKED);
break;
}
Expand Down

0 comments on commit 4edfebb

Please sign in to comment.