file_access.cpp 9.4 KB

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