Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #27 from Petschko/dev
Browse files Browse the repository at this point in the history
Added Update-Functionallity
  • Loading branch information
Petschko authored Feb 26, 2021
2 parents b2dd31b + f48ac15 commit 1ef737e
Show file tree
Hide file tree
Showing 22 changed files with 1,193 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ out/

#Other
config.pref
updateCache.pref
5 changes: 5 additions & 0 deletions cmd-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ There exists commands you can use, these are explained here:
- Will do a Key-Search in `C:\my rpg mv game\` and shows the Key if found, if not found it will ask if you want to generate it out of encrypted images
- Example 2: `java -jar "RPG Maker MV Decrypter.jar" key "C:\my rpg mv game\" false`
- Same as Example 1, just don't ask if search on images, it will always do automatically
- __update__ - Updates the Program (Help: `java -jar "RPG Maker MV Decrypter.jar" update help`)
- This Script updates the Program
- Syntax: `java -jar "RPG Maker MV Decrypter.jar" update [(Optional) Sub-Command]`
- Program Update Command: `java -jar "RPG Maker MV Decrypter.jar" update`
- Open "What's new" in your Default-Browser: `java -jar "RPG Maker MV Decrypter.jar" update whatsnew`
1 change: 1 addition & 0 deletions src/org/petschko/lib/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class Const {
public static final String creator = "Petschko";
public static final String creatorURL = "https://petschko.org/";
public static final String creatorDonationUrl = "https://www.paypal.me/petschko";

// System Constance's
public static final String ds = System.getProperty("file.separator");
Expand Down
30 changes: 30 additions & 0 deletions src/org/petschko/lib/gui/JOptionPane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.petschko.lib.gui;

/**
* Author: Peter Dragicevic
* Authors-Website: https://petschko.org/
* Date: 20.02.2021
* Time: 18:17
* <p>
* Notes: -
*/
public class JOptionPane extends javax.swing.JOptionPane {
/**
* Popups a yes/no question popup
*
* @param msg Message to display
* @param title Title of the Popup Window
* @param type Message type (Error, Warning etc)
* @param defaultYes true/false true means that "yes" is preselected
* @return JOptionPane dialog result
*/
public static int yesNoPopupQuestion(String msg, String title, int type, boolean defaultYes) {
String def;
if(defaultYes)
def = "Yes";
else
def = "No";

return JOptionPane.showOptionDialog(null, msg, title, JOptionPane.YES_NO_OPTION, type, null, new String[] {"Yes", "No"}, def);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,12 @@ protected void setDefaultTitle() {
*/
protected int getJOptionType() {
switch(this.errorLevel) {
case ERROR_LEVEL_NOTICE:
return JOptionPane.INFORMATION_MESSAGE;
case ERROR_LEVEL_WARNING:
return JOptionPane.WARNING_MESSAGE;
case ERROR_LEVEL_ERROR:
return JOptionPane.ERROR_MESSAGE;
case ERROR_LEVEL_FATAL:
return JOptionPane.ERROR_MESSAGE;
case ERROR_LEVEL_NOTICE:
case ERROR_LEVEL_INFO:
default:
return JOptionPane.INFORMATION_MESSAGE;
Expand Down
282 changes: 282 additions & 0 deletions src/org/petschko/lib/update/Update.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
package org.petschko.lib.update;

import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Author: Peter Dragicevic [peter&#064;petschko.org]
* Authors-Website: http://petschko.org/
* Date: 05.10.2017
* Time: 14:37
* Update: -
* Version: 0.0.1
*
* Notes: Update Class
*/
public class Update {
private UpdateCache updateCache;
private URL checkVersionUrl;
private URL whatsNewUrl = null;
private URL downloadURL = null;
private Version currentVersion;
private Version newestVersion = null;
private boolean hasNewVersion = false;
private boolean ignoreCache = false;

/**
* Update Constructor
*
* @param checkVersionUrl - URL to get the newest Version-Number
* @param currentVersion - Current Version
* @throws IOException - IO-Exception
*/
public Update(String checkVersionUrl, String currentVersion) throws IOException {
this.updateCache = new UpdateCache();
this.checkVersionUrl = new URL(checkVersionUrl);
this.currentVersion = new Version(currentVersion);

this.init();
}

/**
* Update Constructor
*
* @param checkVersionUrl - URL to get the newest Version-Number
* @param currentVersion - Current Version
* @param cacheTime - Cache-Time in Sec
* @throws IOException - IO-Exception
*/
public Update(String checkVersionUrl, String currentVersion, long cacheTime) throws IOException {
this.updateCache = new UpdateCache(cacheTime);
this.checkVersionUrl = new URL(checkVersionUrl);
this.currentVersion = new Version(currentVersion);

this.init();
}

/**
* Update Constructor
*
* @param checkVersionUrl - URL to get the newest Version-Number
* @param currentVersion - Current Version
* @param ignoreCache - Should the Cache be ignored?
* @throws IOException - IO-Exception
*/
public Update(String checkVersionUrl, String currentVersion, boolean ignoreCache) throws IOException {
this.updateCache = new UpdateCache();
this.checkVersionUrl = new URL(checkVersionUrl);
this.currentVersion = new Version(currentVersion);
this.ignoreCache = ignoreCache;

this.init();
}

/**
* Gets the Whats-New URL
*
* @return - Whats-New URL
*/
public URL getWhatsNewUrl() {
return whatsNewUrl;
}

/**
* Initiates this instance (may loads cache)
*/
private void init() throws IOException {
if(this.ignoreCache) {
this.getUpdateInfo();
} else {
if(this.updateCache.loadCache()) {
this.newestVersion = this.updateCache.newestVersionCache;
this.downloadURL = this.updateCache.cachedDownloadUrl;
this.whatsNewUrl = this.updateCache.cachedWhatsNewUrl;
} else {
this.getUpdateInfo();
}
}

this.checkVersion();
}

/**
* Get all information from the File for the Update process
*/
private void getUpdateInfo() throws IOException {
// Read the Update-URL
InputStream content = this.checkVersionUrl.openStream();


// Convert the read Content to Strings
int c;
int currentString = 0;
StringBuilder version = new StringBuilder();
StringBuilder downloadUrl = new StringBuilder();
StringBuilder whatsNewUrl = new StringBuilder();

while(true) {
try {
c = content.read();

// Exit loop if file reaches end
if(c == -1)
break;

if(c == (int) ';')
currentString++;
else {
switch(currentString) {
case 0:
version.append((char) c);
break;
case 1:
downloadUrl.append((char) c);
break;
case 2:
whatsNewUrl.append((char) c);
break;
default:
}
}
} catch(IOException e) {
e.printStackTrace();
break;
}
}

this.newestVersion = new Version(version.toString().trim());

try {
this.downloadURL = new URL(downloadUrl.toString().trim());
this.whatsNewUrl = new URL(whatsNewUrl.toString().trim());
} catch(MalformedURLException e) {
e.printStackTrace();
}

this.savesCache();
}

/**
* Saves new Data to the Cache
*/
private void savesCache() {
this.updateCache.newestVersionCache = this.newestVersion;
this.updateCache.cachedDownloadUrl = this.downloadURL;
this.updateCache.cachedWhatsNewUrl = this.whatsNewUrl;

this.updateCache.saveCache();
}

/**
* Checks if the Version is the newest
*/
private void checkVersion() {
if(this.newestVersion == null)
return;

if(! this.currentVersion.versionsEqual(this.newestVersion))
this.hasNewVersion = this.currentVersion.thisIsLowerThan(this.newestVersion);
}

/**
* Shows if the current Version is the newest
*
* @return - Is newest Version
*/
public boolean isHasNewVersion() {
return this.hasNewVersion;
}

/**
* Get the newest Version-Number
*
* @return - Newest Version-Number or null if could not read Update-URL
*/
public String getNewestVersion() {
return newestVersion.getVersion();
}

/**
* Get the current Version
*
* @return - Current Version
*/
public String getCurrentVersion() {
return currentVersion.getVersion();
}

/**
* Starts the updater but not relaunch after update
*
* @param targetJar - Target jar which should be updated
* @param gui - Should the Updater show a GUI window
*/
public void runUpdate(String targetJar, boolean gui) throws UpdateException {
this.runUpdate(targetJar, gui, false, null);
}


/**
* Starts the updater
*
* @param targetJar - Target jar which should be updated
* @param gui - Should the Updater show a GUI window
* @param relaunch - Relaunch this Program after Update
* @param relaunchArgs - Args for relaunch, can be null if none
*/
public void runUpdate(String targetJar, boolean gui, boolean relaunch, @Nullable String[] relaunchArgs) throws UpdateException {
File updaterFile = new File("update.jar");
File targetJarFile = new File(targetJar);

if(this.newestVersion == null)
throw new UpdateException("Newest Version is not set!", this.currentVersion);

if(this.newestVersion.versionsEqual(new Version("")))
throw new UpdateException("Newest Version is empty...", this.currentVersion);

if(this.newestVersion.versionsEqual(this.currentVersion))
throw new UpdateException("This Program is already up to date!", this.currentVersion, this.newestVersion);

if(! targetJarFile.exists() || targetJarFile.isDirectory())
throw new UpdateException("Can not find the Target-Jar", this.currentVersion);

if(! updaterFile.exists() || updaterFile.isDirectory())
throw new UpdateException("Updater not found!", this.currentVersion);

String[] run = {
"java",
"-jar",
"update.jar",
"\"" + targetJar + "\"",
"\"" + this.downloadURL.toString() + "\"",
gui ? "true" : "false",
relaunch ? "true" : "false"
};

// Add args
if(relaunchArgs != null) {
String[] tmp = new String[run.length + relaunchArgs.length];
int i;
for(i = 0; i < run.length; i++)
tmp[i] = run[i];

int n = i;
for(; i < tmp.length; i++)
tmp[i] = relaunchArgs[i - n];

run = tmp;
}

try {
Runtime.getRuntime().exec(run);
} catch (Exception e) {
throw new UpdateException(e.getMessage(), this.currentVersion, e);
}
System.exit(0);
}
}
Loading

0 comments on commit 1ef737e

Please sign in to comment.