2424 * */
2525public class BuiltinPack {
2626 private static final HashMap <Identifier , byte []> DIRECT_RESOURCES = new HashMap <>();
27- private static final TreeMap <Identifier , Identifier > REDIRECTS =
27+ //Stringified identifier, longer is better
28+ private static final TreeMap <String , String > REDIRECTS =
2829 new TreeMap <>((a , b ) -> {
29- //Longer is better
30- String aStr = a .toString ();
31- String bStr = b .toString ();
32- int d = Integer .compare (bStr .length (), aStr .length ());
33- return d != 0 ? d : aStr .compareTo (bStr );
30+ int d = Integer .compare (b .length (), a .length ());
31+ return d != 0 ? d : a .compareTo (b );
3432 });
3533 private static final List <Function <Identifier , byte []>> GENERATORS = new LinkedList <>();
3634 private static final HashMap <Identifier , byte []> CACHED_GENERATOR_RESULTS = new HashMap <>();
@@ -62,13 +60,18 @@ public static void conditional(Function<Identifier, byte[]> func) {
6260 /**
6361 * Registers a resource path redirect.
6462 * <p>
65- * Any requested identifier whose string form starts with {@code targetPrefix }
66- * will be remapped back to {@code sourcePrefix } (simple replacement).
63+ * Any requested identifier whose string form starts with {@code requestedPrefix }
64+ * will be remapped back to {@code actualPrefix } (simple replacement).
6765 * This is mainly intended for compatibility aliases (e.g. cross-version path changes).
6866 */
69- public static void redirect (Identifier sourcePrefix , Identifier targetPrefix ) {
67+ public static void redirect (Identifier requestedPrefix , Identifier actualPrefix ) {
7068 //Namespaces will be redirected!
71- REDIRECTS .put (sourcePrefix , targetPrefix );
69+ String requested = requestedPrefix .toString ();
70+ String actual = actualPrefix .toString ();
71+ if (actual .startsWith (requested )) {
72+ throw new IllegalArgumentException ("Attempting to redirect to child folders, this is not allowed! Redirect with full file name instead!" );
73+ }
74+ REDIRECTS .put (requested , actual );
7275 }
7376
7477 /**
@@ -161,9 +164,9 @@ protected InputStream getInputStreamByName(String resourcePath) throws IOExcepti
161164 return new ByteArrayInputStream (DIRECT_RESOURCES .get (ident ));
162165 }
163166
164- for (Map .Entry <Identifier , Identifier > entry : REDIRECTS .entrySet ()) {
167+ for (Map .Entry <String , String > entry : REDIRECTS .entrySet ()) {
165168 String src = ident .toString ();
166- if (src .startsWith (entry .getValue (). toString ())) {
169+ if (src .startsWith (entry .getKey ())) {
167170 Identifier redirect = handleRedirect (ident , entry .getKey (), entry .getValue ());
168171 return redirect .getResourceStream ();
169172 }
@@ -190,9 +193,9 @@ protected boolean hasResourceName(String resourcePath) {
190193 return true ;
191194 }
192195
193- for (Map .Entry <Identifier , Identifier > entry : REDIRECTS .entrySet ()) {
196+ for (Map .Entry <String , String > entry : REDIRECTS .entrySet ()) {
194197 //Check if it's start with any of the [to]s
195- if (ident .toString ().startsWith (entry .getValue (). toString ())
198+ if (ident .toString ().startsWith (entry .getKey ())
196199 && handleRedirect (ident , entry .getKey (), entry .getValue ()).canLoad ()) {
197200 return true ;
198201 }
@@ -247,9 +250,9 @@ public static InputStream loadServerResource(Identifier ident) throws IOExceptio
247250 return new ByteArrayInputStream (DIRECT_RESOURCES .get (ident ));
248251 }
249252
250- for (Map .Entry <Identifier , Identifier > entry : REDIRECTS .entrySet ()) {
253+ for (Map .Entry <String , String > entry : REDIRECTS .entrySet ()) {
251254 String src = ident .toString ();
252- if (src .startsWith (entry .getValue (). toString ())) {
255+ if (src .startsWith (entry .getKey ())) {
253256 Identifier redirect = handleRedirect (ident , entry .getKey (), entry .getValue ());
254257 return redirect .getResourceStream ();
255258 }
@@ -272,10 +275,10 @@ public static InputStream loadServerResource(Identifier ident) throws IOExceptio
272275 return null ;
273276 }
274277
275- private static Identifier handleRedirect (Identifier src , Identifier sourcePrefix , Identifier targetPrefix ) {
276- //Replace [targetPrefix ] with [sourcePrefix ] to redirect the request back
277- String suffix = src .toString ().substring (targetPrefix . toString () .length ());
278- return new Identifier (sourcePrefix . toString () + suffix );
278+ private static Identifier handleRedirect (Identifier src , String requestedPrefix , String actualPrefix ) {
279+ //Replace [requestedPrefix ] with [actualPrefix ] to redirect the request back
280+ String suffix = src .toString ().substring (requestedPrefix .length ());
281+ return new Identifier (actualPrefix + suffix );
279282 }
280283
281284 private static Identifier nameToLocation (String path ) {
0 commit comments