|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include <filesystem> |
| 6 | +#include <memory> |
| 7 | + |
| 8 | +#include "flutter/fml/backtrace.h" |
| 9 | +#include "flutter/fml/command_line.h" |
| 10 | +#include "flutter/fml/file.h" |
| 11 | +#include "flutter/fml/macros.h" |
| 12 | +#include "flutter/fml/mapping.h" |
| 13 | +#include "impeller/base/strings.h" |
| 14 | +#include "impeller/compiler/utilities.h" |
| 15 | +#include "impeller/scene/importer/importer.h" |
| 16 | +#include "impeller/scene/importer/switches.h" |
| 17 | +#include "impeller/scene/importer/types.h" |
| 18 | + |
| 19 | +#include "third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h" |
| 20 | + |
| 21 | +namespace impeller { |
| 22 | +namespace scene { |
| 23 | +namespace importer { |
| 24 | + |
| 25 | +// Sets the file access mode of the file at path 'p' to 0644. |
| 26 | +static bool SetPermissiveAccess(const std::filesystem::path& p) { |
| 27 | + auto permissions = |
| 28 | + std::filesystem::perms::owner_read | std::filesystem::perms::owner_write | |
| 29 | + std::filesystem::perms::group_read | std::filesystem::perms::others_read; |
| 30 | + std::error_code error; |
| 31 | + std::filesystem::permissions(p, permissions, error); |
| 32 | + if (error) { |
| 33 | + std::cerr << "Failed to set access on file '" << p |
| 34 | + << "': " << error.message() << std::endl; |
| 35 | + return false; |
| 36 | + } |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +bool Main(const fml::CommandLine& command_line) { |
| 41 | + fml::InstallCrashHandler(); |
| 42 | + if (command_line.HasOption("help")) { |
| 43 | + Switches::PrintHelp(std::cout); |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + Switches switches(command_line); |
| 48 | + if (!switches.AreValid(std::cerr)) { |
| 49 | + std::cerr << "Invalid flags specified." << std::endl; |
| 50 | + Switches::PrintHelp(std::cerr); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + auto source_file_mapping = |
| 55 | + fml::FileMapping::CreateReadOnly(switches.source_file_name); |
| 56 | + if (!source_file_mapping) { |
| 57 | + std::cerr << "Could not open input file." << std::endl; |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + fb::MeshT mesh; |
| 62 | + bool success = false; |
| 63 | + switch (switches.input_type) { |
| 64 | + case SourceType::kGLTF: |
| 65 | + success = ParseGLTF(*source_file_mapping, mesh); |
| 66 | + break; |
| 67 | + case SourceType::kUnknown: |
| 68 | + std::cerr << "Unknown input type." << std::endl; |
| 69 | + return false; |
| 70 | + } |
| 71 | + if (!success) { |
| 72 | + std::cerr << "Failed to parse input file." << std::endl; |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + flatbuffers::FlatBufferBuilder builder; |
| 77 | + builder.Finish(fb::Mesh::Pack(builder, &mesh)); |
| 78 | + |
| 79 | + auto output_file_name = std::filesystem::absolute( |
| 80 | + std::filesystem::current_path() / switches.output_file_name); |
| 81 | + fml::NonOwnedMapping mapping(builder.GetCurrentBufferPointer(), |
| 82 | + builder.GetSize()); |
| 83 | + if (!fml::WriteAtomically(*switches.working_directory, |
| 84 | + compiler::Utf8FromPath(output_file_name).c_str(), |
| 85 | + mapping)) { |
| 86 | + std::cerr << "Could not write file to " << switches.output_file_name |
| 87 | + << std::endl; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Tools that consume the geometry data expect the access mode to be 0644. |
| 92 | + if (!SetPermissiveAccess(output_file_name)) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace importer |
| 100 | +} // namespace scene |
| 101 | +} // namespace impeller |
| 102 | + |
| 103 | +int main(int argc, char const* argv[]) { |
| 104 | + return impeller::scene::importer::Main( |
| 105 | + fml::CommandLineFromPlatformOrArgcArgv(argc, argv)) |
| 106 | + ? EXIT_SUCCESS |
| 107 | + : EXIT_FAILURE; |
| 108 | +} |
0 commit comments