Added a module for unpacking OSZ archives.

- Included 'zip4j' library to extract the ZIP archives. (Homepage: http://www.lingala.net/zip4j/)
- OSZ files are unpacked from the root directory by default, and the path can be changed in the configuration file.

Other changes:
- Fixed a critical issue in splash screen where thread would running before other states loaded. (blame: dea7e94)

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han
2014-07-06 01:58:44 -04:00
parent dea7e942a3
commit d9c251ef82
281 changed files with 62406 additions and 33 deletions

View File

@@ -0,0 +1,110 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 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.FilenameFilter;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
/**
* Unpacker for OSZ (ZIP) archives.
*/
public class OszUnpacker {
/**
* The index of the current file being unpacked.
*/
private static int fileIndex = -1;
/**
* The total number of directories to parse.
*/
private static File[] files;
// This class should not be instantiated.
private OszUnpacker() {}
/**
* Invokes the unpacker for each OSZ archive in a root directory.
* @param root the root directory
* @param dest the destination directory
*/
public static void unpackAllFiles(File root, File dest) {
files = root.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".osz");
}
});
if (files.length < 1) {
files = null;
return;
}
for (File file : files) {
fileIndex++;
String dirName = file.getName().substring(0, file.getName().lastIndexOf('.'));
File songDir = new File(dest, dirName);
if (!songDir.isDirectory()) {
songDir.mkdir();
unzip(file, songDir);
}
file.delete(); // delete the OSZ when finished
}
fileIndex = -1;
files = null;
}
/**
* Extracts the contents of a ZIP archive to a destination.
* @param file the ZIP archive
* @param dest the destination directory
*/
private static void unzip(File file, File dest) {
try {
ZipFile zipFile = new ZipFile(file);
zipFile.extractAll(dest.getAbsolutePath());
} catch (ZipException e) {
e.printStackTrace();
}
}
/**
* Returns the name of the current file being unpacked, or null if none.
*/
public static String getCurrentFileName() {
if (files == null || fileIndex == -1)
return null;
return files[fileIndex].getName();
}
/**
* Returns the progress of file unpacking, or -1 if not unpacking.
* @return the completion percent [0, 100] or -1
*/
public static int getUnpackerProgress() {
if (files == null || fileIndex == -1)
return -1;
return (fileIndex + 1) * 100 / files.length;
}
}

View File

