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

Engine settings overhaul #138

Merged
merged 14 commits into from
Feb 4, 2024
Merged
22 changes: 13 additions & 9 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ derivative = "2.2.0"
dashmap = "5.4.0"
once_cell = "1.17.1"
rand = "0.8.5"
vampirc-uci = { git = "https://github.com/franciscoBSalgueiro/vampirc-uci", branch = "master" }
vampirc-uci = { git = "https://github.com/franciscoBSalgueiro/vampirc-uci", rev="b16a8d0986253a49ba866182b8fc857a5b8f65d1", features = ["specta", "serde"] }
tempfile = "3.6.0"
quick-xml = { version = "0.31.0", features = ["serialize"] }
specta = { version = "2.0.0-rc.5", features = ["typescript"] }
tauri-specta = { version = "2.0.0-rc.2", features = ["typescript"] }
specta = { version = "2.0.0-rc.7", features = ["typescript"] }
tauri-specta = { version = "2.0.0-rc.4", features = ["typescript"] }
strsim = "0.11.0"
bincode = "2.0.0-rc.3"
thiserror = "1.0.48"
Expand All @@ -61,6 +61,3 @@ default = ["custom-protocol"]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = ["tauri/custom-protocol"]

[patch.crates-io]
specta = { git = "https://github.com/oscartbeaumont/specta", rev = "034a9c028639881878455f21c5d8667bbe53c42b" }
60 changes: 40 additions & 20 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::{
process::{Child, ChildStdin, ChildStdout, Command},
sync::Mutex,
};
use vampirc_uci::{parse_one, UciInfoAttribute, UciMessage};
use vampirc_uci::{parse_one, UciInfoAttribute, UciMessage, UciOptionConfig};

