file_access_pack.cpp 9.4 KB

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