@@ -79,10 +79,15 @@ public class Options extends BasicGameState {
};
/**
* The current beatmap directory.
* The beatmap directory.
*/
private static File beatmapDir;
/**
* The OSZ archive directory.
*/
private static File oszDir;
/**
* The current skin directory (for user skins).
*/
@@ -1071,7 +1076,7 @@ public class Options extends BasicGameState {
public static boolean isLoadVerbose() { return loadVerbose; }
/**
* Returns the current beatmap directory.
* Returns the beatmap directory.
* If invalid, this will attempt to search for the directory,
* and if nothing found, will create one.
* @return the beatmap directory
@@ -1090,6 +1095,19 @@ public class Options extends BasicGameState {
return beatmapDir;
}
/**
* Returns the OSZ archive directory.
* If invalid, this will return the root directory.
* @return the OSZ archive directory
*/
public static File getOSZDir() {
if (oszDir != null && oszDir.isDirectory())
return oszDir;
oszDir = new File("").getAbsoluteFile();
return oszDir;
}
/**
* Returns the current skin directory.
* If invalid, this will create a "Skins" folder in the root directory.
@@ -1131,6 +1149,9 @@ public class Options extends BasicGameState {
case "BeatmapDirectory":
beatmapDir = new File(value);
break;
case "OSZDirectory":
oszDir = new File(value);
break;
case "Skin":
skinDir = new File(value);
break;
@@ -1250,6 +1271,8 @@ public class Options extends BasicGameState {
// options
writer.write(String.format("BeatmapDirectory = %s", getBeatmapDir().getAbsolutePath()));
writer.newLine();
writer.write(String.format("OSZDirectory = %s", getOSZDir().getAbsolutePath()));
writer.newLine();
writer.write(String.format("Skin = %s", getSkinDir().getAbsolutePath()));
writer.newLine();
writer.write(String.format("Port = %d", port));

View File

@@ -21,9 +21,12 @@ package itdelatrisu.opsu.states;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuGroupList;
import itdelatrisu.opsu.OsuParser;
import itdelatrisu.opsu.OszUnpacker;
import itdelatrisu.opsu.SoundController;
import itdelatrisu.opsu.Utils;
import java.io.File;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
@@ -32,6 +35,11 @@ import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
/**
* "Splash Screen" state.
* <p>
* Loads game resources and enters "Main Menu" state.
*/
public class Splash extends BasicGameState {
/**
* Logo image.
@@ -45,6 +53,7 @@ public class Splash extends BasicGameState {
// game-related variables
private int state;
private boolean init = false;
public Splash(int state) {
this.state = state;
@@ -53,34 +62,12 @@ public class Splash extends BasicGameState {
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
final int width = container.getWidth();
final int height = container.getHeight();
logo = new Image("logo.png");
logo = logo.getScaledCopy((height / 1.2f) / logo.getHeight());
logo = logo.getScaledCopy((container.getHeight() / 1.2f) / logo.getHeight());
logo.setAlpha(0f);
// load Utils class first (needed in other 'init' methods)
Utils.init(container, game);
// load other resources in a new thread
final SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
new Thread() {
@Override
public void run() {
// parse song directory
OsuParser.parseAllFiles(Options.getBeatmapDir(), width, height);
// initialize song list
Opsu.groups.init(OsuGroupList.SORT_TITLE);
menu.setFocus(Opsu.groups.getRandomNode(), -1, true);
// load sounds
SoundController.init();
finished = true;
}
}.start();
}
@Override
@@ -93,23 +80,63 @@ public class Splash extends BasicGameState {
logo.drawCentered(width / 2, height / 2);
// progress tracking
String currentFile = OsuParser.getCurrentFileName();
if (currentFile != null && Options.isLoadVerbose()) {
// display progress
if (Options.isLoadVerbose()) {
g.setColor(Color.white);
g.setFont(Utils.FONT_MEDIUM);
int lineHeight = Utils.FONT_MEDIUM.getLineHeight();
g.drawString(
String.format("Loading... (%s%%)", OsuParser.getParserProgress()),
25, height - 25 - (lineHeight * 2)
);
g.drawString(currentFile, 25, height - 25 - lineHeight);
String unpackedFile = OszUnpacker.getCurrentFileName();
String parsedFile = OsuParser.getCurrentFileName();
if (unpackedFile != null) {
g.drawString(
String.format("Unpacking... (%d%%)", OszUnpacker.getUnpackerProgress()),
25, height - 25 - (lineHeight * 2)
);
g.drawString(unpackedFile, 25, height - 25 - lineHeight);
} else if (parsedFile != null) {
g.drawString(
String.format("Loading... (%d%%)", OsuParser.getParserProgress()),
25, height - 25 - (lineHeight * 2)
);
g.drawString(parsedFile, 25, height - 25 - lineHeight);
}
}
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
if (!init) {
init = true;
// load other resources in a new thread
final int width = container.getWidth();
final int height = container.getHeight();
final SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
new Thread() {
@Override
public void run() {
File beatmapDir = Options.getBeatmapDir();
// unpack all OSZ archives
OszUnpacker.unpackAllFiles(Options.getOSZDir(), beatmapDir);
// parse song directory
OsuParser.parseAllFiles(beatmapDir, width, height);
// initialize song list
Opsu.groups.init(OsuGroupList.SORT_TITLE);
menu.setFocus(Opsu.groups.getRandomNode(), -1, true);
// load sounds
SoundController.init();
finished = true;
}
}.start();
}
// fade in logo
float alpha = logo.getAlpha();
if (alpha < 1f)