From be1b667f3517369d7c7f837685890d98649c2cc7 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Mon, 10 Jun 2019 20:06:15 +0200 Subject: [PATCH] Set working directory for external engines When running an external engine set the working directory of the engine process to .../DroidFish/uci/logs. This makes it possible for the engine to create log files or other auxiliary files in that directory. This is useful for the OpenCL version of LC0, which needs to create a tuning file somewhere. With this change it can be placed in the current working directory. --- .../src/main/java/org/petero/droidfish/DroidFish.java | 3 +++ .../src/main/java/org/petero/droidfish/EngineOptions.java | 6 +++++- .../java/org/petero/droidfish/engine/ExternalEngine.java | 5 ++++- .../java/org/petero/droidfish/engine/InternalStockFish.java | 4 ++-- .../org/petero/droidfish/engine/OpenExchangeEngine.java | 4 ++-- .../java/org/petero/droidfish/engine/UCIEngineBase.java | 6 +++--- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java index 65012de..4040549 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/DroidFish.java @@ -260,6 +260,7 @@ public class DroidFish extends Activity private static String pgnDir = "DroidFish/pgn"; private static String fenDir = "DroidFish/epd"; private static String engineDir = "DroidFish/uci"; + private static String engineLogDir = "DroidFish/uci/logs"; private static String gtbDefaultDir = "DroidFish/gtb"; private static String rtbDefaultDir = "DroidFish/rtb"; private BookOptions bookOptions = new BookOptions(); @@ -726,6 +727,7 @@ public class DroidFish extends Activity new File(extDir + sep + fenDir).mkdirs(); new File(extDir + sep + engineDir).mkdirs(); new File(extDir + sep + engineDir + sep + EngineUtil.openExchangeDir).mkdirs(); + new File(extDir + sep + engineLogDir).mkdirs(); new File(extDir + sep + gtbDefaultDir).mkdirs(); new File(extDir + sep + rtbDefaultDir).mkdirs(); } @@ -1314,6 +1316,7 @@ public class DroidFish extends Activity rtbPath = extDir.getAbsolutePath() + sep + rtbDefaultDir; engineOptions.rtbPath = rtbPath; engineOptions.rtbPathNet = settings.getString("rtbPathNet", "").trim(); + engineOptions.workDir = Environment.getExternalStorageDirectory() + sep + engineLogDir; setEngineOptions(false); setEgtbHints(cb.getSelectedSquare()); diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/EngineOptions.java b/DroidFishApp/src/main/java/org/petero/droidfish/EngineOptions.java index d4ab754..942d7ac 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/EngineOptions.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/EngineOptions.java @@ -31,6 +31,7 @@ public final class EngineOptions { String rtbPath; // Syzygy directory path String rtbPathNet; // Syzygy directory path for network engines public String networkID; // host+port network settings + public String workDir; // Working directory for engine process public EngineOptions() { hashMB = 16; @@ -44,6 +45,7 @@ public final class EngineOptions { rtbPath = ""; rtbPathNet = ""; networkID = ""; + workDir = ""; } public EngineOptions(EngineOptions other) { @@ -58,6 +60,7 @@ public final class EngineOptions { rtbPath = other.rtbPath; rtbPathNet = other.rtbPathNet; networkID = other.networkID; + workDir = other.workDir; } /** Get the GTB path for an engine. */ @@ -90,7 +93,8 @@ public final class EngineOptions { gtbPathNet.equals(other.gtbPathNet) && rtbPath.equals(other.rtbPath) && rtbPathNet.equals(other.rtbPathNet) && - networkID.equals(other.networkID)); + networkID.equals(other.networkID) && + workDir.equals(other.workDir)); } @Override diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/engine/ExternalEngine.java b/DroidFishApp/src/main/java/org/petero/droidfish/engine/ExternalEngine.java index 0614e1e..9ba3165 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/engine/ExternalEngine.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/engine/ExternalEngine.java @@ -37,6 +37,7 @@ public class ExternalEngine extends UCIEngineBase { protected final Context context; private File engineFileName; + private File engineWorkDir; private final Report report; private Process engineProc; private Thread startupThread; @@ -47,10 +48,11 @@ public class ExternalEngine extends UCIEngineBase { private boolean startedOk; private boolean isRunning; - public ExternalEngine(String engine, Report report) { + public ExternalEngine(String engine, String workDir, Report report) { context = DroidFishApp.getContext(); this.report = report; engineFileName = new File(engine); + engineWorkDir = new File(workDir); engineProc = null; startupThread = null; exitThread = null; @@ -75,6 +77,7 @@ public class ExternalEngine extends UCIEngineBase { chmod(exePath); cleanUpExeDir(exeDir, exePath); ProcessBuilder pb = new ProcessBuilder(exePath); + pb.directory(engineWorkDir); synchronized (EngineUtil.nativeLock) { engineProc = pb.start(); } diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/engine/InternalStockFish.java b/DroidFishApp/src/main/java/org/petero/droidfish/engine/InternalStockFish.java index cd13efa..8a3884e 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/engine/InternalStockFish.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/engine/InternalStockFish.java @@ -35,8 +35,8 @@ import android.os.Environment; /** Stockfish engine running as process, started from assets resource. */ public class InternalStockFish extends ExternalEngine { - public InternalStockFish(Report report) { - super("", report); + public InternalStockFish(Report report, String workDir) { + super("", workDir, report); } @Override diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/engine/OpenExchangeEngine.java b/DroidFishApp/src/main/java/org/petero/droidfish/engine/OpenExchangeEngine.java index 2a87a3a..e93d1e0 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/engine/OpenExchangeEngine.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/engine/OpenExchangeEngine.java @@ -28,8 +28,8 @@ import com.kalab.chess.enginesupport.ChessEngineResolver; /** Engine imported from a different android app, resolved using the open exchange format. */ public class OpenExchangeEngine extends ExternalEngine { - public OpenExchangeEngine(String engine, Report report) { - super(engine, report); + public OpenExchangeEngine(String engine, String workDir, Report report) { + super(engine, workDir, report); } @Override diff --git a/DroidFishApp/src/main/java/org/petero/droidfish/engine/UCIEngineBase.java b/DroidFishApp/src/main/java/org/petero/droidfish/engine/UCIEngineBase.java index 422b924..b540e45 100644 --- a/DroidFishApp/src/main/java/org/petero/droidfish/engine/UCIEngineBase.java +++ b/DroidFishApp/src/main/java/org/petero/droidfish/engine/UCIEngineBase.java @@ -42,13 +42,13 @@ public abstract class UCIEngineBase implements UCIEngine { if ("cuckoochess".equals(engine)) return new CuckooChessEngine(); else if ("stockfish".equals(engine)) - return new InternalStockFish(report); + return new InternalStockFish(report, engineOptions.workDir); else if (EngineUtil.isOpenExchangeEngine(engine)) - return new OpenExchangeEngine(engine, report); + return new OpenExchangeEngine(engine, engineOptions.workDir, report); else if (EngineUtil.isNetEngine(engine)) return new NetworkEngine(engine, engineOptions, report); else - return new ExternalEngine(engine, report); + return new ExternalEngine(engine, engineOptions.workDir, report); } protected UCIEngineBase() {