Commit 11bcac00b0f72faba0df367d841402026f3400fd

Authored by Mumfrey
1 parent 1bc2d41a

don't add file to classpath if the file is already present on the classpath - Test #67

java/common/com/mumfrey/liteloader/core/api/EnumeratorModuleFolder.java
@@ -48,12 +48,13 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule @@ -48,12 +48,13 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule
48 48
49 protected boolean readJarFiles; 49 protected boolean readJarFiles;
50 protected boolean loadTweaks; 50 protected boolean loadTweaks;
  51 + protected boolean forceInjection;
51 52
52 /** 53 /**
53 * True if this is a versioned folder and the enumerator should also try to load tweak jars which would normally be ignored 54 * True if this is a versioned folder and the enumerator should also try to load tweak jars which would normally be ignored
54 */ 55 */
55 protected final boolean loadTweakJars; 56 protected final boolean loadTweakJars;
56 - 57 +
57 public EnumeratorModuleFolder(LiteLoaderCoreAPI coreAPI, File directory, boolean loadTweakJars) 58 public EnumeratorModuleFolder(LiteLoaderCoreAPI coreAPI, File directory, boolean loadTweakJars)
58 { 59 {
59 this.coreAPI = coreAPI; 60 this.coreAPI = coreAPI;
@@ -66,6 +67,7 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule @@ -66,6 +67,7 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule
66 { 67 {
67 this.loadTweaks = properties.loadTweaksEnabled(); 68 this.loadTweaks = properties.loadTweaksEnabled();
68 this.readJarFiles = properties.getAndStoreBooleanProperty(LoaderProperties.OPTION_SEARCH_JARFILES, true); 69 this.readJarFiles = properties.getAndStoreBooleanProperty(LoaderProperties.OPTION_SEARCH_JARFILES, true);
  70 + this.forceInjection = properties.getAndStoreBooleanProperty(LoaderProperties.OPTION_FORCE_INJECTION, false);
69 71
70 this.coreAPI.writeDiscoverySettings(); 72 this.coreAPI.writeDiscoverySettings();
71 } 73 }
@@ -77,6 +79,7 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule @@ -77,6 +79,7 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule
77 public void writeSettings(LoaderEnvironment environment, LoaderProperties properties) 79 public void writeSettings(LoaderEnvironment environment, LoaderProperties properties)
78 { 80 {
79 properties.setBooleanProperty(LoaderProperties.OPTION_SEARCH_JARFILES, this.readJarFiles); 81 properties.setBooleanProperty(LoaderProperties.OPTION_SEARCH_JARFILES, this.readJarFiles);
  82 + properties.setBooleanProperty(LoaderProperties.OPTION_FORCE_INJECTION, this.forceInjection);
80 } 83 }
81 84
82 /* (non-Javadoc) 85 /* (non-Javadoc)
@@ -146,6 +149,7 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule @@ -146,6 +149,7 @@ public class EnumeratorModuleFolder implements FilenameFilter, EnumeratorModule
146 for (File file : this.directory.listFiles(this.getFilenameFilter())) 149 for (File file : this.directory.listFiles(this.getFilenameFilter()))
147 { 150 {
148 LoadableFile candidateFile = new LoadableFile(file); 151 LoadableFile candidateFile = new LoadableFile(file);
  152 + candidateFile.setForceInjection(this.forceInjection);
149 try 153 try
150 { 154 {
151 this.inspectFile(enumerator, candidateFile); 155 this.inspectFile(enumerator, candidateFile);
java/common/com/mumfrey/liteloader/interfaces/LoadableFile.java
@@ -39,6 +39,8 @@ public class LoadableFile extends File implements TweakContainer<File> @@ -39,6 +39,8 @@ public class LoadableFile extends File implements TweakContainer<File>
39 */ 39 */
40 protected boolean injected; 40 protected boolean injected;
41 41
  42 + protected boolean forceInjection;
  43 +
42 /** 44 /**
43 * Position to inject the mod file at in the class path, if blank injects at the bottom as usual, alternatively 45 * Position to inject the mod file at in the class path, if blank injects at the bottom as usual, alternatively
44 * the developer can specify "top" to inject at the top, "base" to inject above the game jar, or "above: name" to 46 * the developer can specify "top" to inject at the top, "base" to inject above the game jar, or "above: name" to
@@ -87,6 +89,7 @@ public class LoadableFile extends File implements TweakContainer<File> @@ -87,6 +89,7 @@ public class LoadableFile extends File implements TweakContainer<File>
87 { 89 {
88 super(file.getAbsolutePath()); 90 super(file.getAbsolutePath());
89 this.displayName = this.getName(); 91 this.displayName = this.getName();
  92 + this.forceInjection = file.forceInjection;
90 this.assignJarMetaData(file); 93 this.assignJarMetaData(file);
91 } 94 }
92 95
@@ -295,6 +298,15 @@ public class LoadableFile extends File implements TweakContainer<File> @@ -295,6 +298,15 @@ public class LoadableFile extends File implements TweakContainer<File>
295 return new ArrayList<String>(); 298 return new ArrayList<String>();
296 } 299 }
297 300
  301 + public boolean isInjectionForced()
  302 + {
  303 + return this.forceInjection;
  304 + }
  305 +
  306 + public void setForceInjection(boolean forceInjection)
  307 + {
  308 + this.forceInjection = forceInjection;
  309 + }
298 310
299 @Override 311 @Override
300 public boolean isInjected() 312 public boolean isInjected()
@@ -307,6 +319,15 @@ public class LoadableFile extends File implements TweakContainer&lt;File&gt; @@ -307,6 +319,15 @@ public class LoadableFile extends File implements TweakContainer&lt;File&gt;
307 { 319 {
308 if (!this.injected) 320 if (!this.injected)
309 { 321 {
  322 + this.injected = true;
  323 +
  324 + boolean isOnClassPath = ClassPathUtilities.isJarOnClassPath(this, classLoader);
  325 + if (!this.forceInjection && isOnClassPath)
  326 + {
  327 + LiteLoaderLogger.info("%s already exists on the classpath, skipping injection", this);
  328 + return false;
  329 + }
  330 +
310 ClassPathUtilities.injectIntoClassPath(classLoader, this.getURL(), this.getInjectionStrategy()); 331 ClassPathUtilities.injectIntoClassPath(classLoader, this.getURL(), this.getInjectionStrategy());
311 332
312 if (injectIntoParent) 333 if (injectIntoParent)
@@ -314,8 +335,8 @@ public class LoadableFile extends File implements TweakContainer&lt;File&gt; @@ -314,8 +335,8 @@ public class LoadableFile extends File implements TweakContainer&lt;File&gt;
314 LiteLoaderTweaker.addURLToParentClassLoader(this.getURL()); 335 LiteLoaderTweaker.addURLToParentClassLoader(this.getURL());
315 } 336 }
316 337
317 - this.injected = true;  
318 return true; 338 return true;
  339 +
319 } 340 }
320 341
321 return false; 342 return false;
java/common/com/mumfrey/liteloader/launch/ClassPathUtilities.java
@@ -232,11 +232,21 @@ public abstract class ClassPathUtilities @@ -232,11 +232,21 @@ public abstract class ClassPathUtilities
232 */ 232 */
233 public static boolean isJarOnClassPath(File jarFile) 233 public static boolean isJarOnClassPath(File jarFile)
234 { 234 {
  235 + URLClassLoader classLoader = (URLClassLoader)Launch.class.getClassLoader();
  236 + return ClassPathUtilities.isJarOnClassPath(jarFile, classLoader);
  237 + }
  238 +
  239 + /**
  240 + * @param jarFile
  241 + * @param classLoader
  242 + */
  243 + public static boolean isJarOnClassPath(File jarFile, URLClassLoader classLoader)
  244 + {
235 try 245 try
236 { 246 {
237 String jarURL = jarFile.toURI().toURL().toString(); 247 String jarURL = jarFile.toURI().toURL().toString();
238 248
239 - URL[] classPath = ((URLClassLoader)Launch.class.getClassLoader()).getURLs(); 249 + URL[] classPath = classLoader.getURLs();
240 for (URL classPathEntry : classPath) 250 for (URL classPathEntry : classPath)
241 { 251 {
242 if (classPathEntry.toString().equals(jarURL)) 252 if (classPathEntry.toString().equals(jarURL))
java/common/com/mumfrey/liteloader/launch/LoaderProperties.java
@@ -96,4 +96,5 @@ public interface LoaderProperties @@ -96,4 +96,5 @@ public interface LoaderProperties
96 public static final String OPTION_SEARCH_MODS = "search.mods"; 96 public static final String OPTION_SEARCH_MODS = "search.mods";
97 public static final String OPTION_SEARCH_CLASSPATH = "search.classpath"; 97 public static final String OPTION_SEARCH_CLASSPATH = "search.classpath";
98 public static final String OPTION_SEARCH_JARFILES = "search.jarfiles"; 98 public static final String OPTION_SEARCH_JARFILES = "search.jarfiles";
  99 + public static final String OPTION_FORCE_INJECTION = "forceInjection";
99 } 100 }