Skip to content

Commit

Permalink
Fixes: fix couples bugs around default game loading (#12746)
Browse files Browse the repository at this point in the history
* Fixes: fix couples bugs around default game loading

Fixed new bug where we needed to check keep the check
for 'saveGameToLoad.isPresent()', and the Headless
autosave file loading needs a 'GameRunner.headless()' check.

Fixed latent bug where we need to release the 'ready to load save-game'
latch if we are actually trying to load a save game.

* Simplify, use ClientSetting.defaultGameUri to load save games
  • Loading branch information
DanVanAtta committed Jul 22, 2024
1 parent d283a73 commit 76772bb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void saveAndEndGame(final INode node) {
final Path f = launchAction.getAutoSaveFile();
try {
serverGame.saveGame(f);
gameSelectorModel.setSaveGameFileToLoad(f);
ClientSetting.defaultGameUri.setValueAndFlush(f.toUri().toString());
} catch (final Exception e) {
log.error("Failed to save game: " + f.toAbsolutePath(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class GameSelectorModel extends Observable implements GameSelector {
// just for host bots, so we can get the actions for loading/saving games on the bots from this
// model
@Setter @Getter private ClientModel clientModelForHostBots = null;
private Optional<String> saveGameToLoad = Optional.empty();

// Don't load a save game before the startup task to load the initial map has run, else that task
// may "lose" the race and overwrite the loaded saved game.
Expand Down Expand Up @@ -186,25 +185,20 @@ public void onGameEnded() {
ThreadRunner.runInNewThread(this::loadDefaultGameSameThread);
}

/** Sets the path of a save file that should be loaded. */
public void setSaveGameFileToLoad(final Path filePath) {
saveGameToLoad = Optional.of(filePath.toAbsolutePath().toString());
}

/**
* Runs the load default game logic in same thread. Default game is the one that we loaded on
* startup.
*/
public void loadDefaultGameSameThread() {
final Optional<String> gameUri;
if (Files.exists(new HeadlessAutoSaveFileUtils().getHeadlessAutoSaveFile())) {
if (GameRunner.headless()
&& Files.exists(new HeadlessAutoSaveFileUtils().getHeadlessAutoSaveFile())) {
gameUri =
Optional.of(
new HeadlessAutoSaveFileUtils()
.getHeadlessAutoSaveFile()
.toAbsolutePath()
.toString());
saveGameToLoad = Optional.empty();
} else {
gameUri = ClientSetting.defaultGameUri.getValue();
}
Expand All @@ -219,6 +213,7 @@ public void loadDefaultGameSameThread() {
loadMap(file);
} else {
// try to load it as a saved game whatever the extension
readyForSaveLoad.countDown();
loadSave(file);
}
},
Expand Down

0 comments on commit 76772bb

Please sign in to comment.