|
41 | 41 | import org.terasology.launcher.model.GameIdentifier; |
42 | 42 | import org.terasology.launcher.model.GameRelease; |
43 | 43 | import org.terasology.launcher.model.Profile; |
| 44 | +import org.terasology.launcher.model.ReleaseMetadata; |
44 | 45 | import org.terasology.launcher.repositories.RepositoryManager; |
45 | 46 | import org.terasology.launcher.settings.Settings; |
46 | 47 | import org.terasology.launcher.tasks.DeleteTask; |
|
51 | 52 | import java.io.FileNotFoundException; |
52 | 53 | import java.io.IOException; |
53 | 54 | import java.nio.file.Path; |
| 55 | +import java.util.Date; |
54 | 56 | import java.util.List; |
55 | 57 | import java.util.Optional; |
56 | 58 | import java.util.ResourceBundle; |
| 59 | +import java.util.Set; |
57 | 60 | import java.util.concurrent.ExecutorService; |
58 | 61 | import java.util.concurrent.Executors; |
59 | 62 | import java.util.function.Predicate; |
60 | 63 | import java.util.stream.Collectors; |
| 64 | +import java.util.stream.Stream; |
61 | 65 |
|
62 | 66 | public class ApplicationController { |
63 | 67 |
|
@@ -207,41 +211,70 @@ public void initialize() { |
207 | 211 | * to the selected profile, and derive the currently selected release from the combo box's selection model. |
208 | 212 | */ |
209 | 213 | private void initComboBoxes() { |
210 | | - // derive the releases to display from the selected profile (`selectedProfile`). the resulting list is ordered |
211 | | - // in the way the launcher is supposed to display the versions (currently by release timestamp). |
212 | | - config.addListener((obs, oldVal, cfg) -> { |
213 | | - ObservableList<GameRelease> availableReleases = gameReleaseComboBox.getItems(); |
214 | | - GameIdentifier lastPlayedGame = cfg.getLauncherSettings().lastPlayedGameVersion.get(); |
215 | | - |
216 | | - Optional<GameRelease> lastPlayed = availableReleases.stream() |
217 | | - .filter(release -> release.getId().equals(lastPlayedGame)) |
218 | | - .filter(release -> installedGames.contains(release.getId())) |
219 | | - .findFirst(); |
220 | | - Optional<GameRelease> lastInstalled = availableReleases.stream() |
221 | | - .filter(release -> installedGames.contains(release.getId())) |
222 | | - .findFirst(); |
223 | | - |
224 | | - gameReleaseComboBox.getSelectionModel().select(lastPlayed |
225 | | - .or(() -> lastInstalled) |
226 | | - .or(() -> availableReleases.stream().findFirst()) |
227 | | - .orElse(null)); |
228 | | - }); |
229 | | - |
230 | 214 | final ObjectBinding<ObservableList<GameRelease>> releases = Bindings.createObjectBinding(() -> { |
231 | 215 | LauncherConfiguration cfg = config.getValue(); |
232 | 216 | if (cfg == null || cfg.getRepositoryManager() == null) { |
233 | 217 | return FXCollections.emptyObservableList(); |
234 | 218 | } else { |
235 | 219 | RepositoryManager mngr = config.getValue().getRepositoryManager(); |
| 220 | + |
| 221 | + Set<GameRelease> onlineReleases = mngr.getReleases(); |
| 222 | + // Create dummy game release objects from locally installed games. |
| 223 | + // We need this in case of running the launcher in "offline" mode |
| 224 | + // and the list of game releases fetched via the repository manager |
| 225 | + // is empty, but there are still games installed locally. |
| 226 | + // |
| 227 | + // However, we only want to add these dummy releases if they are |
| 228 | + // not listed in the online releases. Thus, filtering out everything |
| 229 | + // with a GameIdentifier that is already part of the releases fetched |
| 230 | + // from online sources. |
| 231 | + // |
| 232 | + //TODO: This is a weird place to create these dummy releases. |
| 233 | + // Move this code somewhere else, and make sure that we |
| 234 | + // have all the necessary information stored locally, like |
| 235 | + // the timestamp or the changelog. |
| 236 | + Set<GameIdentifier> onlineIds = onlineReleases.stream().map(GameRelease::getId).collect(Collectors.toSet()); |
| 237 | + Stream<GameRelease> localGames = installedGames.stream() |
| 238 | + .filter(id -> !onlineIds.contains(id)) |
| 239 | + .map(id -> new GameRelease(id, null, new ReleaseMetadata("", new Date()))); |
| 240 | + |
| 241 | + Stream<GameRelease> allReleases = Stream.concat(onlineReleases.stream(), localGames); |
| 242 | + |
236 | 243 | List<GameRelease> releasesForProfile = |
237 | | - mngr.getReleases().stream() |
| 244 | + allReleases |
238 | 245 | .filter(release -> release.getId().getProfile() == Profile.OMEGA) |
239 | 246 | .filter(release -> showPreReleases.getValue() || release.getId().getBuild().equals(Build.STABLE)) |
240 | 247 | .sorted(ApplicationController::compareReleases) |
241 | 248 | .collect(Collectors.toList()); |
| 249 | + |
242 | 250 | return FXCollections.observableList(releasesForProfile); |
243 | 251 | } |
244 | | - }, config, showPreReleases); |
| 252 | + }, config, showPreReleases, installedGames); |
| 253 | + |
| 254 | + // derive the releases to display from the selected profile (`selectedProfile`). the resulting list is ordered |
| 255 | + // in the way the launcher is supposed to display the versions (currently by release timestamp). |
| 256 | + final ObjectBinding<GameRelease> releaseToSelect = Bindings.createObjectBinding(()-> { |
| 257 | + GameIdentifier lastPlayedGame = Optional.ofNullable(config.getValue()) |
| 258 | + .map(cfg -> cfg.getLauncherSettings().lastPlayedGameVersion.get()) |
| 259 | + .orElse(null); |
| 260 | + |
| 261 | + Optional<GameRelease> lastPlayed = releases.get().stream() |
| 262 | + .filter(release -> release.getId().equals(lastPlayedGame)) |
| 263 | + .filter(release -> installedGames.contains(release.getId())) |
| 264 | + .findFirst(); |
| 265 | + Optional<GameRelease> lastInstalled = releases.get().stream() |
| 266 | + .filter(release -> installedGames.contains(release.getId())) |
| 267 | + .findFirst(); |
| 268 | + |
| 269 | + return lastPlayed |
| 270 | + .or(() -> lastInstalled) |
| 271 | + .or(() -> releases.get().stream().findFirst()) |
| 272 | + .orElse(null); |
| 273 | + }, releases, config); |
| 274 | + |
| 275 | + releaseToSelect.addListener((obs, old, now) -> { |
| 276 | + gameReleaseComboBox.getSelectionModel().select(now); |
| 277 | + }); |
245 | 278 |
|
246 | 279 | gameReleaseComboBox.itemsProperty().bind(releases); |
247 | 280 | gameReleaseComboBox.buttonCellProperty() |
|
0 commit comments