Skip to content

Commit

Permalink
[0.21] Refactor TransformCache, apply QuiltTransformer etc. statically (
Browse files Browse the repository at this point in the history
#352)

* Refactor TransformCache, apply QuiltTransformer etc. statically

* Fix first of crashes when loading template mod

* just use CopyOption

* Fix REPLACE_EXISTING crashing if the file doesn't already exist

* Add workaround for deleting classes

* Reorganize, add hiding classes

* Cleanup

* Licenses

* Fix

* remove whitespace change

* Scream test it
  • Loading branch information
TheGlitch76 authored Sep 8, 2023
1 parent 4b7e800 commit cd3dbf3
Show file tree
Hide file tree
Showing 22 changed files with 707 additions and 484 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
id 'eclipse'
id 'maven-publish'
id 'org.quiltmc.gradle.licenser' version '1.1.+'
id 'org.quiltmc.loom' version '1.1.+' apply false
id 'org.quiltmc.loom' version '1.3.+' apply false
id 'com.github.johnrengelman.shadow' version '8.1.1'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -68,7 +67,7 @@ public abstract class ModLoadOption extends LoadOption {
@Nullable
public abstract String namespaceMappingFrom();

public abstract boolean needsChasmTransforming();
public abstract boolean needsTransforming();

/** @return A hash of the origin files used for the mod. This is used to cache class transformations (like remapping
* and chasm) between launches. This may be called off-thread. */
Expand Down
34 changes: 4 additions & 30 deletions src/main/java/org/quiltmc/loader/impl/QuiltLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.quiltmc.loader.impl;

import java.awt.GraphicsEnvironment;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -103,7 +102,7 @@
import org.quiltmc.loader.impl.report.QuiltReport.CrashReportSaveFailed;
import org.quiltmc.loader.impl.report.QuiltReportedError;
import org.quiltmc.loader.impl.solver.ModSolveResultImpl;
import org.quiltmc.loader.impl.transformer.TransformCache;
import org.quiltmc.loader.impl.transformer.TransformCacheManager;
import org.quiltmc.loader.impl.transformer.TransformCacheResult;
import org.quiltmc.loader.impl.util.Arguments;
import org.quiltmc.loader.impl.util.AsciiTableGenerator;
Expand All @@ -121,8 +120,6 @@

import net.fabricmc.loader.api.ObjectShare;

import net.fabricmc.accesswidener.AccessWidener;
import net.fabricmc.accesswidener.AccessWidenerReader;
import net.fabricmc.api.EnvType;

@QuiltLoaderInternal(value = QuiltLoaderInternalType.LEGACY_EXPOSED, replacements = QuiltLoader.class)
Expand Down Expand Up @@ -155,7 +152,6 @@ public final class QuiltLoaderImpl {

private final Map<String, LanguageAdapter> adapterMap = new HashMap<>();
private final EntrypointStorage entrypointStorage = new EntrypointStorage();
private final AccessWidener accessWidener = new AccessWidener();

private final ObjectShare objectShare = new ObjectShareImpl();

Expand Down Expand Up @@ -392,13 +388,14 @@ private void setup() throws ModResolutionException {
}

Path transformCacheFolder = getCacheDir().resolve(CACHE_DIR_NAME).resolve("transform-cache-" + suffix);
TransformCacheResult cacheResult = TransformCache.populateTransformBundle(transformCacheFolder, modList, modOriginHash, result);
TransformCacheResult cacheResult = TransformCacheManager.populateTransformBundle(transformCacheFolder, modList, modOriginHash, result);
QuiltZipPath transformedModBundle = cacheResult.transformCacheRoot;

long zipEnd = System.nanoTime();

try {
QuiltLauncherBase.getLauncher().setTransformCache(transformedModBundle.toUri().toURL());
QuiltLauncherBase.getLauncher().setHiddenClasses(cacheResult.hiddenClasses);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Expand All @@ -418,7 +415,7 @@ private void setup() throws ModResolutionException {
for (ModLoadOption modOption : modList) {
Path resourceRoot;

if (!modOption.needsChasmTransforming() && modOption.namespaceMappingFrom() == null) {
if (!modOption.needsTransforming() && modOption.namespaceMappingFrom() == null) {
resourceRoot = modOption.resourceRoot();
} else {
String modid = modOption.id();
Expand Down Expand Up @@ -1159,26 +1156,7 @@ private void setupMods() {
}
}

public void loadAccessWideners() {
AccessWidenerReader accessWidenerReader = new AccessWidenerReader(accessWidener);

for (ModContainerExt mod : mods) {
for (String accessWidener : mod.metadata().accessWideners()) {

Path path = mod.getPath(accessWidener);

if (!FasterFiles.isRegularFile(path)) {
throw new RuntimeException("Failed to find accessWidener file from mod " + mod.metadata().id() + " '" + accessWidener + "'");
}

try (BufferedReader reader = Files.newBufferedReader(path)) {
accessWidenerReader.read(reader, getMappingResolver().getCurrentRuntimeNamespace());
} catch (Exception e) {
throw new RuntimeException("Failed to read accessWidener file from mod " + mod.metadata().id(), e);
}
}
}
}

public void prepareModInit(Path newRunDir, Object gameInstance) {
if (!frozen) {
Expand Down Expand Up @@ -1215,10 +1193,6 @@ public void invokePreLaunch() {
}
}

public AccessWidener getAccessWidener() {
return accessWidener;
}

/**
* Sets the game instance. This is only used in 20w22a+ by the dedicated server and should not be called by anything else.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,9 @@ public interface ModAdder {
public static void findCandidatesStatic(ModAdder out) {
if (QuiltLauncherBase.getLauncher().isDevelopment()) {
Map<Path, List<Path>> pathGroups = getPathGroups();

// Search for URLs which point to 'fabric.mod.json' entries, to be considered as mods.
try {
Enumeration<URL> fabricMods = QuiltLauncherBase.getLauncher().getTargetClassLoader().getResources("fabric.mod.json");
Enumeration<URL> quiltMods = QuiltLauncherBase.getLauncher().getTargetClassLoader().getResources("quilt.mod.json");
while (quiltMods.hasMoreElements()) {
URL url = quiltMods.nextElement();

try {
Path path = LoaderUtil.normalizeExistingPath(UrlUtil.getCodeSource(url, "quilt.mod.json"));
List<Path> paths = pathGroups.get(path);

if (paths == null) {
out.addMod(Collections.singletonList(path));
} else {
out.addMod(paths);
}
} catch (UrlConversionException e) {
Log.debug(LogCategory.DISCOVERY, "Error determining location for quilt.mod.json from %s", url, e);
}
}
while (fabricMods.hasMoreElements()) {
URL url = fabricMods.nextElement();

try {
Path path = LoaderUtil.normalizeExistingPath(UrlUtil.getCodeSource(url, "fabric.mod.json"));
List<Path> paths = pathGroups.get(path);

if (paths == null) {
out.addMod(Collections.singletonList(path));
} else {
out.addMod(paths);
}
} catch (UrlConversionException e) {
Log.debug(LogCategory.DISCOVERY, "Error determining location for fabric.mod.json from %s", url, e);
}
}
findCandidates("quilt.mod.json", pathGroups, out);
findCandidates("fabric.mod.json", pathGroups, out);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -104,6 +70,37 @@ public static void findCandidatesStatic(ModAdder out) {
}
}

private static void findCandidates(String metadataName, Map<Path, List<Path>> pathGroups, ModAdder out) throws IOException {
Enumeration<URL> quiltMods = QuiltLauncherBase.getLauncher().getTargetClassLoader().getResources(metadataName);
while (quiltMods.hasMoreElements()) {
URL url = quiltMods.nextElement();

try {
Path path = LoaderUtil.normalizeExistingPath(UrlUtil.getCodeSource(url, metadataName));
List<Path> paths = pathGroups.get(path);

if (paths == null) {
if (!url.getProtocol().equals("jar")) {
if (!pathGroups.isEmpty()) {
// path groups are enabled, warn for misconfiguration
Log.error(LogCategory.DISCOVERY,"Mod at path " + url + " lacks a class path group! Make sure the loom 'mods' block " +
"is configured correctly!");
}

Log.error(LogCategory.DISCOVERY, "Mod at path %s is not included in the loom 'mods' block! " +
"This will be an error in a future version of Loader!", path);
}

out.addMod(Collections.singletonList(path));
} else {
out.addMod(paths);
}
} catch (UrlConversionException e) {
Log.debug(LogCategory.DISCOVERY, "Error determining location for %s from %s", metadataName, url, e);
}
}
}

/**
* Parse fabric.classPathGroups system property into a path group lookup map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ private <T extends Throwable> P addEntryWithoutParents0(QuiltUnifiedEntry newEnt

protected boolean removeEntry(P path, boolean throwIfMissing) throws IOException {
path = path.toAbsolutePath().normalize();
if ("/quilt_tags/quilt_tags.accesswidener".equals(path.toString())) {
System.out.println("Removing " + path);
}

QuiltUnifiedEntry current = getEntry(path);
if (current == null) {
if (throwIfMissing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ private void copy0(P src, P dst, boolean isMove, CopyOption[] options) throws IO
}

if (canExist) {
delete(dst);
deleteIfExists(dst);
} else if (dstEntry != null) {
throw new FileAlreadyExistsException(dst.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Path copyOnWrite(Path source, Path target, CopyOption... options) throws
}

if (canExist) {
provider().delete(dst);
provider().deleteIfExists(dst);
} else if (dstEntry != null) {
throw new FileAlreadyExistsException(dst.toString());
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public Path mount(Path source, Path target, MountOption... options) throws IOExc


if (canExist) {
provider().delete(dst);
provider().deleteIfExists(dst);
} else if (dstEntry != null) {
throw new FileAlreadyExistsException(dst.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.jar.Manifest;

import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.loader.impl.entrypoint.GameTransformer;
import org.quiltmc.loader.impl.util.QuiltLoaderInternal;
import org.quiltmc.loader.impl.util.QuiltLoaderInternalType;

Expand All @@ -38,6 +39,7 @@ public interface QuiltLauncher {
void addToClassPath(Path path, ModContainer mod, URL origin, String... allowedPrefixes);
void setAllowedPrefixes(Path path, String... prefixes);
void setTransformCache(URL insideTransformCache);
void setHiddenClasses(Set<String> classes);
void hideParentUrl(URL hidden);
void hideParentPath(Path obf);
void validateGameClassLoader(Object gameInstance);
Expand Down Expand Up @@ -76,4 +78,6 @@ public interface QuiltLauncher {
String getTargetNamespace();

List<Path> getClassPath();

GameTransformer getEntrypointTransformer();
}
14 changes: 12 additions & 2 deletions src/main/java/org/quiltmc/loader/impl/launch/knot/Knot.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.quiltmc.loader.impl.QuiltLoaderImpl;
import org.quiltmc.loader.impl.config.QuiltConfigImpl;
import org.quiltmc.loader.impl.entrypoint.EntrypointUtils;
import org.quiltmc.loader.impl.entrypoint.GameTransformer;
import org.quiltmc.loader.impl.game.GameProvider;
import org.quiltmc.loader.impl.launch.common.QuiltLauncherBase;
import org.quiltmc.loader.impl.launch.common.QuiltMixinBootstrap;
Expand Down Expand Up @@ -52,6 +53,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -147,8 +149,6 @@ public ClassLoader init(String[] args) {
loader.load();
loader.freeze();

QuiltLoaderImpl.INSTANCE.loadAccessWideners();

MixinBootstrap.init();
QuiltMixinBootstrap.init(getEnvironmentType(), loader);
QuiltLauncherBase.finishMixinBootstrapping();
Expand Down Expand Up @@ -270,6 +270,11 @@ public List<Path> getClassPath() {
return classPath;
}

@Override
public GameTransformer getEntrypointTransformer() {
return provider.getEntrypointTransformer();
}

@Override
public void addToClassPath(Path path, String... allowedPrefixes) {
addToClassPath(path, null, null, allowedPrefixes);
Expand Down Expand Up @@ -303,6 +308,11 @@ public void setTransformCache(URL insideTransformCache) {
classLoader.getDelegate().setTransformCache(insideTransformCache);
}

@Override
public void setHiddenClasses(Set<String> hiddenClasses) {
classLoader.getDelegate().setHiddenClasses(hiddenClasses);
}

@Override
public void hideParentUrl(URL parent) {
classLoader.getDelegate().hideParentUrl(parent);
Expand Down
Loading

0 comments on commit cd3dbf3

Please sign in to comment.