Skip to content

Commit 2a07e1f

Browse files
committed
Load default args from default_args.properties
This allows us to inject default_args from the download server, e.g. to dynamically create nightly installers.
1 parent 1d282cd commit 2a07e1f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- The ability to override CLI argument defaults from a properties file in the jar. This means the Donator Installer can be generated dynamically by the website.
11+
912
## [0.8.3] - 2020-02-21
1013

1114
### Fixed

src/main/java/io/github/ImpactDevelopment/installer/Args.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@
3131
import io.github.ImpactDevelopment.installer.setting.settings.*;
3232
import io.github.ImpactDevelopment.installer.target.InstallationModeOptions;
3333

34+
import java.io.FileNotFoundException;
3435
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.lang.reflect.Field;
3538
import java.nio.file.Files;
3639
import java.nio.file.Path;
3740
import java.nio.file.Paths;
41+
import java.util.Properties;
3842

3943
public class Args {
4044

@@ -74,6 +78,39 @@ public class Args {
7478
@Parameter(names = {"--version"}, description = "Output version information and exit\n", help = true, order = 1)
7579
public boolean showVersion = false;
7680

81+
public Args() {
82+
// Lets look for a properties file and use it to override defaults
83+
try {
84+
getProperties("default_args.properties").forEach((o1, o2) -> {
85+
String key, value;
86+
try {
87+
key = (String) o1;
88+
value = (String) o2;
89+
} catch (Throwable ignored) {
90+
System.err.println("WTF! unable to cast key or value to string: " + o1 + ", " + o2);
91+
return;
92+
}
93+
94+
try {
95+
Field field = Args.class.getField(key);
96+
if (!field.isAnnotationPresent(Parameter.class)) {
97+
System.err.println("default_args.properties tried to override non-parameter field " + field.getName());
98+
return;
99+
}
100+
// Parse value to the correct type and set the field's value
101+
field.set(this, toType(field.getType(), value));
102+
} catch (Throwable t) {
103+
System.err.println("Error setting default value: " + key + " = " + value);
104+
t.printStackTrace();
105+
}
106+
});
107+
} catch (Throwable t) {
108+
if (!(t instanceof FileNotFoundException)) {
109+
t.printStackTrace();
110+
}
111+
}
112+
}
113+
77114
public void apply(InstallationConfig config) {
78115
if (mcPath != null) {
79116
Path path = Paths.get(mcPath);
@@ -120,4 +157,35 @@ private void setImpactVersion(InstallationConfig config, boolean checkMcVersionV
120157
}
121158
config.setSettingValue(ImpactVersionSetting.INSTANCE, version);
122159
}
160+
161+
// Get a properties file from the classpath
162+
private static Properties getProperties(String filename) throws IOException {
163+
Properties properties = new Properties();
164+
165+
InputStream inputStream = Args.class.getClassLoader().getResourceAsStream(filename);
166+
if (inputStream == null) {
167+
throw new FileNotFoundException(filename + "' not found in the classpath");
168+
}
169+
170+
try {
171+
properties.load(inputStream);
172+
} finally {
173+
inputStream.close();
174+
}
175+
176+
return properties;
177+
}
178+
179+
// Convert a string value to a primitive type
180+
// Won't work for non-primitive types, but luckily all our args are primitives
181+
private static Object toType(Class<?> type, String value ) {
182+
if(Boolean.class == type || Boolean.TYPE == type) return Boolean.parseBoolean(value);
183+
if(Byte.class == type || Byte.TYPE == type) return Byte.parseByte(value);
184+
if(Short.class == type || Short.TYPE == type) return Short.parseShort(value);
185+
if(Integer.class == type || Integer.TYPE == type) return Integer.parseInt(value);
186+
if(Long.class == type || Long.TYPE == type) return Long.parseLong(value);
187+
if(Float.class == type || Float.TYPE == type) return Float.parseFloat(value);
188+
if(Double.class == type || Double.TYPE == type) return Double.parseDouble(value);
189+
return value;
190+
}
123191
}

0 commit comments

Comments
 (0)