Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Sep 17, 2024
1 parent a7b11ee commit 83b5a19
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final void afterEvaluation() {
// Decompiler will be passed to the constructor of GenerateSourcesTask
project.getTasks().register(taskName, GenerateSourcesTask.class, options).configure(task -> {
task.getInputJarName().set(minecraftJar.getName());
task.getOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", minecraftJar.getPath()));
task.getSourcesOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", minecraftJar.getPath()));

task.dependsOn(project.getTasks().named("validateAccessWidener"));
task.setDescription("Decompile minecraft using %s.".formatted(decompilerName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void afterEvaluation() {

final TaskProvider<Task> commonDecompileTask = createDecompileTasks("Common", task -> {
task.getInputJarName().set(commonJar.getName());
task.getOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", commonJar.getPath()));
task.getSourcesOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", commonJar.getPath()));

if (mappingConfiguration.hasUnpickDefinitions()) {
File unpickJar = new File(extension.getMappingConfiguration().mappingsWorkingDir().toFile(), "minecraft-common-unpicked.jar");
Expand All @@ -65,7 +65,7 @@ public void afterEvaluation() {

final TaskProvider<Task> clientOnlyDecompileTask = createDecompileTasks("ClientOnly", task -> {
task.getInputJarName().set(clientOnlyJar.getName());
task.getOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", clientOnlyJar.getPath()));
task.getSourcesOutputJar().fileValue(GenerateSourcesTask.getJarFileWithSuffix("-sources.jar", clientOnlyJar.getPath()));

if (mappingConfiguration.hasUnpickDefinitions()) {
File unpickJar = new File(extension.getMappingConfiguration().mappingsWorkingDir().toFile(), "minecraft-clientonly-unpicked.jar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -127,7 +128,7 @@ protected boolean hasBackupJars(List<MinecraftJar> minecraftJars) {

protected void createBackupJars(List<MinecraftJar> minecraftJars) throws IOException {
for (MinecraftJar minecraftJar : minecraftJars) {
Files.copy(minecraftJar.getPath(), getBackupJarPath(minecraftJar));
Files.copy(minecraftJar.getPath(), getBackupJarPath(minecraftJar), StandardCopyOption.REPLACE_EXISTING);
}
}

Expand Down Expand Up @@ -261,6 +262,7 @@ public static void configureSplitRemapper(RemappedJars remappedJars, TinyRemappe
private void cleanOutputs(List<RemappedJars> remappedJars) throws IOException {
for (RemappedJars remappedJar : remappedJars) {
Files.deleteIfExists(remappedJar.outputJarPath());
Files.deleteIfExists(getBackupJarPath(remappedJar.outputJar()));
}
}

Expand Down
162 changes: 57 additions & 105 deletions src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -61,6 +59,7 @@
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -80,17 +79,14 @@
import net.fabricmc.loom.api.decompilers.DecompilationMetadata;
import net.fabricmc.loom.api.decompilers.DecompilerOptions;
import net.fabricmc.loom.api.decompilers.LoomDecompiler;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.ConfigContextImpl;
import net.fabricmc.loom.configuration.processors.MappingProcessorContextImpl;
import net.fabricmc.loom.configuration.processors.MinecraftJarProcessorManager;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
import net.fabricmc.loom.configuration.providers.minecraft.mapped.AbstractMappedMinecraftProvider;
import net.fabricmc.loom.decompilers.ClassLineNumbers;
import net.fabricmc.loom.decompilers.LineNumberRemapper;
import net.fabricmc.loom.decompilers.cache.CachedData;
import net.fabricmc.loom.decompilers.cache.CachedFileStoreImpl;
import net.fabricmc.loom.decompilers.cache.CachedJarProcessor;
import net.fabricmc.loom.task.service.SourceMappingsService;
import net.fabricmc.loom.util.Checksum;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.ExceptionUtil;
Expand All @@ -104,9 +100,6 @@
import net.fabricmc.loom.util.ipc.IPCClient;
import net.fabricmc.loom.util.ipc.IPCServer;
import net.fabricmc.loom.util.service.ScopedServiceFactory;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
import net.fabricmc.mappingio.format.tiny.Tiny2FileWriter;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

@DisableCachingByDefault
Expand All @@ -121,16 +114,20 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
public abstract Property<String> getInputJarName();

@InputFiles // Only contains a single file
protected abstract ConfigurableFileCollection getInputJar();
protected abstract ConfigurableFileCollection getClassesInputJar();

@InputFiles
public abstract ConfigurableFileCollection getClasspath();
protected abstract ConfigurableFileCollection getClasspath();

@InputFiles
protected abstract ConfigurableFileCollection getMinecraftCompileLibraries();

@OutputFile
public abstract RegularFileProperty getOutputJar();
public abstract RegularFileProperty getSourcesOutputJar();

// Contains the remapped linenumbers
@OutputFile
protected abstract RegularFileProperty getClassesOutputJar();

// Unpick
@InputFile
Expand Down Expand Up @@ -167,6 +164,11 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
@ApiStatus.Experimental
public abstract Property<Boolean> getResetCache();

// Internal inputs
@ApiStatus.Internal
@Nested
protected abstract Property<SourceMappingsService.Options> getMappings();

// Internal outputs
@ApiStatus.Internal
@Internal
Expand All @@ -193,7 +195,7 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
public GenerateSourcesTask(DecompilerOptions decompilerOptions) {
this.decompilerOptions = decompilerOptions;

getInputJar().setFrom(getInputJarName().map(minecraftJarName -> {
getClassesInputJar().setFrom(getInputJarName().map(minecraftJarName -> {
final List<MinecraftJar> minecraftJars = getExtension().getNamedMinecraftProvider().getMinecraftJars();

for (MinecraftJar minecraftJar : minecraftJars) {
Expand All @@ -204,6 +206,8 @@ public GenerateSourcesTask(DecompilerOptions decompilerOptions) {
throw new IllegalStateException("Input minecraft jar not found at: " + backupJarPath);
}

getClassesOutputJar().set(minecraftJar.toFile());

return backupJarPath.toFile();
}
}
Expand All @@ -222,6 +226,8 @@ public GenerateSourcesTask(DecompilerOptions decompilerOptions) {

getUseCache().convention(true);
getResetCache().convention(getExtension().refreshDeps());

getMappings().set(SourceMappingsService.create(getProject()));
}

@TaskAction
Expand Down Expand Up @@ -267,8 +273,9 @@ public void run() throws IOException {
}

private void runWithCache(Path cacheRoot) throws IOException {
final Path inputJar = getInputJar().getSingleFile().toPath();
final Path outputJar = getOutputJar().get().getAsFile().toPath();
final Path classesInputJar = getClassesInputJar().getSingleFile().toPath();
final Path sourcesOutputJar = getSourcesOutputJar().get().getAsFile().toPath();
final Path classesOutputJar = getClassesOutputJar().get().getAsFile().toPath();
final var cacheRules = new CachedFileStoreImpl.CacheRules(50_000, Duration.ofDays(90));
final var decompileCache = new CachedFileStoreImpl<>(cacheRoot, CachedData.SERIALIZER, cacheRules);
final String cacheKey = getCacheKey();
Expand All @@ -278,7 +285,7 @@ private void runWithCache(Path cacheRoot) throws IOException {
getLogger().info("Decompile cache key: {}", cacheKey);

try (var timer = new Timer("Prepare job")) {
workRequest = cachedJarProcessor.prepareJob(inputJar);
workRequest = cachedJarProcessor.prepareJob(classesInputJar);
}

final CachedJarProcessor.WorkJob job = workRequest.job();
Expand Down Expand Up @@ -310,14 +317,13 @@ private void runWithCache(Path cacheRoot) throws IOException {
}

// The final output sources jar
final Path sourcesJar = getOutputJar().get().getAsFile().toPath();
Files.deleteIfExists(sourcesJar);
Files.deleteIfExists(sourcesOutputJar);

try (var timer = new Timer("Complete job")) {
cachedJarProcessor.completeJob(sourcesJar, job, outputLineNumbers);
cachedJarProcessor.completeJob(sourcesOutputJar, job, outputLineNumbers);
}

getLogger().info("Decompiled sources written to {}", sourcesJar);
getLogger().info("Decompiled sources written to {}", sourcesOutputJar);

// Remap the line numbers with the new and existing numbers
final ClassLineNumbers existingLinenumbers = workRequest.lineNumbers();
Expand All @@ -332,19 +338,19 @@ private void runWithCache(Path cacheRoot) throws IOException {
Files.delete(tempJar);

try (var timer = new Timer("Remap line numbers")) {
remapLineNumbers(lineNumbers, inputJar, tempJar);
remapLineNumbers(lineNumbers, classesInputJar, tempJar);
}

Files.move(tempJar, outputJar, StandardCopyOption.REPLACE_EXISTING);
Files.move(tempJar, classesOutputJar, StandardCopyOption.REPLACE_EXISTING);

try (var timer = new Timer("Prune cache")) {
decompileCache.prune();
}
}

private void runWithoutCache() throws IOException {
Path inputJar = getInputJar().getSingleFile().toPath();
final Path outputJar = getOutputJar().get().getAsFile().toPath();
Path inputJar = getClassesInputJar().getSingleFile().toPath();
final Path outputJar = getSourcesOutputJar().get().getAsFile().toPath();

// The final output sources jar

Expand Down Expand Up @@ -522,7 +528,7 @@ private void doWork(@Nullable IPCServer ipcServer, Path inputJar, Path outputJar
params.getInputJar().set(inputJar.toFile());
params.getOutputJar().set(outputJar.toFile());
params.getLinemapFile().set(linemapFile.toFile());
params.getMappings().set(getMappings().toFile());
params.getMappings().set(getMappings());

if (ipcServer != null) {
params.getIPCPath().set(ipcServer.getPath().toFile());
Expand Down Expand Up @@ -576,7 +582,7 @@ public interface DecompileParams extends WorkParameters {
RegularFileProperty getInputJar();
RegularFileProperty getOutputJar();
RegularFileProperty getLinemapFile();
RegularFileProperty getMappings();
Property<SourceMappingsService.Options> getMappings();

RegularFileProperty getIPCPath();

Expand Down Expand Up @@ -620,26 +626,32 @@ private void doDecompile(IOStringConsumer logger) {
throw new RuntimeException("Failed to create decompiler", e);
}

final var metadata = new DecompilationMetadata(
decompilerOptions.maxThreads(),
getParameters().getMappings().get().getAsFile().toPath(),
getLibraries(),
logger,
decompilerOptions.options()
);

decompiler.decompile(
inputJar,
outputJar,
linemap,
metadata
);

// Close the decompile loggers
try {
metadata.logger().accept(ThreadedProgressLoggerConsumer.CLOSE_LOGGERS);
try (var serviceFactory = new ScopedServiceFactory()) {
final SourceMappingsService mappingsService = serviceFactory.get(getParameters().getMappings());

final var metadata = new DecompilationMetadata(
decompilerOptions.maxThreads(),
mappingsService.getMappingsFile(),
getLibraries(),
logger,
decompilerOptions.options()
);

decompiler.decompile(
inputJar,
outputJar,
linemap,
metadata
);

// Close the decompile loggers
try {
metadata.logger().accept(ThreadedProgressLoggerConsumer.CLOSE_LOGGERS);
} catch (IOException e) {
throw new UncheckedIOException("Failed to close loggers", e);
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to close loggers", e);
throw new UncheckedIOException(e);
}
}

Expand All @@ -648,66 +660,6 @@ private Collection<Path> getLibraries() {
}
}

private Path getMappings() {
Path inputMappings = getExtension().getMappingConfiguration().tinyMappings;

MemoryMappingTree mappingTree = new MemoryMappingTree();

try (Reader reader = Files.newBufferedReader(inputMappings, StandardCharsets.UTF_8)) {
MappingReader.read(reader, new MappingSourceNsSwitch(mappingTree, MappingsNamespace.INTERMEDIARY.toString()));
} catch (IOException e) {
throw new RuntimeException("Failed to read mappings", e);
}

final List<MappingsProcessor> mappingsProcessors = new ArrayList<>();

MinecraftJarProcessorManager minecraftJarProcessorManager = MinecraftJarProcessorManager.create(getProject());

if (minecraftJarProcessorManager != null) {
mappingsProcessors.add(mappings -> {
try (var serviceFactory = new ScopedServiceFactory()) {
final var configContext = new ConfigContextImpl(getProject(), serviceFactory, getExtension());
return minecraftJarProcessorManager.processMappings(mappings, new MappingProcessorContextImpl(configContext));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

if (mappingsProcessors.isEmpty()) {
return inputMappings;
}

boolean transformed = false;

for (MappingsProcessor mappingsProcessor : mappingsProcessors) {
if (mappingsProcessor.transform(mappingTree)) {
transformed = true;
}
}

if (!transformed) {
return inputMappings;
}

final Path outputMappings;

try {
outputMappings = Files.createTempFile("loom-transitive-mappings", ".tiny");
} catch (IOException e) {
throw new RuntimeException("Failed to create temp file", e);
}

try (Writer writer = Files.newBufferedWriter(outputMappings, StandardCharsets.UTF_8)) {
var tiny2Writer = new Tiny2FileWriter(writer, false);
mappingTree.accept(new MappingSourceNsSwitch(tiny2Writer, MappingsNamespace.NAMED.toString()));
} catch (IOException e) {
throw new RuntimeException("Failed to write mappings", e);
}

return outputMappings;
}

public static File getJarFileWithSuffix(String suffix, Path runtimeJar) {
final String path = runtimeJar.toFile().getAbsolutePath();

Expand Down
Loading

0 comments on commit 83b5a19

Please sign in to comment.