2017-11-06 10:42:31 +01:00
/ * *
* cemu_UI
*
2018-04-28 12:17:12 +02:00
* Copyright 2017 - 2018 < @Seil0 >
2017-11-06 10:42:31 +01: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 .
*
* 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-11-13 16:44:39 +01:00
package com.cemu_UI.controller ;
2017-11-06 10:42:31 +01:00
import java.io.BufferedReader ;
import java.io.File ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.HttpURLConnection ;
import java.net.URL ;
2017-11-13 16:44:39 +01:00
2017-11-06 10:42:31 +01:00
import javax.swing.ProgressMonitor ;
import javax.swing.ProgressMonitorInputStream ;
import org.apache.commons.io.FileUtils ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-11-13 16:44:39 +01:00
import com.cemu_UI.application.MainWindowController ;
2017-11-06 10:42:31 +01:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonArray ;
import com.eclipsesource.json.JsonObject ;
import com.eclipsesource.json.JsonValue ;
import javafx.application.Platform ;
2017-11-15 22:00:28 +01:00
public class UpdateController implements Runnable {
2017-11-06 10:42:31 +01:00
private MainWindowController mainWindowController ;
2018-12-03 00:09:02 +01:00
private int buildNumber ;
private int updateBuildNumber ; // tag_name from gitea
2017-11-06 10:42:31 +01:00
private String apiOutput ;
2018-12-03 00:09:02 +01:00
@SuppressWarnings ( " unused " )
private String updateName ;
@SuppressWarnings ( " unused " )
private String updateChanges ;
private String browserDownloadUrl ; // update download link
private String giteaApiRelease = " https://git.mosad.xyz/api/v1/repos/Seil0/cemu_UI/releases " ;
private URL giteaApiUrl ;
2017-11-10 15:58:20 +01:00
private boolean useBeta ;
2017-11-06 10:42:31 +01:00
private static final Logger LOGGER = LogManager . getLogger ( UpdateController . class . getName ( ) ) ;
2017-11-15 22:00:28 +01:00
2017-11-06 10:42:31 +01:00
/ * *
2018-03-22 18:54:56 +01:00
* updater for cemu_UI , checks for Updates and download it
2017-11-06 10:42:31 +01:00
* /
2017-11-15 22:00:28 +01:00
public UpdateController ( MainWindowController mwc , String buildNumber , boolean useBeta ) {
2017-11-10 15:58:20 +01:00
mainWindowController = mwc ;
2018-12-03 00:09:02 +01:00
this . buildNumber = Integer . parseInt ( buildNumber ) ;
2017-11-10 15:58:20 +01:00
this . useBeta = useBeta ;
2017-11-06 10:42:31 +01:00
}
2017-11-15 22:00:28 +01:00
@Override
public void run ( ) {
2017-12-11 17:07:20 +01:00
LOGGER . info ( " beta: " + useBeta + " ; checking for updates ... " ) ;
2017-11-06 10:42:31 +01:00
Platform . runLater ( ( ) - > {
2017-12-19 21:35:49 +01:00
mainWindowController . getUpdateBtn ( ) . setText ( mainWindowController . getBundle ( ) . getString ( " updateBtnChecking " ) ) ;
2017-11-15 22:00:28 +01:00
} ) ;
try {
2018-12-03 00:09:02 +01:00
giteaApiUrl = new URL ( giteaApiRelease ) ;
BufferedReader ina = new BufferedReader ( new InputStreamReader ( giteaApiUrl . openStream ( ) ) ) ;
2017-11-06 10:42:31 +01:00
apiOutput = ina . readLine ( ) ;
2017-11-15 22:00:28 +01:00
ina . close ( ) ;
2017-11-06 10:42:31 +01:00
} catch ( IOException e ) {
Platform . runLater ( ( ) - > {
LOGGER . error ( " could not check update version " , e ) ;
} ) ;
}
2018-12-03 00:09:02 +01:00
JsonArray objectArray = Json . parse ( " { \" items \" : " + apiOutput + " } " ) . asObject ( ) . get ( " items " ) . asArray ( ) ;
JsonValue object = objectArray . get ( 0 ) . asObject ( ) ; // set to the latest release as default
JsonObject objectAsset = object . asObject ( ) . get ( " assets " ) . asArray ( ) . get ( 0 ) . asObject ( ) ;
for ( JsonValue objectIt : objectArray ) {
// TODO note this will download still the pre-release if there's a more recent stable version
if ( objectIt . asObject ( ) . getBoolean ( " prerelease " , false ) = = useBeta ) {
// we found the needed release either beta or not
object = objectIt ;
objectAsset = objectIt . asObject ( ) . get ( " assets " ) . asArray ( ) . get ( 0 ) . asObject ( ) ;
break ;
2017-11-15 22:00:28 +01:00
}
2017-11-10 15:58:20 +01:00
}
2018-12-03 00:09:02 +01:00
updateBuildNumber = Integer . parseInt ( object . asObject ( ) . getString ( " tag_name " , " " ) ) ;
updateName = object . asObject ( ) . getString ( " name " , " " ) ;
updateChanges = object . asObject ( ) . getString ( " body " , " " ) ;
2017-11-10 15:58:20 +01:00
2017-11-15 22:00:28 +01:00
LOGGER . info ( " Build: " + buildNumber + " , Update: " + updateBuildNumber ) ;
2018-12-03 00:09:02 +01:00
/ * *
* Compare the program BuildNumber with the current BuildNumber
* if buildNumber < updateBuildNumber then perform a update
* /
if ( buildNumber > = updateBuildNumber ) {
2017-11-06 10:42:31 +01:00
Platform . runLater ( ( ) - > {
2017-12-19 21:35:49 +01:00
mainWindowController . getUpdateBtn ( ) . setText ( mainWindowController . getBundle ( ) . getString ( " updateBtnNoUpdateAvailable " ) ) ;
2017-11-15 22:00:28 +01:00
} ) ;
2017-11-06 10:42:31 +01:00
LOGGER . info ( " no update available " ) ;
2017-11-15 22:00:28 +01:00
} else {
2017-11-06 10:42:31 +01:00
Platform . runLater ( ( ) - > {
2017-12-19 21:35:49 +01:00
mainWindowController . getUpdateBtn ( ) . setText ( mainWindowController . getBundle ( ) . getString ( " updateBtnUpdateAvailable " ) ) ;
2017-11-15 22:00:28 +01:00
} ) ;
2017-11-06 10:42:31 +01:00
LOGGER . info ( " update available " ) ;
2018-12-03 00:09:02 +01:00
browserDownloadUrl = objectAsset . getString ( " browser_download_url " , " " ) ;
2017-11-06 10:42:31 +01:00
LOGGER . info ( " download link: " + browserDownloadUrl ) ;
2017-11-15 22:00:28 +01:00
try {
// open new Http connection, ProgressMonitorInputStream for downloading the data
2017-12-26 17:33:14 +01:00
HttpURLConnection connection = ( HttpURLConnection ) new URL ( browserDownloadUrl ) . openConnection ( ) ;
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream ( null , " Downloading... " , connection . getInputStream ( ) ) ;
2017-11-06 10:42:31 +01:00
ProgressMonitor pm = pmis . getProgressMonitor ( ) ;
2017-11-15 22:00:28 +01:00
pm . setMillisToDecideToPopup ( 0 ) ;
pm . setMillisToPopup ( 0 ) ;
2017-12-26 17:33:14 +01:00
pm . setMinimum ( 0 ) ; // set beginning of the progress bar to 0
pm . setMaximum ( connection . getContentLength ( ) ) ; // set the end to the file length
2017-11-15 22:00:28 +01:00
FileUtils . copyInputStreamToFile ( pmis , new File ( " cemu_UI_update.jar " ) ) ; // download update
2017-12-26 17:33:14 +01:00
org . apache . commons . io . FileUtils . copyFile ( new File ( " cemu_UI_update.jar " ) , new File ( " cemu_UI.jar " ) ) ;
2017-11-15 22:00:28 +01:00
org . apache . commons . io . FileUtils . deleteQuietly ( new File ( " cemu_UI_update.jar " ) ) ; // delete update
2018-06-12 16:01:21 +02:00
new ProcessBuilder ( " java " , " -jar " , " cemu_UI.jar " ) . start ( ) ; // start the new application
2018-12-03 00:09:02 +01:00
System . exit ( 0 ) ; // close the current application
2017-11-06 10:42:31 +01:00
} catch ( IOException e ) {
Platform . runLater ( ( ) - > {
LOGGER . info ( " could not download update files " , e ) ;
} ) ;
}
}
}
}