Skip to content

Commit

Permalink
Simplify download file properties (#7519)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
DanVanAtta authored Sep 3, 2020
1 parent f3adaf7 commit 00799e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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,
Expand All @@ -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<Version> getVersion() {
return Optional.ofNullable(props.getProperty(VERSION_PROPERTY)).map(Version::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Optional<Version> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static DownloadedMaps getDownloadedMaps() {
return new DownloadedMaps() {
@Override
public Optional<Version> getVersionForZipFile(final File mapZipFile) {
return Optional.ofNullable(DownloadFileProperties.loadForZip(mapZipFile).getVersion());
return DownloadFileProperties.loadForZip(mapZipFile).getVersion();
}

@Override
Expand Down

0 comments on commit 00799e8

Please sign in to comment.