2017-04-17 01:02:43 +02:00
/ * *
* cemu_UI
*
* Copyright 2017 < @Seil0 >
*
2017-10-15 13:37:45 +02:00
* This program 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 .
2017-04-17 01:02:43 +02:00
*
2017-10-15 13:37:45 +02:00
* This program 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 this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
2017-04-17 01:02:43 +02:00
* /
2017-10-15 13:37:45 +02:00
2017-11-13 16:44:39 +01:00
package com.cemu_UI.application ;
2017-04-17 01:02:43 +02:00
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.net.URL ;
import java.nio.channels.Channels ;
import java.nio.channels.ReadableByteChannel ;
import java.util.Optional ;
2017-09-16 17:08:50 +02:00
import java.util.Timer ;
import java.util.TimerTask ;
2017-08-27 20:44:08 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-11-13 16:44:39 +01:00
import com.cemu_UI.controller.CloudController ;
2017-04-17 01:02:43 +02:00
import javafx.application.Application ;
2017-09-12 19:45:39 +02:00
import javafx.beans.value.ChangeListener ;
import javafx.beans.value.ObservableValue ;
2017-04-17 01:02:43 +02:00
import javafx.fxml.FXMLLoader ;
import javafx.stage.DirectoryChooser ;
import javafx.stage.Stage ;
import javafx.scene.Scene ;
import javafx.scene.control.Alert ;
import javafx.scene.control.ButtonType ;
import javafx.scene.control.Alert.AlertType ;
import javafx.scene.layout.AnchorPane ;
public class Main extends Application {
Stage primaryStage ;
2017-11-24 17:07:53 +01:00
public MainWindowController mainWindowController ; // TODO find a better way
2017-04-17 01:02:43 +02:00
CloudController cloudController ;
2017-08-01 20:11:17 +02:00
AnchorPane pane ;
2017-11-24 17:07:53 +01:00
Scene scene ; // TODO make private
private String dirWin = System . getProperty ( " user.home " ) + " /Documents/cemu_UI " ; // Windows: C:/Users/"User"/Documents/cemu_UI
private String dirLinux = System . getProperty ( " user.home " ) + " /cemu_UI " ; // Linux: /home/"User"/cemu_UI
2017-04-17 01:02:43 +02:00
private String gamesDBdownloadURL = " https://github.com/Seil0/cemu_UI/raw/master/downloadContent/games.db " ;
private File directory ;
private File configFile ;
private File gamesDBFile ;
@SuppressWarnings ( " unused " )
private File localDB ;
private File pictureCache ;
2017-09-02 17:38:40 +02:00
private static Logger LOGGER ;
2017-04-17 01:02:43 +02:00
@Override
public void start ( Stage primaryStage ) {
2017-11-13 16:44:39 +01:00
try {
this . primaryStage = primaryStage ;
mainWindow ( ) ;
initActions ( ) ;
} catch ( Exception e ) {
LOGGER . error ( " ooooops " , e ) ;
}
2017-04-17 01:02:43 +02:00
}
private void mainWindow ( ) {
try {
2017-11-13 16:44:39 +01:00
FXMLLoader loader = new FXMLLoader ( ) ;
loader . setLocation ( ClassLoader . getSystemResource ( " fxml/MainWindow.fxml " ) ) ;
pane = ( AnchorPane ) loader . load ( ) ;
2017-04-17 01:02:43 +02:00
primaryStage . setTitle ( " cemu_UI " ) ;
2017-06-01 16:21:24 +02:00
// primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("/resources/Homeflix_Icon_64x64.png"))); //adds application icon
2017-04-17 01:02:43 +02:00
2017-11-24 17:07:53 +01:00
mainWindowController = loader . getController ( ) ; // Link of FXMLController and controller class
mainWindowController . setMain ( this ) ; // call setMain
2017-12-07 22:52:32 +01:00
cloudController = new CloudController ( mainWindowController ) ; // call cloudController constructor
2017-04-17 01:02:43 +02:00
2017-11-24 17:07:53 +01:00
// get OS and the specific paths
2017-10-13 21:15:51 +02:00
if ( System . getProperty ( " os.name " ) . equals ( " Linux " ) ) {
2017-04-17 01:02:43 +02:00
directory = new File ( dirLinux ) ;
configFile = new File ( dirLinux + " /config.xml " ) ;
gamesDBFile = new File ( dirLinux + " /games.db " ) ;
localDB = new File ( dirLinux + " /localRoms.db " ) ;
pictureCache = new File ( dirLinux + " /picture_cache " ) ;
2017-10-13 21:15:51 +02:00
} else {
2017-09-02 17:38:40 +02:00
directory = new File ( dirWin ) ;
2017-04-17 01:02:43 +02:00
configFile = new File ( dirWin + " /config.xml " ) ;
gamesDBFile = new File ( dirWin + " /games.db " ) ;
localDB = new File ( dirWin + " /localRoms.db " ) ;
pictureCache = new File ( dirWin + " /picture_cache " ) ;
}
2017-11-24 17:07:53 +01:00
// startup checks
// check if client_secret.jason is present
2017-11-13 16:44:39 +01:00
if ( Main . class . getResourceAsStream ( " /client_secret.json " ) = = null ) {
2017-08-27 20:44:08 +02:00
LOGGER . error ( " client_secret is missing!!!!! " ) ;
2017-08-26 00:13:09 +02:00
Alert alert = new Alert ( AlertType . ERROR ) ;
alert . setTitle ( " cemu_UI " ) ;
alert . setHeaderText ( " Error " ) ;
alert . setContentText ( " client_secret is missing! Please contact the maintainer. \ nIf you compiled cemu_UI by yourself see: \ nhttps://github.com/Seil0/cemu_UI/wiki/Documantation " ) ;
alert . showAndWait ( ) ;
}
2017-08-27 20:44:08 +02:00
LOGGER . info ( " Directory: " + directory . exists ( ) ) ;
LOGGER . info ( " Configfile: " + configFile . exists ( ) ) ;
2017-10-13 21:15:51 +02:00
if ( ! directory . exists ( ) ) {
2017-08-27 20:44:08 +02:00
LOGGER . info ( " creating cemu_UI directory " ) ;
2017-04-17 01:02:43 +02:00
directory . mkdir ( ) ;
pictureCache . mkdir ( ) ;
}
2017-10-13 21:15:51 +02:00
if ( ! configFile . exists ( ) ) {
2017-08-27 20:44:08 +02:00
LOGGER . info ( " firststart, setting default values " ) ;
2017-04-17 01:02:43 +02:00
firstStart ( ) ;
mainWindowController . setColor ( " 00a8cc " ) ;
2017-11-06 10:42:31 +01:00
mainWindowController . setAutoUpdate ( false ) ;
2017-04-17 01:02:43 +02:00
mainWindowController . setxPosHelper ( 0 ) ;
mainWindowController . saveSettings ( ) ;
Runtime . getRuntime ( ) . exec ( " java -jar cemu_UI.jar " ) ; //start again (preventing Bugs)
2017-09-12 19:45:39 +02:00
System . exit ( 0 ) ; //finishes itself
2017-04-17 01:02:43 +02:00
}
2017-10-13 21:15:51 +02:00
if ( pictureCache . exists ( ) ! = true ) {
2017-04-17 01:02:43 +02:00
pictureCache . mkdir ( ) ;
}
2017-10-13 21:15:51 +02:00
if ( gamesDBFile . exists ( ) ! = true ) {
2017-04-17 01:02:43 +02:00
try {
2017-08-27 20:44:08 +02:00
LOGGER . info ( " downloading games.db... " ) ;
2017-04-17 01:02:43 +02:00
URL website = new URL ( gamesDBdownloadURL ) ;
ReadableByteChannel rbc = Channels . newChannel ( website . openStream ( ) ) ;
FileOutputStream fos = new FileOutputStream ( gamesDBFile ) ;
fos . getChannel ( ) . transferFrom ( rbc , 0 , Long . MAX_VALUE ) ;
fos . close ( ) ;
2017-08-27 20:44:08 +02:00
LOGGER . info ( " finished downloading games.db " ) ;
2017-04-17 01:02:43 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2017-11-24 17:07:53 +01:00
// loading settings and initialize UI, dbController.main() loads all databases
2017-11-27 10:57:16 +01:00
mainWindowController . init ( ) ;
2017-05-07 19:57:55 +02:00
mainWindowController . dbController . main ( ) ;
2017-12-07 22:52:32 +01:00
// if cloud sync is activated start sync
2017-04-17 01:02:43 +02:00
if ( mainWindowController . isCloudSync ( ) ) {
cloudController . initializeConnection ( mainWindowController . getCloudService ( ) , mainWindowController . getCemuPath ( ) ) ;
2017-05-04 22:07:50 +02:00
cloudController . stratupCheck ( mainWindowController . getCloudService ( ) , mainWindowController . getCemuPath ( ) ) ;
2017-10-15 12:29:18 +02:00
}
2017-04-17 01:02:43 +02:00
mainWindowController . addUIData ( ) ;
2017-11-24 17:07:53 +01:00
scene = new Scene ( pane ) ; // create new scene, append pane to scene
2017-11-13 16:44:39 +01:00
scene . getStylesheets ( ) . add ( Main . class . getResource ( " /css/MainWindows.css " ) . toExternalForm ( ) ) ;
2017-11-24 17:07:53 +01:00
primaryStage . setScene ( scene ) ; // append scene to stage
primaryStage . show ( ) ; // show stage
2017-04-17 01:02:43 +02:00
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
private void firstStart ( ) {
2017-11-24 17:07:53 +01:00
Alert alert = new Alert ( AlertType . CONFIRMATION ) ; // new alert with file-chooser
2017-04-17 01:02:43 +02:00
alert . setTitle ( " cemu_UI " ) ;
alert . setHeaderText ( " cemu installation " ) ;
alert . setContentText ( " please select your cemu installation " ) ;
Optional < ButtonType > result = alert . showAndWait ( ) ;
2017-10-13 21:15:51 +02:00
if ( result . get ( ) = = ButtonType . OK ) {
2017-04-17 01:02:43 +02:00
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
File selectedDirectory = directoryChooser . showDialog ( primaryStage ) ;
mainWindowController . setCemuPath ( selectedDirectory . getAbsolutePath ( ) ) ;
} else {
mainWindowController . setCemuPath ( null ) ;
}
2017-11-24 17:07:53 +01:00
Alert alert2 = new Alert ( AlertType . CONFIRMATION ) ; // new alert with file-chooser
2017-04-17 01:02:43 +02:00
alert2 . setTitle ( " cemu_UI " ) ;
alert2 . setHeaderText ( " rom directory " ) ;
alert2 . setContentText ( " please select your rom directory " ) ;
Optional < ButtonType > result2 = alert2 . showAndWait ( ) ;
2017-10-13 21:15:51 +02:00
if ( result2 . get ( ) = = ButtonType . OK ) {
2017-04-17 01:02:43 +02:00
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
File selectedDirectory = directoryChooser . showDialog ( primaryStage ) ;
mainWindowController . setRomPath ( selectedDirectory . getAbsolutePath ( ) ) ;
} else {
mainWindowController . setRomPath ( null ) ;
}
}
2017-09-12 19:45:39 +02:00
private void initActions ( ) {
2017-09-15 19:23:44 +02:00
final ChangeListener < Number > widthListener = new ChangeListener < Number > ( ) {
2017-09-16 17:08:50 +02:00
final Timer timer = new Timer ( ) ;
2017-11-24 17:07:53 +01:00
TimerTask saveTask = null ; // task to execute save operation
final long delayTime = 500 ; // delay until the window size is saved, if the window is resized earlier it will be killed, default is 500ms
2017-09-15 19:23:44 +02:00
2017-09-12 19:45:39 +02:00
@Override
public void changed ( ObservableValue < ? extends Number > observable , Number oldValue , final Number newValue ) {
2017-09-15 19:23:44 +02:00
int xPosHelperMax = ( int ) Math . floor ( ( mainWindowController . getMainAnchorPane ( ) . getWidth ( ) - 36 ) / 217 ) ;
2017-09-12 19:45:39 +02:00
mainWindowController . refreshplayBtnPosition ( ) ;
2017-09-15 19:23:44 +02:00
2017-11-24 17:07:53 +01:00
// call only if there is enough space for a new row
2017-09-15 19:23:44 +02:00
if ( mainWindowController . getOldXPosHelper ( ) ! = xPosHelperMax ) {
2017-09-13 23:57:29 +02:00
mainWindowController . refreshUIData ( ) ;
}
2017-09-15 19:23:44 +02:00
2017-11-24 17:07:53 +01:00
// if saveTask is already running kill it
2017-09-16 17:08:50 +02:00
if ( saveTask ! = null ) saveTask . cancel ( ) ;
saveTask = new TimerTask ( ) {
@Override
public void run ( ) {
mainWindowController . saveSettings ( ) ;
}
} ;
timer . schedule ( saveTask , delayTime ) ;
2017-09-12 19:45:39 +02:00
}
} ;
2017-09-15 19:23:44 +02:00
final ChangeListener < Number > heightListener = new ChangeListener < Number > ( ) {
2017-09-16 17:08:50 +02:00
final Timer timer = new Timer ( ) ;
2017-11-24 17:07:53 +01:00
TimerTask saveTask = null ; // task to execute save operation
final long delayTime = 500 ; // delay until the window size is saved, if the window is resized earlier it will be killed, default is 500ms
2017-09-16 17:08:50 +02:00
2017-09-15 19:23:44 +02:00
@Override
public void changed ( ObservableValue < ? extends Number > observable , Number oldValue , final Number newValue ) {
2017-09-16 17:08:50 +02:00
if ( saveTask ! = null ) saveTask . cancel ( ) ;
saveTask = new TimerTask ( ) {
@Override
public void run ( ) {
mainWindowController . saveSettings ( ) ;
}
} ;
timer . schedule ( saveTask , delayTime ) ;
2017-09-15 19:23:44 +02:00
}
} ;
final ChangeListener < Boolean > maximizeListener = new ChangeListener < Boolean > ( ) {
@Override
public void changed ( ObservableValue < ? extends Boolean > ov , Boolean t , Boolean t1 ) {
primaryStage . setMaximized ( false ) ;
Alert alert = new Alert ( AlertType . WARNING ) ;
alert . setTitle ( " edit " ) ;
alert . setHeaderText ( " cemu_UI " ) ;
alert . setContentText ( " maximized Window is not supporte! " ) ;
alert . initOwner ( primaryStage ) ;
alert . showAndWait ( ) ;
LOGGER . warn ( " maximized Window is not supported " ) ;
}
} ;
2017-11-24 17:07:53 +01:00
// add listener to primaryStage
2017-09-15 19:23:44 +02:00
primaryStage . widthProperty ( ) . addListener ( widthListener ) ;
primaryStage . heightProperty ( ) . addListener ( heightListener ) ;
primaryStage . maximizedProperty ( ) . addListener ( maximizeListener ) ;
2017-09-12 19:45:39 +02:00
}
2017-04-17 01:02:43 +02:00
public static void main ( String [ ] args ) {
2017-11-24 17:07:53 +01:00
// delete old log file and create new
2017-09-02 17:38:40 +02:00
if ( System . getProperty ( " os.name " ) . equals ( " Linux " ) ) {
System . setProperty ( " logFilename " , System . getProperty ( " user.home " ) + " /cemu_UI/app.log " ) ;
File logFile = new File ( System . getProperty ( " user.home " ) + " /cemu_UI/app.log " ) ;
logFile . delete ( ) ;
} else {
System . setProperty ( " logFilename " , System . getProperty ( " user.home " ) + " /Documents/cemu_UI/app.log " ) ;
File logFile = new File ( System . getProperty ( " user.home " ) + " /Documents/cemu_UI/app.log " ) ;
logFile . delete ( ) ;
}
LOGGER = LogManager . getLogger ( Main . class . getName ( ) ) ;
2017-04-17 01:02:43 +02:00
launch ( args ) ;
}
2017-10-29 23:57:39 +01:00
@Override
public void stop ( ) {
System . exit ( 0 ) ;
}
2017-04-17 01:02:43 +02:00
}