| 1899da11 |
sago007 |
2015-12-30 19:04 |
1 |
/* |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
2 |
=========================================================================== |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
3 |
blockattack - Block Attack - Rise of the Blocks |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
4 |
Copyright (C) 2005-2015 Poul Sander |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
5 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
6 |
This program is free software: you can redistribute it and/or modify |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
7 |
it under the terms of the GNU General Public License as published by |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
8 |
the Free Software Foundation, either version 2 of the License, or |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
9 |
(at your option) any later version. |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
10 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
11 |
This program is distributed in the hope that it will be useful, |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
14 |
GNU General Public License for more details. |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
15 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
16 |
You should have received a copy of the GNU General Public License |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
17 |
along with this program. If not, see http://www.gnu.org/licenses/ |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
18 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
19 |
Source information and contacts persons can be found at |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
20 |
http://blockattack.net |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
21 |
=========================================================================== |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
22 |
*/ |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
23 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
24 |
#include "os.hpp" |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
25 |
#include <iostream> |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
26 |
|
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
27 |
#ifdef __unix__ |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
28 |
#include <pwd.h> |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
29 |
#include <unistd.h> |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
30 |
#include <stdexcept> |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
31 |
|
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
32 |
|
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
33 |
/** |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
34 |
* Retrives the effective user's home dir. |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
35 |
* If the user is running as root we ignore the HOME environment. It works badly with sudo. |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
36 |
* Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle. |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
37 |
* @return The home directory. HOME environment is respected for non-root users if it exists. |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
38 |
*/ |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
39 |
static std::string getHome() { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
40 |
std::string res; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
41 |
int uid = getuid(); |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
42 |
const char* homeEnv = getenv("HOME"); |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
43 |
if ( uid != 0 && homeEnv) { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
44 |
//We only acknowlegde HOME if not root. |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
45 |
res = homeEnv; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
46 |
return res; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
47 |
} |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
48 |
struct passwd *pw = getpwuid(uid); |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
49 |
if (!pw) { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
50 |
throw std::runtime_error("Unable to get passwd struct."); |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
51 |
} |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
52 |
const char* tempRes = pw->pw_dir; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
53 |
if (!tempRes) { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
54 |
throw std::runtime_error("User has no home directory"); |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
55 |
} |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
56 |
res = tempRes; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
57 |
return res; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
58 |
} |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
59 |
#endif |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
60 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
61 |
/* |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
62 |
*Files will be saved in: |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
63 |
* HOME/.gamesaves/"+GAMENAME (unix) |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
64 |
*or DOCUMENTS/My Games/GAMENAME (Windows) |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
65 |
*/ |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
66 |
#define GAMENAME "blockattack" |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
67 |
|
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
68 |
using namespace std; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
69 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
70 |
#ifdef _WIN32 |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
71 |
//Returns path to "my Documents" in windows: |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
72 |
string getMyDocumentsPath() { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
73 |
char pszPath[MAX_PATH]; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
74 |
//if (SUCCEEDED(SHGetSpecialFolderPath(nullptr, pszPath, CSIDL_PERSONAL, FALSE))) { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
75 |
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_PERSONAL, nullptr, 0, pszPath))) { |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
76 |
// pszPath is now the path that you want |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
77 |
#if DEBUG |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
78 |
cout << "MyDocuments Located: " << pszPath << endl; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
79 |
#endif |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
80 |
string theResult= pszPath; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
81 |
return theResult; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
82 |
} |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
83 |
else { |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
84 |
cout << "Warning: My Documents not found!" << endl; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
85 |
string theResult =""; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
86 |
return theResult; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
87 |
} |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
88 |
} |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
89 |
|
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
90 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
91 |
#endif |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
92 |
|
| 1899da11 |
sago007 |
2015-12-30 19:04 |
93 |
/** |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
94 |
* Returns the path to where all settings must be saved. |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
95 |
* On unix-like systems this is the home-folder under: ~/.gamesaves/GAMENAME |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
96 |
* In Windows it is My Documents/My Games |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
97 |
* Consider changing this for Vista that has a special save games folder |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
98 |
*/ |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
99 |
std::string getPathToSaveFiles() { |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
100 |
#ifdef __unix__ |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
101 |
return (std::string)getenv("HOME")+(std::string)"/.gamesaves/"+GAMENAME; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
102 |
#elif _WIN32 |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
103 |
return getMyDocumentsPath()+(string)"/My Games/"+GAMENAME; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
104 |
#else |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
105 |
return "."; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
106 |
#endif |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
107 |
} |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
108 |
|
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
109 |
std::string getPathToHighscoresEndless() { |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
110 |
return getPathToSaveFiles()+"/endless.dat"; |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
111 |
} |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
112 |
|
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
113 |
std::string getPathToHighscoresTimetrial() { |
| 1f50b970 |
sago007 |
2015-12-30 21:00 |
114 |
return getPathToSaveFiles()+"/timetrial.dat"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
115 |
} |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
116 |
|
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
117 |
std::string getStageClearSavePath() { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
118 |
std::string ret; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
119 |
#if defined(__unix__) |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
120 |
ret = getHome()+"/.gamesaves/blockattack/stageClear.SCsave"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
121 |
#elif defined(_WIN32) |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
122 |
ret = getMyDocumentsPath()+"/My Games/blockattack/stageClear.SCsave"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
123 |
#else |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
124 |
ret = "stageClear.SCsave"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
125 |
#endif |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
126 |
return ret; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
127 |
} |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
128 |
|
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
129 |
std::string getPuzzleSetSavePath() { |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
130 |
std::string ret; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
131 |
#if defined(__unix__) |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
132 |
ret = getHome()+"/.gamesaves/blockattack/puzzle.levels.save"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
133 |
#elif defined(_WIN32) |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
134 |
ret = getMyDocumentsPath()+"/My Games/blockattack/puzzle.levels.save"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
135 |
#else |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
136 |
ret = "puzzle.levels.save"; |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
137 |
#endif |
| 9dc80982 |
sago007 |
2016-01-02 21:08 |
138 |
return ret; |
| 1899da11 |
sago007 |
2015-12-30 19:04 |
139 |
} |