file_access_pack.cpp 10 KB

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