Skip to content

Commit e7d5bc8

Browse files
committed
Fix loading ATs from classpath mods
1 parent 27b9d44 commit e7d5bc8

1 file changed

Lines changed: 113 additions & 43 deletions

File tree

src/main/resources/net/minecraftforge/gradle/GradleForgeHacks.java

Lines changed: 113 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@
1919
*/
2020
package net.minecraftforge.gradle;
2121

22+
import javax.annotation.Nullable;
2223
import java.io.File;
2324
import java.io.FileInputStream;
2425
import java.io.IOException;
2526
import java.lang.reflect.Field;
27+
import java.lang.reflect.InvocationTargetException;
2628
import java.lang.reflect.Method;
29+
import java.net.URISyntaxException;
2730
import java.net.URL;
2831
import java.net.URLClassLoader;
2932
import java.util.Collection;
3033
import java.util.List;
3134
import java.util.Map;
3235
import java.util.Set;
36+
import java.util.jar.Attributes;
3337
import java.util.jar.JarFile;
3438
import java.util.jar.Manifest;
3539

@@ -61,7 +65,7 @@ public class GradleForgeHacks
6165

6266
public static final Map<String, File> coreMap = Maps.newHashMap();
6367

64-
public static void searchCoremods(GradleStartCommon common) throws Exception
68+
public static void searchCoremods(GradleStartCommon common)
6569
{
6670
// check for argument
6771
if (common.extras.contains(NO_CORE_SEARCH))
@@ -75,69 +79,135 @@ public static void searchCoremods(GradleStartCommon common) throws Exception
7579
return;
7680
}
7781

78-
// intialize AT hack Method
79-
Method atRegistrar = null;
80-
try
82+
// initialize AT hack Method
83+
AtRegistrar atRegistrar = new AtRegistrar();
84+
85+
URLClassLoader urlClassLoader = (URLClassLoader) GradleStartCommon.class.getClassLoader();
86+
for (URL url : urlClassLoader.getURLs())
8187
{
82-
atRegistrar = Class.forName(MOD_ATD_CLASS).getDeclaredMethod(MOD_AT_METHOD, JarFile.class);
88+
try
89+
{
90+
searchCoremodAtUrl(url, atRegistrar);
91+
}
92+
catch (IOException | InvocationTargetException | IllegalAccessException | URISyntaxException e)
93+
{
94+
GradleStartCommon.LOGGER.warn("GradleForgeHacks failed to search for coremod at url {}", url, e);
95+
}
8396
}
84-
catch (Throwable t)
85-
{}
8697

