1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
/*************************************************************************/
/* file_access_pack.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "file_access_pack.h"
#include "core/version.h"
#include <stdio.h>
Error PackedData::add_pack(const String &p_path, bool p_replace_files) {
for (int i = 0; i < sources.size(); i++) {
if (sources[i]->try_open_pack(p_path, p_replace_files)) {
return OK;
};
};
return ERR_FILE_UNRECOGNIZED;
};
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) {
PathMD5 pmd5(path.md5_buffer());
//printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b);
bool exists = files.has(pmd5);
PackedFile pf;
pf.pack = pkg_path;
pf.offset = ofs;
pf.size = size;
for (int i = 0; i < 16; i++)
pf.md5[i] = p_md5[i];
pf.src = p_src;
if (!exists || p_replace_files)
files[pmd5] = pf;
if (!exists) {
//search for dir
String p = path.replace_first("res://", "");
PackedDir *cd = root;
if (p.find("/") != -1) { //in a subdir
Vector<String> ds = p.get_base_dir().split("/");
for (int j = 0; j < ds.size(); j++) {
if (!cd->subdirs.has(ds[j])) {
PackedDir *pd = memnew(PackedDir);
pd->name = ds[j];
pd->parent = cd;
cd->subdirs[pd->name] = pd;
cd = pd;
} else {
cd = cd->subdirs[ds[j]];
}
}
}
String filename = path.get_file();
// Don't add as a file if the path points to a directory
if (!filename.empty()) {
cd->files.insert(filename);
}
}
}
void PackedData::add_pack_source(PackSource *p_source) {
if (p_source != NULL) {
sources.push_back(p_source);
}
};
PackedData *PackedData::singleton = NULL;
PackedData::PackedData() {
singleton = this;
root = memnew(PackedDir);
root->parent = NULL;
disabled = false;
add_pack_source(memnew(PackedSourcePCK));
}
void PackedData::_free_packed_dirs(PackedDir *p_dir) {
for (Map<String, PackedDir *>::Element *E = p_dir->subdirs.front(); E; E = E->next())
_free_packed_dirs(E->get());
memdelete(p_dir);
}
PackedData::~PackedData() {
for (int i = 0; i < sources.size(); i++) {
memdelete(sources[i]);
}
_free_packed_dirs(root);
}
//////////////////////////////////////////////////////////////////
bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) {
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
if (!f)
return false;
uint32_t magic = f->get_32();
if (magic != PACK_HEADER_MAGIC) {
//maybe at the end.... self contained exe
f->seek_end();
f->seek(f->get_position() - 4);
magic = f->get_32();
if (magic != PACK_HEADER_MAGIC) {
f->close();
memdelete(f);
return false;
}
f->seek(f->get_position() - 12);
uint64_t ds = f->get_64();
f->seek(f->get_position() - ds - 8);
magic = f->get_32();
if (magic != PACK_HEADER_MAGIC) {
f->close();
memdelete(f);
return false;
}
}
uint32_t version = f->get_32();
uint32_t ver_major = f->get_32();
uint32_t ver_minor = f->get_32();
f->get_32(); // patch number, not used for validation.
if (version != PACK_FORMAT_VERSION) {
f->close();
memdelete(f);
ERR_FAIL_V_MSG(false, "Pack version unsupported: " + itos(version) + ".");
}
if (ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR)) {
f->close();
memdelete(f);
ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
}
for (int i = 0; i < 16; i++) {
//reserved
f->get_32();
}
int file_count = f->get_32();
for (int i = 0; i < file_count; i++) {
uint32_t sl = f->get_32();
CharString cs;
cs.resize(sl + 1);
f->get_buffer((uint8_t *)cs.ptr(), sl);
cs[sl] = 0;
String path;
path.parse_utf8(cs.ptr());
uint64_t ofs = f->get_64();
uint64_t size = f->get_64();
uint8_t md5[16];
f->get_buffer(md5, 16);
PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files);
};
f->close();
memdelete(f);
return true;
};
FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
return memnew(FileAccessPack(p_path, *p_file));
};
//////////////////////////////////////////////////////////////////
Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
ERR_FAIL_V(ERR_UNAVAILABLE);
return ERR_UNAVAILABLE;
}
void FileAccessPack::close() {
f->close();
}
bool FileAccessPack::is_open() const {
return f->is_open();
}
void FileAccessPack::seek(size_t p_position) {
if (p_position > pf.size) {
eof = true;
} else {
eof = false;
}
f->seek(pf.offset + p_position);
pos = p_position;
}
void FileAccessPack::seek_end(int64_t p_position) {
seek(pf.size + p_position);
}
size_t FileAccessPack::get_position() const {
return pos;
}
size_t FileAccessPack::get_len() const {
return pf.size;
}
bool FileAccessPack::eof_reached() const {
return eof;
}
uint8_t FileAccessPack::get_8() const {
if (pos >= pf.size) {
eof = true;
return 0;
}
pos++;
return f->get_8();
}
int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const {
if (eof)
return 0;
uint64_t to_read = p_length;
if (to_read + pos > pf.size) {
eof = true;
to_read = int64_t(pf.size) - int64_t(pos);
}
pos += p_length;
if (to_read <= 0)
return 0;
f->get_buffer(p_dst, to_read);
return to_read;
}
void FileAccessPack::set_endian_swap(bool p_swap) {
FileAccess::set_endian_swap(p_swap);
f->set_endian_swap(p_swap);
}
Error FileAccessPack::get_error() const {
if (eof)
return ERR_FILE_EOF;
return OK;
}
void FileAccessPack::flush() {
ERR_FAIL();
}
void FileAccessPack::store_8(uint8_t p_dest) {
ERR_FAIL();
}
void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) {
ERR_FAIL();
}
bool FileAccessPack::file_exists(const String &p_name) {
return false;
}
FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
pf(p_file),
f(FileAccess::open(pf.pack, FileAccess::READ)) {
ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
f->seek(pf.offset);
pos = 0;
eof = false;
}
FileAccessPack::~FileAccessPack() {
if (f)
memdelete(f);
}
//////////////////////////////////////////////////////////////////////////////////
// DIR ACCESS
//////////////////////////////////////////////////////////////////////////////////
Error DirAccessPack::list_dir_begin() {
list_dirs.clear();
list_files.clear();
for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) {
list_dirs.push_back(E->key());
}
for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
list_files.push_back(E->get());
}
return OK;
}
String DirAccessPack::get_next() {
if (list_dirs.size()) {
cdir = true;
String d = list_dirs.front()->get();
list_dirs.pop_front();
return d;
} else if (list_files.size()) {
cdir = false;
String f = list_files.front()->get();
list_files.pop_front();
return f;
} else {
return String();
}
}
bool DirAccessPack::current_is_dir() const {
return cdir;
}
bool DirAccessPack::current_is_hidden() const {
return false;
}
void DirAccessPack::list_dir_end() {
list_dirs.clear();
list_files.clear();
}
int DirAccessPack::get_drive_count() {
return 0;
}
String DirAccessPack::get_drive(int p_drive) {
return "";
}
Error DirAccessPack::change_dir(String p_dir) {
String nd = p_dir.replace("\\", "/");
bool absolute = false;
if (nd.begins_with("res://")) {
nd = nd.replace_first("res://", "");
absolute = true;
}
nd = nd.simplify_path();
if (nd == "") nd = ".";
if (nd.begins_with("/")) {
nd = nd.replace_first("/", "");
absolute = true;
}
Vector<String> paths = nd.split("/");
PackedData::PackedDir *pd;
if (absolute)
pd = PackedData::get_singleton()->root;
else
pd = current;
for (int i = 0; i < paths.size(); i++) {
String p = paths[i];
if (p == ".") {
continue;
} else if (p == "..") {
if (pd->parent) {
pd = pd->parent;
}
} else if (pd->subdirs.has(p)) {
pd = pd->subdirs[p];
} else {
return ERR_INVALID_PARAMETER;
}
}
current = pd;
return OK;
}
String DirAccessPack::get_current_dir() {
PackedData::PackedDir *pd = current;
String p = current->name;
while (pd->parent) {
pd = pd->parent;
p = pd->name.plus_file(p);
}
return "res://" + p;
}
bool DirAccessPack::file_exists(String p_file) {
p_file = fix_path(p_file);
return current->files.has(p_file);
}
bool DirAccessPack::dir_exists(String p_dir) {
p_dir = fix_path(p_dir);
return current->subdirs.has(p_dir);
}
Error DirAccessPack::make_dir(String p_dir) {
return ERR_UNAVAILABLE;
}
Error DirAccessPack::rename(String p_from, String p_to) {
return ERR_UNAVAILABLE;
}
Error DirAccessPack::remove(String p_name) {
return ERR_UNAVAILABLE;
}
size_t DirAccessPack::get_space_left() {
return 0;
}
String DirAccessPack::get_filesystem_type() const {
return "PCK";
}
DirAccessPack::DirAccessPack() {
current = PackedData::get_singleton()->root;
cdir = false;
}
DirAccessPack::~DirAccessPack() {
}
|