|
9 | 9 | import java.io.InputStream; |
10 | 10 | import java.net.URISyntaxException; |
11 | 11 | import java.net.URL; |
| 12 | +import java.net.URLDecoder; |
| 13 | +import java.util.Arrays; |
12 | 14 | import java.util.Enumeration; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.jar.JarEntry; |
| 18 | +import java.util.jar.JarFile; |
13 | 19 |
|
14 | 20 | import me.otho.customItems.registry.Registry; |
| 21 | +import me.otho.customItems.util.LogHelper; |
15 | 22 |
|
16 | 23 | import org.apache.commons.lang3.ArrayUtils; |
17 | 24 |
|
@@ -96,6 +103,7 @@ private static void mergeGson(JsonSchema data, JsonSchema mergeTo) |
96 | 103 | public static boolean unpackConfigFile(Class obj, String path, String destinationPath) throws IOException, URISyntaxException |
97 | 104 | { |
98 | 105 | String[] resources = getResourceListing(obj, path); |
| 106 | + LogHelper.info(Arrays.deepToString(resources)); |
99 | 107 | int i; |
100 | 108 | if(resources != null){ |
101 | 109 | for(i=0; i< resources.length; i++){ |
@@ -131,16 +139,52 @@ public static boolean unpackConfigFile(Class obj, String path, String destinatio |
131 | 139 | } |
132 | 140 | } |
133 | 141 |
|
134 | | - return true; |
| 142 | + LogHelper.info("Failed to load default config files - 2"); |
| 143 | + return false; |
135 | 144 | } |
136 | 145 |
|
137 | 146 | public static String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException { |
138 | 147 | URL dirURL = clazz.getClassLoader().getResource(path); |
| 148 | + |
139 | 149 | if (dirURL != null && dirURL.getProtocol().equals("file")) { |
140 | 150 | String[] list = new File(dirURL.toURI()).list(); |
141 | 151 | return list; |
142 | | - } |
| 152 | + } |
| 153 | + |
| 154 | + if (dirURL == null) { |
| 155 | + /* |
| 156 | + * In case of a jar file, we can't actually find a directory. |
| 157 | + * Have to assume the same jar as clazz. |
| 158 | + */ |
| 159 | + String me = clazz.getName().replace(".", "/")+".class"; |
| 160 | + dirURL = clazz.getClassLoader().getResource(me); |
| 161 | + } |
143 | 162 |
|
| 163 | + if (dirURL.getProtocol().equals("jar")) { |
| 164 | + /* A JAR path */ |
| 165 | + String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file |
| 166 | + JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); |
| 167 | + Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar |
| 168 | + Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory |
| 169 | + while(entries.hasMoreElements()) { |
| 170 | + String name = entries.nextElement().getName(); |
| 171 | + if (name.startsWith(path)) { //filter according to the path |
| 172 | + String entry = name.substring(path.length()); |
| 173 | + int checkSubdir = entry.indexOf("/"); |
| 174 | + if (checkSubdir >= 0) { |
| 175 | + // if it is a subdirectory, we just return the directory name |
| 176 | + entry = entry.substring(0, checkSubdir); |
| 177 | + } |
| 178 | + result.add(entry); |
| 179 | + } |
| 180 | + } |
| 181 | + jar.close(); |
| 182 | + String[] ret = result.toArray(new String[result.size()]); |
| 183 | + LogHelper.info("Return: " + ret); |
| 184 | + return ret; |
| 185 | + } |
| 186 | + |
| 187 | + LogHelper.info("Failed to load default config files"); |
144 | 188 | throw new UnsupportedOperationException("Cannot list files for URL "+dirURL); |
145 | 189 | } |
146 | 190 |
|
|
0 commit comments