From 00799e874af5b9d34ede73a819ea2dc656348d91 Mon Sep 17 00:00:00 2001 From: Dan Van Atta Date: Wed, 2 Sep 2020 18:53:02 -0700 Subject: [PATCH] Simplify download file properties (#7519) * Simplify DownloadFileProperties 1. Fix up/simplify optional API usage around getVersion, can return an optional directly from DownloadFileProperties to simplify client code 2. Convert saveForZip to be stateful rather than static. * Remove unused properties in map '*.properties' files, we only use the map version The map url, time of download, engine version are unused, we only need the map version in a '.properties' map suffix file. * Simplify DownloadFileProperties construction Rather than construct via no-arg and then inject a version value, we can accept the version value as a constructor argument. --- .../framework/map/download/DownloadFile.java | 7 ++-- .../map/download/DownloadFileProperties.java | 37 ++++++------------- .../download/FileSystemAccessStrategy.java | 3 +- .../map/download/MapDownloadController.java | 2 +- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFile.java b/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFile.java index 06a11cae99..d79fb3d956 100644 --- a/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFile.java +++ b/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFile.java @@ -87,9 +87,10 @@ private Thread newDownloadThread() { return; } - final DownloadFileProperties props = new DownloadFileProperties(); - props.setFrom(download); - DownloadFileProperties.saveForZip(download.getInstallLocation(), props); + if (download.getVersion() != null) { + new DownloadFileProperties(download.getVersion()) + .saveForZip(download.getInstallLocation()); + } downloadListener.downloadStopped(download); }); diff --git a/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFileProperties.java b/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFileProperties.java index 8d68512105..4122bdaf6e 100644 --- a/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFileProperties.java +++ b/game-core/src/main/java/games/strategy/engine/framework/map/download/DownloadFileProperties.java @@ -1,26 +1,29 @@ package games.strategy.engine.framework.map.download; -import games.strategy.engine.ClientContext; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.time.LocalDateTime; -import java.time.ZoneId; +import java.util.Optional; import java.util.Properties; import java.util.logging.Level; +import lombok.NoArgsConstructor; import lombok.extern.java.Log; -import org.triplea.java.DateTimeFormatterUtil; import org.triplea.util.Version; /** Properties file used to know which map versions have been installed. */ @Log +@NoArgsConstructor class DownloadFileProperties { static final String VERSION_PROPERTY = "map.version"; private final Properties props = new Properties(); + DownloadFileProperties(final Version mapVersion) { + props.put(VERSION_PROPERTY, mapVersion.toString()); + } + static DownloadFileProperties loadForZip(final File zipFile) { if (!fromZip(zipFile).exists()) { return new DownloadFileProperties(); @@ -35,9 +38,9 @@ static DownloadFileProperties loadForZip(final File zipFile) { return downloadFileProperties; } - static void saveForZip(final File zipFile, final DownloadFileProperties props) { + void saveForZip(final File zipFile) { try (OutputStream fos = new FileOutputStream(fromZip(zipFile))) { - props.props.store(fos, null); + props.store(fos, null); } catch (final IOException e) { log.log( Level.SEVERE, @@ -50,25 +53,7 @@ private static File fromZip(final File zipFile) { return new File(zipFile.getAbsolutePath() + ".properties"); } - Version getVersion() { - if (!props.containsKey(VERSION_PROPERTY)) { - return null; - } - return new Version(props.getProperty(VERSION_PROPERTY)); - } - - private void setVersion(final Version v) { - if (v != null) { - props.put(VERSION_PROPERTY, v.toString()); - } - } - - void setFrom(final DownloadFileDescription selected) { - setVersion(selected.getVersion()); - props.setProperty("map.url", selected.getUrl()); - props.setProperty( - "download.time", - DateTimeFormatterUtil.toDateString(LocalDateTime.now(ZoneId.systemDefault()))); - props.setProperty("engine.version", ClientContext.engineVersion().toString()); + Optional getVersion() { + return Optional.ofNullable(props.getProperty(VERSION_PROPERTY)).map(Version::new); } } diff --git a/game-core/src/main/java/games/strategy/engine/framework/map/download/FileSystemAccessStrategy.java b/game-core/src/main/java/games/strategy/engine/framework/map/download/FileSystemAccessStrategy.java index faf451564d..3d75e9ea23 100644 --- a/game-core/src/main/java/games/strategy/engine/framework/map/download/FileSystemAccessStrategy.java +++ b/game-core/src/main/java/games/strategy/engine/framework/map/download/FileSystemAccessStrategy.java @@ -24,8 +24,7 @@ Optional getMapVersion(final String mapName) { return Optional.empty(); } - final DownloadFileProperties props = DownloadFileProperties.loadForZip(potentialFile); - return (props.getVersion() == null) ? Optional.empty() : Optional.of(props.getVersion()); + return DownloadFileProperties.loadForZip(potentialFile).getVersion(); } static void remove( diff --git a/game-core/src/main/java/games/strategy/engine/framework/map/download/MapDownloadController.java b/game-core/src/main/java/games/strategy/engine/framework/map/download/MapDownloadController.java index 1c6102d30f..4f1d4684f7 100644 --- a/game-core/src/main/java/games/strategy/engine/framework/map/download/MapDownloadController.java +++ b/game-core/src/main/java/games/strategy/engine/framework/map/download/MapDownloadController.java @@ -96,7 +96,7 @@ private static DownloadedMaps getDownloadedMaps() { return new DownloadedMaps() { @Override public Optional getVersionForZipFile(final File mapZipFile) { - return Optional.ofNullable(DownloadFileProperties.loadForZip(mapZipFile).getVersion()); + return DownloadFileProperties.loadForZip(mapZipFile).getVersion(); } @Override