2018-10-29 11:55:30 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2021-07-10 20:50:53 +00:00
|
|
|
* Copyright (c) 2018-2021 FabricMC
|
2018-10-29 11:55:30 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-12-24 20:58:30 +00:00
|
|
|
package net.fabricmc.loom.configuration.mods;
|
2018-10-29 11:55:30 +00:00
|
|
|
|
2018-11-03 17:24:16 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2018-10-29 11:55:30 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-09-28 16:22:13 +00:00
|
|
|
import java.nio.file.Files;
|
2018-10-29 11:55:30 +00:00
|
|
|
import java.nio.file.Path;
|
2020-09-19 13:57:17 +00:00
|
|
|
import java.util.ArrayList;
|
2020-05-13 18:44:45 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2018-10-29 11:55:30 +00:00
|
|
|
import java.util.jar.JarFile;
|
2020-06-01 11:17:58 +00:00
|
|
|
import java.util.stream.Collectors;
|
2018-10-29 11:55:30 +00:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import com.google.gson.JsonObject;
|
2019-11-09 19:00:36 +00:00
|
|
|
import org.gradle.api.Project;
|
2020-05-13 18:44:45 +00:00
|
|
|
import org.objectweb.asm.commons.Remapper;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.zeroturnaround.zip.ZipUtil;
|
|
|
|
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;
|
|
|
|
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
|
|
|
|
2020-11-15 16:15:02 +00:00
|
|
|
import net.fabricmc.accesswidener.AccessWidenerReader;
|
|
|
|
import net.fabricmc.accesswidener.AccessWidenerRemapper;
|
|
|
|
import net.fabricmc.accesswidener.AccessWidenerWriter;
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2020-12-27 16:25:30 +00:00
|
|
|
import net.fabricmc.loom.LoomGradlePlugin;
|
2021-09-13 16:58:52 +00:00
|
|
|
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.configuration.RemappedConfigurationEntry;
|
|
|
|
import net.fabricmc.loom.configuration.processors.dependency.ModDependencyInfo;
|
2021-06-14 17:39:03 +00:00
|
|
|
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvider;
|
|
|
|
import net.fabricmc.loom.util.Constants;
|
2021-09-14 21:40:47 +00:00
|
|
|
import net.fabricmc.loom.util.TinyRemapperHelper;
|
2020-05-13 18:44:45 +00:00
|
|
|
import net.fabricmc.tinyremapper.InputTag;
|
2021-10-24 12:05:34 +00:00
|
|
|
import net.fabricmc.tinyremapper.NonClassCopyMode;
|
2020-05-13 18:44:45 +00:00
|
|
|
import net.fabricmc.tinyremapper.OutputConsumerPath;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.tinyremapper.TinyRemapper;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-11-01 23:40:51 +00:00
|
|
|
public class ModProcessor {
|
2020-05-13 18:44:45 +00:00
|
|
|
public static void processMods(Project project, List<ModDependencyInfo> processList) throws IOException {
|
2020-06-01 11:17:58 +00:00
|
|
|
if (processList.stream().noneMatch(ModDependencyInfo::requiresRemapping)) {
|
2020-05-13 18:44:45 +00:00
|
|
|
return;
|
2019-04-07 19:31:25 +00:00
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-09-19 13:57:17 +00:00
|
|
|
ArrayList<ModDependencyInfo> remapList = new ArrayList<>();
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
for (ModDependencyInfo info : processList) {
|
2020-09-19 13:57:17 +00:00
|
|
|
if (info.requiresRemapping()) {
|
|
|
|
if (info.getRemappedOutput().exists()) {
|
|
|
|
info.getRemappedOutput().delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
remapList.add(info);
|
2019-04-07 19:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
remapJars(project, processList);
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
for (ModDependencyInfo info : processList) {
|
|
|
|
if (!info.getRemappedOutput().exists()) {
|
2020-09-19 13:57:17 +00:00
|
|
|
throw new RuntimeException("Failed to find remapped mod" + info);
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
2020-09-19 13:57:17 +00:00
|
|
|
}
|
2019-04-07 19:31:25 +00:00
|
|
|
|
2020-09-19 13:57:17 +00:00
|
|
|
for (ModDependencyInfo info : remapList) {
|
|
|
|
stripNestedJars(info.getRemappedOutput());
|
2019-04-07 19:31:25 +00:00
|
|
|
}
|
2019-04-08 09:50:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
private static void stripNestedJars(File file) {
|
2020-07-26 20:32:10 +00:00
|
|
|
// Strip out all contained jar info as we dont want loader to try and load the jars contained in dev.
|
2021-09-14 21:40:47 +00:00
|
|
|
ZipUtil.transformEntries(file, new ZipEntryTransformerEntry[]{(new ZipEntryTransformerEntry("fabric.mod.json", new StringZipEntryTransformer() {
|
2019-04-07 21:40:44 +00:00
|
|
|
@Override
|
2020-07-26 20:32:10 +00:00
|
|
|
protected String transform(ZipEntry zipEntry, String input) {
|
2020-12-27 16:25:30 +00:00
|
|
|
JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class);
|
2019-04-07 21:40:44 +00:00
|
|
|
json.remove("jars");
|
2020-12-27 16:25:30 +00:00
|
|
|
return LoomGradlePlugin.GSON.toJson(json);
|
2019-04-07 21:40:44 +00:00
|
|
|
}
|
|
|
|
}))});
|
2018-10-29 11:55:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 21:40:47 +00:00
|
|
|
/**
|
|
|
|
* Remap another mod's access widener from intermediary to named, so that loader can apply it in our dev-env.
|
|
|
|
*/
|
2020-05-14 00:07:54 +00:00
|
|
|
private static byte[] remapAccessWidener(byte[] input, Remapper remapper) {
|
2021-09-14 21:40:47 +00:00
|
|
|
int version = AccessWidenerReader.readVersion(input);
|
|
|
|
|
|
|
|
AccessWidenerWriter writer = new AccessWidenerWriter(version);
|
|
|
|
AccessWidenerRemapper awRemapper = new AccessWidenerRemapper(
|
|
|
|
writer,
|
|
|
|
remapper,
|
|
|
|
MappingsNamespace.INTERMEDIARY.toString(),
|
|
|
|
MappingsNamespace.NAMED.toString()
|
|
|
|
);
|
|
|
|
AccessWidenerReader reader = new AccessWidenerReader(awRemapper);
|
|
|
|
reader.read(input);
|
|
|
|
return writer.write();
|
2020-04-06 14:28:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
private static void remapJars(Project project, List<ModDependencyInfo> processList) throws IOException {
|
2021-07-13 23:03:21 +00:00
|
|
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
2021-09-13 16:58:52 +00:00
|
|
|
String fromM = MappingsNamespace.INTERMEDIARY.toString();
|
|
|
|
String toM = MappingsNamespace.NAMED.toString();
|
2018-10-29 11:55:30 +00:00
|
|
|
|
2018-11-21 08:03:56 +00:00
|
|
|
MinecraftMappedProvider mappedProvider = extension.getMinecraftMappedProvider();
|
2021-06-14 17:39:03 +00:00
|
|
|
MappingsProviderImpl mappingsProvider = extension.getMappingsProvider();
|
2018-11-04 21:28:46 +00:00
|
|
|
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
Path mc = mappedProvider.getIntermediaryJar().toPath();
|
2020-12-21 19:34:00 +00:00
|
|
|
Path[] mcDeps = project.getConfigurations().getByName(Constants.Configurations.LOADER_DEPENDENCIES).getFiles()
|
2021-09-14 21:40:47 +00:00
|
|
|
.stream().map(File::toPath).toArray(Path[]::new);
|
2018-10-29 11:55:30 +00:00
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
List<ModDependencyInfo> remapList = processList.stream().filter(ModDependencyInfo::requiresRemapping).collect(Collectors.toList());
|
|
|
|
|
|
|
|
project.getLogger().lifecycle(":remapping " + remapList.size() + " mods (TinyRemapper, " + fromM + " -> " + toM + ")");
|
2019-11-13 12:32:44 +00:00
|
|
|
|
2019-11-09 19:00:36 +00:00
|
|
|
TinyRemapper remapper = TinyRemapper.newRemapper()
|
2021-09-14 21:40:47 +00:00
|
|
|
.withMappings(TinyRemapperHelper.create(mappingsProvider.getMappings(), fromM, toM, false))
|
|
|
|
.renameInvalidLocals(false)
|
|
|
|
.build();
|
2018-10-29 11:55:30 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
remapper.readClassPathAsync(mc);
|
|
|
|
remapper.readClassPathAsync(mcDeps);
|
|
|
|
|
|
|
|
final Map<ModDependencyInfo, InputTag> tagMap = new HashMap<>();
|
|
|
|
final Map<ModDependencyInfo, OutputConsumerPath> outputConsumerMap = new HashMap<>();
|
2020-05-14 00:07:54 +00:00
|
|
|
final Map<ModDependencyInfo, byte[]> accessWidenerMap = new HashMap<>();
|
2020-05-13 18:44:45 +00:00
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
for (RemappedConfigurationEntry entry : Constants.MOD_COMPILE_ENTRIES) {
|
2021-05-13 21:06:34 +00:00
|
|
|
for (File inputFile : project.getConfigurations().getByName(entry.sourceConfiguration()).getFiles()) {
|
2020-06-01 11:17:58 +00:00
|
|
|
if (remapList.stream().noneMatch(info -> info.getInputFile().equals(inputFile))) {
|
2020-07-26 20:32:10 +00:00
|
|
|
project.getLogger().debug("Adding " + inputFile + " onto the remap classpath");
|
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
remapper.readClassPathAsync(inputFile.toPath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ModDependencyInfo info : remapList) {
|
2020-05-13 18:44:45 +00:00
|
|
|
InputTag tag = remapper.createInputTag();
|
2020-07-26 20:32:10 +00:00
|
|
|
|
|
|
|
project.getLogger().debug("Adding " + info.getInputFile() + " as a remap input");
|
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
remapper.readInputsAsync(tag, info.getInputFile().toPath());
|
|
|
|
tagMap.put(info, tag);
|
2018-10-29 11:55:30 +00:00
|
|
|
}
|
2018-11-11 23:17:44 +00:00
|
|
|
|
2020-05-13 18:44:45 +00:00
|
|
|
// Apply this in a second loop as we need to ensure all the inputs are on the classpath before remapping.
|
2020-06-01 11:17:58 +00:00
|
|
|
for (ModDependencyInfo info : remapList) {
|
2021-07-10 20:52:38 +00:00
|
|
|
try {
|
2021-09-28 16:22:13 +00:00
|
|
|
OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(info.getRemappedOutput().toPath()).build();
|
2021-07-10 20:52:38 +00:00
|
|
|
|
2021-10-24 12:05:34 +00:00
|
|
|
outputConsumer.addNonClassFiles(info.getInputFile().toPath(), NonClassCopyMode.FIX_META_INF, remapper);
|
2021-09-28 16:22:13 +00:00
|
|
|
outputConsumerMap.put(info, outputConsumer);
|
|
|
|
String accessWidener = info.getAccessWidener();
|
2020-05-13 18:44:45 +00:00
|
|
|
|
2021-09-28 16:22:13 +00:00
|
|
|
if (accessWidener != null) {
|
|
|
|
accessWidenerMap.put(info, remapAccessWidener(ZipUtil.unpackEntry(info.inputFile, accessWidener), remapper.getRemapper()));
|
|
|
|
}
|
|
|
|
|
|
|
|
remapper.apply(outputConsumer, tagMap.get(info));
|
|
|
|
} catch (Exception e) {
|
|
|
|
remapper.finish();
|
|
|
|
Files.deleteIfExists(info.getRemappedOutput().toPath());
|
2020-05-14 00:07:54 +00:00
|
|
|
|
2021-09-28 16:22:13 +00:00
|
|
|
throw new RuntimeException("Failed to remap: " + info.getRemappedNotation(), e);
|
|
|
|
}
|
2018-11-01 23:40:51 +00:00
|
|
|
}
|
2020-05-13 18:44:45 +00:00
|
|
|
|
|
|
|
remapper.finish();
|
|
|
|
|
2020-06-01 11:17:58 +00:00
|
|
|
for (ModDependencyInfo info : remapList) {
|
2020-05-13 18:44:45 +00:00
|
|
|
outputConsumerMap.get(info).close();
|
2020-05-14 00:07:54 +00:00
|
|
|
byte[] accessWidener = accessWidenerMap.get(info);
|
2020-05-13 18:44:45 +00:00
|
|
|
|
2020-05-14 00:07:54 +00:00
|
|
|
if (accessWidener != null) {
|
|
|
|
ZipUtil.replaceEntry(info.getRemappedOutput(), info.getAccessWidener(), accessWidener);
|
2020-05-13 18:44:45 +00:00
|
|
|
}
|
2020-12-21 19:33:23 +00:00
|
|
|
|
|
|
|
info.finaliseRemapping();
|
2020-05-14 00:07:54 +00:00
|
|
|
}
|
2018-11-01 23:40:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:58:30 +00:00
|
|
|
public static JsonObject readInstallerJson(File file, Project project) {
|
2018-12-22 09:31:10 +00:00
|
|
|
try {
|
2021-07-13 23:03:21 +00:00
|
|
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
2018-12-22 09:31:10 +00:00
|
|
|
|
2019-04-21 22:39:09 +00:00
|
|
|
String jsonStr;
|
2018-10-29 11:55:30 +00:00
|
|
|
|
2019-04-21 22:39:09 +00:00
|
|
|
try (JarFile jarFile = new JarFile(file)) {
|
2021-07-13 23:03:21 +00:00
|
|
|
ZipEntry entry = jarFile.getEntry("fabric-installer.json");
|
2018-10-29 11:55:30 +00:00
|
|
|
|
2018-12-22 09:31:10 +00:00
|
|
|
if (entry == null) {
|
2021-07-13 23:03:21 +00:00
|
|
|
return null;
|
2019-04-21 22:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try (InputStream inputstream = jarFile.getInputStream(entry)) {
|
2021-05-13 21:06:34 +00:00
|
|
|
jsonStr = new String(inputstream.readAllBytes(), StandardCharsets.UTF_8);
|
2018-12-22 09:31:10 +00:00
|
|
|
}
|
2018-10-29 11:55:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
return LoomGradlePlugin.GSON.fromJson(jsonStr, JsonObject.class);
|
2018-10-29 11:55:30 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-11-13 09:19:57 +00:00
|
|
|
|
2019-11-12 18:46:57 +00:00
|
|
|
return null;
|
2018-10-29 11:55:30 +00:00
|
|
|
}
|
|
|
|
}
|