Android stage 1: databases, games, the game viewer and the engine

androidApp/: Kotlin + Jetpack Compose over the Rust core (mobile-api, UniFFI).
- Databases: the desktop samples on the list from the first start, files added
  with the system picker and read in place, rename, remove with undo.
- Games: a paged list with a search by players, event, site and date.
- Game: the green board with Merida pieces, the notation as on the desktop
  (variations, comments, NAG), forks, paging games, hold-to-repeat steps,
  comment arrows and squares, the check glow, Latin/figurine/local notation.
- Engine: Stockfish 19 built from the pinned source (scripts/stockfish.sh) and
  run from nativeLibraryDir through the uci crate. Two builds: "full" with the
  network inside, "light" downloading it on request (resume, SHA-256 check).
  The time per position, lines, line preview on the board; the engine quits
  when the screen goes away.
- Settings and About with the licenses.

mobile-api: game search, UCI moves and comment marks for nodes, the Analyzer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-22 01:40:37 +03:00
parent 0c912ac669
commit 68fcabd691
59 changed files with 5049 additions and 25 deletions

View file

@ -26,4 +26,5 @@ chess-core.workspace = true
chess-import.workspace = true
shakmaty.workspace = true
thiserror.workspace = true
uci.workspace = true
uniffi = "0.32.2"

View file

