Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues of engine settings #915

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/main/java/featurecat/lizzie/gui/ConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class ConfigDialog extends JDialog {

public String enginePath = "";
public String weightPath = "";
public String configPath = "";
public String commandHelp = "";

private String osName;
Expand Down Expand Up @@ -1838,8 +1839,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);
Expand All @@ -1849,7 +1849,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;
Expand All @@ -1861,6 +1863,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;
Expand All @@ -1883,25 +1906,35 @@ 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();
}

private void getCommandHelp() {

List<String> commands = new ArrayList<String>();
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();
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/featurecat/lizzie/gui/EngineParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,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 JDialog {
Expand All @@ -30,7 +32,12 @@ public class EngineParameter extends JDialog {
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);
Expand All @@ -48,7 +55,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 =
Expand All @@ -67,7 +77,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);

Expand All @@ -83,6 +95,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"));
Expand All @@ -95,7 +108,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();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/DisplayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ LizzieConfig.lizzie.contributorsTitle=<html><a href="https://github.com/featurec
LizzieConfig.lizzie.contributors=<html><table><tr><td><a href="https://github.com/cngoodboy">cngoodboy</a></td><td><a href="https://github.com/kaorahi">kaorahi</a></td><td><a href="https://github.com/zsalch">zsalch</a></td></tr><tr><td><a href="https://github.com/bittsitt">bittsitt</a></td><td><a href="https://github.com/OlivierBlanvillain">OlivierBlanvillain</a></td><td><a href="https://github.com/dfannius">dfannius</a></td></tr><tr><td><a href="https://github.com/toomasr">toomasr</a></td><td><a href="https://github.com/apetresc">apetresc</a></td><td><a href="https://github.com/TFiFiE">TFiFiE</a></td></tr><tr><td><a href="https://github.com/aerisnju">aerisnju</a></td><td><a href="https://github.com/kuba97531">kuba97531</a></td><td><a href="https://github.com/bvandenbon">bvandenbon</a></td></tr><tr><td><a href="https://github.com/Ka-zam">Ka-zam</a></td><td><a href="https://github.com/typohh">typohh</a></td><td><a href="https://github.com/alreadydone">alreadydone</a></td></tr><tr><td><a href="https://github.com/odCat">odCat</a></td><td><a href="https://github.com/tomasz-warniello">tomasz-warniello</a></td><td><a href="https://github.com/inohiroki">inohiroki</a></td></tr><tr><td><a href="https://github.com/ParmuzinAlexander">ParmuzinAlexander</a></td><td><a href="https://github.com/ygrek">ygrek</a></td><td><a href="https://github.com/pliuphys">pliuphys</a></td></tr><tr><td><a href="https://github.com/infinity0">infinity0</a></td><td><a href="https://github.com/yzyray">yzyray</a></td><td><a href="https://github.com/rexl2018">rexl2018</a></td></tr><tr><td><a href="https://github.com/gjm11">gjm11</a></td><td><a href="https://github.com/bvandenbon-objt">bvandenbon-objt</a></td><td><a href="https://github.com/Yi-Kai">Yi-Kai</a></td></tr><tr><td><a href="https://github.com/DuskEagle">DuskEagle</a></td><td><a href="https://github.com/njfox">njfox</a></td><td><a href="https://github.com/komu">komu</a></td></tr></table></html>
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
Expand Down