diff options
author | Ryan Mitchell <rtmitchell@google.com> | 2021-03-19 14:53:17 -0700 |
---|---|---|
committer | Ryan Mitchell <rtmitchell@google.com> | 2021-03-29 10:22:08 -0700 |
commit | 9634efb6f44d8d3f5edbcc7118ad9e3d8e3f9aa9 (patch) | |
tree | 526922b5dbb1cbb268612646b918692030f06c2e /tools/aapt2/format/binary/BinaryResourceParser.cpp | |
parent | 67eca1c2dac2e36e78755eff72ec1d0e54805298 (diff) |
Prepare aapt2 for multiple ids per type
For the SDK finalization changes, aapt2 must be able to handle
resources of the same type having different type ids. The
ResourceTable data structure currently stores package ids and type ids
on ResourceTablePackage and ResourceTableType respectively. This
prevents resource entries of the same type from having different type
ids without having to create another ResourceTableType structure.
JavaClassGenerator assumes each type only appears once in the
ResourceTable and it would need to dedupe the types to ensure one class
containing all the resource types ids is generated. TableFlattener on
the other hand needs a separate ResourceTableType for each type/id
combination so that the types are flattened into separate
ResTable_types.
This change simplifies aapt2's ResourceTable data structure:
- Resource ids are stored exclusively on ResourceEntry structures
meaning multiple entries can have different type ids while being
stored in the same ResourceTableType. Classes like JavaClassGenerator
can simply iterate over a type to see all the resources of the type
regardless of what their type id is.
- ResourceTable::GetPartitionedView() retrieves a list of resources
sorted and partitioned by package id, type id, and entry id. Classes
like TableFlattener can use this view to get separate
ResourceTavleTypes for each different type id that a type has.
These changes will also make it easy to have a resource span multiple
type ids if it exhausts all of the entry ids in one type id.
The new NewResourcesBuilder replaces the numerous setter methods on
ResourceTable.
Bug: 183102797
Test: aapt2_tests
Change-Id: I60dbcb24143bb958333899cafa7d41faa226d203
Diffstat (limited to 'tools/aapt2/format/binary/BinaryResourceParser.cpp')
-rw-r--r-- | tools/aapt2/format/binary/BinaryResourceParser.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/tools/aapt2/format/binary/BinaryResourceParser.cpp b/tools/aapt2/format/binary/BinaryResourceParser.cpp index cccd9faa9b39..bfb8d5854d6d 100644 --- a/tools/aapt2/format/binary/BinaryResourceParser.cpp +++ b/tools/aapt2/format/binary/BinaryResourceParser.cpp @@ -192,8 +192,7 @@ bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { std::u16string package_name = strcpy16_dtoh((const char16_t*)package_header->name, arraysize(package_header->name)); - ResourceTablePackage* package = - table_->CreatePackage(util::Utf16ToUtf8(package_name), static_cast<uint8_t>(package_id)); + ResourceTablePackage* package = table_->FindOrCreatePackage(util::Utf16ToUtf8(package_name)); if (!package) { diag_->Error(DiagMessage(source_) << "incompatible package '" << package_name << "' with ID " << package_id); @@ -232,13 +231,13 @@ bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { break; case android::RES_TABLE_TYPE_SPEC_TYPE: - if (!ParseTypeSpec(package, parser.chunk())) { + if (!ParseTypeSpec(package, parser.chunk(), package_id)) { return false; } break; case android::RES_TABLE_TYPE_TYPE: - if (!ParseType(package, parser.chunk())) { + if (!ParseType(package, parser.chunk(), package_id)) { return false; } break; @@ -276,7 +275,7 @@ bool BinaryResourceParser::ParsePackage(const ResChunk_header* chunk) { } bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package, - const ResChunk_header* chunk) { + const ResChunk_header* chunk, uint8_t package_id) { if (type_pool_.getError() != NO_ERROR) { diag_->Error(DiagMessage(source_) << "missing type string pool"); return false; @@ -317,14 +316,14 @@ bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package, const uint32_t* type_spec_flags = reinterpret_cast<const uint32_t*>( reinterpret_cast<uintptr_t>(type_spec) + util::DeviceToHost16(type_spec->header.headerSize)); for (size_t i = 0; i < entry_count; i++) { - ResourceId id(package->id.value_or_default(0x0), type_spec->id, static_cast<size_t>(i)); + ResourceId id(package_id, type_spec->id, static_cast<size_t>(i)); entry_type_spec_flags_[id] = util::DeviceToHost32(type_spec_flags[i]); } return true; } bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, - const ResChunk_header* chunk) { + const ResChunk_header* chunk, uint8_t package_id) { if (type_pool_.getError() != NO_ERROR) { diag_->Error(DiagMessage(source_) << "missing type string pool"); return false; @@ -354,13 +353,9 @@ bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, const std::string type_str = util::GetString(type_pool_, type->id - 1); const ResourceType* parsed_type = ParseResourceType(type_str); if (!parsed_type) { - // Be lenient on the name of the type if the table is lenient on resource validation. - bool log_error = table_->GetValidateResources(); - if (log_error) { - diag_->Error(DiagMessage(source_) << "invalid type name '" << type_str - << "' for type with ID " << type->id); - } - return !log_error; + diag_->Warn(DiagMessage(source_) + << "invalid type name '" << type_str << "' for type with ID " << type->id); + return true; } TypeVariant tv(type); @@ -372,7 +367,7 @@ bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, const ResourceName name(package->name, *parsed_type, util::GetString(key_pool_, util::DeviceToHost32(entry->key.index))); - const ResourceId res_id(package->id.value(), type->id, static_cast<uint16_t>(it.index())); + const ResourceId res_id(package_id, type->id, static_cast<uint16_t>(it.index())); std::unique_ptr<Value> resource_value; if (entry->flags & ResTable_entry::FLAG_COMPLEX) { @@ -392,17 +387,13 @@ bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, return false; } - if (!table_->AddResourceWithIdMangled(name, res_id, config, {}, std::move(resource_value), - diag_)) { - return false; - } + NewResourceBuilder res_builder(name); + res_builder.SetValue(std::move(resource_value), config) + .SetId(res_id, OnIdConflict::CREATE_ENTRY) + .SetAllowMangled(true); if (entry->flags & ResTable_entry::FLAG_PUBLIC) { - Visibility visibility; - visibility.level = Visibility::Level::kPublic; - if (!table_->SetVisibilityWithIdMangled(name, visibility, res_id, diag_)) { - return false; - } + res_builder.SetVisibility(Visibility{Visibility::Level::kPublic}); // Erase the ID from the map once processed, so that we don't mark the same symbol more than // once. @@ -415,6 +406,10 @@ bool BinaryResourceParser::ParseType(const ResourceTablePackage* package, if (cache_iter == id_index_.end()) { id_index_.insert({res_id, name}); } + + if (!table_->AddResource(res_builder.Build(), diag_)) { + return false; + } } return true; } @@ -472,7 +467,12 @@ bool BinaryResourceParser::ParseOverlayable(const ResChunk_header* chunk) { OverlayableItem overlayable_item(overlayable); overlayable_item.policies = policy_header->policy_flags; - if (!table_->SetOverlayable(iter->second, overlayable_item, diag_)) { + if (!table_->AddResource(NewResourceBuilder(iter->second) + .SetId(res_id, OnIdConflict::CREATE_ENTRY) + .SetOverlayable(std::move(overlayable_item)) + .SetAllowMangled(true) + .Build(), + diag_)) { return false; } } |