Commit 2465f1b2be6f89424755eb74bdc51feddc587c9e
1 parent
c20429cd
LiteLoader 1.6.4_02 - dev only - allow mods to specify classpath injection strategy in litemod.json
Showing
3 changed files
with
115 additions
and
24 deletions
java/com/mumfrey/liteloader/core/ModFile.java
@@ -95,9 +95,11 @@ public class ModFile extends File | @@ -95,9 +95,11 @@ public class ModFile extends File | ||
95 | private boolean injected; | 95 | private boolean injected; |
96 | 96 | ||
97 | /** | 97 | /** |
98 | - * True if this mod contains base class edits (dirty horribleness) inject at the TOP of the class path | 98 | + * Position to inject the mod file at in the class path, if blank injects at the bottom as usual, alternatively |
99 | + * the developer can specify "top" to inject at the top, "base" to inject above the game jar, or "above: name" to | ||
100 | + * inject above a specified other library matching "name". | ||
99 | */ | 101 | */ |
100 | - private boolean injectAtTop; | 102 | + private String injectAt; |
101 | 103 | ||
102 | /** | 104 | /** |
103 | * @param file | 105 | * @param file |
@@ -154,7 +156,7 @@ public class ModFile extends File | @@ -154,7 +156,7 @@ public class ModFile extends File | ||
154 | 156 | ||
155 | this.tweakClassName = this.metaData.get("tweakClass"); | 157 | this.tweakClassName = this.metaData.get("tweakClass"); |
156 | this.classTransformerClassName = this.metaData.get("classTransformerClass"); | 158 | this.classTransformerClassName = this.metaData.get("classTransformerClass"); |
157 | - this.injectAtTop = "top".equalsIgnoreCase(this.metaData.get("injectAt")); | 159 | + this.injectAt = this.metaData.get("injectAt"); |
158 | } | 160 | } |
159 | 161 | ||
160 | protected String getDefaultName() | 162 | protected String getDefaultName() |
@@ -221,10 +223,18 @@ public class ModFile extends File | @@ -221,10 +223,18 @@ public class ModFile extends File | ||
221 | { | 223 | { |
222 | if (!this.injected) | 224 | if (!this.injected) |
223 | { | 225 | { |
224 | - if (this.injectAtTop) | 226 | + if ("top".equals(this.injectAt)) |
225 | { | 227 | { |
226 | ClassPathInjector.injectIntoClassPath(classLoader, this.toURI().toURL()); | 228 | ClassPathInjector.injectIntoClassPath(classLoader, this.toURI().toURL()); |
227 | } | 229 | } |
230 | + else if ("base".equals(this.injectAt)) | ||
231 | + { | ||
232 | + ClassPathInjector.injectIntoClassPath(classLoader, this.toURI().toURL(), LiteLoaderTweaker.getJarUrl()); | ||
233 | + } | ||
234 | + else if (this.injectAt != null && this.injectAt.startsWith("above:")) | ||
235 | + { | ||
236 | + ClassPathInjector.injectIntoClassPath(classLoader, this.toURI().toURL(), this.injectAt.substring(6)); | ||
237 | + } | ||
228 | 238 | ||
229 | if (injectIntoParent) | 239 | if (injectIntoParent) |
230 | { | 240 | { |
java/com/mumfrey/liteloader/launch/ClassPathInjector.java
@@ -5,35 +5,54 @@ import java.net.URL; | @@ -5,35 +5,54 @@ import java.net.URL; | ||
5 | import java.net.URLClassLoader; | 5 | import java.net.URLClassLoader; |
6 | import java.util.ArrayList; | 6 | import java.util.ArrayList; |
7 | import java.util.Stack; | 7 | import java.util.Stack; |
8 | +import java.util.logging.Level; | ||
9 | +import java.util.logging.Logger; | ||
8 | 10 | ||
9 | import net.minecraft.launchwrapper.LaunchClassLoader; | 11 | import net.minecraft.launchwrapper.LaunchClassLoader; |
10 | import sun.misc.URLClassPath; | 12 | import sun.misc.URLClassPath; |
11 | 13 | ||
12 | /** | 14 | /** |
13 | - * Nasty horrible reflection hack to inject a classpath entry at the top of the classpath stack | 15 | + * Nasty horrible reflection hack to inject a classpath entry at positons in classpath other than at the bottom |
14 | * | 16 | * |
15 | * @author Adam Mummery-Smith | 17 | * @author Adam Mummery-Smith |
16 | */ | 18 | */ |
17 | public abstract class ClassPathInjector | 19 | public abstract class ClassPathInjector |
18 | { | 20 | { |
21 | + private static Logger logger = Logger.getLogger("liteloader"); | ||
22 | + | ||
23 | + /** | ||
24 | + * URLClassLoader::ucp -> instance of URLClassPath | ||
25 | + */ | ||
19 | private static Field ucp; | 26 | private static Field ucp; |
27 | + | ||
28 | + /** | ||
29 | + * URLClassLoader::urls -> instance of Stack<URL> | ||
30 | + */ | ||
20 | private static Field classPathURLs; | 31 | private static Field classPathURLs; |
32 | + | ||
33 | + /** | ||
34 | + * URLClassLoader::path -> instance of ArrayList<URL> | ||
35 | + */ | ||
21 | private static Field classPathPath; | 36 | private static Field classPathPath; |
22 | 37 | ||
38 | + private static boolean canInject; | ||
39 | + | ||
23 | static | 40 | static |
24 | { | 41 | { |
25 | try | 42 | try |
26 | { | 43 | { |
27 | - ucp = URLClassLoader.class.getDeclaredField("ucp"); | ||
28 | - ucp.setAccessible(true); | ||
29 | - classPathURLs = URLClassPath.class.getDeclaredField("urls"); | ||
30 | - classPathURLs.setAccessible(true); | ||
31 | - classPathPath = URLClassPath.class.getDeclaredField("path"); | ||
32 | - classPathPath.setAccessible(true); | 44 | + ClassPathInjector.ucp = URLClassLoader.class.getDeclaredField("ucp"); |
45 | + ClassPathInjector.ucp.setAccessible(true); | ||
46 | + ClassPathInjector.classPathURLs = URLClassPath.class.getDeclaredField("urls"); | ||
47 | + ClassPathInjector.classPathURLs.setAccessible(true); | ||
48 | + ClassPathInjector.classPathPath = URLClassPath.class.getDeclaredField("path"); | ||
49 | + ClassPathInjector.classPathPath.setAccessible(true); | ||
50 | + ClassPathInjector.canInject = true; | ||
33 | } | 51 | } |
34 | - catch (Exception ex) | 52 | + catch (Throwable th) |
35 | { | 53 | { |
36 | - ex.printStackTrace(); | 54 | + ClassPathInjector.logger.log(Level.SEVERE, "ClassPathInjector: Error initialising ClassPathInjector, special class path injection disabled", th); |
55 | + th.printStackTrace(); | ||
37 | } | 56 | } |
38 | } | 57 | } |
39 | 58 | ||
@@ -43,30 +62,82 @@ public abstract class ClassPathInjector | @@ -43,30 +62,82 @@ public abstract class ClassPathInjector | ||
43 | * @param classLoader | 62 | * @param classLoader |
44 | * @param url | 63 | * @param url |
45 | */ | 64 | */ |
46 | - @SuppressWarnings({ "rawtypes", "unchecked" }) | ||
47 | public static void injectIntoClassPath(URLClassLoader classLoader, URL url) | 65 | public static void injectIntoClassPath(URLClassLoader classLoader, URL url) |
48 | { | 66 | { |
49 | - try | 67 | + ClassPathInjector.injectIntoClassPath(classLoader, url, null); |
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * Injects a URL into the classpath at the TOP of the stack | ||
72 | + * | ||
73 | + * @param classLoader | ||
74 | + * @param url | ||
75 | + * @param above | ||
76 | + */ | ||
77 | + @SuppressWarnings({ "unchecked" }) | ||
78 | + public static void injectIntoClassPath(URLClassLoader classLoader, URL url, URL above) | ||
79 | + { | ||
80 | + if (ClassPathInjector.canInject) | ||
50 | { | 81 | { |
51 | - URLClassPath classPath = (URLClassPath)ucp.get(classLoader); | ||
52 | - | ||
53 | - Stack urls = (Stack)classPathURLs.get(classPath); | ||
54 | - ArrayList path = (ArrayList)classPathPath.get(classPath); | 82 | + ClassPathInjector.logger.info(String.format("ClassPathInjector: attempting to inject %s into %s", url, classLoader.getClass().getSimpleName())); |
55 | 83 | ||
56 | - synchronized (urls) | 84 | + try |
57 | { | 85 | { |
58 | - if (!path.contains(url)) | 86 | + URLClassPath classPath = (URLClassPath)ClassPathInjector.ucp.get(classLoader); |
87 | + | ||
88 | + Stack<URL> urls = (Stack<URL>)ClassPathInjector.classPathURLs.get(classPath); | ||
89 | + ArrayList<URL> path = (ArrayList<URL>)ClassPathInjector.classPathPath.get(classPath); | ||
90 | + | ||
91 | + synchronized (urls) | ||
59 | { | 92 | { |
60 | - urls.add(url); | ||
61 | - path.add(0, url); | 93 | + if (!path.contains(url)) |
94 | + { | ||
95 | + urls.add(url); | ||
96 | + | ||
97 | + if (above == null) | ||
98 | + { | ||
99 | + path.add(0, url); | ||
100 | + } | ||
101 | + else | ||
102 | + { | ||
103 | + for (int pos = path.size() - 1; pos > 0; pos--) | ||
104 | + { | ||
105 | + if (above.equals(path.get(pos))) | ||
106 | + path.add(pos, url); | ||
107 | + } | ||
108 | + } | ||
109 | + } | ||
62 | } | 110 | } |
63 | } | 111 | } |
112 | + catch (Exception ex) | ||
113 | + { | ||
114 | + ClassPathInjector.logger.warning(String.format("ClassPathInjector: failed to inject %s", url)); | ||
115 | + } | ||
64 | } | 116 | } |
65 | - catch (Exception ex) {} | ||
66 | 117 | ||
67 | if (classLoader instanceof LaunchClassLoader) | 118 | if (classLoader instanceof LaunchClassLoader) |
68 | { | 119 | { |
69 | ((LaunchClassLoader)classLoader).addURL(url); | 120 | ((LaunchClassLoader)classLoader).addURL(url); |
70 | } | 121 | } |
71 | } | 122 | } |
123 | + | ||
124 | + /** | ||
125 | + * @param classLoader | ||
126 | + * @param url | ||
127 | + * @param above | ||
128 | + */ | ||
129 | + public static void injectIntoClassPath(LaunchClassLoader classLoader, URL url, String above) | ||
130 | + { | ||
131 | + above = above.trim().toLowerCase(); | ||
132 | + if (above.length() < 1) return; | ||
133 | + | ||
134 | + for (URL classPathUrl : classLoader.getURLs()) | ||
135 | + { | ||
136 | + if (classPathUrl.toString().toLowerCase().contains(above)) | ||
137 | + { | ||
138 | + ClassPathInjector.injectIntoClassPath(classLoader, url, classPathUrl); | ||
139 | + return; | ||
140 | + } | ||
141 | + } | ||
142 | + } | ||
72 | } | 143 | } |
java/com/mumfrey/liteloader/launch/LiteLoaderTweaker.java
@@ -52,6 +52,8 @@ public class LiteLoaderTweaker implements ITweaker | @@ -52,6 +52,8 @@ public class LiteLoaderTweaker implements ITweaker | ||
52 | 52 | ||
53 | private static Set<String> modTransformers = new HashSet<String>(); | 53 | private static Set<String> modTransformers = new HashSet<String>(); |
54 | 54 | ||
55 | + private static URL jarUrl; | ||
56 | + | ||
55 | private List<String> singularLaunchArgs = new ArrayList<String>(); | 57 | private List<String> singularLaunchArgs = new ArrayList<String>(); |
56 | 58 | ||
57 | private Map<String, String> launchArgs; | 59 | private Map<String, String> launchArgs; |
@@ -104,6 +106,9 @@ public class LiteLoaderTweaker implements ITweaker | @@ -104,6 +106,9 @@ public class LiteLoaderTweaker implements ITweaker | ||
104 | LiteLoaderTweaker.modsToLoad = this.modsOption.values(this.parsedOptions); | 106 | LiteLoaderTweaker.modsToLoad = this.modsOption.values(this.parsedOptions); |
105 | } | 107 | } |
106 | 108 | ||
109 | + URL[] urls = Launch.classLoader.getURLs(); | ||
110 | + LiteLoaderTweaker.jarUrl = urls[urls.length - 1]; | ||
111 | + | ||
107 | this.preInit(); | 112 | this.preInit(); |
108 | } | 113 | } |
109 | 114 | ||
@@ -345,4 +350,9 @@ public class LiteLoaderTweaker implements ITweaker | @@ -345,4 +350,9 @@ public class LiteLoaderTweaker implements ITweaker | ||
345 | 350 | ||
346 | return false; | 351 | return false; |
347 | } | 352 | } |
353 | + | ||
354 | + public static URL getJarUrl() | ||
355 | + { | ||
356 | + return LiteLoaderTweaker.jarUrl; | ||
357 | + } | ||
348 | } | 358 | } |
349 | \ No newline at end of file | 359 | \ No newline at end of file |