Use configured resolution for login/outgame/ingame
[ryzomcore.git] / ryzom / tools / leveldesign / export / tools.cpp
blob5c1e1d9b323c1e8a93d3c6dbb3c2973a5c790467
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "tools.h"
18 #include <windows.h>
20 #include "nel/misc/common.h"
22 // ---------------------------------------------------------------------------
24 using namespace std;
25 using namespace NLMISC;
27 // ---------------------------------------------------------------------------
28 void CTools::mkdir (const string &dirName)
30 if (dirName.empty())
31 return;
32 // Does the directory exist ?
33 string newDir = pwd();
34 if (SetCurrentDirectory (dirName.c_str()))
36 SetCurrentDirectory (newDir.c_str());
37 return;
39 SetCurrentDirectory (newDir.c_str());
40 // Create upper levels
41 newDir.clear();
42 string::size_type pos = dirName.rfind('\\');
43 if (pos != string::npos)
45 for (uint i = 0; i < pos; ++i)
46 newDir += dirName[i];
47 mkdir (newDir);
49 // Create Directory
50 if (!CreateDirectory(dirName.c_str(),NULL))
51 throw Exception(string("Cannot create directory ")+dirName);
54 // ---------------------------------------------------------------------------
55 void CTools::chdir (const std::string &newDir)
57 if (!SetCurrentDirectory (newDir.c_str()))
58 throwError ((newDir+" : ").c_str ());
61 // ---------------------------------------------------------------------------
62 std::string CTools::pwd ()
64 char sTmp[512];
65 if (GetCurrentDirectory (512, sTmp) == 0)
67 throwError ("Get current directory : ");
69 string sTmp2 = sTmp;
70 return sTmp2;
73 // ---------------------------------------------------------------------------
74 std::string CTools::normalizePath (const std::string &path)
76 // Convert slash to anti-slash
77 string retPath = path;
78 for (uint32 i = 0; i < retPath.size(); ++i)
79 if (retPath[i] == '/')
80 retPath[i] = '\\';
81 return retPath;
84 // ---------------------------------------------------------------------------
85 bool CTools::fileExist (const std::string &sFileName)
87 HANDLE hFile = CreateFile (sFileName.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
88 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
89 if (hFile == INVALID_HANDLE_VALUE)
90 return false;
91 CloseHandle (hFile);
92 return true;
95 // ---------------------------------------------------------------------------
96 int CTools::fileDateCmp (const std::string &file1, const std::string &file2)
98 HANDLE hFile1 = CreateFile (file1.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
99 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
100 HANDLE hFile2 = CreateFile (file2.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
101 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
102 if ((hFile1 == INVALID_HANDLE_VALUE) && (hFile2 == INVALID_HANDLE_VALUE))
103 return 0;
104 if (hFile1 == INVALID_HANDLE_VALUE)
106 CloseHandle(hFile2);
107 return -1;
109 if (hFile2 == INVALID_HANDLE_VALUE)
111 CloseHandle(hFile1);
112 return 1;
115 FILETIME CreationTime1, LastAccessTime1, LastWrite1;
116 GetFileTime (hFile1, &CreationTime1, &LastAccessTime1, &LastWrite1);
117 FILETIME CreationTime2, LastAccessTime2, LastWrite2;
118 GetFileTime (hFile2, &CreationTime2, &LastAccessTime2, &LastWrite2);
120 LONG nRet = CompareFileTime (&LastWrite1, &LastWrite2);
122 CloseHandle(hFile1);
123 CloseHandle(hFile2);
125 return nRet;
128 // ---------------------------------------------------------------------------
129 int CTools::fileDateCmp (const std::string &file1, uint32 nDateLow, uint32 nDateHigh)
131 HANDLE hFile1 = CreateFile (file1.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
132 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
133 if (hFile1 == INVALID_HANDLE_VALUE)
135 return -1;
137 FILETIME CreationTime1, LastAccessTime1, LastWrite1;
138 GetFileTime (hFile1, &CreationTime1, &LastAccessTime1, &LastWrite1);
139 FILETIME Date;
140 Date.dwLowDateTime = nDateLow;
141 Date.dwHighDateTime = nDateHigh;
142 LONG nRet = CompareFileTime (&LastWrite1, &Date);
144 CloseHandle(hFile1);
145 return nRet;
148 // ---------------------------------------------------------------------------
149 void CTools::dir (const std::string &sFilter, std::vector<std::string> &sAllFiles, bool bFullPath)
151 WIN32_FIND_DATA findData;
152 HANDLE hFind;
153 char sCurDir[MAX_PATH];
154 sAllFiles.clear ();
155 GetCurrentDirectory (MAX_PATH, sCurDir);
156 hFind = FindFirstFile (sFilter.c_str(), &findData);
157 while (hFind != INVALID_HANDLE_VALUE)
159 if (!(GetFileAttributes(findData.cFileName)&FILE_ATTRIBUTE_DIRECTORY))
161 if (bFullPath)
162 sAllFiles.push_back(string(sCurDir) + "\\" + findData.cFileName);
163 else
164 sAllFiles.push_back(findData.cFileName);
166 if (FindNextFile (hFind, &findData) == 0)
167 break;
169 FindClose (hFind);
172 // ---------------------------------------------------------------------------
173 void CTools::dirSub (const std::string &sFilter, std::vector<std::string> &sAllFiles, bool bFullPath)
175 sAllFiles.clear();
176 string sTmp;
177 for (uint32 i = 0; i < sFilter.size(); ++i)
178 if (sFilter[i] != '*')
179 sTmp += sFilter[i];
180 dirSubRecurse (sTmp, sAllFiles, bFullPath);
183 // ---------------------------------------------------------------------------
184 void CTools::copy (const std::string &DstFile, const std::string &SrcFile)
186 if (!CopyFile (SrcFile.c_str(), DstFile.c_str(), false))
188 throw Exception(string("Cannot copy ")+SrcFile+" to "+DstFile);
192 // *******
193 // PRIVATE
194 // *******
196 // ---------------------------------------------------------------------------
197 void CTools::throwError (const char *message)
199 LPVOID lpMsgBuf;
200 FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |
201 FORMAT_MESSAGE_FROM_SYSTEM |
202 FORMAT_MESSAGE_IGNORE_INSERTS,
203 NULL,
204 GetLastError(),
205 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
206 (LPTSTR) &lpMsgBuf,
208 NULL );
209 string sTmp = (char*)lpMsgBuf;
210 sTmp = message + sTmp;
211 LocalFree (lpMsgBuf);
212 throw Exception (sTmp);
215 // ---------------------------------------------------------------------------
216 void CTools::dirSubRecurse (const std::string &sFilter, std::vector<std::string> &sAllFiles, bool bFullPath)
218 WIN32_FIND_DATA findData;
219 HANDLE hFind;
220 char sCurDir[MAX_PATH];
222 GetCurrentDirectory (MAX_PATH, sCurDir);
223 hFind = FindFirstFile ("*.*", &findData);
224 while (hFind != INVALID_HANDLE_VALUE)
226 if (!(GetFileAttributes(findData.cFileName)&FILE_ATTRIBUTE_DIRECTORY))
228 string sTmp = findData.cFileName;
229 if (sTmp.size() > sFilter.size())
230 if (strcmp(sTmp.c_str()+sTmp.size()-sFilter.size(), sFilter.c_str()) == 0)
232 if (bFullPath)
233 sAllFiles.push_back(string(sCurDir) + "\\" + findData.cFileName);
234 else
235 sAllFiles.push_back(findData.cFileName);
238 else if ((strcmp(findData.cFileName, ".") != 0) && (strcmp(findData.cFileName, "..") != 0))
240 SetCurrentDirectory (findData.cFileName);
241 dirSubRecurse (sFilter, sAllFiles, bFullPath);
242 SetCurrentDirectory (sCurDir);
244 if (FindNextFile (hFind, &findData) == 0)
245 break;
247 FindClose (hFind);