87-
for (URL url : ((URLClassLoader) GradleStartCommon.class.getClassLoader()).getURLs())
98+
// set property.
99+
Set<String> coremodsSet = Sets.newHashSet();
100+
if (!Strings.isNullOrEmpty(System.getProperty(COREMOD_VAR)))
101+
coremodsSet.addAll(Splitter.on(',').splitToList(System.getProperty(COREMOD_VAR)));
102+
coremodsSet.addAll(coreMap.keySet());
103+
System.setProperty(COREMOD_VAR, Joiner.on(',').join(coremodsSet));
104+
105+
// ok.. tweaker hack now.
106+
if (!Strings.isNullOrEmpty(common.getTweakClass()))
88107
{
89-
if (!url.getProtocol().startsWith("file")) // because file urls start with file://
90-
continue; // this isnt a file
108+
common.extras.add("--tweakClass");
109+
common.extras.add("net.minecraftforge.gradle.tweakers.CoremodTweaker");
110+
}
111+
}
112+
113+
private static void searchCoremodAtUrl(URL url, AtRegistrar atRegistrar) throws IOException, InvocationTargetException, IllegalAccessException, URISyntaxException
114+
{
115+
if (!url.getProtocol().startsWith("file")) // because file urls start with file://
116+
return; // this isn't a file
91117

92-
File coreMod = new File(url.toURI().getPath());
93-
Manifest manifest = null;
118+
File coreMod = new File(url.toURI().getPath());
119+
Manifest manifest = null;
94120

95-
if (!coreMod.exists())
96-
continue;
121+
if (!coreMod.exists())
122+
return;
97123

98-
if (coreMod.isDirectory())
124+
if (coreMod.isDirectory())
125+
{
126+
File manifestMF = new File(coreMod, "META-INF/MANIFEST.MF");
127+
if (manifestMF.exists())
128+
{
129+
FileInputStream stream = new FileInputStream(manifestMF);
130+
manifest = new Manifest(stream);
131+
stream.close();
132+
}
133+
}
134+
else if (coreMod.getName().endsWith("jar")) // its a jar
135+
{
136+
try (JarFile jar = new JarFile(coreMod))
99137
{
100-
File manifestMF = new File(coreMod, "META-INF/MANIFEST.MF");
101-
if (manifestMF.exists())
138+
manifest = jar.getManifest();
139+
if (manifest != null)
102140
{
103-
FileInputStream stream = new FileInputStream(manifestMF);
104-
manifest = new Manifest(stream);
105-
stream.close();
141+
atRegistrar.addJar(jar, manifest);
106142
}
107143
}
108-
else if (coreMod.getName().endsWith("jar")) // its a jar
144+
}
145+
146+
// we got the manifest? use it.
147+
if (manifest != null)
148+
{
149+
String clazz = manifest.getMainAttributes().getValue(COREMOD_MF);
150+
if (!Strings.isNullOrEmpty(clazz))
109151
{
110-
JarFile jar = new JarFile(coreMod);
111-
manifest = jar.getManifest();
112-
if (atRegistrar != null && manifest != null)
113-
atRegistrar.invoke(null, jar);
114-
jar.close();
152+
GradleStartCommon.LOGGER.info("Found and added coremod: " + clazz);
153+
coreMap.put(clazz, coreMod);
115154
}
155+
}
156+
}
157+
158+
/**
159+
* Hack to register jar ATs with Minecraft Forge
160+
*/
161+
private static final class AtRegistrar
162+
{
163+
private static final Attributes.Name FMLAT = new Attributes.Name("FMLAT");
164+
165+
@Nullable
166+
private Method newMethod = null;
167+
@Nullable
168+
private Method oldMethod = null;
116169

117-
// we got the manifest? use it.
118-
if (manifest != null)
170+
private AtRegistrar()
171+
{
172+
try
119173
{
120-
String clazz = manifest.getMainAttributes().getValue(COREMOD_MF);
121-
if (!Strings.isNullOrEmpty(clazz))
174+
Class<?> modAtdClass = Class.forName(MOD_ATD_CLASS);
175+
try
176+
{
177+
newMethod = modAtdClass.getDeclaredMethod(MOD_AT_METHOD, JarFile.class, String.class);
178+
}
179+
catch (NoSuchMethodException | SecurityException ignored)
122180
{
123-
GradleStartCommon.LOGGER.info("Found and added coremod: " + clazz);
124-
coreMap.put(clazz, coreMod);
181+
try
182+
{
183+
oldMethod = modAtdClass.getDeclaredMethod(MOD_AT_METHOD, JarFile.class);
184+
}
185+
catch (NoSuchMethodException | SecurityException ignored2)
186+
{
187+
GradleStartCommon.LOGGER.error("Failed to find method {}.{}", MOD_ATD_CLASS, MOD_AT_METHOD);
188+
}
125189
}
126190
}
191+
catch (ClassNotFoundException e)
192+
{
193+
GradleStartCommon.LOGGER.error("Failed to find class {}", MOD_ATD_CLASS);
194+
}
127195
}
128196

129-
// set property.
130-
Set<String> coremodsSet = Sets.newHashSet();
131-
if (!Strings.isNullOrEmpty(System.getProperty(COREMOD_VAR)))
132-
coremodsSet.addAll(Splitter.on(',').splitToList(System.getProperty(COREMOD_VAR)));
133-
coremodsSet.addAll(coreMap.keySet());
134-
System.setProperty(COREMOD_VAR, Joiner.on(',').join(coremodsSet));
135-
136-
// ok.. tweaker hack now.
137-
if (!Strings.isNullOrEmpty(common.getTweakClass()))
197+
public void addJar(JarFile jarFile, Manifest manifest) throws InvocationTargetException, IllegalAccessException
138198
{
139-
common.extras.add("--tweakClass");
140-
common.extras.add("net.minecraftforge.gradle.tweakers.CoremodTweaker");
199+
if (newMethod != null)
200+
{
201+
String ats = manifest.getMainAttributes().getValue(FMLAT);
202+
if (ats != null && !ats.isEmpty())
203+
{
204+
newMethod.invoke(null, jarFile, ats);
205+
}
206+
}
207+
else if (oldMethod != null)
208+
{
209+
oldMethod.invoke(null, jarFile);
210+
}
141211
}
142212
}
143213

0 commit comments

Comments
 (0)