|
31 | 31 | import io.github.ImpactDevelopment.installer.setting.settings.*; |
32 | 32 | import io.github.ImpactDevelopment.installer.target.InstallationModeOptions; |
33 | 33 |
|
| 34 | +import java.io.FileNotFoundException; |
34 | 35 | import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.lang.reflect.Field; |
35 | 38 | import java.nio.file.Files; |
36 | 39 | import java.nio.file.Path; |
37 | 40 | import java.nio.file.Paths; |
| 41 | +import java.util.Properties; |
38 | 42 |
|
39 | 43 | public class Args { |
40 | 44 |
|
@@ -74,6 +78,39 @@ public class Args { |
74 | 78 | @Parameter(names = {"--version"}, description = "Output version information and exit\n", help = true, order = 1) |
75 | 79 | public boolean showVersion = false; |
76 | 80 |
|
| 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 | + |
77 | 114 | public void apply(InstallationConfig config) { |
78 | 115 | if (mcPath != null) { |
79 | 116 | Path path = Paths.get(mcPath); |
@@ -120,4 +157,35 @@ private void setImpactVersion(InstallationConfig config, boolean checkMcVersionV |
120 | 157 | } |
121 | 158 | config.setSettingValue(ImpactVersionSetting.INSTANCE, version); |
122 | 159 | } |
| 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 | + } |
123 | 191 | } |
0 commit comments