Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kaorahi committed Dec 31, 2023
2 parents 4b340e3 + 88d0ae2 commit 630444d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
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 @@ -103,6 +103,7 @@ public class ConfigDialog extends LizzieDialog {

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

private Path curPath;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -1910,25 +1933,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 @@ -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 {
Expand All @@ -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);
Expand All @@ -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 =
Expand All @@ -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);

Expand All @@ -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"));
Expand All @@ -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();
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/Yi-Kai">Yi-Kai</a></td><td><a href="https://github.com/DuskEagle">DuskEagle</a></td></tr><tr><td><a href="https://github.com/njfox">njfox</a></td><td><a href="https://github.com/komu">komu</a></td><td><a href="https://github.com/NTUST-MITLAB">NTUST-MITLAB</a></td></tr><tr><td><a href="https://github.com/hope366">hope366</a></td><td><a href="https://github.com/izeye">izeye</a></td><td><a href="https://github.com/xiaoyifang">xiaoyifang</a></td></tr><tr><td><a href="https://github.com/zakki">zakki</a></td><td><a href="https://github.com/some2112">some2112</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

0 comments on commit 630444d

Please sign in to comment.