file_access_pack.cpp 9.6 KB

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