2
0

dir_access_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*************************************************************************/
  2. /* dir_access_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #if defined(WINDOWS_ENABLED)
  30. #include "dir_access_windows.h"
  31. #include "os/memory.h"
  32. #include <windows.h>
  33. #include <wchar.h>
  34. #include <stdio.h>
  35. #include "print_string.h"
  36. #ifdef WINRT_ENABLED
  37. #include <Synchapi.h>
  38. #include <collection.h>
  39. #include <ppltasks.h>
  40. #endif
  41. /*
  42. [03:57] <reduz> yessopie, so i dont havemak to rely on unicows
  43. [03:58] <yessopie> reduz- yeah, all of the functions fail, and then you can call GetLastError () which will return 120
  44. [03:58] <drumstick> CategoryApl, hehe, what? :)
  45. [03:59] <CategoryApl> didn't Verona lead to some trouble
  46. [03:59] <yessopie> 120 = ERROR_CALL_NOT_IMPLEMENTED
  47. [03:59] <yessopie> (you can use that constant if you include winerr.h)
  48. [03:59] <CategoryApl> well answer with winning a compo
  49. [04:02] <yessopie> if ( SetCurrentDirectoryW ( L"." ) == FALSE && GetLastError () == ERROR_CALL_NOT_IMPLEMENTED ) { use ANSI }
  50. */
  51. struct DirAccessWindowsPrivate {
  52. HANDLE h; //handle for findfirstfile
  53. WIN32_FIND_DATA f;
  54. WIN32_FIND_DATAW fu; //unicode version
  55. };
  56. // CreateFolderAsync
  57. bool DirAccessWindows::list_dir_begin() {
  58. _cisdir=false;
  59. if (unicode) {
  60. list_dir_end();
  61. p->h = FindFirstFileExW((current_dir+"\\*").c_str(), FindExInfoStandard, &p->fu, FindExSearchNameMatch, NULL, 0);
  62. return (p->h==INVALID_HANDLE_VALUE);
  63. } else {
  64. list_dir_end();
  65. p->h = FindFirstFileExA((current_dir+"\\*").ascii().get_data(), FindExInfoStandard, &p->fu, FindExSearchNameMatch, NULL, 0);
  66. return (p->h==INVALID_HANDLE_VALUE);
  67. }
  68. return false;
  69. }
  70. String DirAccessWindows::get_next() {
  71. if (p->h==INVALID_HANDLE_VALUE)
  72. return "";
  73. if (unicode) {
  74. _cisdir=(p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  75. String name=p->fu.cFileName;
  76. if (FindNextFileW(p->h, &p->fu) == 0) {
  77. FindClose(p->h);
  78. p->h=INVALID_HANDLE_VALUE;
  79. }
  80. return name;
  81. } else {
  82. #ifndef WINRT_ENABLED
  83. _cisdir=(p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  84. String name=p->f.cFileName;
  85. if (FindNextFileA(p->h, &p->f) == 0) {
  86. FindClose(p->h);
  87. p->h=INVALID_HANDLE_VALUE;
  88. }
  89. return name;
  90. #endif
  91. return "";
  92. }
  93. }
  94. bool DirAccessWindows::current_is_dir() const {
  95. return _cisdir;
  96. }
  97. void DirAccessWindows::list_dir_end() {
  98. if (p->h!=INVALID_HANDLE_VALUE) {
  99. FindClose(p->h);
  100. p->h=INVALID_HANDLE_VALUE;
  101. }
  102. }
  103. int DirAccessWindows::get_drive_count() {
  104. return drive_count;
  105. }
  106. String DirAccessWindows::get_drive(int p_drive) {
  107. if (p_drive<0 || p_drive>=drive_count)
  108. return "";
  109. return String::chr(drives[p_drive])+":";
  110. }
  111. Error DirAccessWindows::change_dir(String p_dir) {
  112. GLOBAL_LOCK_FUNCTION
  113. #ifdef WINRT_ENABLED
  114. p_dir = fix_path(p_dir);
  115. current_dir = normalize_path(p_dir);
  116. return OK;
  117. #else
  118. p_dir=fix_path(p_dir);
  119. if (unicode) {
  120. wchar_t real_current_dir_name[2048];
  121. GetCurrentDirectoryW(2048,real_current_dir_name);
  122. String prev_dir=real_current_dir_name;
  123. SetCurrentDirectoryW(current_dir.c_str());
  124. bool worked=(SetCurrentDirectoryW(p_dir.c_str())!=0);
  125. String base = _get_root_path();
  126. if (base!="") {
  127. GetCurrentDirectoryW(2048,real_current_dir_name);
  128. String new_dir;
  129. new_dir = String(real_current_dir_name).replace("\\","/");
  130. if (!new_dir.begins_with(base)) {
  131. worked=false;
  132. }
  133. }
  134. if (worked) {
  135. GetCurrentDirectoryW(2048,real_current_dir_name);
  136. current_dir=real_current_dir_name; // TODO, utf8 parser
  137. current_dir=current_dir.replace("\\","/");
  138. } //else {
  139. SetCurrentDirectoryW(prev_dir.c_str());
  140. //}
  141. return worked?OK:ERR_INVALID_PARAMETER;
  142. } else {
  143. char real_current_dir_name[2048];
  144. GetCurrentDirectoryA(2048,real_current_dir_name);
  145. String prev_dir=real_current_dir_name;
  146. SetCurrentDirectoryA(current_dir.ascii().get_data());
  147. bool worked=(SetCurrentDirectory(p_dir.ascii().get_data())!=0);
  148. if (worked) {
  149. GetCurrentDirectoryA(2048,real_current_dir_name);
  150. current_dir=real_current_dir_name; // TODO, utf8 parser
  151. current_dir=current_dir.replace("\\","/");
  152. }// else {
  153. SetCurrentDirectoryA(prev_dir.ascii().get_data());
  154. //}
  155. return worked?OK:ERR_INVALID_PARAMETER;
  156. }
  157. return OK;
  158. #endif
  159. }
  160. Error DirAccessWindows::make_dir(String p_dir) {
  161. GLOBAL_LOCK_FUNCTION
  162. #ifdef WINRT_ENABLED
  163. return ERR_CANT_CREATE;
  164. #else
  165. p_dir=fix_path(p_dir);
  166. p_dir.replace("/","\\");
  167. bool success;
  168. int err;
  169. if (unicode) {
  170. wchar_t real_current_dir_name[2048];
  171. GetCurrentDirectoryW(2048,real_current_dir_name);
  172. SetCurrentDirectoryW(current_dir.c_str());
  173. success=CreateDirectoryW(p_dir.c_str(), NULL);
  174. err = GetLastError();
  175. SetCurrentDirectoryW(real_current_dir_name);
  176. } else {
  177. char real_current_dir_name[2048];
  178. GetCurrentDirectoryA(2048,real_current_dir_name);
  179. SetCurrentDirectoryA(current_dir.ascii().get_data());
  180. success=CreateDirectoryA(p_dir.ascii().get_data(), NULL);
  181. err = GetLastError();
  182. SetCurrentDirectoryA(real_current_dir_name);
  183. }
  184. if (success) {
  185. return OK;
  186. };
  187. if (err == ERROR_ALREADY_EXISTS) {
  188. return ERR_ALREADY_EXISTS;
  189. };
  190. return ERR_CANT_CREATE;
  191. #endif
  192. }
  193. String DirAccessWindows::get_current_dir() {
  194. String base = _get_root_path();
  195. if (base!="") {
  196. String bd = current_dir.replace("\\","/").replace_first(base,"");
  197. if (bd.begins_with("/"))
  198. return _get_root_string()+bd.substr(1,bd.length());
  199. else
  200. return _get_root_string()+bd;
  201. } else {
  202. }
  203. return current_dir;
  204. }
  205. bool DirAccessWindows::file_exists(String p_file) {
  206. GLOBAL_LOCK_FUNCTION
  207. if (!p_file.is_abs_path())
  208. p_file=get_current_dir()+"/"+p_file;
  209. p_file=fix_path(p_file);
  210. p_file.replace("/","\\");
  211. WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  212. if (unicode) {
  213. DWORD fileAttr;
  214. fileAttr = GetFileAttributesExW(p_file.c_str(), GetFileExInfoStandard, &fileInfo);
  215. if (0 == fileAttr)
  216. return false;
  217. return !(fileAttr&FILE_ATTRIBUTE_DIRECTORY);
  218. } else {
  219. DWORD fileAttr;
  220. fileAttr = GetFileAttributesExA(p_file.ascii().get_data(), GetFileExInfoStandard, &fileInfo);
  221. if (0 == fileAttr)
  222. return false;
  223. return !(fileAttr&FILE_ATTRIBUTE_DIRECTORY);
  224. }
  225. return false;
  226. }
  227. bool DirAccessWindows::dir_exists(String p_dir) {
  228. GLOBAL_LOCK_FUNCTION
  229. if (!p_dir.is_abs_path())
  230. p_dir=get_current_dir()+"/"+p_dir;
  231. p_dir=fix_path(p_dir);
  232. p_dir.replace("/","\\");
  233. WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  234. if (unicode) {
  235. DWORD fileAttr;
  236. fileAttr = GetFileAttributesExW(p_dir.c_str(), GetFileExInfoStandard, &fileInfo);
  237. if (0 == fileAttr)
  238. return false;
  239. return (fileAttr&FILE_ATTRIBUTE_DIRECTORY);
  240. } else {
  241. #ifndef WINRT_ENABLED
  242. DWORD fileAttr;
  243. fileAttr = GetFileAttributesExA(p_dir.ascii().get_data(), GetFileExInfoStandard, &fileInfo);
  244. if (0 == fileAttr)
  245. return false;
  246. return (fileAttr&FILE_ATTRIBUTE_DIRECTORY);
  247. #endif
  248. }
  249. return false;
  250. }
  251. Error DirAccessWindows::rename(String p_path,String p_new_path) {
  252. p_path=fix_path(p_path);
  253. p_new_path=fix_path(p_new_path);
  254. if (file_exists(p_new_path)) {
  255. if (remove(p_new_path) != OK) {
  256. return FAILED;
  257. };
  258. };
  259. return ::_wrename(p_path.c_str(),p_new_path.c_str())==0?OK:FAILED;
  260. }
  261. Error DirAccessWindows::remove(String p_path) {
  262. p_path=fix_path(p_path);
  263. printf("erasing %s\n",p_path.utf8().get_data());
  264. WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  265. DWORD fileAttr = GetFileAttributesExW(p_path.c_str(), GetFileExInfoStandard, &fileInfo);
  266. if (fileAttr == INVALID_FILE_ATTRIBUTES)
  267. return FAILED;
  268. if (fileAttr & FILE_ATTRIBUTE_DIRECTORY)
  269. return ::_wrmdir(p_path.c_str())==0?OK:FAILED;
  270. else
  271. return ::_wunlink(p_path.c_str())==0?OK:FAILED;
  272. }
  273. /*
  274. FileType DirAccessWindows::get_file_type(const String& p_file) const {
  275. wchar_t real_current_dir_name[2048];
  276. GetCurrentDirectoryW(2048,real_current_dir_name);
  277. String prev_dir=real_current_dir_name;
  278. bool worked SetCurrentDirectoryW(current_dir.c_str());
  279. DWORD attr;
  280. if (worked) {
  281. WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  282. attr = GetFileAttributesExW(p_file.c_str(), GetFileExInfoStandard, &fileInfo);
  283. }
  284. SetCurrentDirectoryW(prev_dir.c_str());
  285. if (!worked)
  286. return FILE_TYPE_NONE;
  287. return (attr&FILE_ATTRIBUTE_DIRECTORY)?FILE_TYPE_
  288. }
  289. */
  290. size_t DirAccessWindows::get_space_left() {
  291. return -1;
  292. };
  293. DirAccessWindows::DirAccessWindows() {
  294. p = memnew( DirAccessWindowsPrivate );
  295. p->h=INVALID_HANDLE_VALUE;
  296. current_dir=".";
  297. drive_count=0;
  298. #ifdef WINRT_ENABLED
  299. Windows::Storage::StorageFolder ^install_folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
  300. change_dir(install_folder->Path->Data());
  301. #else
  302. DWORD mask=GetLogicalDrives();
  303. for (int i=0;i<MAX_DRIVES;i++) {
  304. if (mask&(1<<i)) { //DRIVE EXISTS
  305. drives[drive_count]='a'+i;
  306. drive_count++;
  307. }
  308. }
  309. unicode=true;
  310. /* We are running Windows 95/98/ME, so no unicode allowed */
  311. if ( SetCurrentDirectoryW ( L"." ) == FALSE && GetLastError () == ERROR_CALL_NOT_IMPLEMENTED )
  312. unicode=false;
  313. change_dir(".");
  314. #endif
  315. }
  316. DirAccessWindows::~DirAccessWindows() {
  317. memdelete( p );
  318. }
  319. #endif //windows DirAccess support