Skip to content

Commit

Permalink
Externalize static application properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Argent77 committed Dec 4, 2024
1 parent 9cfb3f1 commit f01fe33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Java CI with Apache Ant
on:
push:
branches: [ devel ]
# pull_request:
# branches: [ devel ]

jobs:
build:
Expand All @@ -19,7 +17,7 @@ jobs:
- name: Build with Ant
run: |
hash=$(echo "${{ github.sha }}" | sed -e 's/\(.\{7\}\).*/\1/')
sed -i "s/\(VERSION *= *\"v\?[0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?\)[^\"]*\"/\1-$(date +%Y%m%d) (${hash})\"/" src/org/infinity/NearInfinity.java
sed -i "s/\(app_version\s*=\s*v\?[0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?\).*/\1-$(date +%Y%m%d) (${hash})/" src/nearinfinity.properties
sed -i 's/debug="false"/debug="true"/' build.xml
ant -noinput -buildfile build.xml
- name: Upload artifact
Expand Down
2 changes: 2 additions & 0 deletions src/nearinfinity.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app_version = v2.4-20240914
java_version_min = 8
45 changes: 37 additions & 8 deletions src/org/infinity/NearInfinity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -44,6 +45,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
Expand Down Expand Up @@ -142,12 +144,10 @@
import org.infinity.util.tuples.Couple;

public final class NearInfinity extends JFrame implements ActionListener, ViewableContainer {
// the current Near Infinity version
private static final String VERSION = "v2.4-20240914";

// the minimum supported Java version
private static final int JAVA_VERSION_MIN = 8;
private static final String PROPERTIES_FILENAME = "nearinfinity.properties";

private static final String PROP_APP_VERSION = "app_version";
private static final String PROP_JAVA_VERSION_MIN = "java_version_min";

public static final String KEYFILENAME = "chitin.key";
public static final String WINDOW_SIZEX = "WindowSizeX";
Expand Down Expand Up @@ -288,7 +288,35 @@ public static NearInfinity getInstance() {

/** Returns the current NearInfinity version. */
public static String getVersion() {
return VERSION;
return getAppProperty(PROP_APP_VERSION, "v1.0-19700101");
}

/** Returns the minimum supported Java version. */
public static int getMinJavaVersion() {
try {
return Integer.parseInt(getAppProperty(PROP_JAVA_VERSION_MIN, "0"));
} catch (NumberFormatException e) {
Logger.error(e);
}
return 0;
}

/**
* Returns the value of the specified property from the app-specific properties resource.
*
* @param key The property key.
* @param defValue Default value that is returned if the property could not be read.
* @return The value in the properties resource with the specified key value.
*/
private static String getAppProperty(String key, String defValue) {
try (final InputStream is = ClassLoader.getSystemResourceAsStream(PROPERTIES_FILENAME)) {
final Properties prop = new Properties();
prop.load(is);
return prop.getProperty(key, defValue);
} catch (IOException e) {
Logger.error(e);
}
return defValue;
}

public static void printHelp(String jarFile) {
Expand Down Expand Up @@ -387,8 +415,9 @@ public static void main(String[] args) {
}

// Checking Java version
if (Platform.JAVA_VERSION < JAVA_VERSION_MIN) {
JOptionPane.showMessageDialog(null, String.format("Java %d or later is required to run Near Infinity!", JAVA_VERSION_MIN),
final int minJavaVersion = getMinJavaVersion();
if (Platform.JAVA_VERSION < minJavaVersion) {
JOptionPane.showMessageDialog(null, String.format("Java %d or later is required to run Near Infinity!", minJavaVersion),
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(10);
}
Expand Down

0 comments on commit f01fe33

Please sign in to comment.