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 1 commit
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
7 changes: 7 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,13 @@ static FabricLoader getInstance() {
@Deprecated
File getGameDirectory();

/**
* Get the current directory for temporary files.
*
* @return the cache directory
*/
Path getCacheDir();
LostLuma marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get the current directory for game configuration files.
*
Expand Down
49 changes: 31 additions & 18 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,9 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader {
public static final String VERSION = "0.15.7";
public static final String MOD_ID = "fabricloader";

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 +98,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 @@ -131,8 +131,9 @@ public void setGameProvider(GameProvider provider) {
setGameDir(provider.getLaunchDirectory());
}

private void setGameDir(Path gameDir) {
public void setGameDir(Path gameDir) {
this.gameDir = gameDir;
this.cacheDir = gameDir.resolve(".cache");
this.configDir = gameDir.resolve("config");
LostLuma marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -162,20 +163,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.
*/
@Override
public Path getCacheDir() {
return ensureDirExists(cacheDir, "cache");
}

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

@Override
Expand Down Expand Up @@ -230,16 +243,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 = getCacheDir().resolve(MOD_ID);
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 +283,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
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ public static Map<String, Path> deobfuscate(Map<String, Path> inputFileMap, Stri
}

private static Path getDeobfJarDir(Path gameDir, String gameId, String gameVersion) {
Path ret = gameDir.resolve(FabricLoaderImpl.CACHE_DIR_NAME).resolve(FabricLoaderImpl.REMAPPED_JARS_DIR_NAME);
FabricLoaderImpl loader = FabricLoaderImpl.INSTANCE;

loader.setGameDir(gameDir);
LostLuma marked this conversation as resolved.
Show resolved Hide resolved
Path ret = loader.getCacheDir().resolve(FabricLoaderImpl.MOD_ID).resolve(FabricLoaderImpl.REMAPPED_JARS_DIR_NAME);

StringBuilder versionDirName = new StringBuilder();

if (!gameId.isEmpty()) {
Expand Down