@ -0,0 +1,403 @@
//! The engine for the Android app: Stockfish as a child process supervised by the `uci`
//! crate (timeouts, `stop`, killing a hung engine), as on the desktop. The app asks for
//! the analysis of a position and polls; lines of an older position are never shown
//! for a newer one: every snapshot names its search, and events of earlier searches are
//! dropped.
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use shakmaty::fen::Fen;
use shakmaty::san::SanPlus;
use shakmaty::uci::UciMove;
use shakmaty::{CastlingMode, Chess, Color, Position};
use uci::{
Engine, EngineConfig, EngineEvent, EngineHandle, EnginePosition, Score, SearchId, Side,
WhiteEval,
};
/// Why the engine cannot serve.
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum EngineFailure {
/// The engine did not start or did not answer the handshake.
#[error("{detail}")]
Start { detail: String },
/// The position cannot be sent (a bad FEN or move).
#[error("{detail}")]
Input { detail: String },
/// The engine has stopped (it crashed, hung and was killed, or was quit).
#[error("{detail}")]
Stopped { detail: String },
}
/// One line of the analysis, from White's point of view.
#[derive(Debug, Clone, PartialEq, uniffi::Record)]
pub struct EngineLine {
/// 1 for the best line.
pub multipv: u32,
/// "+0.45", "−1.20", "0.00", "#3" (White mates), "−#2" (Black mates).
pub eval: String,
/// Centipawns for White, when the score is not a mate.
pub cp: Option<i32>,
/// Moves to mate: positive when White mates, negative when Black mates.
pub mate: Option<i32>,
/// The line in SAN with move numbers: "14.Nd5 Bxd5 15.exd5".
pub text: String,
/// The squares of its first move, for an arrow on the board.
pub from_square: Option<String>,
pub to_square: Option<String>,
/// The line move by move, to show it on the board without touching the game.
pub steps: Vec<LineStep>,
}
/// A move of an engine line and the position after it.
#[derive(Debug, Clone, PartialEq, uniffi::Record)]
pub struct LineStep {
/// "14.Nd5", "14…Bxd5".
pub label: String,
pub fen: String,
pub from_square: String,
pub to_square: String,
}
/// What the engine says about the position of `search`.
#[derive(Debug, Clone, PartialEq, uniffi::Record)]
pub struct Analysis {
pub search: u64,
pub depth: u32,
pub nodes_per_second: u64,
/// Ordered by `multipv`.
pub lines: Vec<EngineLine>,
}
struct Current {
id: SearchId,
root: Chess,
depth: u32,
nps: u64,
lines: BTreeMap<u32, EngineLine>,
changed: bool,
}
struct State {
next: u64,
multipv: u32,
current: Option<Current>,
stopped: Option<String>,
}
/// A running engine.
#[derive(uniffi::Object)]
pub struct Analyzer {
handle: Mutex<Option<EngineHandle>>,
state: Mutex<State>,
}
fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
m.lock().unwrap_or_else(|e| e.into_inner())
}
#[uniffi::export]
impl Analyzer {
/// Starts the engine at `path` with its network `eval_file`. Blocks for the
/// handshake (seconds at most): call it off the main thread.
#[uniffi::constructor]
pub fn start(
path: String,
eval_file: String,
threads: u32,
hash_mb: u32,
) -> Result<Arc<Self>, EngineFailure> {
let start = |e: uci::EngineError| EngineFailure::Start {
detail: e.to_string(),
};
let handle = Engine::spawn_with(EngineConfig::new(path), Box::new(|| {})).map_err(start)?;
if !eval_file.is_empty() {
handle
.set_option("EvalFile", Some(&eval_file))
.map_err(start)?;
}
handle.set_threads(threads.max(1)).map_err(start)?;
handle.set_hash_mb(hash_mb.max(1)).map_err(start)?;
Ok(Arc::new(Analyzer {
handle: Mutex::new(Some(handle)),
state: Mutex::new(State {
next: 0,
multipv: 1,
current: None,
stopped: None,
}),
}))
}
/// Analyses the position reached from `start_fen` by `moves` (UCI), with `lines`
/// lines, until [`Analyzer::stop`] or the next call. Returns the search's number:
/// only snapshots with it belong to this position.
pub fn analyze(
&self,
start_fen: String,
moves: Vec<String>,
lines: u32,
) -> Result<u64, EngineFailure> {
let input = |detail: String| EngineFailure::Input { detail };
let fen: Fen = start_fen.parse().map_err(|e| input(format!("{e}")))?;
let mut root: Chess = fen
.into_position(CastlingMode::Standard)
.map_err(|e| input(format!("{e}")))?;
for u in &moves {
let m = u
.parse::<UciMove>()
.ok()
.and_then(|m| m.to_move(&root).ok())
.ok_or_else(|| input(format!("illegal move {u}")))?;
root.play_unchecked(m);
}
let guard = lock(&self.handle);
let handle = guard.as_ref().ok_or_else(|| self.stopped())?;
let mut st = lock(&self.state);
let lines = lines.clamp(1, 5);
if st.multipv != lines {
handle.set_multipv(lines).map_err(|e| self.failed(e))?;
st.multipv = lines;
}
st.next += 1;
let id = SearchId(st.next);
handle
.analyze(EnginePosition::fen(start_fen, moves), id)
.map_err(|e| self.failed(e))?;
st.current = Some(Current {
id,
root,
depth: 0,
nps: 0,
lines: BTreeMap::new(),
changed: false,
});
Ok(id.0)
}
/// Stops the search (its last lines stay valid for its position).
pub fn stop(&self) {
if let Some(h) = lock(&self.handle).as_ref() {
let _ = h.stop();
}
lock(&self.state).current = None;
}
/// Waits up to `timeout_ms` for the engine; returns the analysis of the current
/// search when it has changed, `None` otherwise. Fails once the engine has stopped.
pub fn poll(&self, timeout_ms: u32) -> Result<Option<Analysis>, EngineFailure> {
let mut events = Vec::new();
{
let guard = lock(&self.handle);
let handle = guard.as_ref().ok_or_else(|| self.stopped())?;
if let Some(e) = handle.recv_timeout(Duration::from_millis(timeout_ms.into())) {
events.push(e);
events.extend(handle.drain_events());
}
}
let mut st = lock(&self.state);
for e in events {
match e {
EngineEvent::Info { search_id, info } => {
let Some(cur) = st.current.as_mut().filter(|c| c.id == search_id) else {
continue;
};
if let Some(d) = info.depth {
cur.depth = d;
}
if let Some(n) = info.nps {
cur.nps = n;
}
if let (Some(score), false) = (info.score, info.pv.is_empty()) {
let k = info.multipv_index();
let line = engine_line(&cur.root, k, score.score, &info.pv);
cur.lines.insert(k, line);
cur.changed = true;
}
}
EngineEvent::Terminated(reason) => {
st.stopped = Some(format!("{reason:?}"));
}
_ => {}
}
}
if let Some(reason) = st.stopped.clone() {
*lock(&self.handle) = None;
return Err(EngineFailure::Stopped { detail: reason });
}
let multipv = st.multipv;
Ok(st.current.as_mut().filter(|c| c.changed).map(|c| {
c.changed = false;
Analysis {
search: c.id.0,
depth: c.depth,
nodes_per_second: c.nps,
lines: c
.lines
.values()
.filter(|l| l.multipv <= multipv)
.cloned()
.collect(),
}
}))
}
/// Quits the engine (and kills it if it does not leave in time).
pub fn quit(&self) {
if let Some(h) = lock(&self.handle).take() {
h.quit();
}
}
}
impl Analyzer {
fn stopped(&self) -> EngineFailure {
EngineFailure::Stopped {
detail: lock(&self.state)
.stopped
.clone()
.unwrap_or_else(|| "the engine has stopped".into()),
}
}
fn failed(&self, e: uci::EngineError) -> EngineFailure {
EngineFailure::Stopped {
detail: e.to_string(),
}
}
}
/// A line of the engine in SAN, its score for White.
fn engine_line(root: &Chess, multipv: u32, score: Score, pv: &[String]) -> EngineLine {
let side = if root.turn() == Color::White {
Side::White
} else {
Side::Black
};
let white = uci::EngineScore {
score,
bound: uci::ScoreBound::Exact,
}
.to_white(side);
let (eval, cp, mate) = match white.eval {
WhiteEval::Cp(cp) => (pawns(cp), Some(cp), None),
WhiteEval::Mate { winner, moves } => {
let n = moves as i32;
if winner == Side::White {
(format!("#{n}"), None, Some(n))
} else {
(format!("\u{2212}#{n}"), None, Some(-n))
}
}
};
let mut pos = root.clone();
let mut words = Vec::new();
let mut steps = Vec::new();
let mut squares = None;
for (i, u) in pv.iter().enumerate() {
let Some(m) = u.parse::<UciMove>().ok().and_then(|m| m.to_move(&pos).ok()) else {
break;
};
if i == 0 {
squares = Some((u[0..2].to_owned(), u[2..4].to_owned()));
}
let number = pos.fullmoves().get();
let white = pos.turn() == Color::White;
let san = SanPlus::from_move_and_play_unchecked(&mut pos, m).to_string();
let label = if white {
format!("{number}.{san}")
} else {
format!("{number}\u{2026}{san}")
};
words.push(if white || i == 0 { label.clone() } else { san });
steps.push(LineStep {
label,
fen: Fen::from_position(&pos, shakmaty::EnPassantMode::Legal).to_string(),
from_square: u[0..2].to_owned(),
to_square: u[2..4].to_owned(),
});
}
EngineLine {
multipv,
eval,
cp,
mate,
text: words.join(" "),
from_square: squares.as_ref().map(|s| s.0.clone()),
to_square: squares.map(|s| s.1),
steps,
}
}
/// Centipawns as pawns with a sign: "+0.45", "−1.20", "0.00".
fn pawns(cp: i32) -> String {
let sign = match cp.signum() {
1 => "+",
-1 => "\u{2212}",
_ => "",
};
let a = cp.unsigned_abs();
format!("{sign}{}.{:02}", a / 100, a % 100)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scores_read_for_white() {
let black_to_move: Chess = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"
.parse::<Fen>()
.unwrap()
.into_position(CastlingMode::Standard)
.unwrap();
let l = engine_line(
&black_to_move,
1,
Score::Cp(-30),
&["e7e5".into(), "g1f3".into()],
);
assert_eq!(l.eval, "+0.30");
assert_eq!(l.text, "1\u{2026}e5 2.Nf3");
assert_eq!(
(l.from_square.as_deref(), l.to_square.as_deref()),
(Some("e7"), Some("e5"))
);
let m = engine_line(&black_to_move, 1, Score::Mate(2), &[]);
assert_eq!((m.eval.as_str(), m.mate), ("\u{2212}#2", Some(-2)));
assert_eq!(pawns(0), "0.00");
assert_eq!(pawns(-120), "\u{2212}1.20");
}
/// The fake engine of the `uci` crate (`cargo build -p uci --bin fake_uci`) answers
/// like Stockfish; lines of an older search never show up for a newer one.
#[test]
fn analysis_belongs_to_its_position() {
let fake = concat!(env!("CARGO_MANIFEST_DIR"), "/../../target/debug/fake_uci");
assert!(
std::path::Path::new(fake).exists(),
"build the fake engine first: cargo build -p uci --bin fake_uci"
);
let a = Analyzer::start(fake.into(), String::new(), 1, 16).unwrap();
let start = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1".to_owned();
let first = a.analyze(start.clone(), vec![], 1).unwrap();
let second = a.analyze(start, vec!["e2e4".into()], 1).unwrap();
assert!(second > first);
let mut seen = 0;
for _ in 0..50 {
if let Some(s) = a.poll(100).unwrap() {
assert_eq!(s.search, second);
seen += 1;
}
if seen >= 3 {
break;
}
}
assert!(seen >= 3, "no analysis arrived");
a.stop();
a.quit();
assert!(a.poll(10).is_err());
}
}

