|
| 1 | +// Copyright 2014 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 | +import 'dart:math' as math; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'package:meta/meta.dart'; |
| 9 | +import 'package:pool/pool.dart'; |
| 10 | +import 'package:process/process.dart'; |
| 11 | + |
| 12 | +import '../../artifacts.dart'; |
| 13 | +import '../../base/error_handling_io.dart'; |
| 14 | +import '../../base/file_system.dart'; |
| 15 | +import '../../base/io.dart'; |
| 16 | +import '../../base/logger.dart'; |
| 17 | +import '../../convert.dart'; |
| 18 | +import '../../devfs.dart'; |
| 19 | +import '../build_system.dart'; |
| 20 | + |
| 21 | +/// A wrapper around [SceneImporter] to support hot reload of 3D models. |
| 22 | +class DevelopmentSceneImporter { |
| 23 | + DevelopmentSceneImporter({ |
| 24 | + required SceneImporter sceneImporter, |
| 25 | + required FileSystem fileSystem, |
| 26 | + @visibleForTesting math.Random? random, |
| 27 | + }) : _sceneImporter = sceneImporter, |
| 28 | + _fileSystem = fileSystem, |
| 29 | + _random = random ?? math.Random(); |
| 30 | + |
| 31 | + final SceneImporter _sceneImporter; |
| 32 | + final FileSystem _fileSystem; |
| 33 | + final Pool _compilationPool = Pool(4); |
| 34 | + final math.Random _random; |
| 35 | + |
| 36 | + /// Recompile the input ipscene and return a devfs content that should be |
| 37 | + /// synced to the attached device in its place. |
| 38 | + Future<DevFSContent?> reimportScene(DevFSContent inputScene) async { |
| 39 | + final File output = _fileSystem.systemTempDirectory.childFile('${_random.nextDouble()}.temp'); |
| 40 | + late File inputFile; |
| 41 | + bool cleanupInput = false; |
| 42 | + Uint8List result; |
| 43 | + PoolResource? resource; |
| 44 | + try { |
| 45 | + resource = await _compilationPool.request(); |
| 46 | + if (inputScene is DevFSFileContent) { |
| 47 | + inputFile = inputScene.file as File; |
| 48 | + } else { |
| 49 | + inputFile = _fileSystem.systemTempDirectory.childFile('${_random.nextDouble()}.temp'); |
| 50 | + inputFile.writeAsBytesSync(await inputScene.contentsAsBytes()); |
| 51 | + cleanupInput = true; |
| 52 | + } |
| 53 | + final bool success = await _sceneImporter.importScene( |
| 54 | + input: inputFile, |
| 55 | + outputPath: output.path, |
| 56 | + fatal: false, |
| 57 | + ); |
| 58 | + if (!success) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + result = output.readAsBytesSync(); |
| 62 | + } finally { |
| 63 | + resource?.release(); |
| 64 | + ErrorHandlingFileSystem.deleteIfExists(output); |
| 65 | + if (cleanupInput) { |
| 66 | + ErrorHandlingFileSystem.deleteIfExists(inputFile); |
| 67 | + } |
| 68 | + } |
| 69 | + return DevFSByteContent(result); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// A class the wraps the functionality of the Impeller Scene importer scenec. |
| 74 | +class SceneImporter { |
| 75 | + SceneImporter({ |
| 76 | + required ProcessManager processManager, |
| 77 | + required Logger logger, |
| 78 | + required FileSystem fileSystem, |
| 79 | + required Artifacts artifacts, |
| 80 | + }) : _processManager = processManager, |
| 81 | + _logger = logger, |
| 82 | + _fs = fileSystem, |
| 83 | + _artifacts = artifacts; |
| 84 | + |
| 85 | + final ProcessManager _processManager; |
| 86 | + final Logger _logger; |
| 87 | + final FileSystem _fs; |
| 88 | + final Artifacts _artifacts; |
| 89 | + |
| 90 | + /// The [Source] inputs that targets using this should depend on. |
| 91 | + /// |
| 92 | + /// See [Target.inputs]. |
| 93 | + static const List<Source> inputs = <Source>[ |
| 94 | + Source.pattern( |
| 95 | + '{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/scene_importer.dart'), |
| 96 | + Source.hostArtifact(HostArtifact.scenec), |
| 97 | + ]; |
| 98 | + |
| 99 | + /// Calls scenec, which transforms the [input] 3D model into an imported |
| 100 | + /// ipscene at [outputPath]. |
| 101 | + /// |
| 102 | + /// All parameters are required. |
| 103 | + /// |
| 104 | + /// If the scene importer subprocess fails, it will print the stdout and |
| 105 | + /// stderr to the log and throw a [SceneImporterException]. Otherwise, it |
| 106 | + /// will return true. |
| 107 | + Future<bool> importScene({ |
| 108 | + required File input, |
| 109 | + required String outputPath, |
| 110 | + bool fatal = true, |
| 111 | + }) async { |
| 112 | + final File scenec = _fs.file( |
| 113 | + _artifacts.getHostArtifact(HostArtifact.scenec), |
| 114 | + ); |
| 115 | + if (!scenec.existsSync()) { |
| 116 | + throw SceneImporterException._( |
| 117 | + 'The scenec utility is missing at "${scenec.path}". ' |
| 118 | + 'Run "flutter doctor".', |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + final List<String> cmd = <String>[ |
| 123 | + scenec.path, |
| 124 | + '--input=${input.path}', |
| 125 | + '--output=$outputPath', |
| 126 | + ]; |
| 127 | + _logger.printTrace('scenec command: $cmd'); |
| 128 | + final Process scenecProcess = await _processManager.start(cmd); |
| 129 | + final int code = await scenecProcess.exitCode; |
| 130 | + if (code != 0) { |
| 131 | + final String stdout = await utf8.decodeStream(scenecProcess.stdout); |
| 132 | + final String stderr = await utf8.decodeStream(scenecProcess.stderr); |
| 133 | + _logger.printTrace(stdout); |
| 134 | + _logger.printError(stderr); |
| 135 | + if (fatal) { |
| 136 | + throw SceneImporterException._( |
| 137 | + 'Scene import of "${input.path}" to "$outputPath" ' |
| 138 | + 'failed with exit code $code.\n' |
| 139 | + 'scenec stdout:\n$stdout\n' |
| 140 | + 'scenec stderr:\n$stderr', |
| 141 | + ); |
| 142 | + } |
| 143 | + return false; |
| 144 | + } |
| 145 | + return true; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +class SceneImporterException implements Exception { |
| 150 | + SceneImporterException._(this.message); |
| 151 | + |
| 152 | + final String message; |
| 153 | + |
| 154 | + @override |
| 155 | + String toString() => 'SceneImporterException: $message\n\n'; |
| 156 | +} |
0 commit comments