Fixed language loading from Jar not working
This commit is contained in:
parent
a76dda6444
commit
21121e6f96
1 changed files with 87 additions and 9 deletions
|
@ -1,6 +1,16 @@
|
|||
package com.pahimar.ee3.lib;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.pahimar.ee3.core.helper.LogHelper;
|
||||
|
@ -17,6 +27,7 @@ import com.pahimar.ee3.core.helper.LogHelper;
|
|||
public class Localizations {
|
||||
|
||||
private static final String LANG_RESOURCE_LOCATION = "/mods/ee3/lang/";
|
||||
private static final String JAR_SUBDIRECTORY = "mods/ee3/lang/";
|
||||
|
||||
public static String[] localeFiles;
|
||||
|
||||
|
@ -30,7 +41,39 @@ public class Localizations {
|
|||
*/
|
||||
public static void parseDir() {
|
||||
try {
|
||||
File resourceFolder = new File(Localizations.class.getResource(LANG_RESOURCE_LOCATION).toURI());
|
||||
URL resourceURL = Localizations.class.getResource(LANG_RESOURCE_LOCATION);
|
||||
|
||||
if (resourceURL == null) {
|
||||
throw new Exception("NULL POINTER!");
|
||||
}
|
||||
|
||||
if (resourceURL.getProtocol().equals("file")) {
|
||||
parseNormalDirectory(resourceURL);
|
||||
|
||||
} else if (resourceURL.getProtocol().equals("jar")) {
|
||||
parseJarFile(resourceURL);
|
||||
}
|
||||
|
||||
LogHelper.log(Level.INFO, "Loaded " + localeFiles.length + " localizations");
|
||||
} catch (Exception e) {
|
||||
LogHelper.log(Level.SEVERE, "Unable to load language files!");
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* parseNormalDirectory
|
||||
*
|
||||
* Parses a normal Directory on the filesystem into localeFiles
|
||||
* @author Robotic-Brain
|
||||
*
|
||||
* @param resourceURL Parent directory URL
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
private static void parseNormalDirectory(URL resourceURL) throws URISyntaxException {
|
||||
LogHelper.log(Level.INFO, "Loading FILE");
|
||||
|
||||
File resourceFolder = new File(resourceURL.toURI());
|
||||
File[] files = resourceFolder.listFiles();
|
||||
|
||||
localeFiles = new String[files.length];
|
||||
|
@ -40,9 +83,44 @@ public class Localizations {
|
|||
localeFiles[i++] = LANG_RESOURCE_LOCATION + resource.getName();
|
||||
LogHelper.log(Level.INFO, "Added localization file: " + resource.getName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogHelper.log(Level.WARNING, "Unable to load language files!");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* parseJarFile
|
||||
*
|
||||
* Walks a jar file and ads all files in given "subdirectory" (JAR_SUBDIRECTORY) to localeFiles
|
||||
* @author Robotic-Brain
|
||||
*
|
||||
* @param resourceURL
|
||||
* @throws UnsupportedEncodingException
|
||||
* @throws IOException
|
||||
*/
|
||||
private static void parseJarFile(URL resourceURL) throws UnsupportedEncodingException, IOException {
|
||||
LogHelper.log(Level.INFO, "Loading JAR");
|
||||
|
||||
// Getting Jar file Object
|
||||
String jarFileString = resourceURL.getPath().substring(5, resourceURL.getPath().indexOf("!"));
|
||||
JarFile jarFile = new JarFile(URLDecoder.decode(jarFileString, "UTF-8"));
|
||||
|
||||
// Iterate over entries
|
||||
ArrayList<String> files = new ArrayList<String>();
|
||||
Enumeration<JarEntry> entries = jarFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
String jarSubPath = entries.nextElement().getName();
|
||||
|
||||
// Only search specific Directory
|
||||
if (jarSubPath.startsWith(JAR_SUBDIRECTORY)) {
|
||||
jarSubPath = jarSubPath.substring(JAR_SUBDIRECTORY.length());
|
||||
if (!jarSubPath.trim().isEmpty() && jarSubPath.indexOf("/") < 0) {
|
||||
// If file and not dir add to list
|
||||
files.add(LANG_RESOURCE_LOCATION + jarSubPath);
|
||||
LogHelper.log(Level.INFO, "Added localization file: " + jarSubPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
localeFiles = files.toArray(new String[files.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue