diff --git a/src/main/java/featurecat/lizzie/gui/ConfigDialog.java b/src/main/java/featurecat/lizzie/gui/ConfigDialog.java index 9932c38d..da2154e7 100644 --- a/src/main/java/featurecat/lizzie/gui/ConfigDialog.java +++ b/src/main/java/featurecat/lizzie/gui/ConfigDialog.java @@ -103,6 +103,7 @@ public class ConfigDialog extends LizzieDialog { public String enginePath = ""; public String weightPath = ""; + public String configPath = ""; public String commandHelp = ""; private Path curPath; @@ -1865,8 +1866,7 @@ private String getEngineLine() { if (result == JFileChooser.APPROVE_OPTION) { engineFile = chooser.getSelectedFile(); if (engineFile != null) { - enginePath = engineFile.getAbsolutePath(); - enginePath = relativizePath(engineFile.toPath(), this.curPath); + enginePath = relativizePath(engineFile.toPath(), this.curPath, true); getCommandHelp(); JFileChooser chooserw = new JFileChooser("."); chooserw.setMultiSelectionEnabled(false); @@ -1876,7 +1876,9 @@ private String getEngineLine() { weightFile = chooserw.getSelectedFile(); if (weightFile != null) { weightPath = relativizePath(weightFile.toPath(), this.curPath); - EngineParameter ep = new EngineParameter(enginePath, weightPath, this); + configPath = selectConfigFile(); + EngineParameter ep = + new EngineParameter(enginePath, weightPath, configPath, guessedEngineType(), this); ep.setVisible(true); if (!ep.commandLine.isEmpty()) { engineLine = ep.commandLine; @@ -1888,6 +1890,27 @@ private String getEngineLine() { return engineLine; } + private String selectConfigFile() { + if (!guessedEngineType().equals("katago")) return ""; + String titleKey = "LizzieConfig.prompt.selectConfig"; + // dare to copy code redundantly to keep the original code as much as possible + JFileChooser chooser = new JFileChooser("."); + chooser.setMultiSelectionEnabled(false); + chooser.setDialogTitle(resourceBundle.getString(titleKey)); + int result = chooser.showOpenDialog(this); + if (result != JFileChooser.APPROVE_OPTION) return ""; + File file = chooser.getSelectedFile(); + if (file == null) return ""; + return relativizePath(file.toPath(), this.curPath); + } + + private String guessedEngineType() { + String engineFileName = (new File(enginePath)).toPath().getFileName().toString(); + // fixme: ad hoc! + Boolean isKataGo = engineFileName.toLowerCase().contains("katago"); + return isKataGo ? "katago" : "leelaz"; + } + private String getImagePath() { String imagePath = ""; File imageFile = null; @@ -1910,12 +1933,20 @@ private String getImagePath() { } private String relativizePath(Path path, Path curPath) { + return relativizePath(path, curPath, false); + } + + private String relativizePath(Path path, Path curPath, Boolean isCommand) { Path relatPath; if (path.startsWith(curPath)) { relatPath = curPath.relativize(path); } else { relatPath = path; } + if (isCommand && relatPath.getFileName().equals(relatPath) && !isWindows()) { + // "leelaz" ==> "./leelaz" for Linux + relatPath = (new File(".")).toPath().resolve(relatPath); + } return relatPath.toString(); } @@ -1923,12 +1954,14 @@ private void getCommandHelp() { List commands = new ArrayList(); commands.add(enginePath); + if (guessedEngineType().equals("katago")) { + commands.add("gtp"); + } commands.add("-h"); - ProcessBuilder processBuilder = new ProcessBuilder(commands); - processBuilder.directory(); - processBuilder.redirectErrorStream(true); try { + ProcessBuilder processBuilder = new ProcessBuilder(commands); + processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); inputStream = new BufferedInputStream(process.getInputStream()); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); diff --git a/src/main/java/featurecat/lizzie/gui/EngineParameter.java b/src/main/java/featurecat/lizzie/gui/EngineParameter.java index 352ca751..1f21cef1 100644 --- a/src/main/java/featurecat/lizzie/gui/EngineParameter.java +++ b/src/main/java/featurecat/lizzie/gui/EngineParameter.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; +import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; @@ -14,6 +15,7 @@ import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTextPane; +import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; public class EngineParameter extends LizzieDialog { @@ -29,7 +31,12 @@ public class EngineParameter extends LizzieDialog { private Color oriColor; /** Create the dialog. */ - public EngineParameter(String enginePath, String weightPath, ConfigDialog configDialog) { + public EngineParameter( + String enginePath, + String weightPath, + String configPath, + String engineType, + ConfigDialog configDialog) { setTitle(configDialog.resourceBundle.getString("LizzieConfig.title.parameterConfig")); setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE); setModal(true); @@ -47,7 +54,10 @@ public EngineParameter(String enginePath, String weightPath, ConfigDialog config txtCommandLine = new JTextField(); txtCommandLine.setEditable(false); txtCommandLine.setBounds(89, 12, 565, 26); - txtCommandLine.setText(enginePath + " --weights " + weightPath); + String weightOption = engineType.equals("leelaz") ? " --weights " : " gtp -model "; + String configArgs = + (engineType.equals("leelaz") || configPath.isEmpty()) ? "" : " -config " + configPath + " "; + txtCommandLine.setText(enginePath + weightOption + weightPath + configArgs); contentPanel.add(txtCommandLine); txtCommandLine.setColumns(10); JLabel lblParameter = @@ -66,7 +76,9 @@ public void focusLost(FocusEvent e) { }); txtParameter.setColumns(10); txtParameter.setBounds(89, 44, 565, 26); - txtParameter.setText("-g --lagbuffer 0 "); + if (engineType.equals("leelaz")) { + txtParameter.setText("-g --lagbuffer 0 "); + } oriColor = txtParameter.getBackground(); contentPanel.add(txtParameter); @@ -82,6 +94,7 @@ public void focusLost(FocusEvent e) { txtParams.setFont(font); txtParams.setText(configDialog.commandHelp); txtParams.setEditable(false); + SwingUtilities.invokeLater(() -> txtParams.scrollRectToVisible(new Rectangle(0, 0, 0, 0))); JLabel lblParameterList = new JLabel(configDialog.resourceBundle.getString("LizzieConfig.title.parameterList")); @@ -94,7 +107,7 @@ public void focusLost(FocusEvent e) { okButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { - if (txtParameter.getText().isEmpty()) { + if (txtParameter.getText().isEmpty() && engineType.equals("leelaz")) { txtParameter.setBackground(Color.RED); } else { parameters = txtParameter.getText().trim(); diff --git a/src/main/resources/l10n/DisplayStrings.properties b/src/main/resources/l10n/DisplayStrings.properties index 4134618b..84257cba 100644 --- a/src/main/resources/l10n/DisplayStrings.properties +++ b/src/main/resources/l10n/DisplayStrings.properties @@ -149,6 +149,7 @@ LizzieConfig.lizzie.contributorsTitle=cngoodboykaorahizsalchbittsittOlivierBlanvillaindfanniustoomasrapetrescTFiFiEaerisnjukuba97531bvandenbonKa-zamtypohhalreadydoneodCattomasz-warnielloinohirokiParmuzinAlexanderygrekpliuphysinfinity0yzyrayrexl2018gjm11Yi-KaiDuskEaglenjfoxkomuNTUST-MITLABhope366izeyexiaoyifangzakkisome2112 LizzieConfig.prompt.selectEngine=Please select a engine LizzieConfig.prompt.selectWeight=Please select a weight file +LizzieConfig.prompt.selectConfig=Please select a config file LizzieConfig.button.ok=OK LizzieConfig.button.cancel=Cancel LizzieConfig.button.add=Add