Skip to content

Commit

Permalink
Import official and custom maps. Posibility of select and delete
Browse files Browse the repository at this point in the history
official maps.
  • Loading branch information
cesar-rgon committed Aug 11, 2019
1 parent bc1be41 commit 021f960
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 116 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
41 changes: 0 additions & 41 deletions src/main/java/pojos/kf2factory/Kf2Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -365,43 +363,4 @@ private String generateMapCycleLine(List<Map> mapList) {
sb.append("))");
return sb.toString();
}

protected void checkForNewOfficialMaps(String installationFolder, String filename) {

try {
List<Path> 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<Map> 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);
}
4 changes: 0 additions & 4 deletions src/main/java/pojos/kf2factory/Kf2LinuxImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,4 @@ protected File getSteamExeFile() {
return null;
}

@Override
public void checkForNewOfficialMaps(String installationFolder) {
checkForNewOfficialMaps(installationFolder, "LinuxServer-KFGame.ini");
}
}
4 changes: 0 additions & 4 deletions src/main/java/pojos/kf2factory/Kf2WindowsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,5 @@ protected File getSteamExeFile() {
return null;
}

@Override
public void checkForNewOfficialMaps(String installationFolder) {
checkForNewOfficialMaps(installationFolder, "PCServer-KFGame.ini");
}
}

170 changes: 115 additions & 55 deletions src/main/java/stories/mapsedition/MapsEditionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -279,8 +276,20 @@ private void removeMapsOnAction() {
}
List<Node> removeList = new ArrayList<Node>();
StringBuffer message = new StringBuffer();
ObservableList<Node> nodes = customMapsFlowPane.getChildren();
for (Node node: nodes) {
ObservableList<Node> officialNodes = officialMapsFlowPane.getChildren();
ObservableList<Node> 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);
Expand All @@ -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");
}
Expand Down Expand Up @@ -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<Path> 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<Path> 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<Map> customMapInDataBase = facade.findMapByCode(mapName);
if (!customMapInDataBase.isPresent()) {
customMap = facade.createNewCustomMapFromWorkshop(idWorkShop, mapName, installationFolder);
try {
Optional<Map> 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);
}
}
}
Expand All @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/stories/mapsedition/MapsEditionFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface MapsEditionFacade {
MapDto deleteSelectedMap(String mapName) throws SQLException;
boolean isCorrectInstallationFolder(String installationFolder);
Optional<Map> findMapByCode(String mapName) throws SQLException;
Map insertMap(Map map) throws SQLException;
}
5 changes: 5 additions & 0 deletions src/main/java/stories/mapsedition/MapsEditionFacadeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,9 @@ public boolean isCorrectInstallationFolder(String installationFolder) {
public Optional<Map> findMapByCode(String mapName) throws SQLException {
return MapDao.getInstance().findByCode(mapName);
}

@Override
public Map insertMap(Map map) throws SQLException {
return MapDao.getInstance().insert(map);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/properties/config.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading

0 comments on commit 021f960

Please sign in to comment.