Skip to content

Commit bdc26f7

Browse files
committed
Fix MethodScript usage from path including a "+"
Fix MethodScript usage from a file path that includes a "+" placing its configuration files / extensions / etc in that file path excluding the "+". This was tested on Windows using MethodScript path C:\Users\NAME\Desktop\Test `~!@#$%€^&()-_=+[{]};'.,²³¤€¼½¾‘’¥×¶´ç¿ test\MethodScript\MethodScript.jar.
1 parent 6dfa587 commit bdc26f7

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/main/java/com/laytonsmith/PureUtilities/ClassLoading/ClassDiscovery.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@ public synchronized void addDiscoveryLocation(URL url) {
414414
if(url == null) {
415415
throw new NullPointerException("url cannot be null");
416416
}
417+
418+
// Encode "+" character in URL, which is not encoded in URL creation like other special characters.
419+
try {
420+
url = new URL(url.toString().replace("+", "%2B"));
421+
} catch (MalformedURLException e) {
422+
throw new Error(e);
423+
}
424+
417425
if(urlCache.contains(url) && classCache.containsKey(url)) {
418426
//Already here, so just return.
419427
return;
@@ -1173,17 +1181,21 @@ public static URL GetClassContainer(Class<?> c) {
11731181
try {
11741182
packageRoot = StringUtils.replaceLast(thisClass, Pattern.quote(c.getName().replace('.', '/') + ".class"), "");
11751183
} catch (Exception e) {
1176-
//Hmm, ok, try this then
1184+
1185+
// Get location through protection domain.
11771186
packageRoot = c.getProtectionDomain().getCodeSource().getLocation().toString();
11781187
}
1179-
packageRoot = URLDecoder.decode(packageRoot, "UTF-8");
1180-
if(packageRoot.matches("jar:file:.*!/")) {
1181-
packageRoot = StringUtils.replaceLast(packageRoot, "!/", "");
1182-
packageRoot = packageRoot.replaceFirst("jar:", "");
1188+
1189+
// Encode "+" character in URL, which is not encoded in URL creation like other special characters.
1190+
packageRoot = packageRoot.replace("+", "%2B");
1191+
1192+
// Strip jar file label and suffix.
1193+
if(packageRoot.matches("jar:.*!/")) {
1194+
packageRoot = packageRoot.substring("jar:".length(), packageRoot.length() - "!/".length());
11831195
}
1196+
1197+
// Return encoded URL.
11841198
return new URL(packageRoot);
1185-
} catch (UnsupportedEncodingException e) {
1186-
throw new RuntimeException("While interrogating " + c.getName() + ", an unexpected exception was thrown.", e);
11871199
} catch (MalformedURLException e) {
11881200
throw new RuntimeException("While interrogating " + c.getName() + ", an unexpected exception was thrown for potential URL: \"" + packageRoot + "\"", e);
11891201
}

src/main/java/com/laytonsmith/core/MethodScriptFileLocations.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public File getJarFile() {
4545
return new File(s);
4646
} else {
4747
try {
48-
return new File(URLDecoder.decode(MethodScriptFileLocations.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"));
48+
// Encode "+" character in URL, which is not encoded in URL creation like other special characters.
49+
return new File(URLDecoder.decode(MethodScriptFileLocations.class
50+
.getProtectionDomain().getCodeSource().getLocation().getFile().replace("+", "%2B"), "UTF-8"));
4951
} catch (UnsupportedEncodingException ex) {
5052
throw new Error(ex);
5153
}

0 commit comments

Comments
 (0)