Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 6 additions & 153 deletions src/toniarts/openkeeper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.jme3.app.state.VideoRecorderAppState;
import com.jme3.asset.AssetEventListener;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetManager;
import com.jme3.asset.TextureKey;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.light.AmbientLight;
Expand All @@ -35,11 +34,8 @@
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.opengl.GLRenderer;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.render.batch.BatchRenderConfiguration;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
Expand All @@ -48,7 +44,6 @@
import java.io.InputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
Expand All @@ -64,7 +59,6 @@
import java.util.Queue;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.lwjgl.opengl.*;
import toniarts.openkeeper.audio.plugins.MP2Loader;
import toniarts.openkeeper.cinematics.CameraSweepDataLoader;
Expand All @@ -74,9 +68,6 @@
import toniarts.openkeeper.game.state.SoundState;
import toniarts.openkeeper.game.state.loading.TitleScreenState;
import toniarts.openkeeper.game.state.session.LocalGameSession;
import toniarts.openkeeper.setup.DKConverter;
import toniarts.openkeeper.setup.DKFolderSelector;
import toniarts.openkeeper.setup.IFrameClosingBehavior;
import toniarts.openkeeper.tools.convert.AssetsConverter;
import toniarts.openkeeper.tools.modelviewer.SoundsLoader;
import toniarts.openkeeper.utils.PathUtils;
Expand All @@ -91,14 +82,11 @@
public final class Main extends SimpleApplication {

private static final Logger logger = System.getLogger(Main.class.getName());

private static boolean folderOk = false;
private static boolean conversionOk = false;
public static final String VERSION = "*ALPHA*";

private static final String VERSION = "*ALPHA*";
public static final String TITLE = "OpenKeeper";
private static final String USER_HOME_FOLDER = System.getProperty("user.home").concat(File.separator).concat(".").concat(TITLE).concat(File.separator);
private static final String SCREENSHOTS_FOLDER = USER_HOME_FOLDER.concat("SCRSHOTS").concat(File.separator);
private static final Object LOCK = new Object();
private static Map<String, String> params;
private static boolean debug;

Expand All @@ -109,7 +97,7 @@ private Main() {
super(new StatsAppState(), new DebugKeysAppState());
}

public static void main(String[] args) throws InvocationTargetException, InterruptedException {
public static void main(String[] args) throws Exception {

// Create main application instance
parseArguments(args);
Expand All @@ -126,12 +114,9 @@ public static void main(String[] args) throws InvocationTargetException, Interru
// set a better logging format
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT %4$s %2$s - %5$s%6$s%n");

// Finally start it if everything went ok
if (checkSetup(app)) {
app.start();
} else {
logger.log(Level.WARNING, "Application setup not complete!!");
}
// Check assets
AssetsConverter.main(new String[0]);
app.start();
}

/**
Expand Down Expand Up @@ -165,67 +150,6 @@ private static void parseArguments(String[] args) {
}
}

/**
* Check that we have all we need to run this app
*
* @param app the app (we need asset managers etc.)
* @return true if the app is ok for running!
* @throws InterruptedException lots of threads waiting
*/
private static boolean checkSetup(final Main app) throws InterruptedException {

boolean saveSetup = false;

// First and foremost, the folder
if (!PathUtils.checkDkFolder(getDkIIFolder())) {
logger.log(Level.INFO, "Dungeon Keeper II folder not found or valid! Prompting user!");
saveSetup = true;

// Let the user select
setLookNFeel();
DKFolderSelector frame = new DKFolderSelector() {
@Override
protected void continueOk(String path) {
PathUtils.setDKIIFolder(PathUtils.fixFilePath(path));
folderOk = true;
}
};
openFrameAndWait(frame);
} else {
folderOk = true;
}

// If the folder is ok, check the conversion
if (folderOk && (AssetsConverter.isConversionNeeded(Main.getSettings()))) {
logger.log(Level.INFO, "Need to convert the assets!");
saveSetup = true;

// Convert
setLookNFeel();
AssetManager assetManager = JmeSystem.newAssetManager(
Thread.currentThread().getContextClassLoader()
.getResource("com/jme3/asset/Desktop.cfg")); // Get temporary asset manager instance since we not yet have one ourselves
assetManager.registerLocator(AssetsConverter.getAssetsFolder(), FileLocator.class);
DKConverter frame = new DKConverter(getDkIIFolder(), assetManager) {
@Override
protected void continueOk() {
conversionOk = true;
}
};
openFrameAndWait(frame);
} else if (folderOk) {
conversionOk = true;
}

// If everything is ok, we might need to save the setup
boolean result = folderOk && conversionOk;
if (result && saveSetup) {
SettingUtils.getInstance().saveSettings();
}

return result;
}

private static void initSettings(Main app) {

// Create some folders
Expand Down Expand Up @@ -264,77 +188,6 @@ public static AppSettings getSettings() {
return SettingUtils.getInstance().getSettings();
}

/**
* Sets SWING look and feel
*/
private static void setLookNFeel() {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
System.getLogger(DKFolderSelector.class.getName()).log(Level.ERROR, (String) null, ex);
}
}

/**
* Opens up a given frame and waits for it to finish
*
* @param frame the frame to open
* @throws InterruptedException
*/
private static void openFrameAndWait(final JFrame frame) throws InterruptedException {
frame.setVisible(true);

Thread t = new Thread() {
@Override
public void run() {
synchronized (LOCK) {
while (frame.isVisible()) {
try {
LOCK.wait();
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Lock interrupted!");
}
}
}
}
};
t.start();

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {

// See if it is allowed
if (frame instanceof IFrameClosingBehavior && !((IFrameClosingBehavior) frame).canCloseWindow()) {
return; // You shall not pass!
}

synchronized (LOCK) {
frame.dispose();
LOCK.notify();
}
}
});

// Special
if (frame instanceof DKConverter) {
((DKConverter) frame).startConversion();
}

// Wait the dialog to finish
t.join();
}

