Skip to content

Commit

Permalink
Remove network downloading feature
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBlanvillain committed Oct 6, 2018
1 parent 4b3e9bf commit 5fe05e8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 147 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Just follow the instructions in the provided readme in the
[release](https://github.com/featurecat/lizzie/releases/tag/0.5).

The first run may take a while because Leela Zero needs to set up the
OpenCL tunings and download the latest network if it is not present. Just hang tight, and wait for it to finish, then you
OpenCL tunings. Just hang tight, and wait for it to finish, then you
will see Leela Zero's analysis displayed on the board. Feel free to supply
your own tunings, as this will speed up the process. Do this by copying
any `leelaz_opencl_tuning` file you have into the directory.
Expand All @@ -22,14 +22,15 @@ any `leelaz_opencl_tuning` file you have into the directory.

First, you will need to have a version of Leela Zero that
continually outputs pondering information. You can get this from one
of the Lizzie releases or build it yourself; just compile from the **next** branch of Leela Zero (see http://github.com/gcp/leela-zero/tree/next for more details).
of the Lizzie releases or build it yourself; just compile from the **next**
branch of Leela Zero (see http://github.com/gcp/leela-zero/tree/next for more
details).

git clone -b next http://github.com/gcp/leela-zero.git
$ git clone --recursive --branch next http://github.com/gcp/leela-zero.git

### Building Lizzie

The simplest way to build Lizzie is to use
[Maven](https://maven.apache.org/).
The simplest way to build Lizzie is to use [Maven](https://maven.apache.org/).

To build the code and package it:

Expand Down
1 change: 0 additions & 1 deletion src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ private JSONObject createDefaultConfig() {
leelaz.put("max-game-thinking-time-seconds", 2);
leelaz.put("print-comms", false);
leelaz.put("analyze-update-interval-centisec", 10);
leelaz.put("automatically-download-latest-network", false);

config.put("leelaz", leelaz);

Expand Down
94 changes: 17 additions & 77 deletions src/main/java/featurecat/lizzie/Util.java
Original file line number Diff line number Diff line change
@@ -1,77 +1,17 @@
package featurecat.lizzie;

import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

public class Util {
/**
* @param val the value we want to clamp
* @param min the minimum value that will be returned
* @param max the maximum value that will be returned
* @return the closest number to val within the range [min, max]
*/
public static <T extends Comparable<T>> T clamp(T val, T min, T max) {
if (val.compareTo(min) < 0) return min;
else if (val.compareTo(max) > 0) return max;
else return val;
}

/** @return the sha 256 checksum of decompressed contents from a GZIPed file */
public static String getSha256Sum(File file) {
try (InputStream inputStream = new GZIPInputStream(new FileInputStream(file))) {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);

// we have to read the file. additionally, using a buffer is efficient.
// 8192 was tested as the best value among 4096, 8192, 16384, and 32768.
byte[] buffer = new byte[8192];
while (digestInputStream.read(buffer) != -1) ;

MessageDigest digest = digestInputStream.getMessageDigest();
digestInputStream.close();

byte[] sha256 = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : sha256) {
sb.append(String.format("%02X", b));
}
return sb.toString().toLowerCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (EOFException e) {
// do nothing, just means we need to download a new one
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/** @return the url's contents, downloaded as a string */
public static String downloadAsString(URL url) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
return br.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

/** Downloads the contents of the url, and saves them in a file. */
public static void saveAsFile(URL url, File file) {
try {
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
// package featurecat.lizzie;

// import java.io.*;

// public class Util {
// /**
// * @param val the value we want to clamp
// * @param min the minimum value that will be returned
// * @param max the maximum value that will be returned
// * @return the closest number to val within the range [min, max]
// */
// public static <T extends Comparable<T>> T clamp(T val, T min, T max) {
// if (val.compareTo(min) < 0) return min;
// else if (val.compareTo(max) > 0) return max;
// else return val;
// }
// }
44 changes: 0 additions & 44 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package featurecat.lizzie.analysis;

import featurecat.lizzie.Lizzie;
import featurecat.lizzie.Util;
import featurecat.lizzie.rules.Stone;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.*;
Expand All @@ -24,7 +22,6 @@ public class Leelaz {
ResourceBundle.getBundle("l10n.DisplayStrings");

private static final long MINUTE = 60 * 1000; // number of milliseconds in a minute
private static final String baseURL = "http://zero.sjeng.org";

private long maxAnalyzeTimeMillis; // , maxThinkingTimeMillis;
private int cmdNumber;
Expand Down Expand Up @@ -78,10 +75,6 @@ public Leelaz() throws IOException, JSONException {
printCommunication = config.getBoolean("print-comms");
maxAnalyzeTimeMillis = MINUTE * config.getInt("max-analyze-time-minutes");

if (config.getBoolean("automatically-download-latest-network")) {
updateToLatestNetwork();
}

File startfolder = new File(config.optString("engine-start-location", "."));
String engineCommand = config.getString("engine-command");
String networkFile = config.getString("network-file");
Expand Down Expand Up @@ -127,43 +120,6 @@ public Leelaz() throws IOException, JSONException {
Lizzie.frame.refreshBackground();
}

private void updateToLatestNetwork() {
try {
if (needToDownloadLatestNetwork()) {
int dialogResult =
JOptionPane.showConfirmDialog(
null,
resourceBundle.getString("LizzieFrame.display.download-latest-network-prompt"));
if (dialogResult == JOptionPane.YES_OPTION) {
Util.saveAsFile(
new URL(baseURL + "/networks/" + getBestNetworkHash() + ".gz"),
new File(Lizzie.config.leelazConfig.getString("network-file")));
}
}
} catch (IOException e) {
e.printStackTrace();
// now we're probably still ok. Maybe we're offline -- then it's not a big problem.
}
}

private String getBestNetworkHash() throws IOException {
return Util.downloadAsString(new URL(baseURL + "/best-network-hash")).split("\n")[0];
}

private boolean needToDownloadLatestNetwork() throws IOException {
File networkFile = new File(Lizzie.config.leelazConfig.getString("network-file"));
if (!networkFile.exists()) {
return true;
} else {
String currentNetworkHash = Util.getSha256Sum(networkFile);
if (currentNetworkHash == null) return true;

String bestNetworkHash = getBestNetworkHash();

return !currentNetworkHash.equals(bestNetworkHash);
}
}

/** Initializes the input and output streams */
private void initializeStreams() {
inputStream = new BufferedInputStream(process.getInputStream());
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package featurecat.lizzie.gui;

import static java.lang.Math.max;
import static java.lang.Math.min;

import com.jhlabs.image.GaussianFilter;
import featurecat.lizzie.Lizzie;
import featurecat.lizzie.Util;
import featurecat.lizzie.analysis.GameInfo;
import featurecat.lizzie.analysis.Leelaz;
import featurecat.lizzie.rules.Board;
Expand Down Expand Up @@ -317,8 +319,8 @@ public void paint(Graphics g0) {
int topInset = this.getInsets().top;

// board
int maxSize = (int) (Math.min(getWidth(), getHeight() - topInset) * 0.98);
maxSize = Math.max(maxSize, Board.BOARD_SIZE + 5); // don't let maxWidth become too small
int maxSize = (int) (min(getWidth(), getHeight() - topInset) * 0.98);
maxSize = max(maxSize, Board.BOARD_SIZE + 5); // don't let maxWidth become too small
int boardX = (getWidth() - maxSize) / 2;
int boardY = topInset + (getHeight() - topInset - maxSize) / 2 + 3;

Expand Down Expand Up @@ -384,7 +386,7 @@ public void paint(Graphics g0) {
int subBoardY = gry + grh;
int subBoardWidth = grw;
int subBoardHeight = ponderingY - subBoardY;
int subBoardLength = Math.min(subBoardWidth, subBoardHeight);
int subBoardLength = min(subBoardWidth, subBoardHeight);

if (Lizzie.config.showLargeSubBoard()) {
boardX = getWidth() - maxSize - panelMargin;
Expand Down Expand Up @@ -420,7 +422,7 @@ public void paint(Graphics g0) {
subBoardY = topInset + panelH;
subBoardWidth = spaceW;
subBoardHeight = ponderingY - subBoardY;
subBoardLength = Math.min(subBoardWidth, subBoardHeight);
subBoardLength = min(subBoardWidth, subBoardHeight);
}

// initialize
Expand Down Expand Up @@ -522,8 +524,8 @@ private Graphics2D createBackground() {
Graphics2D g = cachedBackground.createGraphics();

BufferedImage wallpaper = boardRenderer.getWallpaper();
int drawWidth = Math.max(wallpaper.getWidth(), getWidth());
int drawHeight = Math.max(wallpaper.getHeight(), getHeight());
int drawWidth = max(wallpaper.getWidth(), getWidth());
int drawHeight = max(wallpaper.getHeight(), getHeight());
// Support seamless texture
boardRenderer.drawTextureImage(g, wallpaper, 0, 0, drawWidth, drawHeight);

Expand All @@ -541,7 +543,7 @@ private void drawVariationTreeContainer(Graphics2D g, int vx, int vy, int vw, in
}

private void drawPonderingState(Graphics2D g, String text, int x, int y, double size) {
int fontSize = (int) (Math.max(getWidth(), getHeight()) * size);
int fontSize = (int) (max(getWidth(), getHeight()) * size);
Font font = new Font(systemDefaultFontName, Font.PLAIN, fontSize);
FontMetrics fm = g.getFontMetrics(font);
int stringWidth = fm.stringWidth(text);
Expand Down Expand Up @@ -594,19 +596,19 @@ void drawControls() {

Graphics2D g = cachedImage.createGraphics();

int maxSize = Math.min(getWidth(), getHeight());
int maxSize = min(getWidth(), getHeight());
Font font = new Font(systemDefaultFontName, Font.PLAIN, (int) (maxSize * 0.034));
g.setFont(font);

FontMetrics metrics = g.getFontMetrics(font);
int maxCmdWidth = commandsToShow.stream().mapToInt(c -> metrics.stringWidth(c)).max().orElse(0);
int lineHeight = (int) (font.getSize() * 1.15);

int boxWidth = Util.clamp((int) (maxCmdWidth * 1.4), 0, getWidth());
int boxHeight = Util.clamp(commandsToShow.size() * lineHeight, 0, getHeight());
int boxWidth = min((int) (maxCmdWidth * 1.4), getWidth());
int boxHeight = min(commandsToShow.size() * lineHeight, getHeight());

int commandsX = Util.clamp(getWidth() / 2 - boxWidth / 2, 0, getWidth());
int commandsY = Util.clamp(getHeight() / 2 - boxHeight / 2, 0, getHeight());
int commandsX = min(getWidth() / 2 - boxWidth / 2, getWidth());
int commandsY = min(getHeight() / 2 - boxHeight / 2, getHeight());

BufferedImage result = new BufferedImage(boxWidth, boxHeight, BufferedImage.TYPE_INT_ARGB);
filter10.filter(
Expand Down Expand Up @@ -656,7 +658,7 @@ void drawControls() {
private void drawCommandString(Graphics2D g) {
if (userAlreadyKnowsAboutCommandString) return;

int maxSize = (int) (Math.min(getWidth(), getHeight()) * 0.98);
int maxSize = (int) (min(getWidth(), getHeight()) * 0.98);

Font font = new Font(systemDefaultFontName, Font.PLAIN, (int) (maxSize * 0.03));
String commandString = resourceBundle.getString("LizzieFrame.prompt.showControlsHint");
Expand Down Expand Up @@ -739,7 +741,7 @@ private void drawMoveStatistics(Graphics2D g, int posX, int posY, int width, int
strokeRadius = 2;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
setPanelFont(g, (int) (Math.min(width, height) * 0.2));
setPanelFont(g, (int) (min(width, height) * 0.2));

// Last move
if (validLastWinrate && validWinrate) {
Expand Down Expand Up @@ -1099,7 +1101,7 @@ private int drawComment(Graphics2D g, int x, int y, int w, int h, boolean full)
? Lizzie.board.getHistory().getData().comment
: "";
int cHeight = full ? h : (int) (h * 0.5);
int fontSize = (int) (Math.min(getWidth(), getHeight()) * 0.0294);
int fontSize = (int) (min(getWidth(), getHeight()) * 0.0294);
if (Lizzie.config.commentFontSize > 0) {
fontSize = Lizzie.config.commentFontSize;
} else if (fontSize < 16) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/l10n/DisplayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ LizzieFrame.prompt.failedToSaveFile=Failed to save file.
LizzieFrame.prompt.sgfExists=The SGF file already exists, do you want to replace it?
LizzieFrame.prompt.showControlsHint=hold x = view controls
LizzieFrame.display.lastMove=Last move
LizzieFrame.display.pondering=Pondering
LizzieFrame.display.pondering=Pondering
LizzieFrame.display.on=on
LizzieFrame.display.off=off
LizzieFrame.display.loading=Leela Zero is loading...
LizzieFrame.display.download-latest-network-prompt=Download the latest network file? This may take some time.
LizzieFrame.display.leelaz-missing=Did not find Leela Zero, update config.txt or download from Leela Zero homepage
LizzieFrame.display.network-missing=Did not find network weights.\nUpdate config.txt (network-file) or download from Leela Zero homepage
LizzieFrame.display.dynamic-komi=dyn. komi:
1 change: 0 additions & 1 deletion src/main/resources/l10n/DisplayStrings_RO.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ LizzieFrame.display.pondering=Analizeză
LizzieFrame.display.on=pornit
LizzieFrame.display.off=oprit
LizzieFrame.display.loading=Leela Zero se încarcă...
LizzieFrame.display.download-latest-network-prompt=Descărcați cel mai nou fișier rețea? Poate să dureze.
1 change: 0 additions & 1 deletion src/main/resources/l10n/DisplayStrings_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ LizzieFrame.display.pondering=\u5206\u6790
LizzieFrame.display.on=\u5F00\u542F
LizzieFrame.display.off=\u6682\u505C
LizzieFrame.display.loading=Leela Zero\u6B63\u5728\u8F7D\u5165\u4E2D...
LizzieFrame.display.download-latest-network-prompt=Download the latest network file? This may take some time.
LizzieFrame.display.leelaz-missing=Did not find Leela Zero, update config.txt or download from Leela Zero homepage
LizzieFrame.display.network-missing=Did not find network weights.\nUpdate config.txt (network-file) or download from Leela Zero homepage
LizzieFrame.display.dynamic-komi=dyn. komi:

0 comments on commit 5fe05e8

Please sign in to comment.