Skip to content

Commit

Permalink
Simpler Map unzip - delete old & simple unzip (#12773)
Browse files Browse the repository at this point in the history
Updates map extraction algorithm to the following:
  -  on startup, or immediately following map download: detect '.zip' file in 'downloadedMaps' folder
  -  determine target folder for extraction (strip '-master' suffix if present)
  -  delete target folder
  -  unzip the zip file to the target folder

The above is simpler notably because we just 'unzip'. Before the algorithm would check the case whether the extracted contents is a single folder, or if the zip contained all the map files in an exploded format. With this update, it does not matter, we just unzip into a destination folder.

The delete is unfortunate but required. Existing maps will potentially have this format: `[map-name]/*`, rather than something like `[map-name]/[map-name-mastter]/*`. Because of this mismatch, if we just extract, then we would wind up with duplicate map files.
  • Loading branch information
DanVanAtta authored Jul 25, 2024
1 parent 5eb87c9 commit db81eac
Showing 1 changed file with 22 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.triplea.io.ZipExtractor;
import org.triplea.io.ZipExtractor.FileSystemException;
import org.triplea.io.ZipExtractor.ZipReadException;
import org.triplea.java.collections.CollectionUtils;
import org.triplea.map.description.file.MapDescriptionYaml;

/**
Expand Down Expand Up @@ -125,68 +124,34 @@ public static Optional<Path> unzipMap(final Path mapZip) {
mapZip.getFileName().toString().endsWith(".zip"), mapZip.toAbsolutePath());

try {
return unzipMapThrowing(mapZip);
} catch (final IOException e) {
log.warn("Error extracting file: {}, {}", mapZip.toAbsolutePath() + ", " + e.getMessage(), e);
return Optional.empty();
}
}

// unzip map
// if previous folder exists then back it up
private static Optional<Path> unzipMapThrowing(final Path mapZip) throws IOException {
// extraction target is important, it is the folder path we seek to create with
// extracted map contents.
final Path mapsFolder = ClientFileSystemHelper.getUserMapsFolder();
final Path extractionTarget =
mapsFolder.resolve(computeExtractionFolderName(mapZip.getFileName().toString()));

if (!GameRunner.headless() && Files.exists(extractionTarget)) {
// extraction target is important, it is the folder path we seek to create with
// extracted map contents.
final Path mapsFolder = ClientFileSystemHelper.getUserMapsFolder();
final Path extractionTarget =
mapsFolder.resolve(computeExtractionFolderName(mapZip.getFileName().toString()));
log.info(
"Skipping extraction of: {}, extraction target already exists: {}",
"Extracting map zip: {} -> {}",
mapZip.toAbsolutePath(),
extractionTarget.toAbsolutePath());
return Optional.empty();
}

// if this is a bot, then we are updating the map - delete the old folder and extract a new.
if (GameRunner.headless() && Files.exists(extractionTarget)) {
log.info("Deleting old map folder: " + extractionTarget.toAbsolutePath());
FileUtils.deleteDirectory(extractionTarget);
}
ZipExtractor.unzipFile(mapZip, extractionTarget);

log.info(
"Extracting map zip: {} -> {}", mapZip.toAbsolutePath(), extractionTarget.toAbsolutePath());

// extract to a temp folder.
// If the temp folder then contains a single folder:
// -> move that single folder to extraction target and remove the temp folder
// If the temp folder contains many files:
// -> rename the temp folder to extraction target
final Path tempFolder = Files.createTempDirectory(mapsFolder, "map-unzip");
ZipExtractor.unzipFile(mapZip, tempFolder);
final Collection<Path> files = FileUtils.listFiles(tempFolder);
if (files.size() == 1 && Files.isDirectory(CollectionUtils.getAny(files))) {
// temp folder contains a folder that contains all the map files
Files.move(CollectionUtils.getAny(files), extractionTarget);
Files.delete(tempFolder);
} else {
// temp folder contains all the map files. Rename the temp folder
Files.move(tempFolder, extractionTarget);
}

// delete old properties file if they exists
final Path propertiesFile =
mapZip.resolveSibling(mapZip.getFileName().toString() + ".properties");
if (Files.exists(propertiesFile)) {
FileUtils.delete(propertiesFile);
}
// delete old properties file if they exists
final Path propertiesFile =
mapZip.resolveSibling(mapZip.getFileName().toString() + ".properties");
if (Files.exists(propertiesFile)) {
FileUtils.delete(propertiesFile);
}

final boolean successfullyExtracted = Files.exists(extractionTarget);
if (successfullyExtracted) {
Files.delete(mapZip);
return Optional.of(extractionTarget);
} else {
final boolean successfullyExtracted = Files.exists(extractionTarget);
if (successfullyExtracted) {
Files.delete(mapZip);
return Optional.of(extractionTarget);
} else {
return Optional.empty();
}
} catch (final IOException e) {
log.warn("Error extracting file: {}, {}", mapZip.toAbsolutePath(), e.getMessage(), e);
return Optional.empty();
}
}
Expand Down

0 comments on commit db81eac

Please sign in to comment.