|
20 | 20 |
|
21 | 21 | package net.minecraftforge.gradle.common.util; |
22 | 22 |
|
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.Optional; |
| 29 | +import java.util.function.Predicate; |
| 30 | +import java.util.regex.Pattern; |
| 31 | +import java.util.stream.Collectors; |
| 32 | +import javax.annotation.Nullable; |
| 33 | + |
23 | 34 | import org.gradle.api.Project; |
24 | 35 |
|
25 | 36 | public class MojangLicenseHelper { |
26 | | - //TODO: Add a task that people can run to quiet this warning. |
27 | | - //Also output the specific text from the targeted MC version. |
| 37 | + |
| 38 | + public static final String HIDE_LICENSE = "hideOfficialWarningUntilChanged"; |
| 39 | + public static final String SHOW_LICENSE = "reshowOfficialWarning"; |
| 40 | + |
| 41 | + /** |
| 42 | + * @see #displayWarning(Project, String, String) |
| 43 | + */ |
| 44 | + @Deprecated |
28 | 45 | public static void displayWarning(Project project, String channel) { |
| 46 | + displayWarning(project, channel, null); |
| 47 | + } |
| 48 | + |
| 49 | + public static void displayWarning(Project project, String channel, @Nullable String version, @Nullable String updateChannel, @Nullable String updateVersion) { |
| 50 | + displayWarning(project, channel, version); |
| 51 | + |
| 52 | + if (updateChannel == null || Objects.equals(channel, updateChannel)) return; |
| 53 | + |
| 54 | + displayWarning(project, updateChannel, updateVersion); |
| 55 | + } |
| 56 | + |
| 57 | + public static void displayWarning(Project project, String channel, @Nullable String version) { |
29 | 58 | if ("official".equals(channel)) { |
30 | | - String warning = "WARNING: " |
31 | | - + "This project is configured to use the official obfuscation mappings provided by Mojang. " |
32 | | - + "These mapping fall under their associated license, you should be fully aware of this license. " |
33 | | - + "For the latest license text, refer to the mapping file itself, or the reference copy here: " |
34 | | - + "https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md"; |
| 59 | + Optional<String> license = version != null ? getOfficialLicense(project, version) : Optional.empty(); |
| 60 | + Optional<String> licenseHash = license.map(HashFunction.SHA1::hash); |
| 61 | + |
| 62 | + if (license.isPresent() && isHidden(project, licenseHash.get())) return; |
| 63 | + |
| 64 | + String warning = getWarning(license.orElse(null)); |
| 65 | + |
35 | 66 | project.getLogger().warn(warning); |
| 67 | + license.map(s -> "WARNING: " + s).ifPresent(project.getLogger()::warn); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static void hide(Project project, String channel, String version) { |
| 72 | + if (!"official".equals(channel)) return; |
| 73 | + |
| 74 | + String hash = getOfficialLicense(project, version) |
| 75 | + .map(HashFunction.SHA1::hash) |
| 76 | + .orElseThrow(() -> new IllegalStateException("Could not get Mojang license text for " + version)); |
| 77 | + |
| 78 | + Path accepted = getLicensePath(project, hash); |
| 79 | + |
| 80 | + if (Files.exists(accepted)) return; |
| 81 | + |
| 82 | + try { |
| 83 | + Utils.createEmpty(accepted.toFile()); |
| 84 | + |
| 85 | + String warning = "WARNING: These warnings will not be shown again until the license changes " |
| 86 | + + "or the task `{TASK}` is run."; |
| 87 | + |
| 88 | + project.getLogger().warn(warning.replace("{TASK}", SHOW_LICENSE)); |
| 89 | + } catch (IOException exception) { |
| 90 | + project.getLogger().error("Could not accept Mojang license", exception); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static void show(Project project, String channel, String version) { |
| 95 | + if (!"official".equals(channel)) return; |
| 96 | + |
| 97 | + String hash = getOfficialLicense(project, version) |
| 98 | + .map(HashFunction.SHA1::hash) |
| 99 | + .orElseThrow(() -> new IllegalStateException("Could not get Mojang license text for " + version)); |
| 100 | + |
| 101 | + Path accepted = getLicensePath(project, hash); |
| 102 | + |
| 103 | + Utils.delete(accepted.toFile()); |
| 104 | + } |
| 105 | + |
| 106 | + private static String getWarning(String license) { |
| 107 | + String warning = "WARNING: " |
| 108 | + + "This project is configured to use the official obfuscation mappings provided by Mojang. " |
| 109 | + + "These mapping fall under their associated license, you should be fully aware of this license. " |
| 110 | + + "For the latest license text, refer {REFER}, or the reference copy here: " |
| 111 | + + "https://github.com/MinecraftForge/MCPConfig/blob/master/Mojang.md" |
| 112 | + + ", You can hide this warning by running the `{TASK}` task"; |
| 113 | + |
| 114 | + return warning |
| 115 | + .replace("{REFER}", license != null ? "below" : "to the mapping file itself") |
| 116 | + .replace("{TASK}", HIDE_LICENSE); |
| 117 | + } |
| 118 | + |
| 119 | + private static boolean isHidden(Project project, String hash) { |
| 120 | + return Files.exists(getLicensePath(project, hash)); |
| 121 | + } |
| 122 | + |
| 123 | + private static Optional<String> getOfficialLicense(Project project, String version) { |
| 124 | + String minecraftVersion = getMinecraftVersion(version); |
| 125 | + String artifact = "net.minecraft:client:" + minecraftVersion + ":mappings@txt"; |
| 126 | + |
| 127 | + File client = MavenArtifactDownloader.generate(project, artifact, true); |
| 128 | + |
| 129 | + if (client == null) return Optional.empty(); |
| 130 | + |
| 131 | + try { |
| 132 | + return Optional.of( |
| 133 | + Files.lines(client.toPath()) |
| 134 | + .filter(line -> line.startsWith("#")) // Only Comments |
| 135 | + .map(l -> l.substring(1).trim()) // Remove initial # |
| 136 | + .collect(Collectors.joining("\n")) // Join via \n |
| 137 | + ); |
| 138 | + } catch (IOException e) { |
| 139 | + e.printStackTrace(); |
36 | 140 | } |
| 141 | + |
| 142 | + return Optional.empty(); |
| 143 | + } |
| 144 | + |
| 145 | + private static Path getLicensePath(Project project, String hash) { |
| 146 | + return new File(Utils.getCache(project, "licenses"), hash + ".marker").toPath(); |
| 147 | + } |
| 148 | + |
| 149 | + private static final Predicate<String> MCP_CONFIG_TIMESTAMP = Pattern.compile("\\d{8}\\.\\d{6}").asPredicate(); |
| 150 | + |
| 151 | + private static String getMinecraftVersion(String version) { |
| 152 | + int idx = version.lastIndexOf('-'); |
| 153 | + |
| 154 | + if (idx != -1 && MCP_CONFIG_TIMESTAMP.test(version.substring(idx + 1))) { |
| 155 | + version = version.substring(0, idx); |
| 156 | + } |
| 157 | + |
| 158 | + return version; |
37 | 159 | } |
38 | 160 | } |
0 commit comments