Added the Gradle build system and added a new native loader

This commit is contained in:
Lemmmy
2015-08-25 23:00:34 +01:00
parent 20d40dd520
commit dea0104c4d
16 changed files with 546 additions and 78 deletions

View File

@@ -0,0 +1,108 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014, 2015 Jeffrey Han
*
* opsu! 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! 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!. If not, see <http://www.gnu.org/licenses/>.
*/
package itdelatrisu.opsu;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.CodeSource;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Native loader, based on the JarSplice launcher.
*
* @author http://ninjacave.com
*/
public class NativeLoader {
/** Directory where natives are unpacked. */
public static final File NATIVE_DIR = new File("Natives/");
/**
* Unpacks natives for the current operating system to the natives directory.
* @throws IOException
*/
public void loadNatives() throws IOException {
if (!NATIVE_DIR.exists())
NATIVE_DIR.mkdir();
CodeSource src = NativeLoader.class.getProtectionDomain().getCodeSource();
if (src != null) {
URI jar = null;
try {
jar = src.getLocation().toURI();
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
JarFile jarFile = new JarFile(new File(jar), false);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
if (e == null)
break;
File f = new File(NATIVE_DIR, e.getName());
if (isNativeFile(e.getName()) && !e.isDirectory() && e.getName().indexOf('/') == -1 && !f.exists()) {
InputStream in = jarFile.getInputStream(jarFile.getEntry(e.getName()));
OutputStream out = new FileOutputStream(f);
byte[] buffer = new byte[65536];
int bufferSize;
while ((bufferSize = in.read(buffer, 0, buffer.length)) != -1)
out.write(buffer, 0, bufferSize);
in.close();
out.close();
}
}
jarFile.close();
}
}
/**
* Returns whether the given file name is a native file for the current operating system.
* @param entryName the file name
* @return true if the file is a native that should be loaded, false otherwise
*/
private boolean isNativeFile(String entryName) {
String osName = System.getProperty("os.name");
String name = entryName.toLowerCase();
if (osName.startsWith("Win")) {
if (name.endsWith(".dll"))
return true;
} else if (osName.startsWith("Linux")) {
if (name.endsWith(".so"))
return true;
} else if (((osName.startsWith("Mac")) || (osName.startsWith("Darwin"))) && ((name.endsWith(".jnilib")) || (name.endsWith(".dylib")))) {
return true;
}
return false;
}
}

View File

