mirror of
https://github.com/godotengine/godot.git
synced 2025-10-19 16:03:29 +00:00
Implement sparse bundle PCK support.
This commit is contained in:
parent
d89f4ab32f
commit
42733a2a5c
11 changed files with 351 additions and 146 deletions
|
@ -625,6 +625,7 @@ bool EditorExportPlatformAndroid::_should_compress_asset(const String &p_path, c
|
|||
".cfb", // Don't let small config files slow-down startup
|
||||
".scn", // Binary scenes are usually already compressed
|
||||
".ctex", // Streamable textures are usually already compressed
|
||||
".pck", // Pack.
|
||||
// Trailer for easier processing
|
||||
nullptr
|
||||
};
|
||||
|
@ -800,14 +801,25 @@ Error EditorExportPlatformAndroid::save_apk_so(void *p_userdata, const SharedObj
|
|||
Error EditorExportPlatformAndroid::save_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed) {
|
||||
APKExportData *ed = static_cast<APKExportData *>(p_userdata);
|
||||
|
||||
String path = p_path.simplify_path();
|
||||
if (path.begins_with("uid://")) {
|
||||
path = ResourceUID::uid_to_path(path).simplify_path();
|
||||
print_verbose(vformat(R"(UID referenced exported file name "%s" was replaced with "%s".)", p_path, path));
|
||||
String simplified_path = p_path.simplify_path();
|
||||
if (simplified_path.begins_with("uid://")) {
|
||||
simplified_path = ResourceUID::uid_to_path(simplified_path).simplify_path();
|
||||
print_verbose(vformat(R"(UID referenced exported file name "%s" was replaced with "%s".)", p_path, simplified_path));
|
||||
}
|
||||
const String dst_path = path.replace_first("res://", "assets/");
|
||||
|
||||
store_in_apk(ed, dst_path, p_data, _should_compress_asset(path, p_data) ? Z_DEFLATED : 0);
|
||||
Vector<uint8_t> enc_data;
|
||||
EditorExportPlatform::SavedData sd;
|
||||
Error err = _store_temp_file(simplified_path, p_data, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, enc_data, sd);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
const String dst_path = String("assets/") + simplified_path.trim_prefix("res://");
|
||||
print_verbose("Saving project files from " + simplified_path + " into " + dst_path);
|
||||
store_in_apk(ed, dst_path, enc_data, _should_compress_asset(simplified_path, enc_data) ? Z_DEFLATED : 0);
|
||||
|
||||
ed->pd.file_ofs.push_back(sd);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
@ -3409,6 +3421,68 @@ Error EditorExportPlatformAndroid::export_project(const Ref<EditorExportPreset>
|
|||
return export_project_helper(p_preset, p_debug, p_path, export_format, should_sign, p_flags);
|
||||
}
|
||||
|
||||
Error EditorExportPlatformAndroid::_generate_sparse_pck_metadata(const Ref<EditorExportPreset> &p_preset, PackData &p_pack_data, Vector<uint8_t> &r_data) {
|
||||
Error err;
|
||||
Ref<FileAccess> ftmp = FileAccess::create_temp(FileAccess::WRITE_READ, "export_index", "tmp", false, &err);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not create temporary file!"));
|
||||
return err;
|
||||
}
|
||||
int64_t pck_start_pos = ftmp->get_position();
|
||||
uint64_t file_base_ofs = 0;
|
||||
uint64_t dir_base_ofs = 0;
|
||||
EditorExportPlatform::_store_header(ftmp, p_preset->get_enc_pck() && p_preset->get_enc_directory(), true, file_base_ofs, dir_base_ofs);
|
||||
|
||||
// Write directory.
|
||||
uint64_t dir_offset = ftmp->get_position();
|
||||
ftmp->seek(dir_base_ofs);
|
||||
ftmp->store_64(dir_offset - pck_start_pos);
|
||||
ftmp->seek(dir_offset);
|
||||
|
||||
Vector<uint8_t> key;
|
||||
if (p_preset->get_enc_pck() && p_preset->get_enc_directory()) {
|
||||
String script_key = _get_script_encryption_key(p_preset);
|
||||
key.resize(32);
|
||||
if (script_key.length() == 64) {
|
||||
for (int i = 0; i < 32; i++) {
|
||||
int v = 0;
|
||||
if (i * 2 < script_key.length()) {
|
||||
char32_t ct = script_key[i * 2];
|
||||
if (is_digit(ct)) {
|
||||
ct = ct - '0';
|
||||
} else if (ct >= 'a' && ct <= 'f') {
|
||||
ct = 10 + ct - 'a';
|
||||
}
|
||||
v |= ct << 4;
|
||||
}
|
||||
|
||||
if (i * 2 + 1 < script_key.length()) {
|
||||
char32_t ct = script_key[i * 2 + 1];
|
||||
if (is_digit(ct)) {
|
||||
ct = ct - '0';
|
||||
} else if (ct >= 'a' && ct <= 'f') {
|
||||
ct = 10 + ct - 'a';
|
||||
}
|
||||
v |= ct;
|
||||
}
|
||||
key.write[i] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!EditorExportPlatform::_encrypt_and_store_directory(ftmp, p_pack_data, key, p_preset->get_seed(), 0)) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Can't create encrypted file."));
|
||||
return ERR_CANT_CREATE;
|
||||
}
|
||||
|
||||
r_data.resize(ftmp->get_length());
|
||||
ftmp->seek(0);
|
||||
ftmp->get_buffer(r_data.ptrw(), r_data.size());
|
||||
ftmp.unref();
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int export_format, bool should_sign, BitField<EditorExportPlatform::DebugFlags> p_flags) {
|
||||
ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
|
||||
|
||||
|
@ -3536,7 +3610,22 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportP
|
|||
if (p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) {
|
||||
err = export_project_files(p_preset, p_debug, ignore_apk_file, nullptr, &user_data, copy_gradle_so);
|
||||
} else {
|
||||
user_data.pd.path = "assets.sparsepck";
|
||||
user_data.pd.use_sparse_pck = true;
|
||||
err = export_project_files(p_preset, p_debug, rename_and_store_file_in_gradle_project, nullptr, &user_data, copy_gradle_so);
|
||||
|
||||
Vector<uint8_t> enc_data;
|
||||
err = _generate_sparse_pck_metadata(p_preset, user_data.pd, enc_data);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not generate sparse pck metadata!"));
|
||||
return err;
|
||||
}
|
||||
|
||||
err = store_file_at_path(user_data.assets_directory + "/assets.sparsepck", enc_data);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not write PCK directory!"));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not export project files to gradle project."));
|
||||
|
@ -3988,7 +4077,18 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportP
|
|||
APKExportData ed;
|
||||
ed.ep = &ep;
|
||||
ed.apk = unaligned_apk;
|
||||
ed.pd.path = "assets.sparsepck";
|
||||
ed.pd.use_sparse_pck = true;
|
||||
err = export_project_files(p_preset, p_debug, save_apk_file, nullptr, &ed, save_apk_so);
|
||||
|
||||
Vector<uint8_t> enc_data;
|
||||
err = _generate_sparse_pck_metadata(p_preset, ed.pd, enc_data);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not generate sparse pck metadata!"));
|
||||
return err;
|
||||
}
|
||||
|
||||
store_in_apk(&ed, "assets/assets.sparsepck", enc_data, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue