From e0a73a1fbe026e1b0ba3289c39fd6bd467daf258 Mon Sep 17 00:00:00 2001 From: Jannik Date: Mon, 27 Nov 2017 10:57:16 +0100 Subject: [PATCH] code clean up * the mwc init process is now only one call in main * renamed a few methodes for better clearance * added a few TODOs --- .classpath | 1 + .../java/com/cemu_UI/application/Main.java | 10 +-- .../application/MainWindowController.java | 64 +++++++++++---- .../com/cemu_UI/controller/dbController.java | 79 +++++++++++-------- src/test/main/com/cemu_UI/test/AppTest.java | 43 ++++++++++ 5 files changed, 147 insertions(+), 50 deletions(-) create mode 100644 src/test/main/com/cemu_UI/test/AppTest.java diff --git a/.classpath b/.classpath index d230797..9454e24 100644 --- a/.classpath +++ b/.classpath @@ -17,6 +17,7 @@ + diff --git a/src/main/java/com/cemu_UI/application/Main.java b/src/main/java/com/cemu_UI/application/Main.java index 9459df2..504e70e 100644 --- a/src/main/java/com/cemu_UI/application/Main.java +++ b/src/main/java/com/cemu_UI/application/Main.java @@ -85,7 +85,6 @@ public class Main extends Application { FXMLLoader loader = new FXMLLoader(); loader.setLocation(ClassLoader.getSystemResource("fxml/MainWindow.fxml")); pane = (AnchorPane) loader.load(); -// primaryStage.setResizable(false); primaryStage.setTitle("cemu_UI"); // primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("/resources/Homeflix_Icon_64x64.png"))); //adds application icon @@ -157,10 +156,11 @@ public class Main extends Application { } // loading settings and initialize UI, dbController.main() loads all databases - mainWindowController.loadSettings(); - mainWindowController.checkAutoUpdate(); - mainWindowController.initActions(); - mainWindowController.initUI(); + mainWindowController.init(); +// mainWindowController.loadSettings(); +// mainWindowController.checkAutoUpdate(); +// mainWindowController.initActions(); +// mainWindowController.initUI(); mainWindowController.dbController.main(); if(mainWindowController.isCloudSync()) { cloudController.initializeConnection(mainWindowController.getCloudService(), mainWindowController.getCemuPath()); diff --git a/src/main/java/com/cemu_UI/application/MainWindowController.java b/src/main/java/com/cemu_UI/application/MainWindowController.java index 8c30bde..a18b60c 100644 --- a/src/main/java/com/cemu_UI/application/MainWindowController.java +++ b/src/main/java/com/cemu_UI/application/MainWindowController.java @@ -48,7 +48,6 @@ import javax.swing.ProgressMonitorInputStream; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; - import com.cemu_UI.controller.SmmdbAPIController; import com.cemu_UI.controller.UpdateController; import com.cemu_UI.controller.dbController; @@ -61,6 +60,7 @@ import com.cemu_UI.uiElements.JFXOkayCancelDialog; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXColorPicker; import com.jfoenix.controls.JFXHamburger; +import com.jfoenix.controls.JFXSpinner; import com.jfoenix.controls.JFXTextField; import com.jfoenix.controls.JFXToggleButton; import com.jfoenix.controls.JFXTreeTableColumn; @@ -70,6 +70,7 @@ import com.jfoenix.transitions.hamburger.HamburgerBackArrowBasicTransition; import javafx.animation.FadeTransition; import javafx.animation.ParallelTransition; import javafx.animation.TranslateTransition; +import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; @@ -318,7 +319,21 @@ public class MainWindowController { smmdbAPIController = new SmmdbAPIController(); } - void initUI() { + /** + * initialize the MainWindowController + * loadSettings, checkAutoUpdate, initUI and initActions + */ + void init() { + loadSettings(); + checkAutoUpdate(); + initUI(); + initActions(); + } + + /** + * initialize all variable UI parameters and elements + */ + private void initUI() { LOGGER.info("initializing UI ..."); if (getWindowWidth() > 100 && getWindowHeight() > 100) { @@ -368,7 +383,7 @@ public class MainWindowController { /** * initialize all actions not initialized by a own method */ - void initActions() { + private void initActions() { LOGGER.info("initializing Actions ..."); MWC = this; @@ -435,7 +450,7 @@ public class MainWindowController { public void handle(ActionEvent event) { try { games.remove(selectedUIDataIndex); // remove game form games-list - dbController.removeRom(selectedGameTitleID); // remove game from database + dbController.removeGame(selectedGameTitleID); // remove game from database refreshUIData(); // refresh all games at gamesAnchorPane (UI) } catch (Exception e) { LOGGER.error("error while removing ROM from database!", e); @@ -717,10 +732,31 @@ public class MainWindowController { @FXML void reloadRomsBtnAction() throws IOException { - reloadRomsBtn.setText("reloading..."); - dbController.loadRomDirectory(getRomPath()); // TODO own thread - Runtime.getRuntime().exec("java -jar cemu_UI.jar"); // start again (preventing Bugs) - System.exit(0); // finishes itself + + //TODO needs testing + JFXSpinner spinner = new JFXSpinner(); + spinner.setPrefSize(30, 30); + main.pane.getChildren().add(spinner); + AnchorPane.setTopAnchor(spinner, (main.pane.getHeight()-spinner.getPrefHeight())/2); + AnchorPane.setLeftAnchor(spinner, (main.pane.getWidth()-spinner.getPrefWidth())/2); + + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + dbController.loadRomDirectory(getRomPath()); // reload the rom directory + refreshUIData(); // refresh the list of games displayed on screen + + Platform.runLater(() -> { + main.pane.getChildren().remove(spinner); + }); + } + }); + thread.start(); + +// reloadRomsBtn.setText("reloading..."); +// dbController.loadRomDirectory(getRomPath()); +// Runtime.getRuntime().exec("java -jar cemu_UI.jar"); // start again (preventing Bugs) +// System.exit(0); // finishes itself } @FXML @@ -1009,8 +1045,8 @@ public class MainWindowController { } try { - dbController.addRom(title, coverPath, romPath, titleID, "", "", "", "0"); - dbController.loadSingleRom(titleID); + dbController.addGame(title, coverPath, romPath, titleID, "", "", "", "0"); + dbController.loadSingleGame(titleID); if (menuTrue) { sideMenuSlideOut(); burgerTask.setRate(-1.0); @@ -1027,7 +1063,7 @@ public class MainWindowController { public void editBtnReturn(String title, String coverPath, String romPath, String titleID) { dbController.setGameInfo(title, coverPath, romPath, titleID); games.remove(selectedUIDataIndex); - dbController.loadSingleRom(titleID); + dbController.loadSingleGame(titleID); refreshUIData(); LOGGER.info("successfully edited " + titleID + ", new name is \"" + title + "\""); @@ -1038,7 +1074,7 @@ public class MainWindowController { * @param title : game title * @param coverPath : path to cover (cache) * @param romPath : path to ROM file (.rpx) - * @param titleID : ROM ID + * @param titleID : game ID */ public void addGame(String title, String coverPath, String romPath, String titleID){ VBox VBox = new VBox(); @@ -1176,7 +1212,7 @@ public class MainWindowController { lastTimePlayedBtn.setLayoutX((width / 2) + 50 + 20.5); } - void checkAutoUpdate() { + private void checkAutoUpdate() { if (isAutoUpdate()) { try { @@ -1444,7 +1480,7 @@ public class MainWindowController { * loading saved settings from the config.xml file * if a value is not present, default is used instead */ - void loadSettings(){ + private void loadSettings(){ LOGGER.info("loading settings ..."); InputStream inputStream; try { diff --git a/src/main/java/com/cemu_UI/controller/dbController.java b/src/main/java/com/cemu_UI/controller/dbController.java index 056ed7f..2818032 100644 --- a/src/main/java/com/cemu_UI/controller/dbController.java +++ b/src/main/java/com/cemu_UI/controller/dbController.java @@ -55,7 +55,7 @@ public class dbController { private MainWindowController mainWindowController; private ArrayList entries = new ArrayList<>(); - private String DB_PATH; + private String DB_PATH_localRoms; private String DB_PATH_games; private Connection connection = null; private Connection connectionGames = null; @@ -66,20 +66,25 @@ public class dbController { loadRomDatabase(); loadGamesDatabase(); createRomDatabase(); - loadAllRoms(); + loadAllGames(); checkRemoveEntry(); LOGGER.info("<==========finished loading sql==========>"); } + /** + * set the path to the localRoms.db file and initialize the connection + * + * TODO this should be called LocalGames + */ private void loadRomDatabase(){ if (System.getProperty("os.name").equals("Linux")) { - DB_PATH = System.getProperty("user.home") + "/cemu_UI/localRoms.db"; + DB_PATH_localRoms = System.getProperty("user.home") + "/cemu_UI/localRoms.db"; }else{ - DB_PATH = System.getProperty("user.home") + "\\Documents\\cemu_UI" + "\\" + "localRoms.db"; + DB_PATH_localRoms = System.getProperty("user.home") + "\\Documents\\cemu_UI" + "\\" + "localRoms.db"; } try { // create a database connection - connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH); + connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH_localRoms); connection.setAutoCommit(false); //AutoCommit to false -> manual commit is active } catch (SQLException e) { // if the error message is "out of memory", it probably means no database file is found @@ -89,28 +94,34 @@ public class dbController { } /** - * this method is used to load the games database with additional informations about a game - * it is used if a new game is added (automatic or manual) + * set the path to the localRoms.db file and initialize the connection + * + * games.dbcontains a reverence list to for the automatic detection mode + * TODO this should be called ReferenceGameList the games table should be called reference_games */ - private void loadGamesDatabase(){ + private void loadGamesDatabase() { if (System.getProperty("os.name").equals("Linux")) { DB_PATH_games = System.getProperty("user.home") + "/cemu_UI/games.db"; - }else{ + } else { DB_PATH_games = System.getProperty("user.home") + "\\Documents\\cemu_UI" + "\\" + "games.db"; } try { // create a database connection connectionGames = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH_games); - connectionGames.setAutoCommit(false); //AutoCommit to false -> manual commit is active + connectionGames.setAutoCommit(false); // AutoCommit to false -> manual commit is active } catch (SQLException e) { - // if the error message is "out of memory", it probably means no database file is found LOGGER.error("error while loading the games database", e); } LOGGER.info("games database loaded successfull"); } - //creating database, if database has 0 entries search for all .rpx files in the roms directory and add them - void createRomDatabase() { + /** + * creating the local_roms table in localRoms.db + * if the table has no entries, call loadRomDirectory + * + * TODO the local_roms table should be called local_games + */ + void createRomDatabase() { try { Statement stmt = connection.createStatement(); stmt.executeUpdate("create table if not exists local_roms (title, coverPath, romPath, titleID, productCode, region, lastPlayed, timePlayed)"); @@ -119,24 +130,25 @@ public class dbController { } catch (SQLException e) { LOGGER.error("error while creating ROM database", e); } - - try { - Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms"); - while (rs.next()) { + + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms"); + while (rs.next()) { entries.add(rs.getString(2)); } stmt.close(); rs.close(); - }catch (SQLException e){ + } catch (SQLException e) { LOGGER.error("error while loading ROMs from ROM database, local_roms table", e); } - if(entries.size() == 0){ + if (entries.size() == 0) { loadRomDirectory(mainWindowController.getRomPath()); } } - public void addRom(String title, String coverPath, String romPath, String titleID, String productCode, String region, String lastPlayed, String timePlayed) throws SQLException{ + // add a Ggame to the database + public void addGame(String title, String coverPath, String romPath, String titleID, String productCode, String region, String lastPlayed, String timePlayed) throws SQLException{ Statement stmt = connection.createStatement(); stmt.executeUpdate("insert into local_roms values ('"+title+"','"+coverPath+"','"+romPath+"','"+titleID+"'," + "'"+productCode+"','"+region+"','"+lastPlayed+"','"+timePlayed+"')"); @@ -145,7 +157,7 @@ public class dbController { LOGGER.info("added \""+title+"\" to ROM database"); } - public void removeRom(String titleID) throws SQLException{ + public void removeGame(String titleID) throws SQLException{ Statement stmt = connection.createStatement(); stmt.executeUpdate("delete from local_roms where titleID = '"+titleID+"'"); connection.commit(); @@ -154,8 +166,8 @@ public class dbController { } //load all ROMs on startup to the mainWindowController - void loadAllRoms(){ - LOGGER.info("loading all rom's on startup into the mainWindowController ..."); + void loadAllGames(){ + LOGGER.info("loading all games on startup into the mainWindowController ..."); try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms"); @@ -165,13 +177,13 @@ public class dbController { stmt.close(); rs.close(); }catch (Exception e){ - LOGGER.error("error while loading all ROMs into the mainWindowController", e); + LOGGER.error("error while loading all games into the mainWindowController", e); } } //load one single ROM after manual adding into the mainWindowController - public void loadSingleRom(String titleID){ - LOGGER.info("loading a single ROM (ID: "+titleID+") into the mainWindowController ..."); + public void loadSingleGame(String titleID){ + LOGGER.info("loading a single game (ID: "+titleID+") into the mainWindowController ..."); try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM local_roms where titleID = '"+titleID+"'"); @@ -181,11 +193,14 @@ public class dbController { stmt.close(); rs.close(); }catch (Exception e){ - LOGGER.error("error while loading a single ROM into the mainWindowController", e); + LOGGER.error("error while loading a single game into the mainWindowController", e); } } - //get all files with .rpx TODO add other formats + /** + * get all .rpx files from a given directory and add them to the games database if they don't exist there + * @param directory where to search for the .rpx files + */ public void loadRomDirectory(String directory){ File dir = new File(directory); File appFile; @@ -203,6 +218,7 @@ public class dbController { Statement stmt = connectionGames.createStatement(); List files = (List) FileUtils.listFiles(dir, extensions, true); LOGGER.info("Getting all .rpx files in " + dir.getCanonicalPath()+" including those in subdirectories"); + // for all files in dir get the app.xml for (File file : files) { if(System.getProperty("os.name").equals("Linux")){ appFile = new File(file.getParent()+"/app.xml"); @@ -212,10 +228,11 @@ public class dbController { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(appFile); - String title_ID = document.getElementsByTagName("title_id").item(0).getTextContent(); + String title_ID = document.getElementsByTagName("title_id").item(0).getTextContent(); //get titile_ID from app.xml title_ID = title_ID.substring(0, 8) + "-" + title_ID.substring(8, title_ID.length()); LOGGER.info("Name: "+file.getName()+"; Title ID: "+title_ID); ResultSet rs = stmt.executeQuery("SELECT * FROM games WHERE TitleID = '"+title_ID+"';"); + // for all elements in the games table check if it's already present, else add it while (rs.next()) { if (checkEntry(rs.getString(2))) { LOGGER.info(rs.getString(2) + ": game already in database"); @@ -233,7 +250,7 @@ public class dbController { } LOGGER.info(rs.getString(2) + ": adding ROM"); - addRom(rs.getString(2), coverPath, file.getCanonicalPath(), rs.getString(1), rs.getString(3), rs.getString(5),"","0"); + addGame(rs.getString(2), coverPath, file.getCanonicalPath(), rs.getString(1), rs.getString(3), rs.getString(5),"","0"); } } } diff --git a/src/test/main/com/cemu_UI/test/AppTest.java b/src/test/main/com/cemu_UI/test/AppTest.java new file mode 100644 index 0000000..6470ba6 --- /dev/null +++ b/src/test/main/com/cemu_UI/test/AppTest.java @@ -0,0 +1,43 @@ +package com.cemu_UI.test; + +import java.io.File; + +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest extends TestCase { + + /** + * Create the test case + * + * @param testName + * name of the test case + */ + public AppTest(String testName) { + super(testName); + } + + private void testClientsSecret() { + File client_secret = new File("/client_secret.json"); + Assert.assertTrue(client_secret.exists()); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(AppTest.class); + } + + /** + * Rigourous Test :-) + */ + public void testApp() { + assertTrue(true); + } +}