Skip to content

Commit

Permalink
Merge pull request godotengine#42890 from Ev1lbl0w/bugfix-import-zip
Browse files Browse the repository at this point in the history
Allow Godot to import .ZIP files with non-regular structure
  • Loading branch information
akien-mga authored Mar 31, 2021
2 parents 54a690e + 9b1db71 commit 36c11a5
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions editor/project_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class ProjectDialog : public ConfirmationDialog {
FileDialog *fdialog_install;
String zip_path;
String zip_title;
String zip_root;
AcceptDialog *dialog_error;
String fav_dir;

Expand Down Expand Up @@ -200,7 +201,9 @@ class ProjectDialog : public ConfirmationDialog {
char fname[16384];
ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);

if (String(fname).ends_with("project.godot")) {
String fname_str = String(fname);
if (fname_str.ends_with("project.godot")) {
zip_root = fname_str.substr(0, fname_str.rfind("project.godot"));
break;
}

Expand Down Expand Up @@ -533,44 +536,34 @@ class ProjectDialog : public ConfirmationDialog {

String path = fname;

int depth = 1; //stuff from github comes with tag
bool skip = false;
while (depth > 0) {
int pp = path.find("/");
if (pp == -1) {
skip = true;
break;
}
path = path.substr(pp + 1, path.length());
depth--;
}

if (skip || path == String()) {
if (path == String() || path == zip_root || !zip_root.is_subsequence_of(path)) {
//
} else if (path.ends_with("/")) { // a dir

path = path.substr(0, path.length() - 1);
String rel_path = path.substr(zip_root.length());

DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
da->make_dir(dir.plus_file(path));
da->make_dir(dir.plus_file(rel_path));
memdelete(da);

} else {
Vector<uint8_t> data;
data.resize(info.uncompressed_size);
String rel_path = path.substr(zip_root.length());

//read
unzOpenCurrentFile(pkg);
unzReadCurrentFile(pkg, data.ptrw(), data.size());
unzCloseCurrentFile(pkg);

FileAccess *f = FileAccess::open(dir.plus_file(path), FileAccess::WRITE);
FileAccess *f = FileAccess::open(dir.plus_file(rel_path), FileAccess::WRITE);

if (f) {
f->store_buffer(data.ptr(), data.size());
memdelete(f);
} else {
failed_files.push_back(path);
failed_files.push_back(rel_path);
}
}

Expand Down

0 comments on commit 36c11a5

Please sign in to comment.