DefinesScorched.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <stdarg.h>
00024 #include <string>
00025 #include <common/DefinesScorched.h>
00026 #include <common/DefinesFile.h>
00027 #include <common/DefinesString.h>
00028 #include <common/DefinesAssert.h>
00029 #include <common/Logger.h>
00030 #define WIN32_LEAN_AND_MEAN
00031 #include <windows.h>
00032 
00033 #pragma warning(disable : 4996)
00034 
00035 #ifdef HAVE_UNISTD_H
00036 #include <unistd.h>
00037 #endif
00038 
00039 unsigned int S3D::ScorchedPort = 27270;
00040 std::string S3D::ScorchedVersion = "42";
00041 std::string S3D::ScorchedProtocolVersion = "eg";
00042 #ifdef __DATE__
00043 std::string S3D::ScorchedBuildTime = __DATE__;
00044 #else
00045 std::string S3D::ScorchedBuildTime = "Unknown";
00046 #endif
00047 static std::string exeName;
00048 static std::string dataModFile = "none";
00049 static std::string settingsDir = "";
00050 
00051 void S3D::showURL(const std::string &url)
00052 {
00053 #ifdef _WIN32
00054         std::string buffer = S3D::formatStringBuffer("explorer %s", url.c_str());
00055         WinExec(buffer.c_str() ,SW_SHOWDEFAULT);
00056 #else
00057 #ifdef __DARWIN__
00058         std::string buffer = S3D::formatStringBuffer("open %s", url.c_str());
00059         system(buffer.c_str());
00060 #else
00061         std::string buffer = S3D::formatStringBuffer("firefox %s", url.c_str());
00062         system(buffer.c_str());
00063 #endif // __DARWIN__
00064 #endif // _WIN32
00065         Logger::log(url);
00066 }
00067 
00068 void S3D::setExeName(const std::string &name)
00069 {
00070         exeName = name;
00071 }
00072 
00073 std::string S3D::getExeName()
00074 {
00075         return exeName;
00076 }
00077 
00078 std::string S3D::getStartTime()
00079 {
00080         static std::string startTime;
00081         if (startTime.empty())
00082         {
00083                 time_t theTime = time(0);
00084                 startTime = ctime(&theTime);
00085         }
00086         return startTime;
00087 }
00088 
00089 void S3D::setSettingsDir(const std::string &dir)
00090 {
00091         settingsDir = dir;
00092 }
00093 
00094 void S3D::setDataFileMod(const std::string &mod)
00095 {
00096         dataModFile = mod;
00097 }
00098 
00099 std::string S3D::getDataFileMod()
00100 {
00101         return dataModFile;
00102 }
00103 
00104 #ifndef S3D_DATADIR
00105 #define S3D_DATADIR "."
00106 #endif
00107 #ifndef S3D_DOCDIR
00108 #define S3D_DOCDIR "./documentation"
00109 #endif
00110 #ifndef S3D_BINDIR
00111 #define S3D_BINDIR "."
00112 #endif
00113 
00114 static const char *GET_DIR(const char *dir)
00115 {
00116         if (dir[0] == '.')
00117         {
00118                 static char path[1024];
00119 #ifdef _WIN32
00120                         GetCurrentDirectory(sizeof(path), path);
00121 #else
00122                         getcwd(path, sizeof(path));
00123 #endif // _WIN32
00124                 if (strlen(path) + strlen(dir) + 1 < sizeof(path))
00125                 {
00126                         strcat(path, "/");
00127                         strcat(path, dir);
00128                 }
00129                 return path;
00130         }
00131         return dir;
00132 }
00133 
00134 std::string S3D::getDataFile(const std::string &filename)
00135 {
00136         std::string buffer;
00137 
00138         buffer = S3D::getModFile(
00139                 S3D::formatStringBuffer("%s/%s", dataModFile.c_str(), filename.c_str()));
00140         S3D::fileDos2Unix(buffer);
00141         if (S3D::fileExists(buffer)) return buffer;
00142 
00143         buffer = S3D::getGlobalModFile(
00144                 S3D::formatStringBuffer("%s/%s", dataModFile.c_str(), filename.c_str()));
00145         S3D::fileDos2Unix(buffer);
00146         if (S3D::fileExists(buffer)) return buffer;
00147 
00148         buffer = S3D::formatStringBuffer("%s/%s", GET_DIR(S3D_DATADIR), filename.c_str());
00149         S3D::fileDos2Unix(buffer);
00150 
00151         return buffer;
00152 }
00153 
00154 extern bool S3D::checkDataFile(const std::string &filename)
00155 {
00156         std::string dataFileName = S3D::getDataFile(filename);
00157         if (!S3D::fileExists(dataFileName))
00158         {
00159                 if (0 == strstr(filename.c_str(), "none"))
00160                 {
00161                         S3D::dialogMessage("Scorched3D", S3D::formatStringBuffer(
00162                                 "The file \"%s\" does not exist",
00163                                 dataFileName.c_str()));
00164                         return false;
00165                 }
00166         }
00167         return true;
00168 }
00169 
00170 std::string S3D::getDocFile(const std::string &filename)
00171 {
00172         std::string buffer =
00173                 S3D::formatStringBuffer("%s/%s", GET_DIR(S3D_DOCDIR), filename.c_str());
00174         S3D::fileDos2Unix(buffer);
00175         return buffer;
00176 }
00177 
00178 std::string S3D::getHomeFile(const std::string &filename)
00179 {
00180         static std::string homeDir;
00181         if (!homeDir.c_str()[0])
00182         {
00183                 homeDir = GET_DIR(S3D_DATADIR);
00184                 if (S3D::dirExists(S3D::getHomeDir()))
00185                 {
00186                         homeDir = S3D::getHomeDir();
00187                 }
00188         }
00189 
00190         std::string buffer = S3D::formatStringBuffer(
00191                 "%s/%s", homeDir.c_str(), filename.c_str());
00192         S3D::fileDos2Unix(buffer);
00193         return buffer;
00194 }
00195 
00196 std::string S3D::getSettingsFile(const std::string &filename)
00197 {
00198         static std::string homeDir;
00199         if (!homeDir.c_str()[0])
00200         {
00201                 DIALOG_ASSERT(settingsDir.c_str() && settingsDir.c_str()[0]);
00202 
00203                 std::string homeDirStr = S3D::getHomeFile(
00204                         S3D::formatStringBuffer("/%s", settingsDir.c_str()));
00205                 if (!S3D::dirExists(homeDirStr))
00206                 {
00207                         if (!S3D::dirMake(homeDirStr))
00208                         {
00209                                 homeDirStr = S3D::getHomeFile("");
00210                         }
00211                 }
00212                 homeDir = homeDirStr;
00213         }
00214 
00215         std::string buffer = S3D::formatStringBuffer(
00216                 "%s/%s", homeDir.c_str(), filename.c_str());
00217         S3D::fileDos2Unix(buffer);
00218         return buffer;
00219 }
00220 
00221 std::string S3D::getLogFile(const std::string &filename)
00222 {
00223         std::string homeDirStr = S3D::getSettingsFile("");
00224         std::string newDir(std::string(homeDirStr) + std::string("/logs"));
00225         if (S3D::dirExists(newDir)) homeDirStr = newDir;
00226         else if (S3D::dirMake(newDir)) homeDirStr = newDir;
00227 
00228         std::string buffer = S3D::formatStringBuffer(
00229                 "%s/%s", homeDirStr.c_str(), filename.c_str());
00230         S3D::fileDos2Unix(buffer);
00231         return buffer;
00232 }
00233 
00234 std::string S3D::getSaveFile(const std::string &filename)
00235 {
00236         std::string homeDirStr = S3D::getSettingsFile("");
00237         std::string newDir(std::string(homeDirStr) + std::string("/saves"));
00238         if (S3D::dirExists(newDir)) homeDirStr = newDir;
00239         else if (S3D::dirMake(newDir)) homeDirStr = newDir;
00240 
00241         std::string buffer = S3D::formatStringBuffer(
00242                 "%s/%s", homeDirStr.c_str(), filename.c_str());
00243         S3D::fileDos2Unix(buffer);
00244         return buffer;
00245 }
00246 
00247 std::string S3D::getModFile(const std::string &filename)
00248 {
00249         static std::string modDir;
00250         if (!modDir.c_str()[0])
00251         {
00252                 std::string homeDirStr = S3D::getSettingsFile("");
00253                 std::string newDir(std::string(homeDirStr) + std::string("/mods"));
00254                 if (S3D::dirExists(newDir)) homeDirStr = newDir;
00255                 else if (S3D::dirMake(newDir)) homeDirStr = newDir;
00256 
00257                 modDir = homeDirStr;
00258         }
00259                  
00260         std::string buffer = S3D::formatStringBuffer(
00261                 "%s/%s", modDir.c_str(), filename.c_str());
00262         S3D::fileDos2Unix(buffer);
00263         return buffer;
00264 }
00265 
00266 std::string S3D::getGlobalModFile(const std::string &filename)
00267 {
00268         std::string buffer = S3D::formatStringBuffer(
00269                 "%s/data/globalmods/%s", GET_DIR(S3D_DATADIR), filename.c_str());
00270         S3D::fileDos2Unix(buffer);
00271         return buffer;
00272 }
00273 

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3