2929import  java .nio .charset .StandardCharsets ;
3030import  java .nio .file .Files ;
3131import  java .nio .file .Path ;
32+ import  java .util .Arrays ;
33+ import  java .util .Collection ;
3234import  java .util .Optional ;
3335import  java .util .regex .Matcher ;
3436import  java .util .regex .Pattern ;
3941 */ 
4042public  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}
0 commit comments