From a79466999989339af43eaae8e5cf1a44e013be23 Mon Sep 17 00:00:00 2001 From: Adrian Petrescu Date: Sun, 3 Jun 2018 23:26:27 -0400 Subject: [PATCH] Pick a reasonable default leelaz binary. Now that Leela-Zero has the new GTP extensions that Lizzie requires, it's no longer mandatory to compile a custom leelaz binary, which means it's no longer a hard requirement for there to be a leelaz binary in Lizzie's current working directory. To make life easier for packagers (like me), when constructing the default config.txt, Lizzie should examine the PATH and find the system-wide leelaz. Of course, a local ./leelaz should still take priority (even if `.` is not actually on your PATH). This makes that the default behaviour. Works for me on a fresh install with leela-zero installed in /usr/bin. --- src/main/java/featurecat/lizzie/Config.java | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/featurecat/lizzie/Config.java b/src/main/java/featurecat/lizzie/Config.java index 97d3e0789..19398b26d 100644 --- a/src/main/java/featurecat/lizzie/Config.java +++ b/src/main/java/featurecat/lizzie/Config.java @@ -5,7 +5,10 @@ import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; public class Config { @@ -100,7 +103,7 @@ private boolean validateAndCorrectSettings(JSONObject config) { // Check engine configs JSONObject leelaz = config.getJSONObject("leelaz"); // Check if the engine program exists. - String enginePath = leelaz.optString("engine-program", "./leelaz"); + String enginePath = leelaz.optString("engine-program", getBestDefaultLeelazPath()); if (!Files.exists(Paths.get(enginePath)) && !Files.exists(Paths.get(enginePath + ".exe" /* For windows */))) { // FIXME: I don't know how to handle it properly.. Possibly showing a warning dialog may be a good idea? leelaz.put("engine-program", "./leelaz"); @@ -194,6 +197,28 @@ public boolean showLargeSubBoard() { } + /** + * Scans the current directory as well as the current PATH to find a reasonable default leelaz binary. + * + * @return A working path to a leelaz binary. If there are none on the PATH, "./leelaz" is returned for backwards + * compatibility. + */ + private String getBestDefaultLeelazPath() { + List potentialPaths = new ArrayList<>(); + potentialPaths.add("."); + potentialPaths.addAll(Arrays.asList(System.getenv("PATH").split(":"))); + + for (String potentialPath : potentialPaths) { + for (String potentialExtension : Arrays.asList(new String[] {"", ".exe"})) { + File potentialLeelaz = new File(potentialPath, "leelaz" + potentialExtension); + if (potentialLeelaz.exists() && potentialLeelaz.canExecute()) { + return potentialLeelaz.getPath(); + } + } + } + + return "./leelaz"; + } private JSONObject createDefaultConfig() { @@ -202,7 +227,8 @@ private JSONObject createDefaultConfig() { // About engine parameter JSONObject leelaz = new JSONObject(); leelaz.put("network-file", "network.gz"); - leelaz.put("engine-command", "./leelaz --gtp --lagbuffer 0 --weights %network-file --threads 2"); + leelaz.put("engine-command", String.format("%s --gtp --lagbuffer 0 --weights %%network-file --threads 2", + getBestDefaultLeelazPath())); leelaz.put("engine-start-location", "."); leelaz.put("max-analyze-time-minutes", 5); leelaz.put("max-game-thinking-time-seconds", 2);