1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
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.
10 //
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.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
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>
32 
33 #pragma warning(disable : 4996)
34 
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 
39 unsigned int S3D::ScorchedPort = 27270;
40 std::string S3D::ScorchedVersion = "43.3d"
41 #ifdef _DEBUG
42 	" **DEBUG**"
43 #endif
44 ;
45 std::string S3D::ScorchedProtocolVersion = "et";
46 #ifdef __DATE__
47 std::string S3D::ScorchedBuildTime = __DATE__;
48 #else
49 std::string S3D::ScorchedBuildTime = "Unknown";
50 #endif
51 static std::string exeName;
52 static std::string currentMod = "none";
53 static std::string settingsDir = "";
54 
showURL(const std::string & url)55 void S3D::showURL(const std::string &url)
56 {
57 #ifdef _WIN32
58 	std::string buffer = S3D::formatStringBuffer("explorer %s", url.c_str());
59 	WinExec(buffer.c_str() ,SW_SHOWDEFAULT);
60 #else
61 #ifdef __DARWIN__
62 	std::string buffer = S3D::formatStringBuffer("open %s", url.c_str());
63 	system(buffer.c_str());
64 #else
65 	std::string buffer = S3D::formatStringBuffer("firefox %s", url.c_str());
66 	system(buffer.c_str());
67 #endif // __DARWIN__
68 #endif // _WIN32
69 	Logger::log(url);
70 }
71 
setExeName(const std::string & name)72 void S3D::setExeName(const std::string &name)
73 {
74 	exeName = name;
75 }
76 
getExeName()77 std::string S3D::getExeName()
78 {
79 	return exeName;
80 }
81 
getStartTime()82 std::string S3D::getStartTime()
83 {
84 	static std::string startTime;
85 	if (startTime.empty())
86 	{
87 		time_t theTime = time(0);
88 		startTime = ctime(&theTime);
89 	}
90 	return startTime;
91 }
92 
setSettingsDir(const std::string & dir)93 void S3D::setSettingsDir(const std::string &dir)
94 {
95 	settingsDir = dir;
96 
97 	if (S3D::stristr(S3D::ScorchedVersion.c_str(), "BETA"))
98 	{
99 		settingsDir.append("BETA");
100 	}
101 }
102 
setDataFileMod(const std::string & mod)103 void S3D::setDataFileMod(const std::string &mod)
104 {
105 	currentMod = mod;
106 }
107 
getDataFileMod()108 std::string S3D::getDataFileMod()
109 {
110 	return currentMod;
111 }
112 
113 #ifndef S3D_DATADIR
114 #define S3D_DATADIR "."
115 #endif
116 #ifndef S3D_DOCDIR
117 #define S3D_DOCDIR "./documentation"
118 #endif
119 #ifndef S3D_BINDIR
120 #define S3D_BINDIR "."
121 #endif
122 
GET_DIR(const char * dir)123 static const char *GET_DIR(const char *dir)
124 {
125 	if (dir[0] == '.')
126 	{
127 		static char path[1024];
128 #ifdef _WIN32
129 			GetCurrentDirectory(sizeof(path), path);
130 #else
131 			getcwd(path, sizeof(path));
132 #endif // _WIN32
133 		if (strlen(path) + strlen(dir) + 1 < sizeof(path))
134 		{
135 			strcat(path, "/");
136 			strcat(path, dir);
137 		}
138 		return path;
139 	}
140 	return dir;
141 }
142 
getModFile(const std::string & filename)143 std::string S3D::getModFile(const std::string &filename)
144 {
145 	std::string buffer;
146 
147 	buffer = S3D::getSettingsModFile(
148 		S3D::formatStringBuffer("%s/%s", currentMod.c_str(), filename.c_str()));
149 	S3D::fileDos2Unix(buffer);
150 	if (S3D::fileExists(buffer)) return buffer;
151 
152 	buffer = S3D::getGlobalModFile(
153 		S3D::formatStringBuffer("%s/%s", currentMod.c_str(), filename.c_str()));
154 	S3D::fileDos2Unix(buffer);
155 	if (S3D::fileExists(buffer)) return buffer;
156 
157 	if (currentMod != "none")
158 	{
159 		buffer = S3D::getGlobalModFile(
160 			S3D::formatStringBuffer("%s/%s", "none", filename.c_str()));
161 		S3D::fileDos2Unix(buffer);
162 		if (S3D::fileExists(buffer)) return buffer;
163 	}
164 
165 	return buffer;
166 }
167 
getDataFile(const std::string & filename)168 std::string S3D::getDataFile(const std::string &filename)
169 {
170 	std::string buffer = S3D::formatStringBuffer("%s/%s", GET_DIR(S3D_DATADIR), filename.c_str());
171 	S3D::fileDos2Unix(buffer);
172 
173 	return buffer;
174 }
175 
checkDataFile(const std::string & filename)176 extern bool S3D::checkDataFile(const std::string &filename)
177 {
178 	std::string dataFileName = S3D::getModFile(filename);
179 	if (!S3D::fileExists(dataFileName))
180 	{
181 		if (0 == strstr(filename.c_str(), "none"))
182 		{
183 			S3D::dialogMessage("Scorched3D", S3D::formatStringBuffer(
184 				"The file \"%s\" does not exist",
185 				dataFileName.c_str()));
186 			return false;
187 		}
188 	}
189 	return true;
190 }
191 
getDocFile(const std::string & filename)192 std::string S3D::getDocFile(const std::string &filename)
193 {
194 	std::string buffer =
195 		S3D::formatStringBuffer("%s/%s", GET_DIR(S3D_DOCDIR), filename.c_str());
196 	S3D::fileDos2Unix(buffer);
197 	return buffer;
198 }
199 
getHomeFile(const std::string & filename)200 std::string S3D::getHomeFile(const std::string &filename)
201 {
202 	static std::string homeDir;
203 	if (!homeDir.c_str()[0])
204 	{
205 		homeDir = GET_DIR(S3D_DATADIR);
206 		if (S3D::dirExists(S3D::getHomeDir()))
207 		{
208 			homeDir = S3D::getHomeDir();
209 		}
210 	}
211 
212 	std::string buffer = S3D::formatStringBuffer(
213 		"%s/%s", homeDir.c_str(), filename.c_str());
214 	S3D::fileDos2Unix(buffer);
215 	return buffer;
216 }
217 
getSettingsFile(const std::string & filename)218 std::string S3D::getSettingsFile(const std::string &filename)
219 {
220 	static std::string homeDir;
221 	if (!homeDir.c_str()[0])
222 	{
223 		DIALOG_ASSERT(settingsDir.c_str() && settingsDir.c_str()[0]);
224 
225 		std::string homeDirStr = S3D::getHomeFile(
226 			S3D::formatStringBuffer("/%s", settingsDir.c_str()));
227 		if (!S3D::dirExists(homeDirStr))
228 		{
229 			if (!S3D::dirMake(homeDirStr))
230 			{
231 				homeDirStr = S3D::getHomeFile("");
232 			}
233 		}
234 		homeDir = homeDirStr;
235 	}
236 
237 	std::string buffer = S3D::formatStringBuffer(
238 		"%s/%s", homeDir.c_str(), filename.c_str());
239 	S3D::fileDos2Unix(buffer);
240 	return buffer;
241 }
242 
getLogFile(const std::string & filename)243 std::string S3D::getLogFile(const std::string &filename)
244 {
245 	std::string homeDirStr = S3D::getSettingsFile("");
246 	std::string newDir(std::string(homeDirStr) + std::string("/logs"));
247 	if (S3D::dirExists(newDir)) homeDirStr = newDir;
248 	else if (S3D::dirMake(newDir)) homeDirStr = newDir;
249 
250 	std::string buffer = S3D::formatStringBuffer(
251 		"%s/%s", homeDirStr.c_str(), filename.c_str());
252 	S3D::fileDos2Unix(buffer);
253 	return buffer;
254 }
255 
getSaveFile(const std::string & filename)256 std::string S3D::getSaveFile(const std::string &filename)
257 {
258 	std::string homeDirStr = S3D::getSettingsFile("");
259 	std::string newDir(std::string(homeDirStr) + std::string("/saves"));
260 	if (S3D::dirExists(newDir)) homeDirStr = newDir;
261 	else if (S3D::dirMake(newDir)) homeDirStr = newDir;
262 
263 	std::string buffer = S3D::formatStringBuffer(
264 		"%s/%s", homeDirStr.c_str(), filename.c_str());
265 	S3D::fileDos2Unix(buffer);
266 	return buffer;
267 }
268 
getSettingsModFile(const std::string & filename)269 std::string S3D::getSettingsModFile(const std::string &filename)
270 {
271 	static std::string modDir;
272 	if (!modDir.c_str()[0])
273 	{
274 		std::string homeDirStr = S3D::getSettingsFile("");
275 		std::string newDir(std::string(homeDirStr) + std::string("/mods"));
276 		if (S3D::dirExists(newDir)) homeDirStr = newDir;
277 		else if (S3D::dirMake(newDir)) homeDirStr = newDir;
278 
279 		modDir = homeDirStr;
280 	}
281 
282 	std::string buffer = S3D::formatStringBuffer(
283 		"%s/%s", modDir.c_str(), filename.c_str());
284 	S3D::fileDos2Unix(buffer);
285 	return buffer;
286 }
287 
getGlobalModFile(const std::string & filename)288 std::string S3D::getGlobalModFile(const std::string &filename)
289 {
290 	std::string buffer = S3D::formatStringBuffer(
291 		"%s/data/globalmods/%s", GET_DIR(S3D_DATADIR), filename.c_str());
292 	S3D::fileDos2Unix(buffer);
293 	return buffer;
294 }
295 
296