Skip to content

Commit

Permalink
Merge pull request featurecat#281 from apetresc/leelaz-default-path
Browse files Browse the repository at this point in the history
Pick a reasonable default leelaz binary
  • Loading branch information
featurecat authored Jun 4, 2018
2 parents 1912f4a + a794669 commit e9ba00b
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<String> 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() {
Expand All @@ -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);
Expand Down

0 comments on commit e9ba00b

Please sign in to comment.