@Override
public void simpleInitApp() {

Expand Down
54 changes: 49 additions & 5 deletions src/toniarts/openkeeper/tools/convert/AssetsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package toniarts.openkeeper.tools.convert;

import com.jme3.asset.AssetManager;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import java.io.File;
import java.io.IOException;
import java.lang.System.Logger;
Expand All @@ -41,6 +43,7 @@
import toniarts.openkeeper.tools.convert.conversion.task.IConversionTaskUpdate;
import toniarts.openkeeper.tools.convert.map.KwdFile;
import toniarts.openkeeper.utils.PathUtils;
import toniarts.openkeeper.utils.SettingUtils;

/**
*
Expand Down Expand Up @@ -99,9 +102,9 @@ public String toString() {
private final int version;
private final ConvertProcess[] dependencies;
}

private static final Logger logger = System.getLogger(AssetsConverter.class.getName());

private static final boolean OVERWRITE_DATA = true; // Not exhausting your SDD :) or our custom graphics
private static final String ASSETS_FOLDER = "assets" + File.separator + "Converted";
private static final String ABSOLUTE_ASSETS_FOLDER = getCurrentFolder() + ASSETS_FOLDER + File.separator;
Expand All @@ -114,10 +117,10 @@ public String toString() {
public static final String MAP_THUMBNAILS_FOLDER = "Thumbnails";
private static final String INTERFACE_FOLDER = "Interface" + File.separator;
public static final String MOUSE_CURSORS_FOLDER = INTERFACE_FOLDER + "Cursors";
public static final String FONTS_FOLDER = INTERFACE_FOLDER + "Fonts";
public static final String TEXTS_FOLDER = INTERFACE_FOLDER + "Texts";
private static final String FONTS_FOLDER = INTERFACE_FOLDER + "Fonts";
private static final String TEXTS_FOLDER = INTERFACE_FOLDER + "Texts";
public static final String PATHS_FOLDER = INTERFACE_FOLDER + "Paths";

private final String dungeonKeeperFolder;
private final AssetManager assetManager;

Expand All @@ -126,6 +129,47 @@ public AssetsConverter(String dungeonKeeperFolder, AssetManager assetManager) {
this.assetManager = assetManager;
}

public static void main(String[] args) {

String dk2Folder = args.length > 0 ? args[0] : PathUtils.getDKIIFolder();

// First and foremost, the folder
if (!PathUtils.checkDkFolder(dk2Folder)) {
logger.log(Level.ERROR, "DKII folder not found or valid!");
return;
}

// If the folder is ok, check the conversion
if (!AssetsConverter.isConversionNeeded(SettingUtils.getInstance().getSettings())) {
logger.log(Level.INFO, "Assets are up-to-date!");
return;
}

logger.log(Level.INFO, "Need to convert the assets!");

var assetManager = JmeSystem.newAssetManager(Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Desktop.cfg"));
assetManager.registerLocator(AssetsConverter.getAssetsFolder(), FileLocator.class);

var converter = new AssetsConverter(dk2Folder, assetManager) {
@Override
public void onUpdateStatus(Integer currentProgress, Integer totalProgress, ConvertProcess process) {
System.out.printf("Converting %s: %d/%d\n", process, currentProgress, totalProgress);
}

@Override
public void onComplete(ConvertProcess process) {
System.out.printf("Completed %s\n", process);
}

@Override
public void onError(Exception ex, ConvertProcess process) {
System.err.printf("Error in %s: %s\n", process, ex.getMessage());
}
};
converter.convertAssets();
SettingUtils.getInstance().saveSettings();
}

/**
* Checks if asset conversion is needed
*
Expand Down
Loading