Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scenec_main.cc
Go to the documentation of this file.
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"
16#include "impeller/scene/importer/scene_flatbuffers.h"
19
20#include "third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h"
21
22namespace impeller {
23namespace scene {
24namespace importer {
25
26// Sets the file access mode of the file at path 'p' to 0644.
27static bool SetPermissiveAccess(const std::filesystem::path& p) {
28 auto permissions =
29 std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
30 std::filesystem::perms::group_read | std::filesystem::perms::others_read;
31 std::error_code error;
32 std::filesystem::permissions(p, permissions, error);
33 if (error) {
34 std::cerr << "Failed to set access on file '" << p
35 << "': " << error.message() << std::endl;
36 return false;
37 }
38 return true;
39}
40
41bool Main(const fml::CommandLine& command_line) {
43 if (command_line.HasOption("help")) {
44 Switches::PrintHelp(std::cout);
45 return true;
46 }
47
48 Switches switches(command_line);
49 if (!switches.AreValid(std::cerr)) {
50 std::cerr << "Invalid flags specified." << std::endl;
51 Switches::PrintHelp(std::cerr);
52 return false;
53 }
54
55 auto source_file_mapping =
57 if (!source_file_mapping) {
58 std::cerr << "Could not open input file." << std::endl;
59 return false;
60 }
61
62 fb::SceneT scene;
63 bool success = false;
64 switch (switches.input_type) {
66 success = ParseGLTF(*source_file_mapping, scene);
67 break;
69 std::cerr << "Unknown input type." << std::endl;
70 return false;
71 }
72 if (!success) {
73 std::cerr << "Failed to parse input file." << std::endl;
74 return false;
75 }
76
77 flatbuffers::FlatBufferBuilder builder;
78 builder.Finish(fb::Scene::Pack(builder, &scene), fb::SceneIdentifier());
79
80 auto output_file_name = std::filesystem::absolute(
81 std::filesystem::current_path() / switches.output_file_name);
82 fml::NonOwnedMapping mapping(builder.GetCurrentBufferPointer(),
83 builder.GetSize());
85 compiler::Utf8FromPath(output_file_name).c_str(),
86 mapping)) {
87 std::cerr << "Could not write file to " << switches.output_file_name
88 << std::endl;
89 return false;
90 }
91
92 // Tools that consume the geometry data expect the access mode to be 0644.
93 if (!SetPermissiveAccess(output_file_name)) {
94 return false;
95 }
96
97 return true;
98}
99
100} // namespace importer
101} // namespace scene
102} // namespace impeller
103
104int main(int argc, char const* argv[]) {
107 ? EXIT_SUCCESS
108 : EXIT_FAILURE;
109}
bool HasOption(std::string_view name, size_t *index=nullptr) const
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition mapping.cc:20
const uint8_t uint32_t uint32_t GError ** error
char ** argv
Definition library.h:9
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
void InstallCrashHandler()
Definition backtrace.cc:126
CommandLine CommandLineFromPlatformOrArgcArgv(int argc, const char *const *argv)
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition utilities.cc:30
bool Main(const fml::CommandLine &command_line)
static bool SetPermissiveAccess(const std::filesystem::path &p)
bool ParseGLTF(const fml::Mapping &source_mapping, fb::SceneT &out_scene)
Definition main.py:1
std::shared_ptr< fml::UniqueFD > working_directory
Definition switches.h:21
static void PrintHelp(std::ostream &stream)
Definition switches.cc:24
bool AreValid(std::ostream &explain) const
Definition switches.cc:67