From 021f960d137cc4cbd0e289399f18ed1da0d61238 Mon Sep 17 00:00:00 2001 From: cesar-rgon Date: Sun, 11 Aug 2019 11:51:32 +0100 Subject: [PATCH] Import official and custom maps. Posibility of select and delete official maps. --- README.md | 3 +- src/main/java/pojos/kf2factory/Kf2Common.java | 41 ----- .../java/pojos/kf2factory/Kf2LinuxImpl.java | 4 - .../java/pojos/kf2factory/Kf2WindowsImpl.java | 4 - .../mapsedition/MapsEditionController.java | 170 ++++++++++++------ .../mapsedition/MapsEditionFacade.java | 1 + .../mapsedition/MapsEditionFacadeImpl.java | 5 + src/main/resources/css/custom.css | 2 +- .../resources/properties/config.properties | 2 +- src/main/resources/views/mapWebInfo.fxml | 8 +- src/main/resources/views/mapsEdition.fxml | 11 +- 11 files changed, 135 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index 4c7b20f..b343f24 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Application to easily customize and launch a Killing Floor 2 server through a vi ``` Version: 2.0 beta -Last modification date: 2019/08/01 +Last modification date: 2019/08/11 Supported OS: Microsoft Windows and Linux (Ubuntu/Debian) Author: César Rodríguez González Language: English (Spanish soon) @@ -37,6 +37,7 @@ Language: English (Spanish soon) - Add custom maps to the server through Steam's WorkShop. - Remove custom maps from the server. - Administrate profiles, game types, difficulties, lengths and maximun numer of players that can be selected to launch the server. +- Import official and custom maps from the server to the launcher. - All those features are available through the launcher's interface on Windows and Linux OS (no need to use console commands at all). ### Pre-requisites diff --git a/src/main/java/pojos/kf2factory/Kf2Common.java b/src/main/java/pojos/kf2factory/Kf2Common.java index b7604ac..959d3a2 100644 --- a/src/main/java/pojos/kf2factory/Kf2Common.java +++ b/src/main/java/pojos/kf2factory/Kf2Common.java @@ -37,7 +37,6 @@ protected Kf2Common() { public void installOrUpdateServer(String installationFolder, boolean validateFiles, boolean isBeta, String betaBrunch) { if (isValid(installationFolder) && prepareSteamCmd(installationFolder)) { installUpdateKf2Server(installationFolder, validateFiles, isBeta, betaBrunch); - checkForNewOfficialMaps(installationFolder); } } @@ -61,7 +60,6 @@ public String runServer(Profile profile) { String installationFolder = propertyService.getPropertyValue("properties/config.properties", Constants.CONFIG_INSTALLATION_FOLDER); if (isValid(installationFolder)) { createConfigFolder(installationFolder, profile.getName()); - checkForNewOfficialMaps(installationFolder); return runKf2Server(installationFolder, profile); } } else { @@ -365,43 +363,4 @@ private String generateMapCycleLine(List mapList) { sb.append("))"); return sb.toString(); } - - protected void checkForNewOfficialMaps(String installationFolder, String filename) { - - try { - List kfmFilesPathList = Files.walk(Paths.get(installationFolder + "/KFGame/BrewedPC/Maps")) - .filter(Files::isRegularFile) - .filter(f -> f.getFileName().toString().toUpperCase().startsWith("KF-")) - .filter(f -> f.getFileName().toString().toUpperCase().endsWith(".KFM")) - .collect(Collectors.toList()); - - if (kfmFilesPathList != null && !kfmFilesPathList.isEmpty()) { - List officialMapList = databaseService.listOfficialMaps(); - for (Path kfmFilesPath: kfmFilesPathList) { - String filenameWithExtension = kfmFilesPath.getFileName().toString(); - String[] array = filenameWithExtension.split(".kfm"); - String mapName = array[0]; - boolean found = false; - for (Map databaseMap: officialMapList) { - if (databaseMap.getCode().equalsIgnoreCase(mapName)) { - officialMapList.remove(databaseMap); - found = true; - break; - } - } - if (!found) { - // It's a new official map, it must be stored in database - Map newOfficialMap = new Map(mapName, true, null, null, "/KFGame/Web/images/maps/" + mapName + ".jpg", true); - databaseService.insertMap(newOfficialMap); - } - } - } - - } catch (Exception e) { - String message = "Error detecting new official maps. See stacktrace for more details:"; - logger.error(message, e); - } - } - - public abstract void checkForNewOfficialMaps(String installationFolder); } diff --git a/src/main/java/pojos/kf2factory/Kf2LinuxImpl.java b/src/main/java/pojos/kf2factory/Kf2LinuxImpl.java index 2be1346..6aaf103 100644 --- a/src/main/java/pojos/kf2factory/Kf2LinuxImpl.java +++ b/src/main/java/pojos/kf2factory/Kf2LinuxImpl.java @@ -141,8 +141,4 @@ protected File getSteamExeFile() { return null; } - @Override - public void checkForNewOfficialMaps(String installationFolder) { - checkForNewOfficialMaps(installationFolder, "LinuxServer-KFGame.ini"); - } } diff --git a/src/main/java/pojos/kf2factory/Kf2WindowsImpl.java b/src/main/java/pojos/kf2factory/Kf2WindowsImpl.java index d53580b..03a7fa7 100644 --- a/src/main/java/pojos/kf2factory/Kf2WindowsImpl.java +++ b/src/main/java/pojos/kf2factory/Kf2WindowsImpl.java @@ -143,9 +143,5 @@ protected File getSteamExeFile() { return null; } - @Override - public void checkForNewOfficialMaps(String installationFolder) { - checkForNewOfficialMaps(installationFolder, "PCServer-KFGame.ini"); - } } diff --git a/src/main/java/stories/mapsedition/MapsEditionController.java b/src/main/java/stories/mapsedition/MapsEditionController.java index 0c8cee9..f3da4bc 100644 --- a/src/main/java/stories/mapsedition/MapsEditionController.java +++ b/src/main/java/stories/mapsedition/MapsEditionController.java @@ -116,20 +116,17 @@ public void handle(MouseEvent event) { } GridPane gridpane = new GridPane(); gridpane.add(mapPreview, 1, 1); - if (map.getOfficial()) { - gridpane.add(mapNameLabel, 1, 2); - } else { - GridPane.setColumnSpan(mapPreview, 2); - gridpane.add(new CheckBox(), 1, 2); - gridpane.add(mapNameLabel, 2, 2); - if (!map.getDownloaded()) { - Label warningMessage = new Label("Start server to download it"); - warningMessage.setStyle("-fx-text-fill: yellow;"); - GridPane.setColumnSpan(warningMessage, 2); - gridpane.add(warningMessage,1,3); - GridPane.setHalignment(warningMessage, HPos.CENTER); - } + GridPane.setColumnSpan(mapPreview, 2); + gridpane.add(new CheckBox(), 1, 2); + gridpane.add(mapNameLabel, 2, 2); + if (!map.getDownloaded()) { + Label warningMessage = new Label("Start server to download it"); + warningMessage.setStyle("-fx-text-fill: yellow;"); + GridPane.setColumnSpan(warningMessage, 2); + gridpane.add(warningMessage,1,3); + GridPane.setHalignment(warningMessage, HPos.CENTER); } + GridPane.setHalignment(mapNameLabel, HPos.CENTER); return gridpane; } @@ -279,8 +276,20 @@ private void removeMapsOnAction() { } List removeList = new ArrayList(); StringBuffer message = new StringBuffer(); - ObservableList nodes = customMapsFlowPane.getChildren(); - for (Node node: nodes) { + ObservableList officialNodes = officialMapsFlowPane.getChildren(); + ObservableList customNodes = customMapsFlowPane.getChildren(); + + for (Node node: officialNodes) { + GridPane gridpane = (GridPane) node; + CheckBox checkbox = (CheckBox)gridpane.getChildren().get(1); + Label mapNameLabel = (Label)gridpane.getChildren().get(2); + if (checkbox.isSelected()) { + removeList.add(gridpane); + message.append(mapNameLabel.getText()).append("\n"); + } + } + + for (Node node: customNodes) { GridPane gridpane = (GridPane) node; CheckBox checkbox = (CheckBox)gridpane.getChildren().get(1); Label mapNameLabel = (Label)gridpane.getChildren().get(2); @@ -300,14 +309,18 @@ private void removeMapsOnAction() { try { GridPane gridpane = (GridPane) node; Label mapNameLabel = (Label) gridpane.getChildren().get(2); - MapDto customMap = facade.deleteSelectedMap(mapNameLabel.getText()); - if (customMap != null) { - mapsToRemove.add(customMap); - customMapsFlowPane.getChildren().remove(gridpane); - File photo = new File(installationFolder + customMap.getUrlPhoto()); - photo.delete(); - File cacheFoler = new File(installationFolder + "/KFGame/Cache/" + customMap.getIdWorkShop()); - FileUtils.deleteDirectory(cacheFoler); + MapDto map = facade.deleteSelectedMap(mapNameLabel.getText()); + if (map != null) { + mapsToRemove.add(map); + if (map.getOfficial()) { + officialMapsFlowPane.getChildren().remove(gridpane); + } else { + customMapsFlowPane.getChildren().remove(gridpane); + File photo = new File(installationFolder + map.getUrlPhoto()); + photo.delete(); + File cacheFoler = new File(installationFolder + "/KFGame/Cache/" + map.getIdWorkShop()); + FileUtils.deleteDirectory(cacheFoler); + } } else { errors.append(mapNameLabel.getText()).append("\n"); } @@ -345,54 +358,101 @@ private void importMapsFromServerOnAction() { if (result.isPresent() && result.get().equals(ButtonType.OK)) { logger.info("Starting the process to import maps from the server to the launcher"); - File cacheFolder = new File(installationFolder + "/KFGame/Cache/"); - File[] listOfFiles = cacheFolder.listFiles(); - StringBuffer success = new StringBuffer(); - StringBuffer errors = new StringBuffer(); - for (int i=0; i < listOfFiles.length; i++) { - if (listOfFiles[i].isDirectory()) { - Map customMap = null; + StringBuffer successOfficial = new StringBuffer(); + StringBuffer successCustom = new StringBuffer(); + StringBuffer errorsOfficial = new StringBuffer(); + StringBuffer errorsCustom = new StringBuffer(); + List kfmFilesPathList = null; + try { + kfmFilesPathList = Files.walk(Paths.get(installationFolder + "/KFGame/BrewedPC/Maps")) + .filter(Files::isRegularFile) + .filter(f -> f.getFileName().toString().toUpperCase().startsWith("KF-")) + .filter(f -> f.getFileName().toString().toUpperCase().endsWith(".KFM")) + .collect(Collectors.toList()); + + kfmFilesPathList.addAll(Files.walk(Paths.get(installationFolder + "/KFGame/Cache")) + .filter(Files::isRegularFile) + .filter(f -> f.getFileName().toString().toUpperCase().startsWith("KF-")) + .filter(f -> f.getFileName().toString().toUpperCase().endsWith(".KFM")) + .collect(Collectors.toList())); + } catch (Exception e) { + logger.error("Error importing maps from server to the launcher", "See stacktrace for more details", e); + } + + if (kfmFilesPathList != null && !kfmFilesPathList.isEmpty()) { + for (Path kfmFilePath: kfmFilesPathList) { + boolean officialMap = kfmFilePath.toString().contains("/KFGame/BrewedPC/Maps"); + String filenameWithExtension = kfmFilePath.getFileName().toString(); + String[] array = filenameWithExtension.split(".kfm"); + String mapName = array[0]; Long idWorkShop = null; - try { - idWorkShop = Long.parseLong(listOfFiles[i].getName()); - List kfmFilesPath = Files.walk(Paths.get(installationFolder + "/KFGame/Cache/" + idWorkShop)) - .filter(Files::isRegularFile) - .filter(f -> f.getFileName().toString().toUpperCase().startsWith("KF-")) - .filter(f -> f.getFileName().toString().toUpperCase().endsWith(".KFM")) - .collect(Collectors.toList()); + if (!officialMap) { + String[] arrayTwo = kfmFilePath.toString().replace(installationFolder, "").replace("/KFGame/Cache/", "").split("/"); + idWorkShop = Long.parseLong(arrayTwo[0]); + } - if (kfmFilesPath != null && !kfmFilesPath.isEmpty()) { - String filenameWithExtension = kfmFilesPath.get(0).getFileName().toString(); - String[] array = filenameWithExtension.split(".kfm"); - String mapName = array[0]; - Optional customMapInDataBase = facade.findMapByCode(mapName); - if (!customMapInDataBase.isPresent()) { - customMap = facade.createNewCustomMapFromWorkshop(idWorkShop, mapName, installationFolder); + try { + Optional mapInDataBase = facade.findMapByCode(mapName); + if (!mapInDataBase.isPresent()) { + if (officialMap) { + Map newOfficialMap = new Map(mapName, true, null, null, "/KFGame/Web/images/maps/" + mapName + ".jpg", true); + Map insertedMap = facade.insertMap(newOfficialMap); + if (insertedMap != null) { + mapList.add(insertedMap); + GridPane gridpane = createMapGridPane(facade.getDto(insertedMap)); + officialMapsFlowPane.getChildren().add(gridpane); + successOfficial.append("map name: ").append(insertedMap.getCode()).append("\n"); + } else { + logger.error("Error importing the official map with name: " + mapName); + errorsOfficial.append("mapName: ").append(mapName).append("\n"); + } + } else { + Map customMap = facade.createNewCustomMapFromWorkshop(idWorkShop, mapName, installationFolder); if (customMap != null) { mapList.add(customMap); GridPane gridpane = createMapGridPane(facade.getDto(customMap)); customMapsFlowPane.getChildren().add(gridpane); - success.append("map name: ").append(customMap.getCode()).append(" - idWorkShop: ").append(idWorkShop).append("\n"); + successCustom.append("map name: ").append(customMap.getCode()).append(" - idWorkShop: ").append(idWorkShop).append("\n"); } else { - logger.error("Error importing the customMap with idWorkShop: " + idWorkShop); - errors.append("idWorkShop: ").append(idWorkShop).append("\n"); + logger.error("Error importing the custom map with idWorkShop: " + idWorkShop); + errorsCustom.append("map name: ").append(mapName).append(" - idWorkShop: ").append(idWorkShop).append("\n"); } } } } catch (Exception e) { - logger.error("Error importing the customMap: " + customMap.getCode() + " with idWorkShop: " + idWorkShop, e); - errors.append("map name: ").append(customMap.getCode()).append(" - idWorkShop: ").append(idWorkShop).append("\n"); + logger.error("Error importing the map: " + mapName, "See stacktrace for more details", e); + if (officialMap) { + errorsOfficial.append("mapName: ").append(mapName).append("\n"); + } else { + errorsCustom.append("map name: ").append(mapName).append(" - idWorkShop: ").append(idWorkShop).append("\n"); + } } + } } + logger.info("The process to import maps from the server to the launcher has finished."); - if (StringUtils.isNotBlank(success)) { - Utils.infoDialog("These maps were successfully imported from server to the launcher:", success.toString()); + if (StringUtils.isNotBlank(successOfficial) || StringUtils.isNotBlank(successCustom)) { + StringBuffer message = new StringBuffer(); + if (StringUtils.isNotBlank(successOfficial)) { + message.append("\nOFFICIAL MAPS:\n").append(successOfficial); + } + if (StringUtils.isNotBlank(successCustom)) { + message.append("\nCUSTOM MAPS:\n").append(successCustom); + } + Utils.infoDialog("These maps were successfully imported from server to the launcher:", message.toString()); } else { Utils.infoDialog("No maps were imported", "The server does not contain new maps to be imported\nor the maps could not be imported successfully\nSee launcher.log file for more details."); } - if (StringUtils.isNotBlank(errors)) { - Utils.errorDialog("Error importing next maps from server to the launcher:", errors.toString() + "\nSee launcher.log file for more details.", null); + if (StringUtils.isNotBlank(errorsOfficial) || StringUtils.isNotBlank(errorsCustom)) { + StringBuffer message = new StringBuffer(); + if (StringUtils.isNotBlank(errorsOfficial)) { + message.append("\nOFFICIAL MAPS:\n").append(errorsOfficial); + } + if (StringUtils.isNotBlank(errorsCustom)) { + message.append("\nCUSTOM MAPS:\n").append(errorsCustom); + } + Utils.errorDialog("Error importing next maps from server to the launcher:", message.toString() + "\nSee launcher.log file for more details.", null); } } } @@ -412,10 +472,10 @@ private void selectAllMapsOnAction() { checkbox.setSelected(selectMaps); } if (selectMaps) { - selectAllMaps.setText("Unselect all maps"); + selectAllMaps.setText("Unselect custom maps"); selectMaps = false; } else { - selectAllMaps.setText("Select all maps"); + selectAllMaps.setText("Select custom maps"); selectMaps = true; } } diff --git a/src/main/java/stories/mapsedition/MapsEditionFacade.java b/src/main/java/stories/mapsedition/MapsEditionFacade.java index c61b768..1a49674 100644 --- a/src/main/java/stories/mapsedition/MapsEditionFacade.java +++ b/src/main/java/stories/mapsedition/MapsEditionFacade.java @@ -17,4 +17,5 @@ public interface MapsEditionFacade { MapDto deleteSelectedMap(String mapName) throws SQLException; boolean isCorrectInstallationFolder(String installationFolder); Optional findMapByCode(String mapName) throws SQLException; + Map insertMap(Map map) throws SQLException; } diff --git a/src/main/java/stories/mapsedition/MapsEditionFacadeImpl.java b/src/main/java/stories/mapsedition/MapsEditionFacadeImpl.java index 6b876fb..a13f54e 100644 --- a/src/main/java/stories/mapsedition/MapsEditionFacadeImpl.java +++ b/src/main/java/stories/mapsedition/MapsEditionFacadeImpl.java @@ -152,4 +152,9 @@ public boolean isCorrectInstallationFolder(String installationFolder) { public Optional findMapByCode(String mapName) throws SQLException { return MapDao.getInstance().findByCode(mapName); } + + @Override + public Map insertMap(Map map) throws SQLException { + return MapDao.getInstance().insert(map); + } } diff --git a/src/main/resources/css/custom.css b/src/main/resources/css/custom.css index 4170a56..ea83f1e 100644 --- a/src/main/resources/css/custom.css +++ b/src/main/resources/css/custom.css @@ -40,7 +40,7 @@ .button { -fx-background-color: #4d0304; -fx-font-weight: bold; - -fx-font-size: 15; + -fx-font-size: 14; -fx-text-fill: white; -fx-cursor: hand; } diff --git a/src/main/resources/properties/config.properties b/src/main/resources/properties/config.properties index 1f42a69..aaa2398 100644 --- a/src/main/resources/properties/config.properties +++ b/src/main/resources/properties/config.properties @@ -1,6 +1,6 @@ # CONFIG PROPERTIES prop.config.applicationTitle=Simple Killing Floor 2 Server Launcher -prop.config.applicationVersion=2.0 beta 20190801 +prop.config.applicationVersion=2.0 beta 20190811 prop.config.urlSteamcmd=https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip prop.config.betaBrunch=preview prop.config.downloadConnectionTimeout=10000 diff --git a/src/main/resources/views/mapWebInfo.fxml b/src/main/resources/views/mapWebInfo.fxml index 5b536fc..3a12192 100644 --- a/src/main/resources/views/mapWebInfo.fxml +++ b/src/main/resources/views/mapWebInfo.fxml @@ -17,15 +17,15 @@ -