file_access.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*************************************************************************/
  2. /* file_access.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. #include "file_access.h"
  30. #include "globals.h"
  31. #include "os/os.h"
  32. #include "core/io/marshalls.h"
  33. #include "io/md5.h"
  34. #include "core/io/file_access_pack.h"
  35. FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX]={0,0};
  36. FileAccess::FileCloseFailNotify FileAccess::close_fail_notify=NULL;
  37. bool FileAccess::backup_save=false;
  38. FileAccess *FileAccess::create(AccessType p_access){
  39. ERR_FAIL_COND_V( !create_func, 0 );
  40. ERR_FAIL_INDEX_V( p_access,ACCESS_MAX, 0 );
  41. FileAccess *ret = create_func[p_access]();
  42. ret->_set_access_type(p_access);
  43. return ret;
  44. }
  45. bool FileAccess::exists(const String& p_name) {
  46. if (PackedData::get_singleton()->has_path(p_name))
  47. return true;
  48. FileAccess *f=open(p_name,READ);
  49. if (!f)
  50. return false;
  51. memdelete(f);
  52. return true;
  53. }
  54. void FileAccess::_set_access_type(AccessType p_access) {
  55. _access_type = p_access;
  56. };
  57. FileAccess *FileAccess::create_for_path(const String& p_path) {
  58. FileAccess *ret=NULL;
  59. if (p_path.begins_with("res://")) {
  60. ret = create(ACCESS_RESOURCES);
  61. } else if (p_path.begins_with("user://")) {
  62. ret = create(ACCESS_USERDATA);
  63. } else {
  64. ret = create(ACCESS_FILESYSTEM);
  65. }
  66. return ret;
  67. }
  68. Error FileAccess::reopen(const String& p_path, int p_mode_flags) {
  69. return _open(p_path, p_mode_flags);
  70. };
  71. FileAccess *FileAccess::open(const String& p_path, int p_mode_flags, Error *r_error) {
  72. //try packed data first
  73. FileAccess *ret=NULL;
  74. if (!(p_mode_flags&WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
  75. ret = PackedData::get_singleton()->try_open_path(p_path);
  76. if (ret) {
  77. if (r_error)
  78. *r_error=OK;
  79. return ret;
  80. }
  81. }
  82. ret=create_for_path(p_path);
  83. Error err = ret->_open(p_path,p_mode_flags);
  84. if (r_error)
  85. *r_error=err;
  86. if (err!=OK) {
  87. memdelete(ret);
  88. ret=NULL;
  89. }
  90. return ret;
  91. }
  92. FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
  93. return create_func[p_access];
  94. };
  95. String FileAccess::fix_path(const String& p_path) const {
  96. //helper used by file accesses that use a single filesystem
  97. String r_path = p_path.replace("\\", "/");
  98. switch(_access_type) {
  99. case ACCESS_RESOURCES: {
  100. if (Globals::get_singleton()) {
  101. if (r_path.begins_with("res://")) {
  102. String resource_path = Globals::get_singleton()->get_resource_path();
  103. if (resource_path != "") {
  104. return r_path.replace("res:/",resource_path);
  105. };
  106. return r_path.replace("res://", "");
  107. }
  108. }
  109. } break;
  110. case ACCESS_USERDATA: {
  111. if (r_path.begins_with("user://")) {
  112. String data_dir=OS::get_singleton()->get_data_dir();
  113. if (data_dir != "") {
  114. return r_path.replace("user:/",data_dir);
  115. };
  116. return r_path.replace("user://", "");
  117. }
  118. } break;
  119. case ACCESS_FILESYSTEM: {
  120. return r_path;
  121. } break;
  122. }
  123. return r_path;
  124. }
  125. /* these are all implemented for ease of porting, then can later be optimized */
  126. uint16_t FileAccess::get_16()const {
  127. uint16_t res;
  128. uint8_t a,b;
  129. a=get_8();
  130. b=get_8();
  131. if (endian_swap) {
  132. SWAP( a,b );
  133. }
  134. res=b;
  135. res<<=8;
  136. res|=a;
  137. return res;
  138. }
  139. uint32_t FileAccess::get_32() const{
  140. uint32_t res;
  141. uint16_t a,b;
  142. a=get_16();
  143. b=get_16();
  144. if (endian_swap) {
  145. SWAP( a,b );
  146. }
  147. res=b;
  148. res<<=16;
  149. res|=a;
  150. return res;
  151. }
  152. uint64_t FileAccess::get_64()const {
  153. uint64_t res;
  154. uint32_t a,b;
  155. a=get_32();
  156. b=get_32();
  157. if (endian_swap) {
  158. SWAP( a,b );
  159. }
  160. res=b;
  161. res<<=32;
  162. res|=a;
  163. return res;
  164. }
  165. float FileAccess::get_float() const {
  166. MarshallFloat m;
  167. m.i = get_32();
  168. return m.f;
  169. };
  170. real_t FileAccess::get_real() const {
  171. if (real_is_double)
  172. return get_double();
  173. else
  174. return get_float();
  175. }
  176. double FileAccess::get_double() const {
  177. MarshallDouble m;
  178. m.l = get_64();
  179. return m.d;
  180. };
  181. String FileAccess::get_line() const {
  182. CharString line;
  183. CharType c=get_8();
  184. while(!eof_reached()) {
  185. if (c=='\n' || c=='\0') {
  186. line.push_back(0);
  187. return String::utf8(line.get_data());
  188. } else if (c!='\r')
  189. line.push_back(c);
  190. c=get_8();
  191. }
  192. line.push_back(0);
  193. return String::utf8(line.get_data());
  194. }
  195. Vector<String> FileAccess::get_csv_line(String delim) const {
  196. ERR_FAIL_COND_V(delim.length()!=1,Vector<String>());
  197. String l;
  198. int qc=0;
  199. do {
  200. l+=get_line()+"\n";
  201. qc=0;
  202. for(int i=0;i<l.length();i++) {
  203. if (l[i]=='"')
  204. qc++;
  205. }
  206. } while (qc%2);
  207. l=l.substr(0, l.length()-1);
  208. Vector<String> strings;
  209. bool in_quote=false;
  210. String current;
  211. for(int i=0;i<l.length();i++) {
  212. CharType c = l[i];
  213. CharType s[2]={0,0};
  214. if (!in_quote && c==delim[0]) {
  215. strings.push_back(current);
  216. current=String();
  217. } else if (c=='"') {
  218. if (l[i+1]=='"') {
  219. s[0]='"';
  220. current+=s;
  221. i++;
  222. } else {
  223. in_quote=!in_quote;
  224. }
  225. } else {
  226. s[0]=c;
  227. current+=s;
  228. }
  229. }
  230. strings.push_back(current);
  231. return strings;
  232. }
  233. int FileAccess::get_buffer(uint8_t *p_dst,int p_length) const{
  234. int i=0;
  235. for (i=0; i<p_length && !eof_reached(); i++)
  236. p_dst[i]=get_8();
  237. return i;
  238. }
  239. void FileAccess::store_16(uint16_t p_dest) {
  240. uint8_t a,b;
  241. a=p_dest&0xFF;
  242. b=p_dest>>8;
  243. if (endian_swap) {
  244. SWAP( a,b );
  245. }
  246. store_8(a);
  247. store_8(b);
  248. }
  249. void FileAccess::store_32(uint32_t p_dest) {
  250. uint16_t a,b;
  251. a=p_dest&0xFFFF;
  252. b=p_dest>>16;
  253. if (endian_swap) {
  254. SWAP( a,b );
  255. }
  256. store_16(a);
  257. store_16(b);
  258. }
  259. void FileAccess::store_64(uint64_t p_dest) {
  260. uint32_t a,b;
  261. a=p_dest&0xFFFFFFFF;
  262. b=p_dest>>32;
  263. if (endian_swap) {
  264. SWAP( a,b );
  265. }
  266. store_32(a);
  267. store_32(b);
  268. }
  269. void FileAccess::store_real(real_t p_real) {
  270. if (sizeof(real_t)==4)
  271. store_float(p_real);
  272. else
  273. store_double(p_real);
  274. }
  275. void FileAccess::store_float(float p_dest) {
  276. MarshallFloat m;
  277. m.f = p_dest;
  278. store_32(m.i);
  279. };
  280. void FileAccess::store_double(double p_dest) {
  281. MarshallDouble m;
  282. m.d = p_dest;
  283. store_64(m.l);
  284. };
  285. uint64_t FileAccess::get_modified_time(const String& p_file) {
  286. FileAccess *fa = create_for_path(p_file);
  287. ERR_FAIL_COND_V(!fa,0);
  288. uint64_t mt = fa->_get_modified_time(p_file);
  289. memdelete(fa);
  290. return mt;
  291. }
  292. void FileAccess::store_string(const String& p_string) {
  293. if (p_string.length()==0)
  294. return;
  295. CharString cs=p_string.utf8();
  296. store_buffer((uint8_t*)&cs[0],cs.length());
  297. }
  298. void FileAccess::store_pascal_string(const String& p_string) {
  299. CharString cs = p_string.utf8();
  300. store_32(cs.length());
  301. store_buffer((uint8_t*)&cs[0], cs.length());
  302. };
  303. String FileAccess::get_pascal_string() {
  304. uint32_t sl = get_32();
  305. CharString cs;
  306. cs.resize(sl+1);
  307. get_buffer((uint8_t*)cs.ptr(),sl);
  308. cs[sl]=0;
  309. String ret;
  310. ret.parse_utf8(cs.ptr());
  311. return ret;
  312. };
  313. void FileAccess::store_line(const String& p_line) {
  314. store_string(p_line);
  315. store_8('\n');
  316. }
  317. void FileAccess::store_buffer(const uint8_t *p_src,int p_length) {
  318. for (int i=0;i<p_length;i++)
  319. store_8(p_src[i]);
  320. }
  321. Vector<uint8_t> FileAccess::get_file_as_array(const String& p_file) {
  322. FileAccess *f=FileAccess::open(p_file,READ);
  323. ERR_FAIL_COND_V(!f,Vector<uint8_t>());
  324. Vector<uint8_t> data;
  325. data.resize(f->get_len());
  326. f->get_buffer(data.ptr(),data.size());
  327. memdelete(f);
  328. return data;
  329. }
  330. String FileAccess::get_md5(const String& p_file) {
  331. FileAccess *f=FileAccess::open(p_file,READ);
  332. if (!f)
  333. return String();
  334. MD5_CTX md5;
  335. MD5Init(&md5);
  336. unsigned char step[32768];
  337. while(true) {
  338. int br = f->get_buffer(step,32768);
  339. if (br>0) {
  340. MD5Update(&md5,step,br);
  341. }
  342. if (br < 4096)
  343. break;
  344. }
  345. MD5Final(&md5);
  346. String ret = String::md5(md5.digest);
  347. memdelete(f);
  348. return ret;
  349. }
  350. FileAccess::FileAccess() {
  351. endian_swap=false;
  352. real_is_double=false;
  353. _access_type=ACCESS_FILESYSTEM;
  354. };