2018-11-06 09:36:35 +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-11-06 09:36:35 +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.build;
|
2018-11-06 09:36:35 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2021-07-28 07:50:14 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2019-05-10 22:53:50 +00:00
|
|
|
import java.nio.file.Path;
|
2021-07-28 07:50:14 +00:00
|
|
|
import java.util.Collection;
|
2021-07-18 13:13:47 +00:00
|
|
|
import java.util.Objects;
|
2021-07-28 07:50:14 +00:00
|
|
|
import java.util.stream.Collectors;
|
2021-07-18 13:13:47 +00:00
|
|
|
import java.util.stream.Stream;
|
2021-07-28 07:50:14 +00:00
|
|
|
import java.util.stream.StreamSupport;
|
2018-11-06 09:36:35 +00:00
|
|
|
import java.util.zip.ZipEntry;
|
2021-07-28 07:50:14 +00:00
|
|
|
import java.util.zip.ZipFile;
|
2018-11-06 09:36:35 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import com.google.gson.JsonObject;
|
2021-07-28 07:50:14 +00:00
|
|
|
import com.google.gson.JsonPrimitive;
|
2021-07-18 13:13:47 +00:00
|
|
|
import org.gradle.api.Project;
|
2021-07-28 07:50:14 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
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;
|
|
|
|
|
2021-07-18 13:13:47 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2020-12-27 16:25:30 +00:00
|
|
|
import net.fabricmc.loom.LoomGradlePlugin;
|
2021-07-18 13:13:47 +00:00
|
|
|
import net.fabricmc.loom.extension.MixinApExtension;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
public final class MixinRefmapHelper {
|
2019-11-02 20:23:27 +00:00
|
|
|
private MixinRefmapHelper() { }
|
|
|
|
|
2021-07-28 07:50:14 +00:00
|
|
|
private static final String FABRIC_MOD_JSON = "fabric.mod.json";
|
|
|
|
|
2021-07-18 13:13:47 +00:00
|
|
|
public static boolean addRefmapName(Project project, Path outputPath) {
|
2021-07-28 07:50:14 +00:00
|
|
|
try {
|
|
|
|
MixinApExtension mixin = LoomGradleExtension.get(project).getMixin();
|
|
|
|
File output = outputPath.toFile();
|
|
|
|
|
|
|
|
Collection<String> allMixinConfigs = getMixinConfigurationFiles(readFabricModJson(output));
|
|
|
|
|
|
|
|
return mixin.getMixinSourceSetsStream().map(sourceSet -> {
|
|
|
|
MixinApExtension.MixinInformationContainer container = Objects.requireNonNull(
|
|
|
|
MixinApExtension.getMixinInformationContainer(sourceSet)
|
|
|
|
);
|
|
|
|
|
|
|
|
Stream<String> mixinConfigs = sourceSet.getResources()
|
|
|
|
.matching(container.mixinConfigPattern())
|
|
|
|
.getFiles()
|
|
|
|
.stream()
|
|
|
|
.map(File::getName)
|
|
|
|
.filter(allMixinConfigs::contains);
|
|
|
|
|
|
|
|
String refmapName = container.refmapNameProvider().get();
|
|
|
|
|
|
|
|
return ZipUtil.transformEntries(output, mixinConfigs.map(f -> new ZipEntryTransformerEntry(f, new StringZipEntryTransformer("UTF-8") {
|
|
|
|
@Override
|
|
|
|
protected String transform(ZipEntry zipEntry, String input) {
|
|
|
|
JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class);
|
|
|
|
|
|
|
|
if (!json.has("refmap")) {
|
|
|
|
json.addProperty("refmap", refmapName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoomGradlePlugin.GSON.toJson(json);
|
2019-11-02 20:23:27 +00:00
|
|
|
}
|
2021-07-28 07:50:14 +00:00
|
|
|
})).toArray(ZipEntryTransformerEntry[]::new));
|
|
|
|
}).reduce(false, Boolean::logicalOr);
|
|
|
|
} catch (Exception e) {
|
|
|
|
project.getLogger().error(e.getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
private static JsonObject readFabricModJson(File output) {
|
|
|
|
try (ZipFile zip = new ZipFile(output)) {
|
|
|
|
ZipEntry entry = zip.getEntry(FABRIC_MOD_JSON);
|
2018-11-06 09:36:35 +00:00
|
|
|
|
2021-07-28 07:50:14 +00:00
|
|
|
try (InputStreamReader reader = new InputStreamReader(zip.getInputStream(entry))) {
|
|
|
|
return LoomGradlePlugin.GSON.fromJson(reader, JsonObject.class);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Cannot read file fabric.mod.json in the output jar.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
private static Collection<String> getMixinConfigurationFiles(JsonObject fabricModJson) {
|
|
|
|
return StreamSupport.stream(fabricModJson.getAsJsonArray("mixins").spliterator(), false)
|
|
|
|
.map(e -> {
|
|
|
|
if (e instanceof JsonPrimitive str) {
|
|
|
|
return str.getAsString();
|
|
|
|
} else if (e instanceof JsonObject obj) {
|
|
|
|
return obj.get("config").getAsString();
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Incorrect fabric.mod.json format");
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toSet());
|
2019-11-02 20:23:27 +00:00
|
|
|
}
|
2018-11-06 09:36:35 +00:00
|
|
|
}
|