11package xyz .gianlu .librespot ;
22
3+ import com .electronwill .nightconfig .core .Config ;
4+ import com .electronwill .nightconfig .core .ConfigFormat ;
35import com .electronwill .nightconfig .core .file .CommentedFileConfig ;
6+ import com .electronwill .nightconfig .core .file .FileConfig ;
47import com .electronwill .nightconfig .core .file .FileNotFoundAction ;
8+ import com .electronwill .nightconfig .core .file .FormatDetector ;
9+ import com .electronwill .nightconfig .core .io .ConfigParser ;
10+ import com .electronwill .nightconfig .core .io .ConfigWriter ;
511import org .apache .log4j .Logger ;
612import org .jetbrains .annotations .NotNull ;
713import org .jetbrains .annotations .Nullable ;
1218import xyz .gianlu .librespot .player .codecs .AudioQuality ;
1319
1420import java .io .File ;
21+ import java .io .FileReader ;
22+ import java .io .IOException ;
1523import java .io .InputStream ;
24+ import java .util .Map ;
25+ import java .util .Properties ;
26+ import java .util .function .Supplier ;
1627
1728/**
1829 * @author Gianlu
1930 */
2031public final class FileConfiguration extends AbsConfiguration {
2132 private static final Logger LOGGER = Logger .getLogger (FileConfiguration .class );
33+
34+ static {
35+ FormatDetector .registerExtension ("properties" , new PropertiesFormat ());
36+ }
37+
2238 private final CommentedFileConfig config ;
2339
24- public FileConfiguration (@ Nullable String [] override ) {
40+ public FileConfiguration (@ Nullable String [] override ) throws IOException {
2541 File confFile = null ;
2642 if (override != null ) {
2743 for (String arg : override ) {
@@ -32,12 +48,27 @@ public FileConfiguration(@Nullable String[] override) {
3248
3349 if (confFile == null ) confFile = new File ("config.toml" );
3450
51+ if (!confFile .exists ()) {
52+ File oldConf = new File ("conf.properties" );
53+ if (oldConf .exists ()) confFile = oldConf ;
54+ }
55+
56+ boolean migrating = FormatDetector .detect (confFile ) instanceof PropertiesFormat ;
57+
3558 InputStream defaultConfig = FileConfiguration .class .getClassLoader ().getResourceAsStream ("default.toml" );
3659 if (defaultConfig == null ) throw new IllegalStateException ();
3760
38- config = CommentedFileConfig .builder (confFile ).onFileNotFound (FileNotFoundAction .copyData (defaultConfig )).build ();
61+ config = CommentedFileConfig .builder (migrating ? new File ( "config.toml" ) : confFile ).onFileNotFound (FileNotFoundAction .copyData (defaultConfig )).build ();
3962 config .load ();
4063
64+ if (migrating ) {
65+ migrateOldConfig (confFile , config );
66+ config .save ();
67+ confFile .delete ();
68+
69+ LOGGER .info ("Your configuration has been migrated to `config.toml`, change your input file if needed." );
70+ }
71+
4172 if (override != null && override .length > 0 ) {
4273 for (String str : override ) {
4374 if (str == null ) continue ;
@@ -57,6 +88,27 @@ public FileConfiguration(@Nullable String[] override) {
5788 }
5889 }
5990
91+ private static void migrateOldConfig (@ NotNull File confFile , @ NotNull FileConfig config ) throws IOException {
92+ Properties old = new Properties ();
93+ try (FileReader fr = new FileReader (confFile )) {
94+ old .load (fr );
95+ }
96+
97+ for (Object key : old .keySet ()) {
98+ String val = old .getProperty ((String ) key );
99+ if ("true" .equals (val ) || "false" .equals (val )) {
100+ config .set ((String ) key , Boolean .parseBoolean (val ));
101+ } else {
102+ try {
103+ int i = Integer .parseInt (val );
104+ config .set ((String ) key , i );
105+ } catch (NumberFormatException ex ) {
106+ config .set ((String ) key , val );
107+ }
108+ }
109+ }
110+ }
111+
60112 @ NotNull
61113 private String [] getStringArray (@ NotNull String key ) {
62114 String str = config .get (key );
@@ -191,4 +243,26 @@ public int zeroconfListenPort() {
191243 public String [] zeroconfInterfaces () {
192244 return getStringArray ("zeroconf.interfaces" );
193245 }
246+
247+ private final static class PropertiesFormat implements ConfigFormat <Config > {
248+ @ Override
249+ public ConfigWriter createWriter () {
250+ return null ;
251+ }
252+
253+ @ Override
254+ public ConfigParser <Config > createParser () {
255+ return null ;
256+ }
257+
258+ @ Override
259+ public Config createConfig (Supplier <Map <String , Object >> mapCreator ) {
260+ return null ;
261+ }
262+
263+ @ Override
264+ public boolean supportsComments () {
265+ return false ;
266+ }
267+ }
194268}
0 commit comments