View file

@ -10,6 +10,10 @@ use std::sync::atomic::AtomicBool;
use chess_core::{Game, MoveKind, NodeId};
mod engine;
pub use engine::{Analysis, Analyzer, EngineFailure, EngineLine, LineStep};
uniffi::setup_scaffolding!();
/// What went wrong opening or reading a database, in words for the user (the Kotlin
@ -17,14 +21,14 @@ uniffi::setup_scaffolding!();
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum ApiError {
/// The file could not be read.
#[error("{message}")]
Io { message: String },
#[error("{detail}")]
Io { detail: String },
/// A known format that cannot be read (password, ChessBase 2CBH).
#[error("{message}")]
Unsupported { message: String },
#[error("{detail}")]
Unsupported { detail: String },
/// The file is damaged or not a database.
#[error("{message}")]
Corrupt { message: String },
#[error("{detail}")]
Corrupt { detail: String },
/// An archive without a database inside.
#[error("no PGN or ChessBase database in the archive")]
Empty,
@ -38,15 +42,15 @@ impl From<chess_import::ImportError> for ApiError {
use chess_import::ImportError as E;
match e {
E::Io { source, .. } => ApiError::Io {
message: source.to_string(),
detail: source.to_string(),
},
E::Unsupported(u) => ApiError::Unsupported {
message: u.to_string(),
detail: u.to_string(),
},
E::Corrupt(m) => ApiError::Corrupt { message: m },
E::Corrupt(m) => ApiError::Corrupt { detail: m },
E::Empty => ApiError::Empty,
E::Cancelled => ApiError::Io {
message: "cancelled".into(),
detail: "cancelled".into(),
},
}
}
@ -109,10 +113,24 @@ pub struct MoveNode {
pub to_square: Option<String>,
/// A null move ("--") of a study.
pub null_move: bool,
/// The move in UCI notation, for the engine's history; empty for the root and a
/// null move (after which the engine gets the position as a FEN).
pub uci: String,
/// Arrows (`[%cal]`) and marked squares (`[%csl]`) of the comments after the move.
pub marks: Vec<Mark>,
/// On the main line of the game (not inside a variation).
pub main_line: bool,
}
/// An arrow (`to` set) or a marked square of a comment, in a comment color:
/// 'R', 'G', 'B', 'Y' or another letter.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct Mark {
pub color: String,
pub from: String,
pub to: Option<String>,
}
/// A game ready to show.
#[derive(Debug, Clone, PartialEq, uniffi::Record)]
pub struct GameData {
@ -152,7 +170,7 @@ fn read_fd(fd: i32) -> Result<Vec<u8>, ApiError> {
use std::os::fd::FromRawFd;
if fd < 0 {
return Err(ApiError::Io {
message: "no file".into(),
detail: "no file".into(),
});
}
// SAFETY: the caller hands the descriptor over (detached from its owner); the File
@ -160,7 +178,7 @@ fn read_fd(fd: i32) -> Result<Vec<u8>, ApiError> {
let mut file = unsafe { std::fs::File::from_raw_fd(fd) };
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).map_err(|e| ApiError::Io {
message: e.to_string(),
detail: e.to_string(),
})?;
Ok(bytes)
}
@ -256,20 +274,44 @@ impl Database {
.iter()
.skip(start as usize)
.take(count as usize)
.map(|e| {
let tag = |name: &str| e.tag(name).unwrap_or("").to_owned();
GameRow {
index: e.index as u32,
white: tag("White"),
black: tag("Black"),
result: tag("Result"),
date: tag("Date"),
event: tag("Event"),
eco: tag("ECO"),
white_elo: tag("WhiteElo"),
black_elo: tag("BlackElo"),
.map(row)
.collect()
}
/// The numbers of the games whose players, event, site or date contain every word
/// of `text` (case does not matter); all games for an empty text.
pub fn find(&self, text: String) -> Vec<u32> {
let Some(pgn) = self.db.as_pgn() else {
return Vec::new();
};
let words: Vec<String> = text.split_whitespace().map(str::to_lowercase).collect();
pgn.entries()
.iter()
.filter(|e| {
words.is_empty() || {
let hay = ["White", "Black", "Event", "Site", "Date"]
.iter()
.filter_map(|t| e.tag(t))
.collect::<Vec<_>>()
.join(" ")
.to_lowercase();
words.iter().all(|w| hay.contains(w.as_str()))
}
})
.map(|e| e.index as u32)
.collect()
}
/// The rows of these games (from [`Database::find`]), in this order.
pub fn rows_of(&self, indices: Vec<u32>) -> Vec<GameRow> {
let Some(pgn) = self.db.as_pgn() else {
return Vec::new();
};
let entries = pgn.entries();
indices
.iter()
.filter_map(|&i| entries.get(i as usize))
.map(row)
.collect()
}
@ -283,6 +325,58 @@ impl Database {
}
}
fn row(e: &chess_core::GameEntry) -> GameRow {
let tag = |name: &str| e.tag(name).unwrap_or("").to_owned();
GameRow {
index: e.index as u32,
white: tag("White"),
black: tag("Black"),
result: tag("Result"),
date: tag("Date"),
event: tag("Event"),
eco: tag("ECO"),
white_elo: tag("WhiteElo"),
black_elo: tag("BlackElo"),
}
}
fn marks(comments: &[String]) -> Vec<Mark> {
use chess_core::{Annotation, CommentPart, ShapeColor};
let letter = |c: ShapeColor| {
match c {
ShapeColor::Red => 'R',
ShapeColor::Green => 'G',
ShapeColor::Blue => 'B',
ShapeColor::Yellow => 'Y',
ShapeColor::Other(c) => c,
}
.to_string()
};
let mut out = Vec::new();
for c in comments {
for part in chess_core::parse_comment(c) {
match part {
CommentPart::Annotation(Annotation::Arrows(list)) => {
out.extend(list.into_iter().map(|(c, from, to)| Mark {
color: letter(c),
from: from.to_string(),
to: Some(to.to_string()),
}))
}
CommentPart::Annotation(Annotation::Squares(list)) => {
out.extend(list.into_iter().map(|(c, sq)| Mark {
color: letter(c),
from: sq.to_string(),
to: None,
}))
}
_ => {}
}
}
}
out
}
fn game_data(game: &Game, diagnostics: &[chess_core::Diagnostic]) -> GameData {
// Ids in tree order (the root first, each line before its variations' tails), mapped
// to their places in the list.
@ -293,7 +387,12 @@ fn game_data(game: &Game, diagnostics: &[chess_core::Diagnostic]) -> GameData {
order.extend(game.children(id).iter().copied());
i += 1;
}
let place = |id: NodeId| order.iter().position(|x| *x == id).map(|p| p as u32);
let places: std::collections::HashMap<NodeId, u32> = order
.iter()
.enumerate()
.map(|(i, id)| (*id, i as u32))
.collect();
let place = |id: NodeId| places.get(&id).copied();
let main: std::collections::HashSet<NodeId> = game.mainline().into_iter().collect();
let nodes = order
.iter()
@ -336,6 +435,8 @@ fn game_data(game: &Game, diagnostics: &[chess_core::Diagnostic]) -> GameData {
from_square: squares.as_ref().map(|s| s.0.clone()),
to_square: squares.map(|s| s.1),
null_move: mv.is_some_and(|m| matches!(m.kind, MoveKind::Null)),
uci: uci.clone().unwrap_or_default(),
marks: node.map(|n| marks(n.comments_after())).unwrap_or_default(),
main_line: id == NodeId::ROOT || main.contains(&id),
}
})
@ -376,6 +477,44 @@ mod tests {
assert!(db.rows(db.len(), 10).is_empty());
}
#[test]
fn comment_marks_and_uci_moves_reach_the_app() {
let pgn = b"[Event \"t\"]\n\n1. e4 {[%cal Ge2e4,Rd7d5] [%csl Yd5] Center} e5 *\n".to_vec();
let db = open_bytes("t.pgn".into(), pgn).unwrap();
let g = db.game(0).unwrap();
let e4 = &g.nodes[1];
assert_eq!(e4.uci, "e2e4");
assert_eq!(e4.comment_after, "Center");
let m = |c: &str, f: &str, t: Option<&str>| Mark {
color: c.into(),
from: f.into(),
to: t.map(Into::into),
};
assert_eq!(
e4.marks,
vec![
m("G", "e2", Some("e4")),
m("R", "d7", Some("d5")),
m("Y", "d5", None)
]
);
assert!(g.nodes[0].uci.is_empty());
}
#[test]
fn games_are_found_by_words() {
let db = classics();
assert_eq!(db.find(String::new()).len() as u32, db.len());
let morphy = db.find("MORPHY opera".into());
assert!(!morphy.is_empty());
let rows = db.rows_of(morphy.clone());
assert!(
rows.iter()
.all(|r| r.white.contains("Morphy") || r.black.contains("Morphy"))
);
assert!(db.find("no such player anywhere".into()).is_empty());
}
#[test]
fn a_game_is_a_tree_with_positions() {
let db = classics();