package com.faforever.client.map.generator;

import com.faforever.client.config.ClientProperties;
import com.faforever.client.fx.PlatformService;
import com.faforever.client.i18n.I18n;
import com.faforever.client.task.CompletableTask;
import com.faforever.client.task.ResourceLocks;
import com.faforever.commons.io.ByteCopier;
import com.google.common.annotations.VisibleForTesting;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Slf4j
public class DownloadMapGeneratorTask extends CompletableTask<Void> {

  private final MapGeneratorService mapGeneratorService;
  private final ClientProperties clientProperties;
  private final I18n i18n;
  private final PlatformService platformService;

  @Setter
  @Getter
  @VisibleForTesting
  private ComparableVersion version;

  @Inject
  public DownloadMapGeneratorTask(MapGeneratorService mapGeneratorService, ClientProperties clientProperties, I18n i18n, PlatformService platformService) {
    super(Priority.HIGH);

    this.mapGeneratorService = mapGeneratorService;
    this.clientProperties = clientProperties;
    this.i18n = i18n;
    this.platformService = platformService;
  }

  @Override
  protected Void call() throws Exception {
    Objects.requireNonNull(version, "Version hasn't been set.");

    updateTitle(i18n.get("game.mapGeneration.downloadGenerator.title", version));

    URL url = new URL(String.format(clientProperties.getMapGenerator().getDownloadUrlFormat(), version));

    URLConnection urlConnection = url.openConnection();

    Path targetFile = mapGeneratorService.getGeneratorExecutablePath(version);
    Path tempFile = Files.createTempFile(targetFile.getParent(), "generator", null);

    if (!Files.exists(targetFile.getParent())) {
      try {
        Files.createDirectory(targetFile.getParent());
      } catch (IOException e) {
        log.error("Could not create map generator executable directory.", e);
      }
    }

    ResourceLocks.acquireDownloadLock();
    try (InputStream inputStream = url.openStream(); OutputStream outputStream = Files.newOutputStream(tempFile)) {
      ByteCopier.from(inputStream)
          .to(outputStream)
          .totalBytes(urlConnection.getContentLength())
          .listener(this::updateProgress)
          .copy();

      Files.move(tempFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
    } finally {
      ResourceLocks.freeDownloadLock();
      try {
        Files.deleteIfExists(tempFile);
      } catch (IOException e) {
        log.warn("Could not delete temporary file: " + tempFile.toAbsolutePath(), e);
      }
    }

    platformService.setUnixExecutableAndWritableBits(targetFile);

    return null;
  }
}
@@ -2191,16 +2191,69 @@
 rsion);%0A
+    Files.createDirectories(targetFile.getParent());%0A
     Path
@@ -2334,249 +2334,8 @@
 );%0A%0A
-    if (!Files.exists(targetFile.getParent())) %7B%0A      try %7B%0A        Files.createDirectory(targetFile.getParent());%0A      %7D catch (IOException e) %7B%0A        log.error(%22Could not create map generator executable directory.%22, e);%0A      %7D%0A    %7D%0A%0A
     

Create mapgen directory before creating the tempfile