use crate::{
db::{is_position_in_db, PositionQuery},
Expand Down Expand Up @@ -117,16 +117,14 @@ impl EngineProcess {
let mv = uci.to_move(&pos)?;
pos.play_unchecked(&mv);
}
self.real_multipv = options.multipv.min(pos.legal_moves().len() as u16);
if options.threads != self.options.threads {
self.set_option("Threads", &options.threads).await?;
}
if options.multipv != self.options.multipv {
self.set_option("MultiPV", &options.multipv).await?;
}
if options.hash != self.options.hash {
self.set_option("Hash", options.hash).await?;
}
let multipv = options
.extra_options
.iter()
.find(|x| x.name == "MultiPV")
.map(|x| x.value.parse().unwrap_or(1))
.unwrap_or(1);

self.real_multipv = multipv.min(pos.legal_moves().len() as u16);

for option in &options.extra_options {
if !self.options.extra_options.contains(option) {
Expand Down Expand Up @@ -341,9 +339,9 @@ async fn send_command(stdin: &mut ChildStdin, command: impl AsRef<str>) {
#[serde(rename_all = "camelCase")]
#[derivative(Default)]
pub struct EngineOptions {
pub multipv: u16,
pub threads: u16,
pub hash: u16,
// pub multipv: u16,
// pub threads: u16,
// pub hash: u16,
#[derivative(Default(value = "Fen::default().to_string()"))]
pub fen: String,
pub moves: Vec<String>,
Expand Down Expand Up @@ -601,12 +599,15 @@ pub async fn analyze_game(
)?;

proc.set_options(EngineOptions {
threads: 4,
multipv: 2,
hash: 16,
// threads: 4,
// multipv: 2,
// hash: 16,
fen: options.fen.clone(),
moves: moves.clone(),
extra_options: Vec::new(),
extra_options: vec![EngineOption {
name: "MultiPV".to_string(),
value: "2".to_string(),
}],
})
.await?;

Expand Down Expand Up @@ -816,18 +817,37 @@ mod tests {
}
}

#[derive(Type, Default, Serialize, Debug)]
pub struct EngineConfig {
pub name: String,
pub options: Vec<UciOptionConfig>,
}

#[tauri::command]
pub async fn get_engine_name(path: PathBuf) -> Result<String, Error> {
#[specta::specta]
pub async fn get_engine_config(path: PathBuf) -> Result<EngineConfig, Error> {
let mut child = start_engine(path)?;
let (mut stdin, mut stdout) = get_handles(&mut child)?;

send_command(&mut stdin, "uci\n").await;

let mut config = EngineConfig::default();

loop {
if let Some(line) = stdout.next_line().await? {
if let UciMessage::Id { name, author: _ } = parse_one(&line) {
return Ok(name.unwrap_or_default());
if let Some(name) = name {
config.name = name;
}
}
if let UciMessage::Option(opt) = parse_one(&line) {
config.options.push(opt);
}
if let UciMessage::UciOk = parse_one(&line) {
break;
}
}
}
println!("{:?}", config);
Ok(config)
}
14 changes: 1 addition & 13 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,7 @@ impl serde::Serialize for Error {
}

impl Type for Error {
fn definition(_opts: specta::DefOpts) -> specta::DataType {
specta::DataType::Primitive(specta::PrimitiveType::String)
}
fn definition_generics() -> Vec<specta::GenericType> {
vec![]
}
fn reference(
opts: specta::DefOpts,
generics: &[specta::DataType],
) -> specta::reference::Reference {
specta::reference::inline::<Error>(opts, generics)
}
fn inline(_opts: specta::DefOpts, _generics: &[specta::DataType]) -> specta::DataType {
fn inline(_type_map: &mut specta::TypeMap, _generics: &[specta::DataType]) -> specta::DataType {
specta::DataType::Primitive(specta::PrimitiveType::String)
}
}
8 changes: 4 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use derivative::Derivative;
use fide::FidePlayer;
use log::LevelFilter;
use oauth::AuthState;
use specta::ts::{BigIntExportBehavior, ExportConfig};
use sysinfo::SystemExt;
use tauri::{
api::path::{resolve_path, BaseDirectory},
Expand All @@ -33,9 +34,7 @@ use tauri::{
use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};
use tauri_plugin_log::LogTarget;

use crate::chess::{
analyze_game, get_engine_logs, get_engine_name, kill_engines, stop_engine,
};
use crate::chess::{analyze_game, get_engine_config, get_engine_logs, kill_engines, stop_engine};
use crate::db::{
clear_games, convert_pgn, create_indexes, delete_database, delete_indexes,
get_players_game_info, get_tournaments, search_position,
Expand Down Expand Up @@ -109,6 +108,7 @@ const LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
fn main() {
let specta_builder = {
let specta_builder = tauri_specta::ts::builder()
.config(ExportConfig::new().bigint(BigIntExportBehavior::BigInt))
.commands(tauri_specta::collect_commands!(
close_splashscreen,
find_fide_player,
Expand All @@ -124,6 +124,7 @@ fn main() {
get_opening_from_fen,
get_opening_from_name,
get_players_game_info,
get_engine_config,
))
.events(tauri_specta::collect_events!(BestMovesPayload, Progress));

Expand Down Expand Up @@ -219,7 +220,6 @@ fn main() {
search_position,
is_bmi2_compatible,
clear_games,
get_engine_name,
set_file_as_executable,
count_pgn_games,
read_games,
Expand Down
34 changes: 14 additions & 20 deletions src/atoms/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BestMoves } from "@/bindings";
import { BestMoves, GoMode } from "@/bindings";
import { Card, buildFromTree } from "@/components/files/opening";
import { LocalOptions } from "@/components/panels/database/DatabasePanel";
import { DatabaseInfo } from "@/utils/db";
Expand Down Expand Up @@ -330,28 +330,18 @@ export const tabEngineSettingsFamily = atomFamily(
tab,
engineName,
defaultSettings,
defaultGo,
}: {
tab: string;
engineName: string;
defaultSettings?: EngineSettings;
defaultGo?: GoMode;
}) => {
return atom<EngineSettings>(
defaultSettings
? { ...defaultSettings, enabled: false }
: {
enabled: false,
go: {
t: "Depth",
c: 24,
},
options: {
threads: 2,
multipv: 3,
hash: 16,
extraOptions: [],
},
},
);
return atom<{ enabled: boolean; settings: EngineSettings; go: GoMode }>({
enabled: false,
settings: defaultSettings || [],
go: defaultGo || { t: "Infinite" },
});
},
(a, b) => a.tab === b.tab && a.engineName === b.engineName,
);
Expand All @@ -366,7 +356,9 @@ export const allEnabledAtom = loadable(
const atom = tabEngineSettingsFamily({
tab: get(activeTabAtom)!,
engineName: engine.name,
defaultSettings: engine.settings ?? undefined,
defaultSettings:
engine.type === "local" ? engine.settings || [] : undefined,
defaultGo: engine.go ?? undefined,
});
return get(atom).enabled;
});
Expand All @@ -383,7 +375,9 @@ export const enableAllAtom = atom(null, (get, set, value: boolean) => {
const atom = tabEngineSettingsFamily({
tab: get(activeTabAtom)!,
engineName: engine.name,
defaultSettings: engine.settings ?? undefined,
defaultSettings:
engine.type === "local" ? engine.settings || [] : undefined,
defaultGo: engine.go ?? undefined,
});
set(atom, { ...get(atom), enabled: value });
}
Expand Down
Loading
Loading