Skip to content

Commit 6652461

Browse files
authored
支持以类型安全的方式读写 Navigation#getSettings() (#4504)
1 parent a0568e3 commit 6652461

27 files changed

+256
-177
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorWizardDisplayer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.jackhuang.hmcl.ui.construct.Navigator;
2424
import org.jackhuang.hmcl.ui.construct.PageCloseEvent;
2525
import org.jackhuang.hmcl.ui.wizard.*;
26+
import org.jackhuang.hmcl.util.SettingsMap;
2627

27-
import java.util.Map;
2828
import java.util.concurrent.ConcurrentLinkedQueue;
2929

3030
public class DecoratorWizardDisplayer extends DecoratorTransitionPage implements WizardDisplayer {
@@ -94,7 +94,7 @@ public void navigateTo(Node page, Navigation.NavigationDirection nav) {
9494
}
9595

9696
@Override
97-
public void handleTask(Map<String, Object> settings, Task<?> task) {
97+
public void handleTask(SettingsMap settings, Task<?> task) {
9898
displayer.handleTask(settings, task);
9999
}
100100

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/AbstractInstallersPage.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
3636
import org.jackhuang.hmcl.ui.wizard.WizardController;
3737
import org.jackhuang.hmcl.ui.wizard.WizardPage;
38-
39-
import java.util.Map;
38+
import org.jackhuang.hmcl.util.SettingsMap;
4039

4140
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
4241

@@ -80,12 +79,12 @@ protected InstallerItem.Style getInstallerItemStyle() {
8079
protected abstract void reload();
8180

8281
@Override
83-
public void onNavigate(Map<String, Object> settings) {
82+
public void onNavigate(SettingsMap settings) {
8483
reload();
8584
}
8685

8786
@Override
88-
public abstract void cleanup(Map<String, Object> settings);
87+
public abstract void cleanup(SettingsMap settings);
8988

9089
protected abstract void onInstall();
9190

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/AdditionalInstallersPage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import org.jackhuang.hmcl.ui.InstallerItem;
3030
import org.jackhuang.hmcl.ui.wizard.WizardController;
3131
import org.jackhuang.hmcl.util.Lang;
32+
import org.jackhuang.hmcl.util.SettingsMap;
3233

33-
import java.util.Map;
3434
import java.util.Optional;
3535

3636
import static org.jackhuang.hmcl.download.LibraryAnalyzer.LibraryType.MINECRAFT;
@@ -109,6 +109,6 @@ protected void reload() {
109109
}
110110

111111
@Override
112-
public void cleanup(Map<String, Object> settings) {
112+
public void cleanup(SettingsMap settings) {
113113
}
114114
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/DownloadPage.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@
5151
import org.jackhuang.hmcl.ui.wizard.Navigation;
5252
import org.jackhuang.hmcl.ui.wizard.WizardController;
5353
import org.jackhuang.hmcl.ui.wizard.WizardProvider;
54+
import org.jackhuang.hmcl.util.SettingsMap;
5455
import org.jackhuang.hmcl.util.TaskCancellationAction;
5556
import org.jackhuang.hmcl.util.io.FileUtils;
5657
import org.jetbrains.annotations.Nullable;
5758

5859
import java.nio.file.Path;
59-
import java.util.HashMap;
6060
import java.util.Locale;
61-
import java.util.Map;
6261
import java.util.concurrent.CancellationException;
6362
import java.util.function.Supplier;
6463

@@ -223,7 +222,7 @@ public void showWorldDownloads() {
223222
}
224223

225224
private static final class DownloadNavigator implements Navigation {
226-
private final Map<String, Object> settings = new HashMap<>();
225+
private final SettingsMap settings = new SettingsMap();
227226

228227
@Override
229228
public void onStart() {
@@ -260,7 +259,7 @@ public void onCancel() {
260259
}
261260

262261
@Override
263-
public Map<String, Object> getSettings() {
262+
public SettingsMap getSettings() {
264263
return settings;
265264
}
266265

@@ -287,37 +286,39 @@ public VanillaInstallWizardProvider(Profile profile, GameRemoteVersion gameVersi
287286
}
288287

289288
@Override
290-
public void start(Map<String, Object> settings) {
291-
settings.put(PROFILE, profile);
289+
public void start(SettingsMap settings) {
290+
settings.put(ModpackPage.PROFILE, profile);
292291
settings.put(LibraryAnalyzer.LibraryType.MINECRAFT.getPatchId(), gameVersion);
293292
}
294293

295-
private Task<Void> finishVersionDownloadingAsync(Map<String, Object> settings) {
294+
private Task<Void> finishVersionDownloadingAsync(SettingsMap settings) {
296295
GameBuilder builder = dependencyManager.gameBuilder();
297296

298297
String name = (String) settings.get("name");
299298
builder.name(name);
300299
builder.gameVersion(((RemoteVersion) settings.get(LibraryAnalyzer.LibraryType.MINECRAFT.getPatchId())).getGameVersion());
301300

302-
for (Map.Entry<String, Object> entry : settings.entrySet())
303-
if (!LibraryAnalyzer.LibraryType.MINECRAFT.getPatchId().equals(entry.getKey()) && entry.getValue() instanceof RemoteVersion)
304-
builder.version((RemoteVersion) entry.getValue());
301+
settings.asStringMap().forEach((key, value) -> {
302+
if (!LibraryAnalyzer.LibraryType.MINECRAFT.getPatchId().equals(key)
303+
&& value instanceof RemoteVersion remoteVersion)
304+
builder.version(remoteVersion);
305+
});
305306

306307
return builder.buildAsync().whenComplete(any -> profile.getRepository().refreshVersions())
307308
.thenRunAsync(Schedulers.javafx(), () -> profile.setSelectedVersion(name));
308309
}
309310

310311
@Override
311-
public Object finish(Map<String, Object> settings) {
312+
public Object finish(SettingsMap settings) {
312313
settings.put("title", i18n("install.new_game.installation"));
313314
settings.put("success_message", i18n("install.success"));
314-
settings.put("failure_callback", (FailureCallback) (settings1, exception, next) -> UpdateInstallerWizardProvider.alertFailureMessage(exception, next));
315+
settings.put(FailureCallback.KEY, (settings1, exception, next) -> UpdateInstallerWizardProvider.alertFailureMessage(exception, next));
315316

316317
return finishVersionDownloadingAsync(settings);
317318
}
318319

319320
@Override
320-
public Node createPage(WizardController controller, int step, Map<String, Object> settings) {
321+
public Node createPage(WizardController controller, int step, SettingsMap settings) {
321322
switch (step) {
322323
case 0:
323324
return new InstallersPage(controller, profile.getRepository(), ((RemoteVersion) controller.getSettings().get("game")).getGameVersion(), downloadProvider);
@@ -330,7 +331,5 @@ public Node createPage(WizardController controller, int step, Map<String, Object
330331
public boolean cancel() {
331332
return true;
332333
}
333-
334-
public static final String PROFILE = "PROFILE";
335334
}
336335
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/InstallersPage.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import org.jackhuang.hmcl.ui.construct.RequiredValidator;
2828
import org.jackhuang.hmcl.ui.construct.Validator;
2929
import org.jackhuang.hmcl.ui.wizard.WizardController;
30-
31-
import java.util.Map;
30+
import org.jackhuang.hmcl.util.SettingsMap;
3231

3332
import static javafx.beans.binding.Bindings.createBooleanBinding;
3433
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
@@ -73,7 +72,7 @@ protected void reload() {
7372
}
7473

7574
@Override
76-
public void cleanup(Map<String, Object> settings) {
75+
public void cleanup(SettingsMap settings) {
7776
}
7877

7978
private static boolean checkName(String name) {

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/LocalModpackPage.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@
3636
import org.jackhuang.hmcl.ui.construct.RequiredValidator;
3737
import org.jackhuang.hmcl.ui.construct.Validator;
3838
import org.jackhuang.hmcl.ui.wizard.WizardController;
39+
import org.jackhuang.hmcl.util.SettingsMap;
3940
import org.jackhuang.hmcl.util.StringUtils;
4041
import org.jackhuang.hmcl.util.io.CompressingUtils;
4142
import org.jackhuang.hmcl.util.io.FileUtils;
4243

4344
import java.nio.charset.Charset;
4445
import java.nio.file.Path;
45-
import java.util.Map;
46-
import java.util.Optional;
4746

48-
import static org.jackhuang.hmcl.util.Lang.tryCast;
4947
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
5048
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
5149

@@ -58,11 +56,11 @@ public final class LocalModpackPage extends ModpackPage {
5856
public LocalModpackPage(WizardController controller) {
5957
super(controller);
6058

61-
Profile profile = (Profile) controller.getSettings().get("PROFILE");
59+
Profile profile = controller.getSettings().get(ModpackPage.PROFILE);
6260

63-
Optional<String> name = tryCast(controller.getSettings().get(MODPACK_NAME), String.class);
64-
if (name.isPresent()) {
65-
txtModpackName.setText(name.get());
61+
String name = controller.getSettings().get(MODPACK_NAME);
62+
if (name != null) {
63+
txtModpackName.setText(name);
6664
txtModpackName.setDisable(true);
6765
} else {
6866
FXUtils.onChangeAndOperate(installAsVersion, installAsVersion -> {
@@ -83,9 +81,9 @@ public LocalModpackPage(WizardController controller) {
8381
btnDescription.setVisible(false);
8482

8583
Path selectedFile;
86-
Optional<Path> filePath = tryCast(controller.getSettings().get(MODPACK_FILE), Path.class);
87-
if (filePath.isPresent()) {
88-
selectedFile = filePath.get();
84+
Path filePath = controller.getSettings().get(MODPACK_FILE);
85+
if (filePath != null) {
86+
selectedFile = filePath;
8987
} else {
9088
FileChooser chooser = new FileChooser();
9189
chooser.setTitle(i18n("modpack.choose"));
@@ -112,7 +110,7 @@ public LocalModpackPage(WizardController controller) {
112110
lblName.setText(FileUtils.getName(selectedFile));
113111
installAsVersion.set(false);
114112

115-
if (name.isEmpty()) {
113+
if (name == null) {
116114
// trim: https://github.com/HMCL-dev/HMCL/issues/962
117115
txtModpackName.setText(FileUtils.getNameWithoutExtension(selectedFile));
118116
}
@@ -133,7 +131,7 @@ public LocalModpackPage(WizardController controller) {
133131
lblVersion.setText(manifest.getVersion());
134132
lblAuthor.setText(manifest.getAuthor());
135133

136-
if (name.isEmpty()) {
134+
if (name == null) {
137135
// trim: https://github.com/HMCL-dev/HMCL/issues/962
138136
txtModpackName.setText(manifest.getName().trim());
139137
}
@@ -144,7 +142,7 @@ public LocalModpackPage(WizardController controller) {
144142
}
145143

146144
@Override
147-
public void cleanup(Map<String, Object> settings) {
145+
public void cleanup(SettingsMap settings) {
148146
settings.remove(MODPACK_FILE);
149147
}
150148

@@ -177,9 +175,9 @@ protected void onDescribe() {
177175
Controllers.navigate(new WebPage(i18n("modpack.description"), manifest.getDescription()));
178176
}
179177

180-
public static final String MODPACK_FILE = "MODPACK_FILE";
181-
public static final String MODPACK_NAME = "MODPACK_NAME";
182-
public static final String MODPACK_MANIFEST = "MODPACK_MANIFEST";
183-
public static final String MODPACK_CHARSET = "MODPACK_CHARSET";
184-
public static final String MODPACK_MANUALLY_CREATED = "MODPACK_MANUALLY_CREATED";
178+
public static final SettingsMap.Key<Path> MODPACK_FILE = new SettingsMap.Key<>("MODPACK_FILE");
179+
public static final SettingsMap.Key<String> MODPACK_NAME = new SettingsMap.Key<>("MODPACK_NAME");
180+
public static final SettingsMap.Key<Modpack> MODPACK_MANIFEST = new SettingsMap.Key<>("MODPACK_MANIFEST");
181+
public static final SettingsMap.Key<Charset> MODPACK_CHARSET = new SettingsMap.Key<>("MODPACK_CHARSET");
182+
public static final SettingsMap.Key<Boolean> MODPACK_MANUALLY_CREATED = new SettingsMap.Key<>("MODPACK_MANUALLY_CREATED");
185183
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackInstallWizardProvider.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
3333
import org.jackhuang.hmcl.ui.wizard.WizardController;
3434
import org.jackhuang.hmcl.ui.wizard.WizardProvider;
35+
import org.jackhuang.hmcl.util.SettingsMap;
3536

3637
import java.io.FileNotFoundException;
3738
import java.io.IOException;
3839
import java.nio.charset.Charset;
3940
import java.nio.file.Path;
40-
import java.util.Map;
4141

42-
import static org.jackhuang.hmcl.util.Lang.tryCast;
4342
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
4443

4544
public final class ModpackInstallWizardProvider implements WizardProvider {
@@ -66,21 +65,21 @@ public ModpackInstallWizardProvider(Profile profile, Path modpackFile, String up
6665
}
6766

6867
@Override
69-
public void start(Map<String, Object> settings) {
68+
public void start(SettingsMap settings) {
7069
if (file != null)
7170
settings.put(LocalModpackPage.MODPACK_FILE, file);
7271
if (updateVersion != null)
7372
settings.put(LocalModpackPage.MODPACK_NAME, updateVersion);
74-
settings.put(PROFILE, profile);
73+
settings.put(ModpackPage.PROFILE, profile);
7574
}
7675

77-
private Task<?> finishModpackInstallingAsync(Map<String, Object> settings) {
78-
Path selected = tryCast(settings.get(LocalModpackPage.MODPACK_FILE), Path.class).orElse(null);
79-
ServerModpackManifest serverModpackManifest = tryCast(settings.get(RemoteModpackPage.MODPACK_SERVER_MANIFEST), ServerModpackManifest.class).orElse(null);
80-
Modpack modpack = tryCast(settings.get(LocalModpackPage.MODPACK_MANIFEST), Modpack.class).orElse(null);
81-
String name = tryCast(settings.get(LocalModpackPage.MODPACK_NAME), String.class).orElse(null);
82-
Charset charset = tryCast(settings.get(LocalModpackPage.MODPACK_CHARSET), Charset.class).orElse(null);
83-
boolean isManuallyCreated = tryCast(settings.get(LocalModpackPage.MODPACK_MANUALLY_CREATED), Boolean.class).orElse(false);
76+
private Task<?> finishModpackInstallingAsync(SettingsMap settings) {
77+
Path selected = settings.get(LocalModpackPage.MODPACK_FILE);
78+
ServerModpackManifest serverModpackManifest = settings.get(RemoteModpackPage.MODPACK_SERVER_MANIFEST);
79+
Modpack modpack = settings.get(LocalModpackPage.MODPACK_MANIFEST);
80+
String name = settings.get(LocalModpackPage.MODPACK_NAME);
81+
Charset charset = settings.get(LocalModpackPage.MODPACK_CHARSET);
82+
boolean isManuallyCreated = settings.getOrDefault(LocalModpackPage.MODPACK_MANUALLY_CREATED, false);
8483

8584
if (isManuallyCreated) {
8685
return ModpackHelper.getInstallManuallyCreatedModpackTask(profile, selected, name, charset);
@@ -119,29 +118,26 @@ private Task<?> finishModpackInstallingAsync(Map<String, Object> settings) {
119118
}
120119

121120
@Override
122-
public Object finish(Map<String, Object> settings) {
121+
public Object finish(SettingsMap settings) {
123122
settings.put("title", i18n("install.modpack.installation"));
124123
settings.put("success_message", i18n("install.success"));
125-
settings.put("failure_callback", new FailureCallback() {
126-
@Override
127-
public void onFail(Map<String, Object> settings, Exception exception, Runnable next) {
128-
if (exception instanceof ModpackCompletionException) {
129-
if (exception.getCause() instanceof FileNotFoundException) {
130-
Controllers.dialog(i18n("modpack.type.curse.not_found"), i18n("install.failed"), MessageType.ERROR, next);
131-
} else {
132-
Controllers.dialog(i18n("install.success"), i18n("install.success"), MessageType.SUCCESS, next);
133-
}
124+
settings.put(FailureCallback.KEY, (ignored, exception, next) -> {
125+
if (exception instanceof ModpackCompletionException) {
126+
if (exception.getCause() instanceof FileNotFoundException) {
127+
Controllers.dialog(i18n("modpack.type.curse.not_found"), i18n("install.failed"), MessageType.ERROR, next);
134128
} else {
135-
UpdateInstallerWizardProvider.alertFailureMessage(exception, next);
129+
Controllers.dialog(i18n("install.success"), i18n("install.success"), MessageType.SUCCESS, next);
136130
}
131+
} else {
132+
UpdateInstallerWizardProvider.alertFailureMessage(exception, next);
137133
}
138134
});
139135

140136
return finishModpackInstallingAsync(settings);
141137
}
142138

143139
@Override
144-
public Node createPage(WizardController controller, int step, Map<String, Object> settings) {
140+
public Node createPage(WizardController controller, int step, SettingsMap settings) {
145141
switch (step) {
146142
case 0:
147143
return new ModpackSelectionPage(controller);
@@ -161,6 +157,4 @@ else if (controller.getSettings().containsKey(RemoteModpackPage.MODPACK_SERVER_M
161157
public boolean cancel() {
162158
return true;
163159
}
164-
165-
public static final String PROFILE = "PROFILE";
166160
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/download/ModpackPage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
import javafx.scene.control.Label;
88
import javafx.scene.layout.BorderPane;
99
import javafx.scene.layout.VBox;
10+
import org.jackhuang.hmcl.setting.Profile;
1011
import org.jackhuang.hmcl.ui.FXUtils;
1112
import org.jackhuang.hmcl.ui.construct.ComponentList;
1213
import org.jackhuang.hmcl.ui.construct.SpinnerPane;
1314
import org.jackhuang.hmcl.ui.wizard.WizardController;
1415
import org.jackhuang.hmcl.ui.wizard.WizardPage;
16+
import org.jackhuang.hmcl.util.SettingsMap;
1517

1618
import static javafx.beans.binding.Bindings.createBooleanBinding;
1719
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
1820

1921
public abstract class ModpackPage extends SpinnerPane implements WizardPage {
22+
public static final SettingsMap.Key<Profile> PROFILE = new SettingsMap.Key<>("PROFILE");
23+
2024
protected final WizardController controller;
2125

2226
protected final Label lblName;

0 commit comments

Comments
 (0)