Skip to content

Commit

Permalink
CreateGame maplist a bit more robust against errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Axle1975 committed Apr 14, 2021
1 parent 7cefda5 commit f2c92db
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
20 changes: 18 additions & 2 deletions src/main/java/com/faforever/client/fa/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,33 @@ static private List<String[]> run(Path gamePath, String hpiSpecs, String mapName
logger.info("Enumerating maps: {}", String.join(" ", processBuilder.command()));

List<String[]> mapList = new ArrayList<>();

boolean logEnable = false;
String maptoolDataReceived = new String();

try {
final String UNIT_SEPARATOR = Character.toString((char)0x1f);
Process process = processBuilder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
mapList.add(line.split(UNIT_SEPARATOR));
maptoolDataReceived += line + "\n";
String parts[] = line.split(UNIT_SEPARATOR);
if (parts.length < 9) {
logEnable = true;
}
mapList.add(parts);
}
if (logEnable) {
logger.warn("Received too few fields from mapTool:\n{}\n{}", processBuilder.command(), maptoolDataReceived);
while ((line = input.readLine()) != null) {
logger.warn("but theres more:{}", line);
}
}
input.close();
process.waitFor();
}
catch (IOException e)
catch (IOException | InterruptedException e )
{
logger.error("unable to process maps: {}", e.getMessage());
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/faforever/client/game/CreateGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
import org.springframework.stereotype.Component;

import java.lang.ref.WeakReference;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -199,8 +202,12 @@ public Integer fromString(String string) {
featuredModListView.setItems(FXCollections.observableList(featuredModBeans).filtered(FeaturedMod::isVisible));
featuredModListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> Platform.runLater(() -> {
setAvailableMaps(newValue.getTechnicalName());
Path installedExePath = preferencesService.getTotalAnnihilation(newValue.getTechnicalName()).getInstalledExePath();
setGamePathButton.setStyle(installedExePath == null || !Files.isExecutable(installedExePath)
? "-fx-background-color: -fx-accent"
: "-fx-background-color: -fx-background");
selectLastMap();
}));
}));
selectLastOrDefaultGameType();
}));

Expand Down Expand Up @@ -264,7 +271,9 @@ private void init() {

createGameButton.disableProperty().bind(
titleTextField.textProperty().isEmpty()
.or(featuredModListView.getSelectionModel().selectedItemProperty().isNull().or(fafService.connectionStateProperty().isNotEqualTo(CONNECTED)))
.or(featuredModListView.getSelectionModel().selectedItemProperty().isNull())
.or(fafService.connectionStateProperty().isNotEqualTo(CONNECTED))
.or(mapListView.getSelectionModel().selectedItemProperty().isNull())
);
}

Expand Down Expand Up @@ -304,13 +313,12 @@ protected void setAvailableMaps(String modTechnical) {
mapService.getInstalledMaps(modTechnical).filtered(mapBean -> mapBean.getType() == Type.SKIRMISH).sorted((o1, o2) -> o1.getMapName().compareToIgnoreCase(o2.getMapName()))
);

mapListView.setItems(filteredMapBeans);

if (filteredMapBeans.isEmpty()) {
this.setGamePathButton.setStyle(String.format("-fx-background-color: -fx-accent"));
Path installedExePath = preferencesService.getTotalAnnihilation(modTechnical).getInstalledExePath();
if (filteredMapBeans.isEmpty() && installedExePath != null && Files.isExecutable(installedExePath)) {
mapListView.setItems(mapService.getOfficialMaps());
}
else {
this.setGamePathButton.setStyle(String.format("-fx-background-color: -fx-background"));
mapListView.setItems(filteredMapBeans);
}
}

Expand Down
36 changes: 30 additions & 6 deletions src/main/java/com/faforever/client/map/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,14 @@ private void loadInstalledMaps(Installation installation) {
protected Void call() {
updateTitle(i18n.get("mapVault.loadingMaps"));

Path gamePath = preferencesService.getTotalAnnihilation(installation.modTechnicalName).getInstalledPath();
if (gamePath == null) {
Path exePath = preferencesService.getTotalAnnihilation(installation.modTechnicalName).getInstalledExePath();
if (exePath == null || !Files.isExecutable(exePath)) {
synchronized(installation.enumerationsRequested) {
installation.enumerationsRequested = 0;
}
return null;
}
Path gamePath = exePath.getParent();

List<MapBean> mapList = new ArrayList<>();
for (String[] details: MapTool.listMapsInstalled(gamePath, preferencesService.getCacheDirectory().resolve("maps"), false)) {
Expand All @@ -358,6 +359,7 @@ protected Void call() {
installation.maps.clear();
installation.maps.addAll(mapList);
if (installation.maps.isEmpty()) {
logger.warn("no maps found for mod={}. inserting OTA maps", installation.modTechnicalName);
for (String map : otaMaps) {
installation.addMap(map, null);
}
Expand Down Expand Up @@ -387,10 +389,26 @@ protected Void call() {
@NotNull
public MapBean readMap(String mapName, String [] mapDetails) {
MapBean mapBean = new MapBean();
String archiveName = mapDetails != null ? mapDetails[MAP_DETAIL_COLUMN_ARCHIVE] : "<unknown hpi>";
String description = mapDetails != null ? mapDetails[MAP_DETAIL_COLUMN_DESCRIPTION] : mapName;
String mapSizeStr = mapDetails != null ? mapDetails[MAP_DETAIL_COLUMN_SIZE] : "16 x 16";
String crc = mapDetails != null ? mapDetails[MAP_DETAIL_COLUMN_CRC] : "ffffffff";

String archiveName = "unknown.ufo";
String description = mapName;
String mapSizeStr = "16 x 16";
String crc = "00000000";

try {
if (mapDetails != null) {
archiveName = mapDetails[MAP_DETAIL_COLUMN_ARCHIVE];
description = mapDetails[MAP_DETAIL_COLUMN_DESCRIPTION];
mapSizeStr = mapDetails[MAP_DETAIL_COLUMN_SIZE];
crc = mapDetails[MAP_DETAIL_COLUMN_CRC];
}
else {
logger.warn("null map details for map: {}", mapName);
}
}
catch (ArrayIndexOutOfBoundsException e) {
logger.warn("index out of bounds for map: {}. details: {}", String.join("/",mapDetails));
}

String mapSizeArray[] = mapSizeStr.replaceAll("[^0-9x]", "").split("x");

Expand Down Expand Up @@ -422,6 +440,12 @@ public ObservableList<MapBean> getInstalledMaps(String modTechnical) {
return installation.maps;
}

public ObservableList<MapBean> getOfficialMaps() {
ObservableList<MapBean> maps = FXCollections.observableArrayList();
maps.add(readMap("SHERWOOD", new String[]{"SHERWOOD", "totala2.hpi", "ead82fc5", "TAF had trouble finding your maps so we just put this one in cos we know you probably have it", "7 x 7", "3"}));
return maps;
}

public Optional<MapBean> getMapLocallyFromName(String modTechnical, String mapName) {
logger.debug("Trying to find map '{}' locally", mapName);
return Optional.ofNullable(installations.getOrDefault(modTechnical, new Installation(modTechnical)).mapsByName.get(mapName));
Expand Down

0 comments on commit f2c92db

Please sign in to comment.