|
26 | 26 | import net.raphimc.javadowngrader.impl.classtransform.classprovider.LazyFileClassProvider; |
27 | 27 | import net.raphimc.javadowngrader.impl.classtransform.classprovider.PathClassProvider; |
28 | 28 | import net.raphimc.javadowngrader.impl.classtransform.util.ClassNameUtil; |
29 | | -import net.raphimc.javadowngrader.impl.classtransform.util.FileSystemUtil; |
30 | 29 | import net.raphimc.javadowngrader.runtime.RuntimeRoot; |
31 | 30 | import net.raphimc.javadowngrader.standalone.progress.MultiThreadedProgressBar; |
32 | 31 | import net.raphimc.javadowngrader.standalone.util.GeneralUtil; |
33 | | -import net.raphimc.javadowngrader.util.Constants; |
34 | 32 | import org.slf4j.Logger; |
35 | 33 | import org.slf4j.LoggerFactory; |
36 | 34 |
|
37 | 35 | import java.io.File; |
38 | 36 | import java.io.IOException; |
| 37 | +import java.io.InputStream; |
39 | 38 | import java.io.UncheckedIOException; |
40 | 39 | import java.net.URI; |
41 | 40 | import java.nio.file.FileSystem; |
42 | 41 | import java.nio.file.FileSystems; |
43 | 42 | import java.nio.file.Files; |
44 | 43 | import java.nio.file.Path; |
45 | 44 | import java.time.Duration; |
| 45 | +import java.util.Collection; |
46 | 46 | import java.util.Collections; |
47 | 47 | import java.util.List; |
48 | 48 | import java.util.Locale; |
49 | | -import java.util.concurrent.Callable; |
50 | | -import java.util.concurrent.ExecutorService; |
51 | | -import java.util.concurrent.Executors; |
52 | | -import java.util.concurrent.TimeUnit; |
| 49 | +import java.util.concurrent.*; |
53 | 50 | import java.util.stream.Collectors; |
54 | 51 | import java.util.stream.Stream; |
55 | 52 |
|
@@ -179,12 +176,17 @@ private static void doConversion( |
179 | 176 | try (FileSystem inFs = FileSystems.newFileSystem(inputFile.toPath(), null)) { |
180 | 177 | final Path inRoot = inFs.getRootDirectories().iterator().next(); |
181 | 178 |
|
| 179 | + final Collection<String> runtimeDeps = Collections.newSetFromMap(new ConcurrentHashMap<>()); |
182 | 180 | final TransformerManager transformerManager = new TransformerManager( |
183 | 181 | new PathClassProvider(inRoot, new LazyFileClassProvider(libraryPath, new BasicClassProvider())) |
184 | 182 | ); |
185 | | - transformerManager.addBytecodeTransformer(new JavaDowngraderTransformer( |
186 | | - transformerManager, targetVersion.getVersion(), c -> Files.isRegularFile(inRoot.resolve(ClassNameUtil.toClassFilename(c))) |
187 | | - )); |
| 183 | + transformerManager.addBytecodeTransformer( |
| 184 | + JavaDowngraderTransformer.builder(transformerManager) |
| 185 | + .targetVersion(targetVersion.getVersion()) |
| 186 | + .classFilter(c -> Files.isRegularFile(inRoot.resolve(ClassNameUtil.toClassFilename(c)))) |
| 187 | + .depCollector(runtimeDeps::add) |
| 188 | + .build() |
| 189 | + ); |
188 | 190 |
|
189 | 191 | try (FileSystem outFs = FileSystems.newFileSystem(new URI("jar:" + outputFile.toURI()), Collections.singletonMap("create", "true"))) { |
190 | 192 | final Path outRoot = outFs.getRootDirectories().iterator().next(); |
@@ -245,22 +247,21 @@ private static void doConversion( |
245 | 247 | throw new IllegalStateException("Thread pool didn't shutdown correctly"); |
246 | 248 | } |
247 | 249 |
|
248 | | - LOGGER.info("Copying runtime classes"); |
249 | | - try (FileSystem runtimeRootFs = FileSystemUtil.getOrCreateFileSystem(RuntimeRoot.class.getResource("").toURI())) { |
250 | | - final Path runtimeRoot = runtimeRootFs.getPath(Constants.JAVADOWNGRADER_RUNTIME_PACKAGE); |
251 | | - try (Stream<Path> stream = Files.walk(runtimeRoot)) { |
252 | | - stream.filter(Files::isRegularFile) |
253 | | - .filter(p -> !p.getFileName().toString().equals(Constants.JAVADOWNGRADER_RUNTIME_ROOT)) |
254 | | - .forEach(path -> { |
255 | | - final String relative = ClassNameUtil.slashName(runtimeRoot.relativize(path)); |
256 | | - final Path dest = outRoot.resolve(Constants.JAVADOWNGRADER_RUNTIME_PACKAGE + relative); |
257 | | - try { |
258 | | - Files.createDirectories(dest.getParent()); |
259 | | - Files.copy(path, dest); |
260 | | - } catch (IOException e) { |
261 | | - throw new UncheckedIOException(e); |
262 | | - } |
263 | | - }); |
| 250 | + LOGGER.info("Copying {} runtime class(es)", runtimeDeps.size()); |
| 251 | + for (final String runtimeDep : runtimeDeps) { |
| 252 | + final String classPath = runtimeDep.concat(".class"); |
| 253 | + LOGGER.debug("Copying {}", classPath); |
| 254 | + try (InputStream is = RuntimeRoot.class.getResourceAsStream("/" + classPath)) { |
| 255 | + if (is == null) { |
| 256 | + LOGGER.warn("Runtime class '{}' not found! Skipping.", runtimeDep); |
| 257 | + continue; |
| 258 | + } |
| 259 | + final Path dest = outRoot.resolve(classPath); |
| 260 | + final Path parent = dest.getParent(); |
| 261 | + if (parent != null) { |
| 262 | + Files.createDirectories(parent); |
| 263 | + } |
| 264 | + Files.copy(is, dest); |
264 | 265 | } |
265 | 266 | } |
266 | 267 | LOGGER.info("Writing final JAR"); |
|
0 commit comments