Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache directory API #906

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/net/fabricmc/loader/api/FabricLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ static FabricLoader getInstance() {
@Deprecated
File getGameDirectory();

/**
* Get the current directory for temporary files for a particular mod.
*
* @param modId The ID of the mod to get the cache directory for
* @return the cache directory
*/
Path getCacheDir(String modId);

/**
* Get the current directory for game configuration files.
*
Expand Down
52 changes: 35 additions & 17 deletions src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader {
public static final String VERSION = "0.15.7";
public static final String MOD_ID = "fabricloader";

// Relative to game dir
private static final String MOD_CACHE_DIR = ".cache";
private static final String MOD_CONFIG_DIR = "config";

public static final String CACHE_DIR_NAME = ".fabric"; // relative to game dir
LostLuma marked this conversation as resolved.
Show resolved Hide resolved
private static final String PROCESSED_MODS_DIR_NAME = "processedMods"; // relative to cache dir
public static final String REMAPPED_JARS_DIR_NAME = "remappedJars"; // relative to cache dir
private static final String TMP_DIR_NAME = "tmp"; // relative to cache dir
private static final String PROCESSED_MODS_DIR_NAME = "processedMods"; // relative to loader cache dir
public static final String REMAPPED_JARS_DIR_NAME = "remappedJars"; // relative to loader cache dir
private static final String TMP_DIR_NAME = "tmp"; // relative to loader cache dir
LostLuma marked this conversation as resolved.
Show resolved Hide resolved

protected final Map<String, ModContainerImpl> modMap = new HashMap<>();
private List<ModCandidate> modCandidates;
Expand All @@ -99,6 +103,7 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader {
private MappingResolver mappingResolver;
private GameProvider provider;
private Path gameDir;
private Path cacheDir;
private Path configDir;

private FabricLoaderImpl() { }
Expand Down Expand Up @@ -133,7 +138,8 @@ public void setGameProvider(GameProvider provider) {

private void setGameDir(Path gameDir) {
this.gameDir = gameDir;
this.configDir = gameDir.resolve("config");
this.cacheDir = gameDir.resolve(MOD_CACHE_DIR);
this.configDir = gameDir.resolve(MOD_CONFIG_DIR);
}

@Override
Expand Down Expand Up @@ -162,20 +168,32 @@ public File getGameDirectory() {
return getGameDir().toFile();
}

/**
* @return The game instance's configuration directory.
*/
@Override
public Path getConfigDir() {
if (!Files.exists(configDir)) {
private Path ensureDirExists(Path path, String name) {
if (!Files.exists(path)) {
try {
Files.createDirectories(configDir);
Files.createDirectories(path);
} catch (IOException e) {
throw new RuntimeException("Creating config directory", e);
throw new RuntimeException("Creating " + name + " directory", e);
}
}

return configDir;
return path;
}

/**
* @return The game instance's temporary file directory for a particular mod.
*/
@Override
public Path getCacheDir(String modId) {
return ensureDirExists(cacheDir.resolve(modId), "cache");
}

/**
* @return The game instance's configuration directory.
*/
@Override
public Path getConfigDir() {
return ensureDirExists(configDir, "config");
}

@Override
Expand Down Expand Up @@ -230,16 +248,16 @@ private void setup() throws ModResolutionException {

dumpModList(modCandidates);

Path cacheDir = gameDir.resolve(CACHE_DIR_NAME);
Path outputdir = cacheDir.resolve(PROCESSED_MODS_DIR_NAME);
Path loaderCacheDir = gameDir.resolve(CACHE_DIR_NAME);
Path outputDir = loaderCacheDir.resolve(PROCESSED_MODS_DIR_NAME);

// runtime mod remapping

if (remapRegularMods) {
if (System.getProperty(SystemProperties.REMAP_CLASSPATH_FILE) == null) {
Log.warn(LogCategory.MOD_REMAP, "Runtime mod remapping disabled due to no fabric.remapClasspathFile being specified. You may need to update loom.");
} else {
RuntimeModRemapper.remap(modCandidates, cacheDir.resolve(TMP_DIR_NAME), outputdir);
RuntimeModRemapper.remap(modCandidates, loaderCacheDir.resolve(TMP_DIR_NAME), outputDir);
}
}

Expand Down Expand Up @@ -270,7 +288,7 @@ private void setup() throws ModResolutionException {
for (ModCandidate mod : modCandidates) {
if (!mod.hasPath() && !mod.isBuiltin()) {
try {
mod.setPaths(Collections.singletonList(mod.copyToDir(outputdir, false)));
mod.setPaths(Collections.singletonList(mod.copyToDir(outputDir, false)));
} catch (IOException e) {
throw new RuntimeException("Error extracting mod "+mod, e);
}
Expand Down
Loading