better jar handling; get native files from manifest instead of iterator over every file in the jar

This commit is contained in:
yugecin
2017-05-25 01:03:15 +02:00
parent ec53f531c8
commit 6ccedf4636
9 changed files with 197 additions and 108 deletions

View File

@@ -18,9 +18,7 @@
package yugecin.opsudance.core;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.jar.JarFile;
import static yugecin.opsudance.core.Constants.PROJECT_NAME;
@@ -28,7 +26,7 @@ public class Environment {
public final boolean isJarRunning;
public final File workingdir;
public final JarFile jarfile;
public final File jarfile;
public Environment() {
Class thiz = Environment.class;
@@ -38,7 +36,7 @@ public class Environment {
this.workingdir = Paths.get(".").toAbsolutePath().normalize().toFile();
this.jarfile = null;
} else {
String wdir = thisClassLocation.substring(6); // remove the jar://
String wdir = thisClassLocation.substring(9); // remove jar:file:
String separator = "!/";
int separatorIdx = wdir.indexOf(separator);
int lastSeparatorIdx = wdir.lastIndexOf(separator);
@@ -47,15 +45,8 @@ public class Environment {
PROJECT_NAME, thisClassLocation.substring(0, lastSeparatorIdx));
throw new RuntimeException(msg);
}
File jar = new File(wdir.substring(0, separatorIdx));
this.workingdir = jar.getParentFile();
try {
this.jarfile = new JarFile(jar);
} catch (IOException e) {
String msg = String.format("Cannot read from jarfile (%s): %s", jar.getAbsolutePath(),
e.getMessage());
throw new RuntimeException(msg, e);
}
this.jarfile = new File(wdir.substring(0, separatorIdx));
this.workingdir = jarfile.getParentFile();
}
}

View File

@@ -29,8 +29,14 @@ import yugecin.opsudance.options.Configuration;
import yugecin.opsudance.options.OptionsService;
import yugecin.opsudance.render.GameObjectRenderer;
import yugecin.opsudance.skinning.SkinService;
import yugecin.opsudance.utils.ManifestWrapper;
import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import static yugecin.opsudance.utils.SyntacticSugar.closeAndSwallow;
public class InstanceContainer {
@@ -60,9 +66,22 @@ public class InstanceContainer {
public static void kickstart() {
updater = new Updater();
env = new Environment();
config = new Configuration();
NativeLoader.loadNatives();
JarFile jarfile = getJarfile();
ManifestWrapper manifest = new ManifestWrapper(getJarManifest(jarfile));
config = new Configuration(manifest);
if (jarfile != null) {
try {
NativeLoader.loadNatives(jarfile, manifest);
} catch (IOException e) {
String msg = String.format("Could not unpack native(s): %s", e.getMessage());
throw new RuntimeException(msg, e);
} finally {
closeAndSwallow(jarfile);
}
}
NativeLoader.setNativePath();
ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/")));
optionservice = new OptionsService();
@@ -86,4 +105,31 @@ public class InstanceContainer {
pauseState = new GamePauseMenu();
}
@Nullable
private static JarFile getJarfile() {
if (env.jarfile == null) {
return null;
}
try {
return new JarFile(env.jarfile);
} catch (IOException e) {
String msg = String.format("Cannot read from jarfile (%s): %s", env.jarfile.getAbsolutePath(),
e.getMessage());
throw new RuntimeException(msg, e);
}
}
@Nullable
private static Manifest getJarManifest(@Nullable JarFile jarfile) {
if (jarfile == null) {
return null;
}
try {
return jarfile.getManifest();
} catch (IOException e) {
String msg = String.format("Cannot read manifest from jarfile: %s", e.getMessage());
throw new RuntimeException(msg, e);
}
}
}

View File

@@ -17,6 +17,7 @@
*/
package yugecin.opsudance.options;
import com.sun.istack.internal.Nullable;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinReg;
@@ -30,17 +31,15 @@ import org.lwjgl.opengl.GL11;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.utils.ManifestWrapper;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -74,8 +73,8 @@ public class Configuration {
public File replayImportDir;
public File skinRootDir;
public Configuration() {
USE_XDG = areXDGDirectoriesEnabled();
public Configuration(ManifestWrapper jarmanifest) {
USE_XDG = jarmanifest.valueOrDefault(null, "Use-XDG", "").equalsIgnoreCase("true");
CONFIG_DIR = getXDGBaseDir("XDG_CONFIG_HOME", ".config");
DATA_DIR = getXDGBaseDir("XDG_DATA_HOME", ".local/share");
@@ -176,23 +175,6 @@ public class Configuration {
return loadDirectory(dir, defaultDir, kind);
}
private boolean areXDGDirectoriesEnabled() {
if (env.jarfile == null) {
return false;
}
try {
Manifest manifest = env.jarfile.getManifest();
if (manifest == null) {
return false;
}
Attributes attributes = manifest.getMainAttributes();
String value = attributes.getValue("Use-XDG");
return (value != null && value.equalsIgnoreCase("true"));
} catch (IOException e) {
return false;
}
}
/**
* Returns the directory based on the XDG base directory specification for
* Unix-like operating systems, only if the "XDG" flag is enabled.

View File

@@ -0,0 +1,54 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.utils;
import yugecin.opsudance.core.NotNull;
import yugecin.opsudance.core.Nullable;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
public class ManifestWrapper {
@Nullable
public final Manifest manifest;
public ManifestWrapper(@Nullable Manifest manifest) {
this.manifest = manifest;
}
/**
* @param attribute attribute in jarfile or null for default attributes
*/
public String valueOrDefault(@Nullable String attribute, @NotNull String key, @Nullable String dfault) {
if (manifest == null) {
return dfault;
}
Attributes attributes =
attribute == null ? manifest.getMainAttributes() : manifest.getAttributes(attribute);
if (attributes == null) {
return dfault;
}
String val = attributes.getValue(key);
if (val == null) {
return dfault;
}
return val;
}
}

View File

@@ -0,0 +1,34 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.utils;
import java.io.Closeable;
import java.io.IOException;
public class SyntacticSugar {
/**
* close the closeable and swallow exceptions, if any
*/
public static void closeAndSwallow(Closeable closable) {
try {
closable.close();
} catch (IOException ignored) {}
}
}