Skip to content

Commit

Permalink
Add decompile cache rules properties. (#1217)
Browse files Browse the repository at this point in the history
Closes #1206
  • Loading branch information
modmuss50 authored Nov 22, 2024
1 parent 13ed992 commit 6492178
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.IOStringConsumer;
import net.fabricmc.loom.util.Platform;
import net.fabricmc.loom.util.gradle.GradleUtils;
import net.fabricmc.loom.util.gradle.SyncTaskBuildService;
import net.fabricmc.loom.util.gradle.ThreadedProgressLoggerConsumer;
import net.fabricmc.loom.util.gradle.ThreadedSimpleProgressLogger;
Expand Down Expand Up @@ -174,6 +175,14 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
@Internal
protected abstract RegularFileProperty getDecompileCacheFile();

@ApiStatus.Internal
@Input
protected abstract Property<Integer> getMaxCachedFiles();

@ApiStatus.Internal
@Input
protected abstract Property<Integer> getMaxCacheFileAge();

// Injects
@Inject
protected abstract WorkerExecutor getWorkerExecutor();
Expand Down Expand Up @@ -238,6 +247,9 @@ public GenerateSourcesTask(DecompilerOptions decompilerOptions) {

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

getMaxCachedFiles().set(GradleUtils.getIntegerPropertyProvider(getProject(), Constants.Properties.DECOMPILE_CACHE_MAX_FILES).orElse(50_000));
getMaxCacheFileAge().set(GradleUtils.getIntegerPropertyProvider(getProject(), Constants.Properties.DECOMPILE_CACHE_MAX_AGE).orElse(90));

mustRunAfter(getProject().getTasks().withType(AbstractRemapJarTask.class));
}

Expand Down Expand Up @@ -273,9 +285,17 @@ public void run() throws IOException {
}

// TODO ensure we have a lock on this file to prevent multiple tasks from running at the same time
// TODO handle being unable to read the cache file
Files.createDirectories(cacheFile.getParent());

if (Files.exists(cacheFile)) {
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(cacheFile, true)) {
// Success, cache exists and can be read
} catch (IOException e) {
getLogger().warn("Discarding invalid decompile cache file: {}", cacheFile, e);
Files.delete(cacheFile);
}
}

try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(cacheFile, true)) {
runWithCache(fs.getRoot());
}
Expand All @@ -289,13 +309,14 @@ private void runWithCache(Path cacheRoot) throws IOException {
final Path classesInputJar = getClassesInputJar().getSingleFile().toPath();
final Path sourcesOutputJar = getSourcesOutputJar().get().getAsFile().toPath();
final Path classesOutputJar = getClassesOutputJar().getSingleFile().toPath();
final var cacheRules = new CachedFileStoreImpl.CacheRules(50_000, Duration.ofDays(90));
final var cacheRules = new CachedFileStoreImpl.CacheRules(getMaxCachedFiles().get(), Duration.ofDays(getMaxCacheFileAge().get()));
final var decompileCache = new CachedFileStoreImpl<>(cacheRoot, CachedData.SERIALIZER, cacheRules);
final String cacheKey = getCacheKey();
final CachedJarProcessor cachedJarProcessor = new CachedJarProcessor(decompileCache, cacheKey);
final CachedJarProcessor.WorkRequest workRequest;

getLogger().info("Decompile cache key: {}", cacheKey);
getLogger().debug("Decompile cache rules: {}", cacheRules);

try (var timer = new Timer("Prepare job")) {
workRequest = cachedJarProcessor.prepareJob(classesInputJar);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/fabricmc/loom/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public static final class Properties {
* Only set this when you have a good reason to do so, the default should be fine for almost all cases.
*/
public static final String RUNTIME_JAVA_COMPATIBILITY_VERSION = "fabric.loom.runtimeJavaCompatibilityVersion";
public static final String DECOMPILE_CACHE_MAX_FILES = "fabric.loom.decompileCacheMaxFiles";
public static final String DECOMPILE_CACHE_MAX_AGE = "fabric.loom.decompileCacheMaxAge";
}

public static final class Manifest {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/fabricmc/loom/util/gradle/GradleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ public static Provider<Boolean> getBooleanPropertyProvider(Project project, Stri
});
}

public static Provider<Integer> getIntegerPropertyProvider(Project project, String key) {
return project.provider(() -> {
final Object value = project.findProperty(key);

if (value == null) {
return null;
}

try {
return Integer.parseInt(value.toString());
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException("Property " + key + " must be an integer", ex);
}
});
}

public static boolean getBooleanProperty(Project project, String key) {
return getBooleanPropertyProvider(project, key).getOrElse(false);
}
Expand Down

0 comments on commit 6492178

Please sign in to comment.