9
9
#include < unistd.h>
10
10
#elif defined(_WIN32)
11
11
#include < Windows.h>
12
+ #elif defined(__APPLE__)
13
+ #include < mach-o/dyld.h>
12
14
#endif
13
15
14
16
15
17
#include " Exception.h"
16
18
#include " XMLReader.h"
19
+ #include " board/logger.h"
17
20
18
21
namespace chesspp
19
22
{
@@ -22,34 +25,51 @@ namespace chesspp
22
25
class configuration
23
26
{
24
27
protected:
25
- std::string executable_path;
26
- std::string getExecutablePath ()
28
+ std::string res_path;
29
+
30
+ // Linux and Windows, resource path is defined as the absolute path the folder where the application executable is stored.
31
+ // <exe_location>/res/img/... should be where resources are stored.
32
+ // OS x, resource path is defined as the absolute path to the Resources folder of the .app structure.
33
+ // <.app>/Contents/Resources/res/img... should be where resources are stored.
34
+ std::string getResourcePath ()
27
35
{
28
36
char buf[1024 ];
37
+ uint32_t size = sizeof (buf);
29
38
memset (buf, 0 , sizeof (buf));
30
39
std::string ret;
31
40
#if defined(__linux__)
32
41
if (readlink (" /proc/self/exe" , buf, sizeof (buf)) == -1 )
33
42
throw chesspp::exception (" Unable to determine executable path on Linux." );
34
43
ret = buf;
44
+ ret = ret.substr (0 , ret.find_last_of (' /' )+1 );
35
45
36
46
#elif defined(_WIN32)
37
47
if (GetModuleFileNameA (NULL , buf, sizeof (buf)) == 0 )
38
48
throw chesspp::exception (" Unable to determine executable path on Windows." );
39
49
ret = buf;
40
50
boost::replace_all (ret, " \\ " , " /" );
51
+ ret = ret.substr (0 , ret.find_last_of (' /' )+1 );
52
+
53
+ #elif defined(__APPLE__)
54
+ if (_NSGetExecutablePath (buf, &size) != 0 )
55
+ throw chesspp::exception (" Unable to determine executable path on OS x. (Buffer size too small?)" );
56
+ ret = buf;
57
+ ret = ret.substr (0 , ret.find_last_of (' /' )+1 ) + " ../Resources/" ;
58
+ // Need to go up one directory because the exe is stored in <.app>/Contents/MacOS/,
59
+ // And we need <.app>/Contents/Resources
41
60
42
61
#else
43
62
throw chesspp::exception (" Unknown OS. Unable to determine executable path." );
44
63
#endif
45
64
46
- return ret. substr ( 0 , ret. find_last_of ( ' / ' )+ 1 ) ;
65
+ return ret;
47
66
}
48
67
49
68
XMLReader reader;
50
69
public:
51
- configuration (const std::string &configFile) : executable_path(getExecutablePath ()), reader(executable_path + " / " + configFile) {}
70
+ configuration (const std::string &configFile) : res_path(getResourcePath ()), reader(getResourcePath() + configFile) {}
52
71
virtual ~configuration () {}
72
+
53
73
};
54
74
55
75
class BoardConfig : public configuration
@@ -61,7 +81,7 @@ namespace chesspp
61
81
public:
62
82
BoardConfig () : configuration(" config.xml" )
63
83
{
64
- initial_layout = reader.getProperty <std::string>(" chesspp.data.board.initial_layout" );
84
+ initial_layout = res_path + reader.getProperty <std::string>(" chesspp.data.board.initial_layout" );
65
85
board_width = reader.getProperty <uint8_t >(" chesspp.data.board.width" );
66
86
board_height = reader.getProperty <uint8_t >(" chesspp.data.board.height" );
67
87
cell_width = reader.getProperty <uint16_t >(" chesspp.data.board.cell_width" );
@@ -82,9 +102,9 @@ namespace chesspp
82
102
public:
83
103
GraphicsConfig () : configuration(" config.xml" )
84
104
{
85
- path_board = reader.getProperty <std::string>(" chesspp.images.board" );
86
- path_pieces = reader.getProperty <std::string>(" chesspp.images.pieces" );
87
- path_validMove = reader.getProperty <std::string>(" chesspp.images.validMove" );
105
+ path_board = res_path + reader.getProperty <std::string>(" chesspp.images.board" );
106
+ path_pieces = res_path + reader.getProperty <std::string>(" chesspp.images.pieces" );
107
+ path_validMove = res_path + reader.getProperty <std::string>(" chesspp.images.validMove" );
88
108
}
89
109
90
110
std::string getSpritePath_board () { return path_board; }
0 commit comments