2019-07-23 11:54:43 +02:00
|
|
|
package dev.kske.chess.ui;
|
|
|
|
|
2019-07-23 16:28:53 +02:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2019-07-23 11:54:43 +02:00
|
|
|
import java.io.IOException;
|
2019-07-23 16:28:53 +02:00
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.Serializable;
|
2019-07-23 11:54:43 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import dev.kske.chess.uci.UCIHandle;
|
|
|
|
import dev.kske.chess.uci.UCIListener;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>MenuBar.java</strong><br>
|
|
|
|
* Created: <strong>23.07.2019</strong><br>
|
|
|
|
* Author: <strong>Leon Hofmeister</strong>
|
|
|
|
*/
|
|
|
|
public class EngineUtil {
|
|
|
|
|
|
|
|
private static volatile List<EngineInfo> engineInfos;
|
|
|
|
|
2019-07-23 16:28:53 +02:00
|
|
|
private static final String engineInfoFile = "engine_infos.ser";
|
|
|
|
|
2019-07-23 11:54:43 +02:00
|
|
|
static {
|
2019-07-23 16:28:53 +02:00
|
|
|
loadEngineInfos();
|
2019-07-23 11:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private EngineUtil() {}
|
|
|
|
|
2019-07-30 06:18:24 +02:00
|
|
|
public static void addEngine(String enginePath) {
|
2019-07-23 16:28:53 +02:00
|
|
|
try {
|
|
|
|
EngineInfo info = new EngineInfo(enginePath);
|
|
|
|
UCIHandle handle = new UCIHandle(enginePath);
|
|
|
|
handle.setListener(new UCIListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIdName(String name) {
|
|
|
|
info.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIdAuthor(String author) {
|
|
|
|
info.author = author;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUCIOk() {
|
|
|
|
engineInfos.add(info);
|
|
|
|
handle.quit();
|
|
|
|
saveEngineInfos();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
handle.start();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static void loadEngineInfos() {
|
|
|
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(engineInfoFile))) {
|
|
|
|
Object obj = in.readObject();
|
|
|
|
if (obj instanceof ArrayList<?>) engineInfos = (ArrayList<EngineInfo>) obj;
|
|
|
|
else throw new IOException("Serialized object has the wrong class.");
|
|
|
|
} catch (ClassNotFoundException | IOException ex) {
|
|
|
|
engineInfos = new ArrayList<>();
|
|
|
|
}
|
2019-07-23 11:54:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 16:28:53 +02:00
|
|
|
private static void saveEngineInfos() {
|
|
|
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(engineInfoFile))) {
|
|
|
|
out.writeObject(engineInfos);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class EngineInfo implements Serializable {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -474177108900833005L;
|
|
|
|
|
2019-07-23 11:54:43 +02:00
|
|
|
public String path, name, author;
|
|
|
|
|
|
|
|
public EngineInfo(String path) {
|
|
|
|
this.path = path;
|
|
|
|
}
|
2019-07-23 16:28:53 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return name + " by " + author + " at " + path;
|
|
|
|
}
|
2019-07-23 11:54:43 +02:00
|
|
|
}
|
2019-07-23 16:28:53 +02:00
|
|
|
|
|
|
|
public static List<EngineInfo> getEngineInfos() { return engineInfos; }
|
2019-07-23 11:54:43 +02:00
|
|
|
}
|