Skip to content

Commit

Permalink
Fix, prevent NPE in FileSystems#getFileSystem when default game URI i…
Browse files Browse the repository at this point in the history
…s missing a scheme (#8058)

Bug report #8015 has a NPE in getFileSystem, this looks to happen if the default
game URI is missing a scheme. That can be something trivial like an empty
value for the default game URI (empty but not null), or could be caused by
other reasons.

Regardless, instead of throwing an exception in such cases, we can fall back to resetting
the default game to a default value.
  • Loading branch information
DanVanAtta authored Nov 2, 2020
1 parent 2e5fdad commit a8c881a
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Observable;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -204,19 +205,31 @@ public void onGameEnded() {
public void loadDefaultGameSameThread() {
ClientSetting.defaultGameUri
.getValue()
.filter(Predicate.not(String::isBlank))
.filter(GameSelectorModel::gameUriExistsOnFileSystem)
.map(URI::create)
// we don't want to load a game file by default that is not within the map folders we
// can load. (ie: if a previous version of triplea was using running a game within its
// root folder, we shouldn't open it)
.filter(
defaultGame ->
getDefaultGameRealPath(defaultGame)
.startsWith(ClientFileSystemHelper.getUserRootFolder().toPath()))
// ensure the default game hasn't been deleted since it was last loaded
.filter(defaultGame -> getDefaultGameRealPath(defaultGame).toFile().exists())
.ifPresentOrElse(this::load, this::resetDefaultGame);
}

@SuppressWarnings("ReturnValueIgnored")
private static boolean gameUriExistsOnFileSystem(final String gameUri) {
try {
Path.of(URI.create(gameUri));
} catch (final IllegalArgumentException ignored) {
// thrown if the URI is invalid (EG: missing URI scheme)
return false;
}

final Path realPath = getDefaultGameRealPath(URI.create(gameUri));

// starts with check is because we don't want to load a game file by default that is not within
// the map folders. (ie: if a previous version of triplea was using running a game within its
// root folder, we shouldn't open it)

return realPath.startsWith(ClientFileSystemHelper.getUserRootFolder().toPath())
&& realPath.toFile().exists();
}

/**
* Determine the real path of the default game.
*
Expand Down

0 comments on commit a8c881a

Please sign in to comment.