Merge from development branch heightmap to main.
[scorched3d.git] / src / common / common / DefinesScorched.cpp
blob038238ae2247a62662da7a7c76a122a6c5b2c997
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2003
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string>
25 #include <common/DefinesScorched.h>
26 #include <common/DefinesFile.h>
27 #include <common/DefinesString.h>
28 #include <common/DefinesAssert.h>
29 #include <common/Logger.h>
30 #define WIN32_LEAN_AND_MEAN
31 #include <windows.h>
33 #pragma warning(disable : 4996)
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 unsigned int S3D::ScorchedPort = 27270;
40 std::string S3D::ScorchedVersion = "42";
41 std::string S3D::ScorchedProtocolVersion = "ea";
42 #ifdef __DATE__
43 std::string S3D::ScorchedBuildTime = __DATE__;
44 #else
45 std::string S3D::ScorchedBuildTime = "Unknown";
46 #endif
47 static std::string exeName;
48 static std::string dataModFile = "none";
49 static std::string settingsDir = ".scorched3d";
51 void S3D::showURL(const std::string &url)
53 #ifdef _WIN32
54 std::string buffer = S3D::formatStringBuffer("explorer %s", url.c_str());
55 WinExec(buffer.c_str() ,SW_SHOWDEFAULT);
56 #else
57 #ifdef __DARWIN__
58 std::string buffer = S3D::formatStringBuffer("open %s", url.c_str());
59 system(buffer.c_str());
60 #else
61 std::string buffer = S3D::formatStringBuffer("firefox %s", url.c_str());
62 system(buffer.c_str());
63 #endif // __DARWIN__
64 #endif // _WIN32
65 Logger::log(url);
68 void S3D::setExeName(const std::string &name)
70 exeName = name;
73 std::string S3D::getExeName()
75 return exeName;
78 std::string S3D::getStartTime()
80 static std::string startTime;
81 if (startTime.empty())
83 time_t theTime = time(0);
84 startTime = ctime(&theTime);
86 return startTime;
89 void S3D::setSettingsDir(const std::string &dir)
91 settingsDir = dir;
94 void S3D::setDataFileMod(const std::string &mod)
96 dataModFile = mod;
99 std::string S3D::getDataFileMod()
101 return dataModFile;
104 #ifndef S3D_DATADIR
105 #define S3D_DATADIR "."
106 #endif
107 #ifndef S3D_DOCDIR
108 #define S3D_DOCDIR "./documentation"
109 #endif
110 #ifndef S3D_BINDIR
111 #define S3D_BINDIR "."
112 #endif
114 static const char *GET_DIR(const char *dir)
116 if (dir[0] == '.')
118 static char path[1024];
119 #ifdef _WIN32
120 GetCurrentDirectory(sizeof(path), path);
121 #else
122 getcwd(path, sizeof(path));
123 #endif // _WIN32
124 if (strlen(path) + strlen(dir) + 1 < sizeof(path))
126 strcat(path, "/");
127 strcat(path, dir);
129 return path;
131 return dir;
134 std::string S3D::getDataFile(const std::string &filename)
136 std::string buffer;
138 buffer = S3D::getModFile(
139 S3D::formatStringBuffer("%s/%s", dataModFile.c_str(), filename.c_str()));
140 S3D::fileDos2Unix(buffer);
141 if (S3D::fileExists(buffer)) return buffer;
143 buffer = S3D::getGlobalModFile(
144 S3D::formatStringBuffer("%s/%s", dataModFile.c_str(), filename.c_str()));
145 S3D::fileDos2Unix(buffer);
146 if (S3D::fileExists(buffer)) return buffer;
148 buffer = S3D::formatStringBuffer("%s/%s", GET_DIR(S3D_DATADIR), filename.c_str());
149 S3D::fileDos2Unix(buffer);
151 return buffer;
154 extern bool S3D::checkDataFile(const std::string &filename)
156 std::string dataFileName = S3D::getDataFile(filename);
157 if (!S3D::fileExists(dataFileName))
159 if (0 == strstr(filename.c_str(), "none"))
161 S3D::dialogMessage("Scorched3D", S3D::formatStringBuffer(
162 "The file \"%s\" does not exist",
163 dataFileName.c_str()));
164 return false;
167 return true;
170 std::string S3D::getDocFile(const std::string &filename)
172 std::string buffer =
173 S3D::formatStringBuffer("%s/%s", GET_DIR(S3D_DOCDIR), filename.c_str());
174 S3D::fileDos2Unix(buffer);
175 return buffer;
178 std::string S3D::getHomeFile(const std::string &filename)
180 static std::string homeDir;
181 if (!homeDir.c_str()[0])
183 homeDir = GET_DIR(S3D_DATADIR);
184 if (S3D::dirExists(S3D::getHomeDir()))
186 homeDir = S3D::getHomeDir();
190 std::string buffer = S3D::formatStringBuffer(
191 "%s/%s", homeDir.c_str(), filename.c_str());
192 S3D::fileDos2Unix(buffer);
193 return buffer;
196 std::string S3D::getSettingsFile(const std::string &filename)
198 static std::string homeDir;
199 if (!homeDir.c_str()[0])
201 std::string homeDirStr = S3D::getHomeFile(
202 S3D::formatStringBuffer("/%s", settingsDir.c_str()));
203 if (!S3D::dirExists(homeDirStr))
205 if (!S3D::dirMake(homeDirStr))
207 homeDirStr = S3D::getHomeFile("");
210 homeDir = homeDirStr;
213 std::string buffer = S3D::formatStringBuffer(
214 "%s/%s", homeDir.c_str(), filename.c_str());
215 S3D::fileDos2Unix(buffer);
216 return buffer;
219 std::string S3D::getLogFile(const std::string &filename)
221 std::string homeDirStr = S3D::getSettingsFile("");
222 std::string newDir(std::string(homeDirStr) + std::string("/logs"));
223 if (S3D::dirExists(newDir)) homeDirStr = newDir;
224 else if (S3D::dirMake(newDir)) homeDirStr = newDir;
226 std::string buffer = S3D::formatStringBuffer(
227 "%s/%s", homeDirStr.c_str(), filename.c_str());
228 S3D::fileDos2Unix(buffer);
229 return buffer;
232 std::string S3D::getSaveFile(const std::string &filename)
234 std::string homeDirStr = S3D::getSettingsFile("");
235 std::string newDir(std::string(homeDirStr) + std::string("/saves"));
236 if (S3D::dirExists(newDir)) homeDirStr = newDir;
237 else if (S3D::dirMake(newDir)) homeDirStr = newDir;
239 std::string buffer = S3D::formatStringBuffer(
240 "%s/%s", homeDirStr.c_str(), filename.c_str());
241 S3D::fileDos2Unix(buffer);
242 return buffer;
245 std::string S3D::getModFile(const std::string &filename)
247 static std::string modDir;
248 if (!modDir.c_str()[0])
250 std::string homeDirStr = S3D::getSettingsFile("");
251 std::string newDir(std::string(homeDirStr) + std::string("/mods"));
252 if (S3D::dirExists(newDir)) homeDirStr = newDir;
253 else if (S3D::dirMake(newDir)) homeDirStr = newDir;
255 modDir = homeDirStr;
258 std::string buffer = S3D::formatStringBuffer(
259 "%s/%s", modDir.c_str(), filename.c_str());
260 S3D::fileDos2Unix(buffer);
261 return buffer;
264 std::string S3D::getGlobalModFile(const std::string &filename)
266 std::string buffer = S3D::formatStringBuffer(
267 "%s/data/globalmods/%s", GET_DIR(S3D_DATADIR), filename.c_str());
268 S3D::fileDos2Unix(buffer);
269 return buffer;