Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup behavior: Simplify extraction algorithm #12767

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import games.strategy.engine.ClientFileSystemHelper;
import games.strategy.engine.framework.GameRunner;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -131,36 +132,41 @@ private static Optional<Path> unzipMapThrowing(final Path mapZip) throws IOExcep
final Path extractionTarget =
mapsFolder.resolve(computeExtractionFolderName(mapZip.getFileName().toString()));

if (!GameRunner.headless() && Files.exists(extractionTarget)) {
log.info(
"Skipping extraction of: {}, extraction target already exists: {}",
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);
}

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

// extract into a temp folder first
// put the temp folder in the maps folder, so they're on the same partition (to move later)
final Path tempFolder = Files.createTempDirectory("triplea-unzip");
// 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);
tempFolder.toFile().deleteOnExit();

// Typically, the next step is to move and rename the temp folder to the maps folder.
// But, if we just extracted exactly one folder, then we need to move and rename *that* folder.
final Collection<Path> files = FileUtils.listFiles(tempFolder);
final Path tempFolderWithExtractedMap =
files.size() == 1 && Files.isDirectory(CollectionUtils.getAny(files))
? CollectionUtils.getAny(files)
: tempFolder;

// replace extraction target folder contents with the temp folder containing the extracted zip
final boolean folderReplaced =
FileUtils.replaceFolder(tempFolderWithExtractedMap, extractionTarget);

// Either we have moved and renamed the temp folder, or we have moved and renamed the contents
// of the temp folder. Either way, we can go ahead and remove the now possibly empty tempFolder
FileUtils.deleteDirectory(tempFolder);

if (!folderReplaced) {
return Optional.empty();
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 properties file if it exists
// delete old properties file if they exists
final Path propertiesFile =
mapZip.resolveSibling(mapZip.getFileName().toString() + ".properties");
if (Files.exists(propertiesFile)) {
Expand Down