|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright 2020 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +r"""A script to import a .unitypackage into a project without Unity. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + import_unity_package.py --projects=path/to/unity/project \ |
| 21 | + --packages=mypackage.unitypackage |
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +import shutil |
| 26 | +import tarfile |
| 27 | +import tempfile |
| 28 | +from absl import app |
| 29 | +from absl import flags |
| 30 | +from absl import logging |
| 31 | + |
| 32 | +FLAGS = flags.FLAGS |
| 33 | + |
| 34 | +flags.DEFINE_multi_string( |
| 35 | + "projects", None, "Paths to Unity project directories to unpack packages " |
| 36 | + "into. This should be the directory that contains the Assets directory, " |
| 37 | + "i.e my/project not my/project/Assets") |
| 38 | +flags.DEFINE_multi_string( |
| 39 | + "packages", None, "Set of packages to unpack into a project. Packages are " |
| 40 | + "unpacked in the order they're specified.") |
| 41 | + |
| 42 | + |
| 43 | +def files_exist(paths_to_check): |
| 44 | + """Determine whether the specified files exist. |
| 45 | +
|
| 46 | + Args: |
| 47 | + paths_to_check: List of files to check whether they exist. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + List of files that do not exist. |
| 51 | + """ |
| 52 | + return [p for p in paths_to_check if not os.path.isfile(os.path.realpath(p))] |
| 53 | + |
| 54 | + |
| 55 | +def directories_exist(paths_to_check): |
| 56 | + """Determine whether the specified directories exist. |
| 57 | +
|
| 58 | + Args: |
| 59 | + paths_to_check: List of directories to check whether they exist. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + List of directories that do not exist. |
| 63 | + """ |
| 64 | + return [p for p in paths_to_check if not os.path.isdir(os.path.realpath(p))] |
| 65 | + |
| 66 | + |
| 67 | +def unpack_to_directory(directory, packages): |
| 68 | + """Unpack a set of .unitypackage files to a directory. |
| 69 | +
|
| 70 | + Args: |
| 71 | + directory: Directory to unpack into. |
| 72 | + packages: List of .unitypackage filesname to unpack. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + Dictionary containing a list of files that could not be extracted, keyed by |
| 76 | + package archive filename. |
| 77 | + """ |
| 78 | + ignored_files_by_package = {} |
| 79 | + for unitypackage in packages: |
| 80 | + with tarfile.open(unitypackage) as unitypackage_file: |
| 81 | + member_names = unitypackage_file.getnames() |
| 82 | + guid_to_path = {} |
| 83 | + extracted_files = set() |
| 84 | + |
| 85 | + # Map each asset GUID to an extract path the path of each extracted asset. |
| 86 | + for filename in member_names: |
| 87 | + if os.path.basename(filename) == "pathname": |
| 88 | + guid = os.path.dirname(filename) |
| 89 | + with unitypackage_file.extractfile(filename) as pathname_file: |
| 90 | + pathname = pathname_file.read().decode("utf8").strip() |
| 91 | + if guid and pathname: |
| 92 | + extracted_files.add(filename) |
| 93 | + guid_to_path[guid] = pathname |
| 94 | + |
| 95 | + # Extract each asset to the appropriate path in the output directory. |
| 96 | + for filename in member_names: |
| 97 | + basename = os.path.basename(filename) |
| 98 | + if basename == "asset" or basename == "asset.meta": |
| 99 | + guid = os.path.dirname(filename) |
| 100 | + if guid: |
| 101 | + pathname = guid_to_path.get(guid) |
| 102 | + if pathname: |
| 103 | + with unitypackage_file.extractfile(filename) as member_file: |
| 104 | + extension = os.path.splitext(basename)[1] |
| 105 | + output_filename = os.path.join(directory, pathname + extension) |
| 106 | + os.makedirs(os.path.dirname(output_filename), exist_ok=True) |
| 107 | + with open(output_filename, "wb") as output_file: |
| 108 | + shutil.copyfileobj(member_file, output_file) |
| 109 | + extracted_files.add(filename) |
| 110 | + |
| 111 | + # Returns the list of files that could not be extracted in the archive's |
| 112 | + # order. |
| 113 | + ignored_files = [] |
| 114 | + for member in member_names: |
| 115 | + if member not in extracted_files: |
| 116 | + if unitypackage_file.getmember(member).isfile(): |
| 117 | + ignored_files.append(member) |
| 118 | + if ignored_files: |
| 119 | + ignored_files_by_package[unitypackage] = ignored_files |
| 120 | + return ignored_files_by_package |
| 121 | + |
| 122 | + |
| 123 | +def main(unused_argv): |
| 124 | + """Unpacks a set of .unitypackage files into a set of Unity projects. |
| 125 | +
|
| 126 | + Args: |
| 127 | + unused_argv: Not used. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + 0 if successful, 1 otherwise. |
| 131 | + """ |
| 132 | + # Make sure all input files and output directories exist. |
| 133 | + missing_packages = files_exist(FLAGS.packages) |
| 134 | + missing_projects = directories_exist(FLAGS.projects) |
| 135 | + if missing_packages: |
| 136 | + logging.error("Specified packages %s not found.", missing_packages) |
| 137 | + if missing_projects: |
| 138 | + logging.error("Specified projects %s not found.", missing_projects) |
| 139 | + if missing_packages or missing_projects: |
| 140 | + return 1 |
| 141 | + |
| 142 | + with tempfile.TemporaryDirectory() as unpack_directory: |
| 143 | + # Unpack all packages into a single directory. |
| 144 | + for package, files in unpack_to_directory(unpack_directory, FLAGS.packages): |
| 145 | + logging.error("Failed to unpack files %s from package %s", package, files) |
| 146 | + |
| 147 | + # Copy unpacked packages into each project. |
| 148 | + for project in FLAGS.projects: |
| 149 | + for dirname, _, filenames in os.walk(unpack_directory): |
| 150 | + for filename in filenames: |
| 151 | + source_filename = os.path.join(dirname, filename) |
| 152 | + relative_filename = source_filename[len(unpack_directory) + 1:] |
| 153 | + if os.path.isfile(source_filename): |
| 154 | + target_filename = os.path.join(project, relative_filename) |
| 155 | + os.makedirs(os.path.dirname(target_filename), exist_ok=True) |
| 156 | + shutil.copyfile(source_filename, target_filename) |
| 157 | + return 0 |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + flags.mark_flag_as_required("projects") |
| 162 | + flags.mark_flag_as_required("packages") |
| 163 | + app.run(main) |
0 commit comments