file_access.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*************************************************************************/
  2. /* file_access.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "file_access.h"
  31. #include "core/io/file_access_pack.h"
  32. #include "core/io/marshalls.h"
  33. #include "globals.h"
  34. #include "os/os.h"
  35. #include "thirdparty/misc/md5.h"
  36. #include "thirdparty/misc/sha256.h"
  37. FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { 0, 0 };
  38. FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = NULL;
  39. bool FileAccess::backup_save = false;
  40. FileAccess *FileAccess::create(AccessType p_access) {
  41. ERR_FAIL_COND_V(!create_func, 0);
  42. ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, 0);
  43. FileAccess *ret = create_func[p_access]();
  44. ret->_set_access_type(p_access);
  45. return ret;
  46. }
  47. bool FileAccess::exists(const String &p_name) {
  48. if (PackedData::get_singleton()->has_path(p_name))
  49. return true;
  50. FileAccess *f = open(p_name, READ);
  51. if (!f)
  52. return false;
  53. memdelete(f);
  54. return true;
  55. }
  56. void FileAccess::_set_access_type(AccessType p_access) {
  57. _access_type = p_access;
  58. };
  59. FileAccess *FileAccess::create_for_path(const String &p_path) {
  60. FileAccess *ret = NULL;
  61. if (p_path.begins_with("res://")) {
  62. ret = create(ACCESS_RESOURCES);
  63. } else if (p_path.begins_with("user://")) {
  64. ret = create(ACCESS_USERDATA);
  65. } else {
  66. ret = create(ACCESS_FILESYSTEM);
  67. }
  68. return ret;
  69. }
  70. Error FileAccess::reopen(const String &p_path, int p_mode_flags) {
  71. return _open(p_path, p_mode_flags);
  72. };
  73. FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) {
  74. //try packed data first
  75. FileAccess *ret = NULL;
  76. if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
  77. ret = PackedData::get_singleton()->try_open_path(p_path);
  78. if (ret) {
  79. if (r_error)
  80. *r_error = OK;
  81. return ret;
  82. }
  83. }
  84. ret = create_for_path(p_path);
  85. Error err = ret->_open(p_path, p_mode_flags);
  86. if (r_error)
  87. *r_error = err;
  88. if (err != OK) {
  89. memdelete(ret);
  90. ret = NULL;
  91. }
  92. return ret;
  93. }
  94. FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
  95. return create_func[p_access];
  96. };
  97. String FileAccess::fix_path(const String &p_path) const {
  98. //helper used by file accesses that use a single filesystem
  99. String r_path = p_path.replace("\\", "/");
  100. switch (_access_type) {
  101. case ACCESS_RESOURCES: {
  102. if (Globals::get_singleton()) {
  103. if (r_path.begins_with("res://")) {
  104. String resource_path = Globals::get_singleton()->get_resource_path();
  105. if (resource_path != "") {
  106. return r_path.replace("res:/", resource_path);
  107. };
  108. return r_path.replace("res://", "");
  109. }
  110. }
  111. } break;
  112. case ACCESS_USERDATA: {
  113. if (r_path.begins_with("user://")) {
  114. String data_dir = OS::get_singleton()->get_data_dir();
  115. if (data_dir != "") {
  116. return r_path.replace("user:/", data_dir);
  117. };
  118. return r_path.replace("user://", "");
  119. }
  120. } break;
  121. case ACCESS_FILESYSTEM: {
  122. return r_path;
  123. } break;
  124. }
  125. return r_path;
  126. }
  127. /* these are all implemented for ease of porting, then can later be optimized */
  128. uint16_t FileAccess::get_16() const {
  129. uint16_t res;
  130. uint8_t a, b;
  131. a = get_8();
  132. b = get_8();
  133. if (endian_swap) {
  134. SWAP(a, b);
  135. }
  136. res = b;
  137. res <<= 8;
  138. res |= a;
  139. return res;
  140. }
  141. uint32_t FileAccess::get_32() const {
  142. uint32_t res;
  143. uint16_t a, b;
  144. a = get_16();
  145. b = get_16();
  146. if (endian_swap) {
  147. SWAP(a, b);
  148. }
  149. res = b;
  150. res <<= 16;
  151. res |= a;
  152. return res;
  153. }
  154. uint64_t FileAccess::get_64() const {
  155. uint64_t res;
  156. uint32_t a, b;
  157. a = get_32();
  158. b = get_32();
  159. if (endian_swap) {
  160. SWAP(a, b);
  161. }
  162. res = b;
  163. res <<= 32;
  164. res |= a;
  165. return res;
  166. }
  167. float FileAccess::get_float() const {
  168. MarshallFloat m;
  169. m.i = get_32();
  170. return m.f;
  171. };
  172. real_t FileAccess::get_real() const {
  173. if (real_is_double)
  174. return get_double();
  175. else
  176. return get_float();
  177. }
  178. double FileAccess::get_double() const {
  179. MarshallDouble m;
  180. m.l = get_64();
  181. return m.d;
  182. };
  183. String FileAccess::get_line() const {
  184. CharString line;
  185. CharType c = get_8();
  186. while (!eof_reached()) {
  187. if (c == '\n' || c == '\0') {
  188. line.push_back(0);
  189. return String::utf8(line.get_data());
  190. } else if (c != '\r')
  191. line.push_back(c);
  192. c = get_8();
  193. }
  194. line.push_back(0);
  195. return String::utf8(line.get_data());
  196. }
  197. Vector<String> FileAccess::get_csv_line(String delim) const {
  198. ERR_FAIL_COND_V(delim.length() != 1, Vector<String>());
  199. String l;
  200. int qc = 0;
  201. do {
  202. ERR_FAIL_COND_V(eof_reached(), Vector<String>());
  203. l += get_line() + "\n";
  204. qc = 0;
  205. for (int i = 0; i < l.length(); i++) {
  206. if (l[i] == '"')
  207. qc++;
  208. }
  209. } while (qc % 2);
  210. l = l.substr(0, l.length() - 1);
  211. Vector<String> strings;
  212. bool in_quote = false;
  213. String current;
  214. for (int i = 0; i < l.length(); i++) {
  215. CharType c = l[i];
  216. CharType s[2] = { 0, 0 };
  217. if (!in_quote && c == delim[0]) {
  218. strings.push_back(current);
  219. current = String();
  220. } else if (c == '"') {
  221. if (l[i + 1] == '"') {
  222. s[0] = '"';
  223. current += s;
  224. i++;
  225. } else {
  226. in_quote = !in_quote;
  227. }
  228. } else {
  229. s[0] = c;
  230. current += s;
  231. }
  232. }
  233. strings.push_back(current);
  234. return strings;
  235. }
  236. int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const {
  237. int i = 0;
  238. for (i = 0; i < p_length && !eof_reached(); i++)
  239. p_dst[i] = get_8();
  240. return i;
  241. }
  242. void FileAccess::store_16(uint16_t p_dest) {
  243. uint8_t a, b;
  244. a = p_dest & 0xFF;
  245. b = p_dest >> 8;
  246. if (endian_swap) {
  247. SWAP(a, b);
  248. }
  249. store_8(a);
  250. store_8(b);
  251. }
  252. void FileAccess::store_32(uint32_t p_dest) {
  253. uint16_t a, b;
  254. a = p_dest & 0xFFFF;
  255. b = p_dest >> 16;
  256. if (endian_swap) {
  257. SWAP(a, b);
  258. }
  259. store_16(a);
  260. store_16(b);
  261. }
  262. void FileAccess::store_64(uint64_t p_dest) {
  263. uint32_t a, b;
  264. a = p_dest & 0xFFFFFFFF;
  265. b = p_dest >> 32;
  266. if (endian_swap) {
  267. SWAP(a, b);
  268. }
  269. store_32(a);
  270. store_32(b);
  271. }
  272. void FileAccess::store_real(real_t p_real) {
  273. if (sizeof(real_t) == 4)
  274. store_float(p_real);
  275. else
  276. store_double(p_real);
  277. }
  278. void FileAccess::store_float(float p_dest) {
  279. MarshallFloat m;
  280. m.f = p_dest;
  281. store_32(m.i);
  282. };
  283. void FileAccess::store_double(double p_dest) {
  284. MarshallDouble m;
  285. m.d = p_dest;
  286. store_64(m.l);
  287. };
  288. uint64_t FileAccess::get_modified_time(const String &p_file) {
  289. FileAccess *fa = create_for_path(p_file);
  290. ERR_FAIL_COND_V(!fa, 0);
  291. uint64_t mt = fa->_get_modified_time(p_file);
  292. memdelete(fa);
  293. return mt;
  294. }
  295. void FileAccess::store_string(const String &p_string) {
  296. if (p_string.length() == 0)
  297. return;
  298. CharString cs = p_string.utf8();
  299. store_buffer((uint8_t *)&cs[0], cs.length());
  300. }
  301. void FileAccess::store_pascal_string(const String &p_string) {
  302. CharString cs = p_string.utf8();
  303. store_32(cs.length());
  304. store_buffer((uint8_t *)&cs[0], cs.length());
  305. };
  306. String FileAccess::get_pascal_string() {
  307. uint32_t sl = get_32();
  308. CharString cs;
  309. cs.resize(sl + 1);
  310. get_buffer((uint8_t *)cs.ptr(), sl);
  311. cs[sl] = 0;
  312. String ret;
  313. ret.parse_utf8(cs.ptr());
  314. return ret;
  315. };
  316. void FileAccess::store_line(const String &p_line) {
  317. store_string(p_line);
  318. store_8('\n');
  319. }
  320. void FileAccess::store_buffer(const uint8_t *p_src, int p_length) {
  321. for (int i = 0; i < p_length; i++)
  322. store_8(p_src[i]);
  323. }
  324. Vector<uint8_t> FileAccess::get_file_as_array(const String &p_file) {
  325. FileAccess *f = FileAccess::open(p_file, READ);
  326. ERR_FAIL_COND_V(!f, Vector<uint8_t>());
  327. Vector<uint8_t> data;
  328. data.resize(f->get_len());
  329. f->get_buffer(data.ptr(), data.size());
  330. memdelete(f);
  331. return data;
  332. }
  333. String FileAccess::get_md5(const String &p_file) {
  334. FileAccess *f = FileAccess::open(p_file, READ);
  335. if (!f)
  336. return String();
  337. MD5_CTX md5;
  338. MD5Init(&md5);
  339. unsigned char step[32768];
  340. while (true) {
  341. int br = f->get_buffer(step, 32768);
  342. if (br > 0) {
  343. MD5Update(&md5, step, br);
  344. }
  345. if (br < 4096)
  346. break;
  347. }
  348. MD5Final(&md5);
  349. String ret = String::md5(md5.digest);
  350. memdelete(f);
  351. return ret;
  352. }
  353. String FileAccess::get_sha256(const String &p_file) {
  354. FileAccess *f = FileAccess::open(p_file, READ);
  355. if (!f)
  356. return String();
  357. sha256_context sha256;
  358. sha256_init(&sha256);
  359. unsigned char step[32768];
  360. while (true) {
  361. int br = f->get_buffer(step, 32768);
  362. if (br > 0) {
  363. sha256_hash(&sha256, step, br);
  364. }
  365. if (br < 4096)
  366. break;
  367. }
  368. unsigned char hash[32];
  369. sha256_done(&sha256, hash);
  370. memdelete(f);
  371. return String::hex_encode_buffer(hash, 32);
  372. }
  373. FileAccess::FileAccess() {
  374. endian_swap = false;
  375. real_is_double = false;
  376. _access_type = ACCESS_FILESYSTEM;
  377. };