Skip to content

Commit 25b4b4d

Browse files
committed
Fixed some bot selection issues
1 parent a8ed440 commit 25b4b4d

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

game/src/spaghetti/BotProgram.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class BotProgram extends BoardController {
1313
protected final File logFile;
1414
protected Process process;
1515
protected boolean side;
16-
public final String cmd;
16+
public final String[] cmd;
1717

18-
public BotProgram(String name, String command, Board board, File logFile) {
18+
public BotProgram(String name, String[] command, Board board, File logFile) {
1919
super(name, board);
2020
board.addBoardListener(this);
2121

swing/src/spaghetti/gui/swing/PlayerSelection.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,25 +251,32 @@ public BoardController create(GraphicalBoard gb, Board board) {
251251
}
252252
break;
253253
case 2:
254-
String cmd = null;
254+
String[] cmd = null;
255255
if (execRadio.isSelected()) {
256256
if (fileExec != null && fileExec.exists())
257-
cmd = Utils.getPathWithoutSpaces(fileExec.getAbsolutePath());
257+
cmd = new String[]{fileExec.getAbsolutePath()};
258258
} else if (javaRadio.isSelected()) {
259259
if (fileJava != null && fileJava.exists()) {
260-
String javaExe = "java";
261260
if (Utils.getFileExtension(fileJava).equals("class")) {
262261
String dir = fileJava.getParent();
263-
cmd = javaExe + " -classpath " + Utils.getPathWithoutSpaces(dir) +
264-
" " + fileJava.getName().replaceFirst("[.][^.]+$", "");
262+
cmd = new String[]{
263+
"java",
264+
"-classpath",
265+
dir,
266+
fileJava.getName().replaceFirst("[.][^.]+$", "")
267+
};
265268
}
266269
else if (Utils.getFileExtension(fileJava).equals("jar")) {
267270
String file = fileJava.getAbsolutePath();
268-
cmd = javaExe + " -jar " + Utils.getPathWithoutSpaces(file) + "";
271+
cmd = new String[] {
272+
"java",
273+
"-jar",
274+
file
275+
};
269276
}
270277
}
271278
} else {
272-
cmd = otherField.getText();
279+
cmd = Utils.splitBackslash(otherField.getText(), ' ');
273280
}
274281
if (cmd != null) {
275282
r = new BotProgram(name, cmd, board, logCheckBox.isSelected()? new File(logDirChooser.getSelectedFile(), logTextField.getText()): null);

utils/src/spaghetti/utils/Utils.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package spaghetti.utils;
22

33
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
public final class Utils {
68
public static String getFileExtension(File f) {
@@ -12,11 +14,25 @@ public static String getFileExtension(File f) {
1214
return extension.toLowerCase();
1315
}
1416

15-
public static String getPathWithoutSpaces(String dir) {
16-
if (System.getProperty("os.name").startsWith("Windows"))
17-
return '"' + dir + '"';
18-
else
19-
return dir.replaceAll(" ", "\\\\ ");
17+
public static String[] splitBackslash(String string, char regex) {
18+
List<String> arr = new ArrayList<>();
19+
StringBuilder sub = new StringBuilder();
20+
boolean prevBackslash = false;
21+
for (int i = 0; i < string.length(); ++i) {
22+
char c = string.charAt(i);
23+
if (c == regex && !prevBackslash) {
24+
arr.add(sub.toString());
25+
sub = new StringBuilder();
26+
} else {
27+
if (prevBackslash || c != '\\') {
28+
if (c != regex && c != '\\' && prevBackslash) sub.append('\\');
29+
sub.append(c);
30+
}
31+
}
32+
prevBackslash = c == '\\' && !prevBackslash;
33+
}
34+
arr.add(sub.toString());
35+
return arr.toArray(new String[0]);
2036
}
2137

2238
public static <T> Pair<T, T> swap(T a, T b) {

0 commit comments

Comments
 (0)