Skip to content

Commit 671095d

Browse files
committed
improve MC dependency names
- include dist in module - include actual version in version - use a shorter, cleaner hash for readability - mark as changing to avoid collisions
1 parent 28b13e6 commit 671095d

File tree

6 files changed

+75
-33
lines changed

6 files changed

+75
-33
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repositories {
1818

1919
val mc by minecraft.creating {
2020
version = "1.21.7"
21-
distribution = Distribution.SERVER
21+
distribution = Distribution.MERGED
2222

2323
runs {
2424
register("server") {

plugin/src/main/java/fish/cichlidmc/cichlid_gradle/cache/mcmaven/MinecraftMaven.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.nio.charset.StandardCharsets;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
32+
import java.util.Arrays;
33+
import java.util.Collection;
3234
import java.util.Optional;
3335
import java.util.regex.Matcher;
3436
import java.util.regex.Pattern;
@@ -39,14 +41,16 @@
3941
*/
4042
public final class MinecraftMaven {
4143
public static final String GROUP = "net.minecraft";
42-
public static final String MODULE = "minecraft";
44+
public static final Collection<String> MODULES = Arrays.stream(Distribution.values())
45+
.map(dist -> "minecraft-" + dist.name)
46+
.toList();
4347

4448
/**
4549
* Regex for files that could possibly be provided.
4650
*/
4751
public static final Pattern VALID_FILE = Pattern.compile(
48-
// groups: |1 | |2 | |- 3 -||- 4 -|
49-
"/net/minecraft/minecraft/(.+)/minecraft-(.+)\\.(pom|jar)(.md5|.sha1|.sha256|.sha512)?"
52+
// groups: |- 1 -| |2 | |- 3 -| |4 | |- 5 -||- 6 -|
53+
"/net/minecraft/minecraft-(client|server|merged)/(.+)/minecraft-(client|server|merged)-(.+)\\.(pom|jar)(.md5|.sha1|.sha256|.sha512)?"
5054
);
5155

5256
private static final Logger logger = Logging.getLogger(MinecraftMaven.class);
@@ -67,12 +71,19 @@ public InputStream get(URI uri) throws IOException {
6771

6872
MinecraftDefinitionImpl def = (MinecraftDefinitionImpl) this.defs.findByName(request.def);
6973
if (def == null) {
70-
throw new InvalidUserDataException("Minecraft definition '" + request.def + "' does not exist");
74+
throw new IllegalStateException("Minecraft definition '" + request.def + "' does not exist");
7175
}
7276

7377
String version = def.version();
78+
Distribution dist = def.dist();
7479

75-
InputStream stream = this.getArtifact(version, def.dist(), def.getTransformers(), request);
80+
// validate that the request matches
81+
if (request.dist != dist || !request.version.equals(version)) {
82+
throw new IllegalStateException("Malformed request for Minecraft definition " + request.def);
83+
}
84+
85+
86+
InputStream stream = this.getArtifact(version, dist, def.getTransformers(), request);
7687
if (stream == null || request.hashAlgorithm.isEmpty())
7788
return stream;
7889

@@ -135,7 +146,7 @@ private InputStream getArtifact(String versionId, Distribution dist, Transformer
135146
private InputStream getPom(FullVersion version, Distribution dist, Request request) throws IOException {
136147
Path template = this.cache.pomTemplates.get(version.id, dist);
137148
if (!Files.exists(template)) {
138-
PomGenerator.generate(version, template);
149+
PomGenerator.generate(version, dist, template);
139150
}
140151

141152
// file should now exist
@@ -152,9 +163,17 @@ private static Request extractRequest(URI uri) {
152163
if (!matcher.matches())
153164
return null;
154165

166+
// extract and verify dist
167+
String dist1 = matcher.group(1);
168+
String dist2 = matcher.group(3);
169+
if (!dist1.equals(dist2))
170+
return null;
171+
172+
Distribution dist = Distribution.ofName(dist1).orElseThrow();
173+
155174
// make sure both versions are the same
156-
String version1 = matcher.group(1);
157-
String version2 = matcher.group(2);
175+
String version1 = matcher.group(2);
176+
String version2 = matcher.group(4);
158177

159178
// -sources will get caught in group 2 if present, check for it
160179
boolean sources = false;
@@ -166,15 +185,16 @@ private static Request extractRequest(URI uri) {
166185
if (!version1.equals(version2))
167186
return null;
168187

169-
// version is defName$hash, extract the name
170-
int separator = version1.indexOf('$');
171-
if (separator == -1)
188+
// version is version$def$hash, extract the name
189+
String[] split = version1.split("\\$");
190+
if (split.length != 3)
172191
return null;
173192

174-
String defName = version1.substring(0, separator);
175-
String hash = version1.substring(separator + 1);
193+
String version = split[0];
194+
String defName = split[1];
195+
String hash = split[2];
176196

177-
Artifact artifact = switch (matcher.group(3)) {
197+
Artifact artifact = switch (matcher.group(5)) {
178198
case "jar" -> sources ? Artifact.SOURCES : Artifact.JAR;
179199
case "pom" -> Artifact.POM;
180200
default -> null;
@@ -184,7 +204,7 @@ private static Request extractRequest(URI uri) {
184204
return null;
185205

186206
HashAlgorithm hashAlgorithm;
187-
switch (matcher.group(4)) {
207+
switch (matcher.group(6)) {
188208
case ".md5" -> hashAlgorithm = HashAlgorithm.MD5;
189209
case ".sha1" -> hashAlgorithm = HashAlgorithm.SHA1;
190210
case ".sha256" -> hashAlgorithm = HashAlgorithm.SHA256;
@@ -196,9 +216,9 @@ private static Request extractRequest(URI uri) {
196216
}
197217
}
198218

199-
logger.debug("Intercepted request for Minecraft definition {}, {}, {}", defName, artifact, hashAlgorithm);
219+
logger.quiet("Intercepted request for Minecraft definition {}, {}, {}", defName, artifact, hashAlgorithm);
200220

201-
return new Request(defName, hash, artifact, Optional.ofNullable(hashAlgorithm));
221+
return new Request(dist, version1, version, defName, hash, artifact, Optional.ofNullable(hashAlgorithm));
202222
}
203223

204224
public static String createProtocol(Project project) {
@@ -221,10 +241,7 @@ private static char filterChar(char c) {
221241
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '+' || c == '-' || c == '.') ? c : '-';
222242
}
223243

224-
private record Request(String def, String hash, Artifact artifact, Optional<HashAlgorithm> hashAlgorithm) {
225-
// the version specified in the pom needs to match what gradle requested exactly (defName$hash) or it'll be rejected
226-
private String gradleRequestedVersion() {
227-
return this.def + '$' + this.hash;
228-
}
244+
private record Request(Distribution dist, String gradleRequestedVersion, String version, String def,
245+
String hash, Artifact artifact, Optional<HashAlgorithm> hashAlgorithm) {
229246
}
230247
}

plugin/src/main/java/fish/cichlidmc/cichlid_gradle/cache/mcmaven/PomGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fish.cichlidmc.cichlid_gradle.cache.mcmaven;
22

3+
import fish.cichlidmc.cichlid_gradle.util.Distribution;
34
import fish.cichlidmc.cichlid_gradle.util.XmlBuilder;
45
import fish.cichlidmc.pistonmetaparser.FullVersion;
56
import fish.cichlidmc.pistonmetaparser.rule.Features;
@@ -19,11 +20,11 @@
1920
public final class PomGenerator {
2021
public static final String VERSION_PLACEHOLDER = "${version}";
2122

22-
public static void generate(FullVersion version, Path output) throws IOException {
23+
public static void generate(FullVersion version, Distribution dist, Path output) throws IOException {
2324
XmlBuilder.create().add(new XmlBuilder.XmlElement("project", List.of(
2425
new XmlBuilder.XmlElement("modelVersion", "4.0.0"),
2526
new XmlBuilder.XmlElement("groupId", "net.minecraft"),
26-
new XmlBuilder.XmlElement("artifactId", "minecraft"),
27+
new XmlBuilder.XmlElement("artifactId", "minecraft-" + dist.name),
2728
new XmlBuilder.XmlElement("version", VERSION_PLACEHOLDER),
2829
new XmlBuilder.XmlElement("dependencies", version.libraries.stream().flatMap(PomGenerator::makeDependencyElements).toList())
2930
))).write(output);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package fish.cichlidmc.cichlid_gradle.extension.def;
2+
3+
import fish.cichlidmc.cichlid_gradle.util.hash.Encoding;
4+
5+
public record DefHash(byte[] bytes, String shortString, String longString) {
6+
public static final int SHORT_LENGTH = 6;
7+
8+
public static DefHash of(byte[] bytes) {
9+
return new DefHash(
10+
bytes,
11+
Encoding.BASE36.encode(bytes).substring(0, SHORT_LENGTH),
12+
Encoding.BASE_FUNNY.encode(bytes)
13+
);
14+
}
15+
}

plugin/src/main/java/fish/cichlidmc/cichlid_gradle/extension/def/MinecraftDefinitionImpl.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import fish.cichlidmc.cichlid_gradle.run.RunConfigurationImpl;
66
import fish.cichlidmc.cichlid_gradle.util.Distribution;
77
import fish.cichlidmc.cichlid_gradle.util.Utils;
8-
import fish.cichlidmc.cichlid_gradle.util.hash.Encoding;
98
import fish.cichlidmc.cichlid_gradle.util.hash.HashAlgorithm;
109
import org.gradle.api.InvalidUserDataException;
1110
import org.gradle.api.NamedDomainObjectContainer;
@@ -34,7 +33,7 @@ public final class MinecraftDefinitionImpl implements MinecraftDefinition {
3433

3534
private final Provider<ExternalModuleDependency> dependency;
3635

37-
private String hash;
36+
private DefHash hash;
3837

3938
public MinecraftDefinitionImpl(String name, Project project) {
4039
if (name.contains("$")) {
@@ -56,9 +55,7 @@ public MinecraftDefinitionImpl(String name, Project project) {
5655
this.transformers = TransformersImpl.of(name, project.getConfigurations());
5756

5857
DependencyFactory depFactory = project.getDependencyFactory();
59-
this.dependency = project.getProviders().provider(
60-
() -> depFactory.create("net.minecraft", "minecraft", name + '$' + this.hash())
61-
);
58+
this.dependency = project.getProviders().provider(() -> this.createDependency(depFactory));
6259
}
6360

6461
@Override
@@ -103,7 +100,7 @@ public Distribution dist() {
103100
return this.distribution.get();
104101
}
105102

106-
private String hash() throws IOException {
103+
public DefHash hash() throws IOException {
107104
if (this.hash != null)
108105
return this.hash;
109106

@@ -126,7 +123,19 @@ private String hash() throws IOException {
126123
}
127124
}
128125

129-
this.hash = Encoding.BASE_FUNNY.encode(digest.digest());
126+
this.hash = DefHash.of(digest.digest());
130127
return this.hash;
131128
}
129+
130+
private ExternalModuleDependency createDependency(DependencyFactory factory) throws IOException {
131+
String module = "minecraft-" + this.dist();
132+
String version = this.version() + '$' + this.name + '$' + this.hash().shortString();
133+
ExternalModuleDependency dep = factory.create("net.minecraft", module, version);
134+
135+
// set as changing so gradle only caches it for 24 hours.
136+
// since the hash is short, collisions aren't out of the question over a long period of time.
137+
dep.setChanging(true);
138+
139+
return dep;
140+
}
132141
}

plugin/src/main/java/fish/cichlidmc/cichlid_gradle/extension/repo/MinecraftReposExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MavenArtifactRepository versions() {
5050

5151
repos.exclusiveContent(exclusive -> exclusive.forRepositories(maven).filter(contents -> {
5252
contents.includeGroup(MinecraftMaven.GROUP);
53-
contents.includeModule(MinecraftMaven.GROUP, MinecraftMaven.MODULE);
53+
MinecraftMaven.MODULES.forEach(module -> contents.includeModule(MinecraftMaven.GROUP, module));
5454
}));
5555

5656
return maven;

0 commit comments

Comments
 (0)