file_access.cpp 10.0 KB

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