@@ -22,26 +22,8 @@ import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.db.DBController;
import itdelatrisu.opsu.downloads.DownloadList;
import itdelatrisu.opsu.downloads.Updater;
import itdelatrisu.opsu.states.ButtonMenu;
import itdelatrisu.opsu.states.DownloadsMenu;
import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.states.GamePauseMenu;
import itdelatrisu.opsu.states.GameRanking;
import itdelatrisu.opsu.states.MainMenu;
import itdelatrisu.opsu.states.OptionsMenu;
import itdelatrisu.opsu.states.SongMenu;
import itdelatrisu.opsu.states.Splash;
import itdelatrisu.opsu.states.*;
import itdelatrisu.opsu.ui.UI;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
@@ -53,6 +35,12 @@ import org.newdawn.slick.util.FileSystemLocation;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import java.io.*;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
/**
* Main class.
* <p>
@@ -131,10 +119,30 @@ public class Opsu extends StateBasedGame {
System.exit(1);
}
// set path for lwjgl natives - NOT NEEDED if using JarSplice
File nativeDir = new File("./target/natives/");
if (nativeDir.isDirectory())
System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
File nativeDir;
if ((nativeDir = new File("./target/natives/")).isDirectory() ||
(nativeDir = new File("./build/natives/")).isDirectory())
;
else {
nativeDir = NativeLoader.NATIVE_DIR;
try {
new NativeLoader().loadNatives();
} catch (IOException e) {
Log.error("Error loading natives.", e);
}
}
System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
System.setProperty("java.library.path", nativeDir.getAbsolutePath());
try {
// Workaround for "java.library.path" property being read-only.
// http://stackoverflow.com/a/24988095
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
Log.warn("Failed to set 'sys_paths' field.", e);
}
// set the resource paths
ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/")));
@@ -219,10 +227,10 @@ public class Opsu extends StateBasedGame {
// show confirmation dialog if any downloads are active
if (DownloadList.get().hasActiveDownloads() &&
UI.showExitConfirmation(DownloadList.EXIT_CONFIRMATION))
UI.showExitConfirmation(DownloadList.EXIT_CONFIRMATION))
return false;
if (Updater.get().getStatus() == Updater.Status.UPDATE_DOWNLOADING &&
UI.showExitConfirmation(Updater.EXIT_CONFIRMATION))
UI.showExitConfirmation(Updater.EXIT_CONFIRMATION))
return false;
return true;
@@ -257,7 +265,7 @@ public class Opsu extends StateBasedGame {
// JARs will not run properly inside directories containing '!'
// http://bugs.java.com/view_bug.do?bug_id=4523159
if (Utils.isJarRunning() && Utils.getRunningDirectory() != null &&
Utils.getRunningDirectory().getAbsolutePath().indexOf('!') != -1)
Utils.getRunningDirectory().getAbsolutePath().indexOf('!') != -1)
ErrorHandler.error("JARs cannot be run from some paths containing '!'. Please move or rename the file and try again.", null, false);
else
ErrorHandler.error(message, e, true);

View File

@@ -80,7 +80,7 @@ public class Utils {
24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47
};
static {
Arrays.sort(illegalChars);
Arrays.sort(illegalChars);
}
// game-related variables
@@ -323,15 +323,15 @@ public class Utils {
return null;
boolean doReplace = (replace > 0 && Arrays.binarySearch(illegalChars, replace) < 0);
StringBuilder cleanName = new StringBuilder();
for (int i = 0, n = badFileName.length(); i < n; i++) {
int c = badFileName.charAt(i);
if (Arrays.binarySearch(illegalChars, c) < 0)
cleanName.append((char) c);
else if (doReplace)
cleanName.append(replace);
}
return cleanName.toString();
StringBuilder cleanName = new StringBuilder();
for (int i = 0, n = badFileName.length(); i < n; i++) {
int c = badFileName.charAt(i);
if (Arrays.binarySearch(illegalChars, c) < 0)
cleanName.append((char) c);
else if (doReplace)
cleanName.append(replace);
}
return cleanName.toString();
}
/**
@@ -585,4 +585,11 @@ public class Utils {
public static boolean parseBoolean(String s) {
return (Integer.parseInt(s) == 1);
}
/**
* Linear interpolation of a and b at t.
*/
public static float lerp(float a, float b, float t) {
return a * (1 - t) + b * t;
}
}

View File

@@ -19,6 +19,7 @@
package itdelatrisu.opsu.objects.curves;
import itdelatrisu.opsu.ErrorHandler;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.beatmap.HitObject;
import org.newdawn.slick.Color;
@@ -161,7 +162,7 @@ public class CircumscribedCircle extends Curve {
@Override
public float[] pointAt(float t) {
float ang = lerp(startAng, endAng, t);
float ang = Utils.lerp(startAng, endAng, t);
return new float[] {
(float) (Math.cos(ang) * radius + circleCenter.x),
(float) (Math.sin(ang) * radius + circleCenter.y)

View File

@@ -150,14 +150,6 @@ public abstract class Curve {
* @param i the control point index
*/
public float getY(int i) { return (i == 0) ? y : sliderY[i - 1]; }
/**
* Linear interpolation of a and b at t.
*/
protected float lerp(float a, float b, float t) {
return a * (1 - t) + b * t;
}
/**
* Discards the slider cache (only used for mmsliders).
*/

View File

@@ -18,6 +18,7 @@
package itdelatrisu.opsu.objects.curves;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.beatmap.HitObject;
import java.util.Iterator;
@@ -94,7 +95,7 @@ public abstract class EqualDistanceMultiCurve extends Curve {
// interpolate the point between the two closest distances
if (distanceAt - lastDistanceAt > 1) {
float t = (prefDistance - lastDistanceAt) / (distanceAt - lastDistanceAt);
curve[i] = new Vec2f(lerp(lastCurve.x, thisCurve.x, t), lerp(lastCurve.y, thisCurve.y, t));
curve[i] = new Vec2f(Utils.lerp(lastCurve.x, thisCurve.x, t), Utils.lerp(lastCurve.y, thisCurve.y, t));
} else
curve[i] = thisCurve;
}
@@ -128,8 +129,8 @@ public abstract class EqualDistanceMultiCurve extends Curve {
Vec2f poi2 = curve[index + 1];
float t2 = indexF - index;
return new float[] {
lerp(poi.x, poi2.x, t2),
lerp(poi.y, poi2.y, t2)
Utils.lerp(poi.x, poi2.x, t2),
Utils.lerp(poi.y, poi2.y, t2)
};
}
}