Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f2e65f67 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: Cleanup proto classes/methods and add XML serialization"

parents 078208d2 8cdca1bd
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -106,9 +106,8 @@ cc_library_host_static {
        "optimize/ResourceDeduper.cpp",
        "optimize/VersionCollapser.cpp",
        "process/SymbolTable.cpp",
        "proto/ProtoHelpers.cpp",
        "proto/TableProtoDeserializer.cpp",
        "proto/TableProtoSerializer.cpp",
        "proto/ProtoDeserialize.cpp",
        "proto/ProtoSerialize.cpp",
        "split/TableSplitter.cpp",
        "text/Unicode.cpp",
        "text/Utf8Iterator.cpp",
+15 −12
Original line number Diff line number Diff line
@@ -248,8 +248,9 @@ static bool CompileTable(IAaptContext* context, const CompileOptions& options,
    // ZeroCopyOutputStream interface.
    CopyingOutputStreamAdaptor copying_adaptor(writer);

    std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(&table);
    if (!pb_table->SerializeToZeroCopyStream(&copying_adaptor)) {
    pb::ResourceTable pb_table;
    SerializeTableToPb(table, &pb_table);
    if (!pb_table.SerializeToZeroCopyStream(&copying_adaptor)) {
      context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
      return false;
    }
@@ -282,9 +283,10 @@ static bool WriteHeaderAndBufferToWriter(const StringPiece& output_path, const R
    // Number of CompiledFiles.
    output_stream.WriteLittleEndian32(1);

    std::unique_ptr<pb::internal::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
    output_stream.WriteCompiledFile(compiled_file.get());
    output_stream.WriteData(&buffer);
    pb::internal::CompiledFile pb_compiled_file;
    SerializeCompiledFileToPb(file, &pb_compiled_file);
    output_stream.WriteCompiledFile(pb_compiled_file);
    output_stream.WriteData(buffer);

    if (output_stream.HadError()) {
      diag->Error(DiagMessage(output_path) << "failed to write data");
@@ -319,8 +321,9 @@ static bool WriteHeaderAndMmapToWriter(const StringPiece& output_path, const Res
    // Number of CompiledFiles.
    output_stream.WriteLittleEndian32(1);

    std::unique_ptr<pb::internal::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
    output_stream.WriteCompiledFile(compiled_file.get());
    pb::internal::CompiledFile pb_compiled_file;
    SerializeCompiledFileToPb(file, &pb_compiled_file);
    output_stream.WriteCompiledFile(pb_compiled_file);
    output_stream.WriteData(map.getDataPtr(), map.getDataLength());

    if (output_stream.HadError()) {
@@ -346,13 +349,13 @@ static bool FlattenXmlToOutStream(IAaptContext* context, const StringPiece& outp
    return false;
  }

  std::unique_ptr<pb::internal::CompiledFile> pb_compiled_file =
      SerializeCompiledFileToPb(xmlres->file);
  out->WriteCompiledFile(pb_compiled_file.get());
  out->WriteData(&buffer);
  pb::internal::CompiledFile pb_compiled_file;
  SerializeCompiledFileToPb(xmlres->file, &pb_compiled_file);
  out->WriteCompiledFile(pb_compiled_file);
  out->WriteData(buffer);

  if (out->HadError()) {
    context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write data");
    context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write XML data");
    return false;
  }
  return true;
+68 −74
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@
#include "Flags.h"
#include "io/ZipArchive.h"
#include "process/IResourceTableConsumer.h"
#include "proto/ProtoSerialize.h"
#include "proto/ProtoDeserialize.h"
#include "unflatten/BinaryResourceParser.h"
#include "util/Files.h"

@@ -33,29 +33,28 @@ namespace aapt {

bool DumpCompiledFile(const pb::internal::CompiledFile& pb_file, const void* data, size_t len,
                      const Source& source, IAaptContext* context) {
  std::unique_ptr<ResourceFile> file =
      DeserializeCompiledFileFromPb(pb_file, source, context->GetDiagnostics());
  if (!file) {
    context->GetDiagnostics()->Warn(DiagMessage() << "failed to read compiled file");
  ResourceFile file;
  std::string error;
  if (!DeserializeCompiledFileFromPb(pb_file, &file, &error)) {
    context->GetDiagnostics()->Warn(DiagMessage(source)
                                    << "failed to read compiled file: " << error);
    return false;
  }

  std::cout << "Resource: " << file->name << "\n"
            << "Config:   " << file->config << "\n"
            << "Source:   " << file->source << "\n";
  std::cout << "Resource: " << file.name << "\n"
            << "Config:   " << file.config << "\n"
            << "Source:   " << file.source << "\n";
  return true;
}

bool TryDumpFile(IAaptContext* context, const std::string& file_path) {
  std::unique_ptr<ResourceTable> table;

  std::string err;
  std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(file_path, &err);
  if (zip) {
    io::IFile* file = zip->FindFile("resources.arsc.flat");
    if (file) {
    ResourceTable table;
    if (io::IFile* file = zip->FindFile("resources.arsc.flat")) {
      std::unique_ptr<io::IData> data = file->OpenAsData();
      if (!data) {
      if (data == nullptr) {
        context->GetDiagnostics()->Error(DiagMessage(file_path)
                                         << "failed to open resources.arsc.flat");
        return false;
@@ -67,33 +66,33 @@ bool TryDumpFile(IAaptContext* context, const std::string& file_path) {
        return false;
      }

      table = DeserializeTableFromPb(pb_table, Source(file_path), context->GetDiagnostics());
      if (!table) {
      ResourceTable table;
      if (!DeserializeTableFromPb(pb_table, &table, &err)) {
        context->GetDiagnostics()->Error(DiagMessage(file_path)
                                         << "failed to parse table: " << err);
        return false;
      }
    }

    if (!table) {
      file = zip->FindFile("resources.arsc");
      if (file) {
    } else if (io::IFile* file = zip->FindFile("resources.arsc")) {
      std::unique_ptr<io::IData> data = file->OpenAsData();
      if (!data) {
          context->GetDiagnostics()->Error(DiagMessage(file_path)
                                           << "failed to open resources.arsc");
        context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.arsc");
        return false;
      }

        table = util::make_unique<ResourceTable>();
        BinaryResourceParser parser(context, table.get(), Source(file_path), data->data(),
                                    data->size());
      BinaryResourceParser parser(context, &table, Source(file_path), data->data(), data->size());
      if (!parser.Parse()) {
        return false;
      }
    }
    }

    DebugPrintTableOptions options;
    options.show_sources = true;
    Debug::PrintTable(&table, options);
    return true;
  }

  if (!table) {
  err.clear();

  Maybe<android::FileMap> file = file::MmapPath(file_path, &err);
  if (!file) {
    context->GetDiagnostics()->Error(DiagMessage(file_path) << err);
@@ -102,16 +101,20 @@ bool TryDumpFile(IAaptContext* context, const std::string& file_path) {

  android::FileMap* file_map = &file.value();

    // Try as a compiled table.
  // Check to see if this is a loose ResourceTable.
  pb::ResourceTable pb_table;
  if (pb_table.ParseFromArray(file_map->getDataPtr(), file_map->getDataLength())) {
      table = DeserializeTableFromPb(pb_table, Source(file_path), context->GetDiagnostics());
    ResourceTable table;
    if (DeserializeTableFromPb(pb_table, &table, &err)) {
      DebugPrintTableOptions options;
      options.show_sources = true;
      Debug::PrintTable(&table, options);
      return true;
    }
  }

    if (!table) {
  // Try as a compiled file.
  CompiledFileInputStream input(file_map->getDataPtr(), file_map->getDataLength());

  uint32_t num_files = 0;
  if (!input.ReadLittleEndian32(&num_files)) {
    return false;
@@ -135,15 +138,6 @@ bool TryDumpFile(IAaptContext* context, const std::string& file_path) {
      return false;
    }
  }
    }
  }

  if (table) {
    DebugPrintTableOptions options;
    options.show_sources = true;
    Debug::PrintTable(table.get(), options);
  }

  return true;
}

+14 −9
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@
#include "optimize/VersionCollapser.h"
#include "process/IResourceTableConsumer.h"
#include "process/SymbolTable.h"
#include "proto/ProtoDeserialize.h"
#include "proto/ProtoSerialize.h"
#include "split/TableSplitter.h"
#include "unflatten/BinaryResourceParser.h"
@@ -281,8 +282,10 @@ static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, cons
    return {};
  }

  std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag);
  if (!table) {
  std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
  std::string error;
  if (!DeserializeTableFromPb(pb_table, table.get(), &error)) {
    diag->Error(DiagMessage(source) << "invalid compiled table: " << error);
    return {};
  }
  return table;
@@ -917,8 +920,9 @@ class LinkCommand {
  }

  bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) {
    std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table);
    return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer);
    pb::ResourceTable pb_table;
    SerializeTableToPb(*table, &pb_table);
    return io::CopyProtoToArchive(context_, &pb_table, "resources.arsc.flat", 0, writer);
  }

  bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
@@ -1397,14 +1401,15 @@ class LinkCommand {
          return false;
        }

        std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb(
            compiled_file, file->GetSource(), context_->GetDiagnostics());
        if (!resource_file) {
        ResourceFile resource_file;
        std::string error;
        if (!DeserializeCompiledFileFromPb(compiled_file, &resource_file, &error)) {
          context_->GetDiagnostics()->Error(DiagMessage(src)
                                            << "failed to read compiled header: " << error);
          return false;
        }

        if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(),
                               override)) {
        if (!MergeCompiledFile(file->CreateFileSegment(offset, len), &resource_file, override)) {
          return false;
        }
      }
+918 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading