|
| 1 | +/* |
| 2 | + * Basic .unitypackage Viewer |
| 3 | + * Copyright (C) 2024 Michael Sabin |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package unitypackage.model; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.TreeMap; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.zip.GZIPInputStream; |
| 32 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 33 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 34 | + |
| 35 | +/** |
| 36 | + * Indexes a .unitypackage and provides methods to read assets out of it. |
| 37 | + */ |
| 38 | +public class UnityPackage { |
| 39 | + |
| 40 | + private static final String ROOT_ICON = ".icon.png"; |
| 41 | + |
| 42 | + private final File unitypackageFile; |
| 43 | + private final List<UnityAsset> unityAssetList; |
| 44 | + |
| 45 | + public UnityPackage(File unitypackageFile) throws IOException { |
| 46 | + this.unitypackageFile = unitypackageFile; |
| 47 | + |
| 48 | + // TODO are empty asset directories possible? |
| 49 | + // that would break this program |
| 50 | + |
| 51 | + TreeMap<String, UnityAssetBuilder> rootGuidDirectories = new TreeMap<>(); |
| 52 | + |
| 53 | + try (TarArchiveInputStream tarInput = getTarInputStream()) { |
| 54 | + |
| 55 | + boolean hasDotRootDirectory = false; |
| 56 | + |
| 57 | + TarArchiveEntry tarEntry; |
| 58 | + while ((tarEntry = tarInput.getNextEntry()) != null) { |
| 59 | + |
| 60 | + final String rawFilePathString = tarEntry.getName(); |
| 61 | + final boolean isDirectory = tarEntry.isDirectory(); |
| 62 | + |
| 63 | + Path rawPath = Paths.get(rawFilePathString); |
| 64 | + |
| 65 | + if (rawPath.getNameCount() == 1) { |
| 66 | + String shouldBeDirName = rawPath.getName(0).toString(); |
| 67 | + if (isDirectory) { |
| 68 | + if (".".equals(shouldBeDirName)) { |
| 69 | + hasDotRootDirectory = true; |
| 70 | + continue; |
| 71 | + } |
| 72 | + } else if (!ROOT_ICON.equals(shouldBeDirName)) { |
| 73 | + // I don't know if the root ".icon.png" would be next to a root "." or under it |
| 74 | + throw new RuntimeException("Found root path that is not a directory or " + ROOT_ICON + ": " + rawFilePathString); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + if (hasDotRootDirectory) { |
| 80 | + // Trim off the "." before continuing |
| 81 | + rawPath = rawPath.subpath(1, rawPath.getNameCount()); |
| 82 | + } |
| 83 | + |
| 84 | + Path rawPathParent = rawPath.getParent(); |
| 85 | + |
| 86 | + String guidDirectory; |
| 87 | + String fileName; |
| 88 | + |
| 89 | + if (isDirectory) { |
| 90 | + if (rawPathParent != null) { |
| 91 | + throw new RuntimeException("Found nested directory " + rawFilePathString); |
| 92 | + } |
| 93 | + guidDirectory = rawPath.toString(); |
| 94 | + fileName = null; |
| 95 | + } else { |
| 96 | + fileName = rawPath.getFileName().toString(); |
| 97 | + guidDirectory = rawPathParent == null ? null : rawPathParent.toString(); |
| 98 | + } |
| 99 | + |
| 100 | + if (guidDirectory == null) { |
| 101 | + if (fileName.equals(ROOT_ICON)) { |
| 102 | + // Image icon exists in the root |
| 103 | + // TODO do something with this |
| 104 | + // For now ignore it |
| 105 | + continue; |
| 106 | + } else { |
| 107 | + throw new RuntimeException("Found nested directory " + rawFilePathString); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + UnityAssetBuilder builder = rootGuidDirectories.get(guidDirectory); |
| 112 | + |
| 113 | + if (builder == null) { |
| 114 | + if (isDirectory) { |
| 115 | + builder = new UnityAssetBuilder(guidDirectory); |
| 116 | + } else { |
| 117 | + // Do .tar archives always put a directory definition before any files under it? |
| 118 | + // In any case, be flexible. |
| 119 | + builder = new UnityAssetBuilder(guidDirectory, fileName, tarEntry, tarInput); |
| 120 | + } |
| 121 | + rootGuidDirectories.put(guidDirectory, builder); |
| 122 | + } else { |
| 123 | + if (isDirectory) |
| 124 | + builder.assertGuidMatchesDirectoryName(guidDirectory); |
| 125 | + else |
| 126 | + builder.addFileFoundInDirectory(guidDirectory, fileName, tarEntry, tarInput); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + List<UnityAsset> assets = rootGuidDirectories |
| 132 | + .values() |
| 133 | + .stream() |
| 134 | + .map(tad -> tad.makeUnityAsset()) |
| 135 | + .collect(Collectors.toList()); |
| 136 | + |
| 137 | + unityAssetList = Collections.unmodifiableList(assets); |
| 138 | + } |
| 139 | + |
| 140 | + public File getUnitypackageFile() { |
| 141 | + return unitypackageFile; |
| 142 | + } |
| 143 | + |
| 144 | + public List<UnityAsset> getUnityAssetList() { |
| 145 | + return unityAssetList; |
| 146 | + } |
| 147 | + |
| 148 | + final public TarArchiveInputStream getTarInputStream() throws IOException { |
| 149 | + return new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(unitypackageFile))); |
| 150 | + } |
| 151 | + |
| 152 | + public UnityArchiveInputStream getUnityArchiveInputStream() throws IOException { |
| 153 | + return new UnityArchiveInputStream(this); |
| 154 | + } |
| 155 | + |
| 156 | + public InputStream getFileStream(UnityAsset assetToExtract) throws IOException { |
| 157 | + |
| 158 | + UnityArchiveInputStream unityInputStream = new UnityArchiveInputStream(getTarInputStream(), |
| 159 | + Collections.singletonList(assetToExtract)); |
| 160 | + |
| 161 | + UnityAsset assetFound = unityInputStream.getNextEntry(); |
| 162 | + |
| 163 | + // Sanity check |
| 164 | + if (assetToExtract != assetFound) { |
| 165 | + throw new IllegalArgumentException("This should never happen"); |
| 166 | + } |
| 167 | + |
| 168 | + return unityInputStream; |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments