This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/main/java/dev/kske/chess/io/EngineUtil.java

142 lines
3.2 KiB
Java

package dev.kske.chess.io;
import java.io.*;
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>EngineUtil.java</strong><br>
* Created: <strong>23.07.2019</strong><br>
*
* @since Chess v0.2-alpha
* @author Kai S. K. Engelbart
* @author Leon Hofmeister
*/
public class EngineUtil {
private static volatile List<EngineInfo> engineInfos;
private static final String engineInfoFile = "engine_infos.ser";
static {
loadEngineInfos();
}
private EngineUtil() {}
/**
* Stores information about an engine while checking its availability.
*
* @param enginePath the path to the executable of the engine
*/
public static void addEngine(String enginePath) {
try {
EngineInfo info = new EngineInfo(enginePath);
UCIHandle handle = new UCIHandle(enginePath);
handle.registerListener(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<>();
}
}
private static void saveEngineInfos() {
try (
ObjectOutputStream out
= new ObjectOutputStream(new FileOutputStream(engineInfoFile))
) {
out.writeObject(engineInfos);
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Stores the name and author of an engine, as well as a path to its
* executable.<br>
* <br>
* Project: <strong>Chess</strong><br>
* File: <strong>EngineUtil.java</strong><br>
* Created: <strong>23.07.2019</strong><br>
*
* @since Chess v0.2-alpha
* @author Kai S. K. Engelbart
*/
public static class EngineInfo implements Serializable {
private static final long serialVersionUID = -474177108900833005L;
/**
* The path to the executable of the engine
*/
public String path;
/**
* The name of the engine
*/
public String name;
/**
* The author of the engine
*/
public String author;
/**
* Creates an instance of {@link EngineInfo}.
*
* @param path the path of the engine executable
*/
public EngineInfo(String path) {
this.path = path;
}
@Override
public String toString() {
return name + " by " + author + " at " + path;
}
}
/**
* @return a list of all stored engine infos
*/
public static List<EngineInfo> getEngineInfos() { return engineInfos; }
}