file_access_pack.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*************************************************************************/
  2. /* file_access_pack.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_pack.h"
  31. #include "core/version.h"
  32. #include <stdio.h>
  33. #define PACK_VERSION 1
  34. Error PackedData::add_pack(const String &p_path, bool p_replace_files) {
  35. for (int i = 0; i < sources.size(); i++) {
  36. if (sources[i]->try_open_pack(p_path, p_replace_files)) {
  37. return OK;
  38. };
  39. };
  40. return ERR_FILE_UNRECOGNIZED;
  41. };
  42. void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) {
  43. PathMD5 pmd5(path.md5_buffer());
  44. //printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b);
  45. bool exists = files.has(pmd5);
  46. PackedFile pf;
  47. pf.pack = pkg_path;
  48. pf.offset = ofs;
  49. pf.size = size;
  50. for (int i = 0; i < 16; i++)
  51. pf.md5[i] = p_md5[i];
  52. pf.src = p_src;
  53. if (!exists || p_replace_files)
  54. files[pmd5] = pf;
  55. if (!exists) {
  56. //search for dir
  57. String p = path.replace_first("res://", "");
  58. PackedDir *cd = root;
  59. if (p.find("/") != -1) { //in a subdir
  60. Vector<String> ds = p.get_base_dir().split("/");
  61. for (int j = 0; j < ds.size(); j++) {
  62. if (!cd->subdirs.has(ds[j])) {
  63. PackedDir *pd = memnew(PackedDir);
  64. pd->name = ds[j];
  65. pd->parent = cd;
  66. cd->subdirs[pd->name] = pd;
  67. cd = pd;
  68. } else {
  69. cd = cd->subdirs[ds[j]];
  70. }
  71. }
  72. }
  73. String filename = path.get_file();
  74. // Don't add as a file if the path points to a directory
  75. if (!filename.empty()) {
  76. cd->files.insert(filename);
  77. }
  78. }
  79. }
  80. void PackedData::add_pack_source(PackSource *p_source) {
  81. if (p_source != NULL) {
  82. sources.push_back(p_source);
  83. }
  84. };
  85. PackedData *PackedData::singleton = NULL;
  86. PackedData::PackedData() {
  87. singleton = this;
  88. root = memnew(PackedDir);
  89. root->parent = NULL;
  90. disabled = false;
  91. add_pack_source(memnew(PackedSourcePCK));
  92. }
  93. void PackedData::_free_packed_dirs(PackedDir *p_dir) {
  94. for (Map<String, PackedDir *>::Element *E = p_dir->subdirs.front(); E; E = E->next())
  95. _free_packed_dirs(E->get());
  96. memdelete(p_dir);
  97. }
  98. PackedData::~PackedData() {
  99. for (int i = 0; i < sources.size(); i++) {
  100. memdelete(sources[i]);
  101. }
  102. _free_packed_dirs(root);
  103. }
  104. //////////////////////////////////////////////////////////////////
  105. bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) {
  106. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  107. if (!f)
  108. return false;
  109. //printf("try open %ls!\n", p_path.c_str());
  110. uint32_t magic = f->get_32();
  111. if (magic != 0x43504447) {
  112. //maybe at the end.... self contained exe
  113. f->seek_end();
  114. f->seek(f->get_position() - 4);
  115. magic = f->get_32();
  116. if (magic != 0x43504447) {
  117. f->close();
  118. memdelete(f);
  119. return false;
  120. }
  121. f->seek(f->get_position() - 12);
  122. uint64_t ds = f->get_64();
  123. f->seek(f->get_position() - ds - 8);
  124. magic = f->get_32();
  125. if (magic != 0x43504447) {
  126. f->close();
  127. memdelete(f);
  128. return false;
  129. }
  130. }
  131. uint32_t version = f->get_32();
  132. uint32_t ver_major = f->get_32();
  133. uint32_t ver_minor = f->get_32();
  134. f->get_32(); // ver_rev
  135. if (version != PACK_VERSION) {
  136. f->close();
  137. memdelete(f);
  138. ERR_FAIL_V_MSG(false, "Pack version unsupported: " + itos(version) + ".");
  139. }
  140. if (ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR)) {
  141. f->close();
  142. memdelete(f);
  143. ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
  144. }
  145. for (int i = 0; i < 16; i++) {
  146. //reserved
  147. f->get_32();
  148. }
  149. int file_count = f->get_32();
  150. for (int i = 0; i < file_count; i++) {
  151. uint32_t sl = f->get_32();
  152. CharString cs;
  153. cs.resize(sl + 1);
  154. f->get_buffer((uint8_t *)cs.ptr(), sl);
  155. cs[sl] = 0;
  156. String path;
  157. path.parse_utf8(cs.ptr());
  158. uint64_t ofs = f->get_64();
  159. uint64_t size = f->get_64();
  160. uint8_t md5[16];
  161. f->get_buffer(md5, 16);
  162. PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files);
  163. };
  164. f->close();
  165. memdelete(f);
  166. return true;
  167. };
  168. FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
  169. return memnew(FileAccessPack(p_path, *p_file));
  170. };
  171. //////////////////////////////////////////////////////////////////
  172. Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
  173. ERR_FAIL_V(ERR_UNAVAILABLE);
  174. return ERR_UNAVAILABLE;
  175. }
  176. void FileAccessPack::close() {
  177. f->close();
  178. }
  179. bool FileAccessPack::is_open() const {
  180. return f->is_open();
  181. }
  182. void FileAccessPack::seek(size_t p_position) {
  183. if (p_position > pf.size) {
  184. eof = true;
  185. } else {
  186. eof = false;
  187. }
  188. f->seek(pf.offset + p_position);
  189. pos = p_position;
  190. }
  191. void FileAccessPack::seek_end(int64_t p_position) {
  192. seek(pf.size + p_position);
  193. }
  194. size_t FileAccessPack::get_position() const {
  195. return pos;
  196. }
  197. size_t FileAccessPack::get_len() const {
  198. return pf.size;
  199. }
  200. bool FileAccessPack::eof_reached() const {
  201. return eof;
  202. }
  203. uint8_t FileAccessPack::get_8() const {
  204. if (pos >= pf.size) {
  205. eof = true;
  206. return 0;
  207. }
  208. pos++;
  209. return f->get_8();
  210. }
  211. int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const {
  212. if (eof)
  213. return 0;
  214. uint64_t to_read = p_length;
  215. if (to_read + pos > pf.size) {
  216. eof = true;
  217. to_read = int64_t(pf.size) - int64_t(pos);
  218. }
  219. pos += p_length;
  220. if (to_read <= 0)
  221. return 0;
  222. f->get_buffer(p_dst, to_read);
  223. return to_read;
  224. }
  225. void FileAccessPack::set_endian_swap(bool p_swap) {
  226. FileAccess::set_endian_swap(p_swap);
  227. f->set_endian_swap(p_swap);
  228. }
  229. Error FileAccessPack::get_error() const {
  230. if (eof)
  231. return ERR_FILE_EOF;
  232. return OK;
  233. }
  234. void FileAccessPack::flush() {
  235. ERR_FAIL();
  236. }
  237. void FileAccessPack::store_8(uint8_t p_dest) {
  238. ERR_FAIL();
  239. }
  240. void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) {
  241. ERR_FAIL();
  242. }
  243. bool FileAccessPack::file_exists(const String &p_name) {
  244. return false;
  245. }
  246. FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
  247. pf(p_file),
  248. f(FileAccess::open(pf.pack, FileAccess::READ)) {
  249. ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
  250. f->seek(pf.offset);
  251. pos = 0;
  252. eof = false;
  253. }
  254. FileAccessPack::~FileAccessPack() {
  255. if (f)
  256. memdelete(f);
  257. }
  258. //////////////////////////////////////////////////////////////////////////////////
  259. // DIR ACCESS
  260. //////////////////////////////////////////////////////////////////////////////////
  261. Error DirAccessPack::list_dir_begin() {
  262. list_dirs.clear();
  263. list_files.clear();
  264. for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) {
  265. list_dirs.push_back(E->key());
  266. }
  267. for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
  268. list_files.push_back(E->get());
  269. }
  270. return OK;
  271. }
  272. String DirAccessPack::get_next() {
  273. if (list_dirs.size()) {
  274. cdir = true;
  275. String d = list_dirs.front()->get();
  276. list_dirs.pop_front();
  277. return d;
  278. } else if (list_files.size()) {
  279. cdir = false;
  280. String f = list_files.front()->get();
  281. list_files.pop_front();
  282. return f;
  283. } else {
  284. return String();
  285. }
  286. }
  287. bool DirAccessPack::current_is_dir() const {
  288. return cdir;
  289. }
  290. bool DirAccessPack::current_is_hidden() const {
  291. return false;
  292. }
  293. void DirAccessPack::list_dir_end() {
  294. list_dirs.clear();
  295. list_files.clear();
  296. }
  297. int DirAccessPack::get_drive_count() {
  298. return 0;
  299. }
  300. String DirAccessPack::get_drive(int p_drive) {
  301. return "";
  302. }
  303. Error DirAccessPack::change_dir(String p_dir) {
  304. String nd = p_dir.replace("\\", "/");
  305. bool absolute = false;
  306. if (nd.begins_with("res://")) {
  307. nd = nd.replace_first("res://", "");
  308. absolute = true;
  309. }
  310. nd = nd.simplify_path();
  311. if (nd == "") nd = ".";
  312. if (nd.begins_with("/")) {
  313. nd = nd.replace_first("/", "");
  314. absolute = true;
  315. }
  316. Vector<String> paths = nd.split("/");
  317. PackedData::PackedDir *pd;
  318. if (absolute)
  319. pd = PackedData::get_singleton()->root;
  320. else
  321. pd = current;
  322. for (int i = 0; i < paths.size(); i++) {
  323. String p = paths[i];
  324. if (p == ".") {
  325. continue;
  326. } else if (p == "..") {
  327. if (pd->parent) {
  328. pd = pd->parent;
  329. }
  330. } else if (pd->subdirs.has(p)) {
  331. pd = pd->subdirs[p];
  332. } else {
  333. return ERR_INVALID_PARAMETER;
  334. }
  335. }
  336. current = pd;
  337. return OK;
  338. }
  339. String DirAccessPack::get_current_dir() {
  340. PackedData::PackedDir *pd = current;
  341. String p = current->name;
  342. while (pd->parent) {
  343. pd = pd->parent;
  344. p = pd->name.plus_file(p);
  345. }
  346. return "res://" + p;
  347. }
  348. bool DirAccessPack::file_exists(String p_file) {
  349. p_file = fix_path(p_file);
  350. return current->files.has(p_file);
  351. }
  352. bool DirAccessPack::dir_exists(String p_dir) {
  353. p_dir = fix_path(p_dir);
  354. return current->subdirs.has(p_dir);
  355. }
  356. Error DirAccessPack::make_dir(String p_dir) {
  357. return ERR_UNAVAILABLE;
  358. }
  359. Error DirAccessPack::rename(String p_from, String p_to) {
  360. return ERR_UNAVAILABLE;
  361. }
  362. Error DirAccessPack::remove(String p_name) {
  363. return ERR_UNAVAILABLE;
  364. }
  365. size_t DirAccessPack::get_space_left() {
  366. return 0;
  367. }
  368. String DirAccessPack::get_filesystem_type() const {
  369. return "PCK";
  370. }
  371. DirAccessPack::DirAccessPack() {
  372. current = PackedData::get_singleton()->root;
  373. cdir = false;
  374. }
  375. DirAccessPack::~DirAccessPack() {
  376. }