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

Commit 1ef737e

Browse files
authored
Merge pull request #27 from Petschko/dev
Added Update-Functionallity
2 parents b2dd31b + f48ac15 commit 1ef737e

22 files changed

+1193
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ out/
2020

2121
#Other
2222
config.pref
23+
updateCache.pref

cmd-instructions.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ There exists commands you can use, these are explained here:
5454
- 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
5555
- Example 2: `java -jar "RPG Maker MV Decrypter.jar" key "C:\my rpg mv game\" false`
5656
- Same as Example 1, just don't ask if search on images, it will always do automatically
57+
- __update__ - Updates the Program (Help: `java -jar "RPG Maker MV Decrypter.jar" update help`)
58+
- This Script updates the Program
59+
- Syntax: `java -jar "RPG Maker MV Decrypter.jar" update [(Optional) Sub-Command]`
60+
- Program Update Command: `java -jar "RPG Maker MV Decrypter.jar" update`
61+
- Open "What's new" in your Default-Browser: `java -jar "RPG Maker MV Decrypter.jar" update whatsnew`

src/org/petschko/lib/Const.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class Const {
1414
public static final String creator = "Petschko";
1515
public static final String creatorURL = "https://petschko.org/";
16+
public static final String creatorDonationUrl = "https://www.paypal.me/petschko";
1617

1718
// System Constance's
1819
public static final String ds = System.getProperty("file.separator");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.petschko.lib.gui;
2+
3+
/**
4+
* Author: Peter Dragicevic
5+
* Authors-Website: https://petschko.org/
6+
* Date: 20.02.2021
7+
* Time: 18:17
8+
* <p>
9+
* Notes: -
10+
*/
11+
public class JOptionPane extends javax.swing.JOptionPane {
12+
/**
13+
* Popups a yes/no question popup
14+
*
15+
* @param msg Message to display
16+
* @param title Title of the Popup Window
17+
* @param type Message type (Error, Warning etc)
18+
* @param defaultYes true/false true means that "yes" is preselected
19+
* @return JOptionPane dialog result
20+
*/
21+
public static int yesNoPopupQuestion(String msg, String title, int type, boolean defaultYes) {
22+
String def;
23+
if(defaultYes)
24+
def = "Yes";
25+
else
26+
def = "No";
27+
28+
return JOptionPane.showOptionDialog(null, msg, title, JOptionPane.YES_NO_OPTION, type, null, new String[] {"Yes", "No"}, def);
29+
}
30+
}

src/org/petschko/lib/gui/notification/NotificationWindow.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,12 @@ protected void setDefaultTitle() {
211211
*/
212212
protected int getJOptionType() {
213213
switch(this.errorLevel) {
214-
case ERROR_LEVEL_NOTICE:
215-
return JOptionPane.INFORMATION_MESSAGE;
216214
case ERROR_LEVEL_WARNING:
217215
return JOptionPane.WARNING_MESSAGE;
218216
case ERROR_LEVEL_ERROR:
219-
return JOptionPane.ERROR_MESSAGE;
220217
case ERROR_LEVEL_FATAL:
221218
return JOptionPane.ERROR_MESSAGE;
219+
case ERROR_LEVEL_NOTICE:
222220
case ERROR_LEVEL_INFO:
223221
default:
224222
return JOptionPane.INFORMATION_MESSAGE;
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
package org.petschko.lib.update;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
11+
/**
12+
* Author: Peter Dragicevic [peter&#064;petschko.org]
13+
* Authors-Website: http://petschko.org/
14+
* Date: 05.10.2017
15+
* Time: 14:37
16+
* Update: -
17+
* Version: 0.0.1
18+
*
19+
* Notes: Update Class
20+
*/
21+
public class Update {
22+
private UpdateCache updateCache;
23+
private URL checkVersionUrl;
24+
private URL whatsNewUrl = null;
25+
private URL downloadURL = null;
26+
private Version currentVersion;
27+
private Version newestVersion = null;
28+
private boolean hasNewVersion = false;
29+
private boolean ignoreCache = false;
30+
31+
/**
32+
* Update Constructor
33+
*
34+
* @param checkVersionUrl - URL to get the newest Version-Number
35+
* @param currentVersion - Current Version
36+
* @throws IOException - IO-Exception
37+
*/
38+
public Update(String checkVersionUrl, String currentVersion) throws IOException {
39+
this.updateCache = new UpdateCache();
40+
this.checkVersionUrl = new URL(checkVersionUrl);
41+
this.currentVersion = new Version(currentVersion);
42+
43+
this.init();
44+
}
45+
46+
/**
47+
* Update Constructor
48+
*
49+
* @param checkVersionUrl - URL to get the newest Version-Number
50+
* @param currentVersion - Current Version
51+
* @param cacheTime - Cache-Time in Sec
52+
* @throws IOException - IO-Exception
53+
*/
54+
public Update(String checkVersionUrl, String currentVersion, long cacheTime) throws IOException {
55+
this.updateCache = new UpdateCache(cacheTime);
56+
this.checkVersionUrl = new URL(checkVersionUrl);
57+
this.currentVersion = new Version(currentVersion);
58+
59+
this.init();
60+
}
61+
62+
/**
63+
* Update Constructor
64+
*
65+
* @param checkVersionUrl - URL to get the newest Version-Number
66+
* @param currentVersion - Current Version
67+
* @param ignoreCache - Should the Cache be ignored?
68+
* @throws IOException - IO-Exception
69+
*/
70+
public Update(String checkVersionUrl, String currentVersion, boolean ignoreCache) throws IOException {
71+
this.updateCache = new UpdateCache();
72+
this.checkVersionUrl = new URL(checkVersionUrl);
73+
this.currentVersion = new Version(currentVersion);
74+
this.ignoreCache = ignoreCache;
75+
76+
this.init();
77+
}
78+
79+
/**
80+
* Gets the Whats-New URL
81+
*
82+
* @return - Whats-New URL
83+
*/
84+
public URL getWhatsNewUrl() {
85+
return whatsNewUrl;
86+
}
87+
88+
/**
89+
* Initiates this instance (may loads cache)
90+
*/
91+
private void init() throws IOException {
92+
if(this.ignoreCache) {
93+
this.getUpdateInfo();
94+
} else {
95+
if(this.updateCache.loadCache()) {
96+
this.newestVersion = this.updateCache.newestVersionCache;
97+
this.downloadURL = this.updateCache.cachedDownloadUrl;
98+
this.whatsNewUrl = this.updateCache.cachedWhatsNewUrl;
99+
} else {
100+
this.getUpdateInfo();
101+
}
102+
}
103+
104+
this.checkVersion();
105+
}
106+
107+
/**
108+
* Get all information from the File for the Update process
109+
*/
110+
private void getUpdateInfo() throws IOException {
111+
// Read the Update-URL
112+
InputStream content = this.checkVersionUrl.openStream();
113+
114+
115+
// Convert the read Content to Strings
116+
int c;
117+
int currentString = 0;
118+
StringBuilder version = new StringBuilder();
119+
StringBuilder downloadUrl = new StringBuilder();
120+
StringBuilder whatsNewUrl = new StringBuilder();
121+
122+
while(true) {
123+
try {
124+
c = content.read();
125+
126+
// Exit loop if file reaches end
127+
if(c == -1)
128+
break;
129+
130+
if(c == (int) ';')
131+
currentString++;
132+
else {
133+
switch(currentString) {
134+
case 0:
135+
version.append((char) c);
136+
break;
137+
case 1:
138+
downloadUrl.append((char) c);
139+
break;
140+
case 2:
141+
whatsNewUrl.append((char) c);
142+
break;
143+
default:
144+
}
145+
}
146+
} catch(IOException e) {
147+
e.printStackTrace();
148+
break;
149+
}
150+
}
151+
152+
this.newestVersion = new Version(version.toString().trim());
153+
154+
try {
155+
this.downloadURL = new URL(downloadUrl.toString().trim());
156+
this.whatsNewUrl = new URL(whatsNewUrl.toString().trim());
157+
} catch(MalformedURLException e) {
158+
e.printStackTrace();
159+
}
160+
161+
this.savesCache();
162+
}
163+
164+
/**
165+
* Saves new Data to the Cache
166+
*/
167+
private void savesCache() {
168+
this.updateCache.newestVersionCache = this.newestVersion;
169+
this.updateCache.cachedDownloadUrl = this.downloadURL;
170+
this.updateCache.cachedWhatsNewUrl = this.whatsNewUrl;
171+
172+
this.updateCache.saveCache();
173+
}
174+
175+
/**
176+
* Checks if the Version is the newest
177+
*/
178+
private void checkVersion() {
179+
if(this.newestVersion == null)
180+
return;
181+
182+
if(! this.currentVersion.versionsEqual(this.newestVersion))
183+
this.hasNewVersion = this.currentVersion.thisIsLowerThan(this.newestVersion);
184+
}
185+
186+
/**
187+
* Shows if the current Version is the newest
188+
*
189+
* @return - Is newest Version
190+
*/
191+
public boolean isHasNewVersion() {
192+
return this.hasNewVersion;
193+
}
194+
195+
/**
196+
* Get the newest Version-Number
197+
*
198+
* @return - Newest Version-Number or null if could not read Update-URL
199+
*/
200+
public String getNewestVersion() {
201+
return newestVersion.getVersion();
202+
}
203+
204+
/**
205+
* Get the current Version
206+
*
207+
* @return - Current Version
208+
*/
209+
public String getCurrentVersion() {
210+
return currentVersion.getVersion();
211+
}
212+
213+
/**
214+
* Starts the updater but not relaunch after update
215+
*
216+
* @param targetJar - Target jar which should be updated
217+
* @param gui - Should the Updater show a GUI window
218+
*/
219+
public void runUpdate(String targetJar, boolean gui) throws UpdateException {
220+
this.runUpdate(targetJar, gui, false, null);
221+
}
222+
223+
224+
/**
225+
* Starts the updater
226+
*
227+
* @param targetJar - Target jar which should be updated
228+
* @param gui - Should the Updater show a GUI window
229+
* @param relaunch - Relaunch this Program after Update
230+
* @param relaunchArgs - Args for relaunch, can be null if none
231+
*/
232+
public void runUpdate(String targetJar, boolean gui, boolean relaunch, @Nullable String[] relaunchArgs) throws UpdateException {
233+
File updaterFile = new File("update.jar");
234+
File targetJarFile = new File(targetJar);
235+
236+
if(this.newestVersion == null)
237+
throw new UpdateException("Newest Version is not set!", this.currentVersion);
238+
239+
if(this.newestVersion.versionsEqual(new Version("")))
240+
throw new UpdateException("Newest Version is empty...", this.currentVersion);
241+
242+
if(this.newestVersion.versionsEqual(this.currentVersion))
243+
throw new UpdateException("This Program is already up to date!", this.currentVersion, this.newestVersion);
244+
245+
if(! targetJarFile.exists() || targetJarFile.isDirectory())
246+
throw new UpdateException("Can not find the Target-Jar", this.currentVersion);
247+
248+
if(! updaterFile.exists() || updaterFile.isDirectory())
249+
throw new UpdateException("Updater not found!", this.currentVersion);
250+
251+
String[] run = {
252+
"java",
253+
"-jar",
254+
"update.jar",
255+
"\"" + targetJar + "\"",
256+
"\"" + this.downloadURL.toString() + "\"",
257+
gui ? "true" : "false",
258+
relaunch ? "true" : "false"
259+
};
260+
261+
// Add args
262+
if(relaunchArgs != null) {
263+
String[] tmp = new String[run.length + relaunchArgs.length];
264+
int i;
265+
for(i = 0; i < run.length; i++)
266+
tmp[i] = run[i];
267+
268+
int n = i;
269+
for(; i < tmp.length; i++)
270+
tmp[i] = relaunchArgs[i - n];
271+
272+
run = tmp;
273+
}
274+
275+
try {
276+
Runtime.getRuntime().exec(run);
277+
} catch (Exception e) {
278+
throw new UpdateException(e.getMessage(), this.currentVersion, e);
279+
}
280+
System.exit(0);
281+
}
282+
}

0 commit comments

Comments
 (0)