mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-14 06:17:47 -04:00
Move config serialization/deserialization to Config class
This commit is contained in:
parent
929ce398eb
commit
5b0b926cbd
@ -17,12 +17,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.setting;
|
package org.jackhuang.hmcl.setting;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import org.hildan.fxgson.creators.ObservableListCreator;
|
||||||
|
import org.hildan.fxgson.creators.ObservableMapCreator;
|
||||||
|
import org.hildan.fxgson.creators.ObservableSetCreator;
|
||||||
|
import org.hildan.fxgson.factories.JavaFxPropertyTypeAdapterFactory;
|
||||||
import org.jackhuang.hmcl.Launcher;
|
import org.jackhuang.hmcl.Launcher;
|
||||||
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer;
|
import org.jackhuang.hmcl.auth.authlibinjector.AuthlibInjectorServer;
|
||||||
|
import org.jackhuang.hmcl.util.FileTypeAdapter;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
import javafx.beans.property.BooleanProperty;
|
import javafx.beans.property.BooleanProperty;
|
||||||
@ -36,9 +45,25 @@ import javafx.beans.property.StringProperty;
|
|||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.collections.ObservableMap;
|
import javafx.collections.ObservableMap;
|
||||||
|
import javafx.collections.ObservableSet;
|
||||||
|
|
||||||
public final class Config implements Cloneable {
|
public final class Config implements Cloneable {
|
||||||
|
|
||||||
|
private static final Gson CONFIG_GSON = new GsonBuilder()
|
||||||
|
.registerTypeAdapter(VersionSetting.class, VersionSetting.Serializer.INSTANCE)
|
||||||
|
.registerTypeAdapter(Profile.class, Profile.Serializer.INSTANCE)
|
||||||
|
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
|
||||||
|
.registerTypeAdapter(ObservableList.class, new ObservableListCreator())
|
||||||
|
.registerTypeAdapter(ObservableSet.class, new ObservableSetCreator())
|
||||||
|
.registerTypeAdapter(ObservableMap.class, new ObservableMapCreator())
|
||||||
|
.registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true))
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.create();
|
||||||
|
|
||||||
|
public static Config fromJson(String json) throws JsonParseException {
|
||||||
|
return CONFIG_GSON.fromJson(json, Config.class);
|
||||||
|
}
|
||||||
|
|
||||||
@SerializedName("last")
|
@SerializedName("last")
|
||||||
public final StringProperty selectedProfile = new SimpleStringProperty("");
|
public final StringProperty selectedProfile = new SimpleStringProperty("");
|
||||||
|
|
||||||
@ -104,8 +129,12 @@ public final class Config implements Cloneable {
|
|||||||
|
|
||||||
public final ObservableList<AuthlibInjectorServer> authlibInjectorServers = FXCollections.observableArrayList();
|
public final ObservableList<AuthlibInjectorServer> authlibInjectorServers = FXCollections.observableArrayList();
|
||||||
|
|
||||||
|
public String toJson() {
|
||||||
|
return CONFIG_GSON.toJson(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Config clone() {
|
public Config clone() {
|
||||||
return Settings.GSON.fromJson(Settings.GSON.toJson(this), Config.class);
|
return fromJson(this.toJson());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,21 +17,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.setting;
|
package org.jackhuang.hmcl.setting;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
|
|
||||||
import javafx.beans.property.ObjectProperty;
|
import javafx.beans.property.ObjectProperty;
|
||||||
import javafx.beans.property.StringProperty;
|
import javafx.beans.property.StringProperty;
|
||||||
import javafx.beans.value.ObservableValue;
|
import javafx.beans.value.ObservableValue;
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.collections.ObservableMap;
|
|
||||||
import javafx.collections.ObservableSet;
|
|
||||||
import javafx.scene.text.Font;
|
import javafx.scene.text.Font;
|
||||||
|
|
||||||
import org.hildan.fxgson.creators.ObservableListCreator;
|
|
||||||
import org.hildan.fxgson.creators.ObservableMapCreator;
|
|
||||||
import org.hildan.fxgson.creators.ObservableSetCreator;
|
|
||||||
import org.hildan.fxgson.factories.JavaFxPropertyTypeAdapterFactory;
|
|
||||||
import org.jackhuang.hmcl.Launcher;
|
import org.jackhuang.hmcl.Launcher;
|
||||||
import org.jackhuang.hmcl.auth.Account;
|
import org.jackhuang.hmcl.auth.Account;
|
||||||
import org.jackhuang.hmcl.auth.AccountFactory;
|
import org.jackhuang.hmcl.auth.AccountFactory;
|
||||||
@ -60,16 +50,6 @@ import static org.jackhuang.hmcl.util.Lang.tryCast;
|
|||||||
import static org.jackhuang.hmcl.util.Logging.LOG;
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
public static final Gson GSON = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(VersionSetting.class, VersionSetting.Serializer.INSTANCE)
|
|
||||||
.registerTypeAdapter(Profile.class, Profile.Serializer.INSTANCE)
|
|
||||||
.registerTypeAdapter(File.class, FileTypeAdapter.INSTANCE)
|
|
||||||
.registerTypeAdapter(ObservableList.class, new ObservableListCreator())
|
|
||||||
.registerTypeAdapter(ObservableSet.class, new ObservableSetCreator())
|
|
||||||
.registerTypeAdapter(ObservableMap.class, new ObservableMapCreator())
|
|
||||||
.registerTypeAdapterFactory(new JavaFxPropertyTypeAdapterFactory(true, true))
|
|
||||||
.setPrettyPrinting()
|
|
||||||
.create();
|
|
||||||
|
|
||||||
public static final String SETTINGS_FILE_NAME = "hmcl.json";
|
public static final String SETTINGS_FILE_NAME = "hmcl.json";
|
||||||
public static final File SETTINGS_FILE = new File(SETTINGS_FILE_NAME).getAbsoluteFile();
|
public static final File SETTINGS_FILE = new File(SETTINGS_FILE_NAME).getAbsoluteFile();
|
||||||
@ -131,7 +111,7 @@ public class Settings {
|
|||||||
if (StringUtils.isBlank(str))
|
if (StringUtils.isBlank(str))
|
||||||
Logging.LOG.finer("Settings file is empty, use the default settings.");
|
Logging.LOG.finer("Settings file is empty, use the default settings.");
|
||||||
else {
|
else {
|
||||||
Config d = GSON.fromJson(str, Config.class);
|
Config d = Config.fromJson(str);
|
||||||
if (d != null)
|
if (d != null)
|
||||||
c = d;
|
c = d;
|
||||||
}
|
}
|
||||||
@ -152,7 +132,7 @@ public class Settings {
|
|||||||
SETTINGS.accounts.add(storage);
|
SETTINGS.accounts.add(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileUtils.writeText(SETTINGS_FILE, GSON.toJson(SETTINGS));
|
FileUtils.writeText(SETTINGS_FILE, SETTINGS.toJson());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Logging.LOG.log(Level.SEVERE, "Failed to save config", ex);
|
Logging.LOG.log(Level.SEVERE, "Failed to save config", ex);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ public final class ExportWizardProvider implements WizardProvider {
|
|||||||
config.logLines.set(100);
|
config.logLines.set(100);
|
||||||
config.configurations.clear();
|
config.configurations.clear();
|
||||||
|
|
||||||
zip.putTextFile(Settings.GSON.toJson(config), Settings.SETTINGS_FILE_NAME);
|
zip.putTextFile(config.toJson(), Settings.SETTINGS_FILE_NAME);
|
||||||
zip.putFile(tempModpack, "modpack.zip");
|
zip.putFile(tempModpack, "modpack.zip");
|
||||||
|
|
||||||
File bg = new File("bg").getAbsoluteFile();
|
File bg = new File("bg").getAbsoluteFile();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user