Fix crash when removed all profiles
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
package org.jackhuang.hmcl.setting;
|
package org.jackhuang.hmcl.setting;
|
||||||
|
|
||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.beans.InvalidationListener;
|
import javafx.beans.InvalidationListener;
|
||||||
import javafx.beans.Observable;
|
import javafx.beans.Observable;
|
||||||
import javafx.beans.property.*;
|
import javafx.beans.property.*;
|
||||||
@@ -120,14 +121,20 @@ public final class Profile implements Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Profile(String name, File initialGameDir, VersionSetting global) {
|
public Profile(String name, File initialGameDir, VersionSetting global) {
|
||||||
|
this(name, initialGameDir, global, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Profile(String name, File initialGameDir, VersionSetting global, String selectedVersion, boolean useRelativePath) {
|
||||||
this.name = new ImmediateStringProperty(this, "name", name);
|
this.name = new ImmediateStringProperty(this, "name", name);
|
||||||
gameDir = new ImmediateObjectProperty<>(this, "gameDir", initialGameDir);
|
gameDir = new ImmediateObjectProperty<>(this, "gameDir", initialGameDir);
|
||||||
repository = new HMCLGameRepository(this, initialGameDir);
|
repository = new HMCLGameRepository(this, initialGameDir);
|
||||||
modManager = new ModManager(repository);
|
modManager = new ModManager(repository);
|
||||||
this.global.set(global == null ? new VersionSetting() : global);
|
this.global.set(global == null ? new VersionSetting() : global);
|
||||||
|
this.selectedVersion.set(selectedVersion);
|
||||||
|
this.useRelativePath.set(useRelativePath);
|
||||||
|
|
||||||
gameDir.addListener((a, b, newValue) -> repository.changeDirectory(newValue));
|
gameDir.addListener((a, b, newValue) -> repository.changeDirectory(newValue));
|
||||||
selectedVersion.addListener(o -> checkSelectedVersion());
|
this.selectedVersion.addListener(o -> checkSelectedVersion());
|
||||||
helper.add(EventBus.EVENT_BUS.channel(RefreshedVersionsEvent.class).registerWeak(event -> checkSelectedVersion()));
|
helper.add(EventBus.EVENT_BUS.channel(RefreshedVersionsEvent.class).registerWeak(event -> checkSelectedVersion()));
|
||||||
|
|
||||||
addPropertyChangedListener(onInvalidating(this::invalidate));
|
addPropertyChangedListener(onInvalidating(this::invalidate));
|
||||||
@@ -224,7 +231,7 @@ public final class Profile implements Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void invalidate() {
|
protected void invalidate() {
|
||||||
observableHelper.invalidate();
|
Platform.runLater(observableHelper::invalidate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class Serializer implements JsonSerializer<Profile>, JsonDeserializer<Profile> {
|
public static final class Serializer implements JsonSerializer<Profile>, JsonDeserializer<Profile> {
|
||||||
@@ -253,10 +260,11 @@ public final class Profile implements Observable {
|
|||||||
JsonObject obj = (JsonObject) json;
|
JsonObject obj = (JsonObject) json;
|
||||||
String gameDir = Optional.ofNullable(obj.get("gameDir")).map(JsonElement::getAsString).orElse("");
|
String gameDir = Optional.ofNullable(obj.get("gameDir")).map(JsonElement::getAsString).orElse("");
|
||||||
|
|
||||||
Profile profile = new Profile("Default", new File(gameDir), context.deserialize(obj.get("global"), VersionSetting.class));
|
return new Profile("Default",
|
||||||
profile.setSelectedVersion(Optional.ofNullable(obj.get("selectedMinecraftVersion")).map(JsonElement::getAsString).orElse(""));
|
new File(gameDir),
|
||||||
profile.setUseRelativePath(Optional.ofNullable(obj.get("useRelativePath")).map(JsonElement::getAsBoolean).orElse(false));
|
context.deserialize(obj.get("global"), VersionSetting.class),
|
||||||
return profile;
|
Optional.ofNullable(obj.get("selectedMinecraftVersion")).map(JsonElement::getAsString).orElse(""),
|
||||||
|
Optional.ofNullable(obj.get("useRelativePath")).map(JsonElement::getAsBoolean).orElse(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import javafx.beans.property.SimpleObjectProperty;
|
|||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import org.jackhuang.hmcl.Launcher;
|
import org.jackhuang.hmcl.Launcher;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
import static javafx.collections.FXCollections.observableArrayList;
|
import static javafx.collections.FXCollections.observableArrayList;
|
||||||
@@ -82,11 +83,8 @@ public final class Profiles {
|
|||||||
|
|
||||||
private static void checkProfiles() {
|
private static void checkProfiles() {
|
||||||
if (profiles.isEmpty()) {
|
if (profiles.isEmpty()) {
|
||||||
Profile current = new Profile(Profiles.DEFAULT_PROFILE);
|
Profile current = new Profile(Profiles.DEFAULT_PROFILE, new File(".minecraft"), new VersionSetting(), null, true);
|
||||||
current.setUseRelativePath(true);
|
|
||||||
|
|
||||||
Profile home = new Profile(Profiles.HOME_PROFILE, Launcher.MINECRAFT_DIRECTORY);
|
Profile home = new Profile(Profiles.HOME_PROFILE, Launcher.MINECRAFT_DIRECTORY);
|
||||||
|
|
||||||
profiles.addAll(current, home);
|
profiles.addAll(current, home);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ public class GameAdvancedListItem extends AdvancedListItem {
|
|||||||
if (profile != null)
|
if (profile != null)
|
||||||
profile.selectedVersionProperty().removeListener(listener);
|
profile.selectedVersionProperty().removeListener(listener);
|
||||||
profile = newProfile;
|
profile = newProfile;
|
||||||
profile.selectedVersionProperty().addListener(listener);
|
if (profile != null)
|
||||||
|
profile.selectedVersionProperty().addListener(listener);
|
||||||
loadVersion();
|
loadVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ public class DefaultGameRepository implements GameRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasVersion(String id) {
|
public boolean hasVersion(String id) {
|
||||||
return versions.containsKey(id);
|
return id != null && versions.containsKey(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user