flatland-client-lib 0.2.16

Flatland3 remote game client library (TCP session, bots, game state)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Gfx sprite bundle sync for installed clients (Firebase Storage / GCS).

use std::collections::BTreeMap;
use std::io::Read;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

pub const DEFAULT_FIREBASE_BUCKET: &str = "flatland-8911e.appspot.com";
pub const ASSETS_INDEX_OBJECT: &str = "flatland3/client-assets/latest.json";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AssetFileEntry {
    pub sha256: String,
    pub size: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AssetBundleIndex {
    /// Stable revision from `assets/.content-publish.json` (not process-local `content_rev`).
    pub publish_rev: u64,
    pub published_at: String,
    pub files: BTreeMap<String, AssetFileEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LocalAssetState {
    pub publish_rev: u64,
    pub sprites_dir: PathBuf,
}

pub fn assets_root_dir() -> anyhow::Result<PathBuf> {
    let base = dirs::home_dir().ok_or_else(|| anyhow::anyhow!("home directory not found"))?;
    Ok(base.join(".flatland3").join("assets"))
}

pub fn local_state_path() -> anyhow::Result<PathBuf> {
    Ok(assets_root_dir()?.join("state.json"))
}

pub fn read_local_state() -> Option<LocalAssetState> {
    let path = local_state_path().ok()?;
    let bytes = std::fs::read(path).ok()?;
    serde_json::from_slice(&bytes).ok()
}

pub fn write_local_state(state: &LocalAssetState) -> anyhow::Result<()> {
    let path = local_state_path()?;
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(path, serde_json::to_vec_pretty(state)?)?;
    Ok(())
}

pub fn sprites_dir_for_rev(publish_rev: u64) -> anyhow::Result<PathBuf> {
    Ok(assets_root_dir()?.join(format!("rev-{publish_rev}")))
}

pub fn current_symlink_path() -> anyhow::Result<PathBuf> {
    Ok(assets_root_dir()?.join("current"))
}

pub fn activate_rev_dir(rev_dir: &Path) -> anyhow::Result<()> {
    let link = current_symlink_path()?;
    if link.exists() {
        std::fs::remove_file(&link).or_else(|_| std::fs::remove_dir_all(&link))?;
    }
    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(rev_dir, &link)?;
    }
    #[cfg(not(unix))]
    {
        // Windows: record the active rev path beside `current` (no symlink required).
        let marker = link.with_extension("path");
        std::fs::write(&marker, rev_dir.to_string_lossy().as_bytes())?;
        let _ = std::fs::create_dir_all(&link);
        let _ = std::fs::copy(rev_dir.join("manifest.yaml"), link.join("manifest.yaml"));
    }
    Ok(())
}

pub fn find_repo_sprites_dir() -> Option<PathBuf> {
    let mut dir = std::env::current_dir().ok()?;
    for _ in 0..8 {
        let candidate = dir.join("assets/gfx/sprites");
        if candidate.join("manifest.yaml").is_file() {
            return Some(candidate);
        }
        if !dir.pop() {
            break;
        }
    }
    None
}

/// True when the checkout sprites should win over a synced `~/.flatland3` cache.
pub(crate) fn repo_sprites_outrank_cache(repo_rev: Option<u64>, local_rev: Option<u64>) -> bool {
    match (repo_rev, local_rev) {
        (Some(rr), Some(lr)) => rr >= lr,
        (Some(_), None) => true,
        _ => false,
    }
}

/// Prefer the repo checkout when it is at least as new as the synced cache.
///
/// Local play often has a stale `~/.flatland3/assets/rev-N` from an older sync.
/// `needs_asset_sync` already treats a matching repo as up to date, but loading
/// still went through the stale cache — so new sheets (e.g. lodging) fell back
/// to `other.default` after publish.
pub fn resolve_sprites_dir() -> Option<PathBuf> {
    if let Ok(p) = std::env::var("FLATLAND_SPRITES_DIR") {
        let path = PathBuf::from(p);
        if path.join("manifest.yaml").is_file() {
            return Some(path);
        }
    }

    let repo = find_repo_sprites_dir();
    let repo_rev = read_repo_publish_rev();
    let local = read_local_state().filter(|s| s.sprites_dir.join("manifest.yaml").is_file());
    let local_rev = local.as_ref().map(|s| s.publish_rev);

    if repo_sprites_outrank_cache(repo_rev, local_rev) {
        if let Some(repo_dir) = repo {
            return Some(repo_dir);
        }
    }
    if let Some(local) = local {
        return Some(local.sprites_dir);
    }
    if let Some(repo_dir) = repo {
        return Some(repo_dir);
    }

    let current = current_symlink_path().ok()?;
    if current.join("manifest.yaml").is_file() {
        return Some(current);
    }
    None
}

pub fn assets_index_url() -> String {
    std::env::var("FLATLAND_ASSETS_INDEX_URL").unwrap_or_else(|_| {
        firebase_download_url(DEFAULT_FIREBASE_BUCKET, ASSETS_INDEX_OBJECT)
    })
}

pub fn firebase_download_url(bucket: &str, object_path: &str) -> String {
    let encoded = urlencoding_encode(object_path);
    format!(
        "https://firebasestorage.googleapis.com/v0/b/{bucket}/o/{encoded}?alt=media"
    )
}

pub fn assets_storage_prefix() -> String {
    std::env::var("FLATLAND_ASSETS_PREFIX")
        .unwrap_or_else(|_| "flatland3/client-assets".to_string())
}

pub fn firebase_object_path_for_prefix(prefix: &str, publish_rev: u64, relative: &str) -> String {
    format!("{prefix}/rev-{publish_rev}/{relative}")
}

pub fn firebase_object_path(publish_rev: u64, relative: &str) -> String {
    firebase_object_path_for_prefix(&assets_storage_prefix(), publish_rev, relative)
}

fn urlencoding_encode(path: &str) -> String {
    path.bytes()
        .map(|b| match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                (b as char).to_string()
            }
            _ => format!("%{b:02X}"),
        })
        .collect()
}

pub fn sha256_file(path: &Path) -> anyhow::Result<String> {
    let mut file = std::fs::File::open(path)?;
    let mut hasher = Sha256::new();
    let mut buf = [0u8; 8192];
    loop {
        let n = file.read(&mut buf)?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }
    Ok(hex::encode(hasher.finalize()))
}

/// Walk `assets/gfx/sprites` and build a publish manifest (relative paths → hash).
pub fn build_bundle_index(sprites_dir: &Path, publish_rev: u64, published_at: &str) -> anyhow::Result<AssetBundleIndex> {
    let mut files = BTreeMap::new();
    walk_sprites_dir(sprites_dir, sprites_dir, &mut files)?;
    Ok(AssetBundleIndex {
        publish_rev,
        published_at: published_at.to_string(),
        files,
    })
}

fn walk_sprites_dir(
    root: &Path,
    dir: &Path,
    files: &mut BTreeMap<String, AssetFileEntry>,
) -> anyhow::Result<()> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            let name = entry.file_name();
            if name == "previews" {
                continue;
            }
            walk_sprites_dir(root, &path, files)?;
            continue;
        }
        let rel = path
            .strip_prefix(root)?
            .to_string_lossy()
            .replace('\\', "/");
        if rel.starts_with('.') || rel.contains("/.") {
            continue;
        }
        if rel.ends_with(".html") {
            continue;
        }
        let meta = std::fs::metadata(&path)?;
        files.insert(
            rel,
            AssetFileEntry {
                sha256: sha256_file(&path)?,
                size: meta.len(),
            },
        );
    }
    Ok(())
}

pub struct AssetSyncOptions {
    pub index_url: String,
    pub storage_prefix: String,
    pub target_rev: Option<u64>,
    pub quiet: bool,
}

/// Outcome of [`sync_assets`] for logging / HUD toasts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssetSyncKind {
    /// Downloaded or verified files against remote `latest.json`.
    Remote,
    /// Remote index missing; reused `~/.flatland3` cache.
    LocalCacheNoRemote,
    /// Remote index missing; using repo `assets/gfx/sprites` (dev checkout).
    RepoDevNoRemote,
}

#[derive(Debug, Clone)]
pub struct AssetSyncResult {
    pub state: LocalAssetState,
    pub kind: AssetSyncKind,
}

impl Default for AssetSyncOptions {
    fn default() -> Self {
        Self {
            index_url: assets_index_url(),
            storage_prefix: assets_storage_prefix(),
            target_rev: None,
            quiet: false,
        }
    }
}

pub async fn sync_assets(opts: AssetSyncOptions) -> anyhow::Result<AssetSyncResult> {
    // Local checkout: prefer repo sprites and skip remote download so gfx play does not
    // freeze on Firebase after login (no render loop until sync finishes).
    if std::env::var_os("FLATLAND_FORCE_ASSET_SYNC").is_none() {
        if let Some(repo_dir) = find_repo_sprites_dir() {
            if let Some(publish_rev) = read_repo_publish_rev() {
                if opts.target_rev.map_or(true, |t| publish_rev >= t) {
                    if !opts.quiet {
                        println!(
                            "Using repo sprites at {} (rev {publish_rev}); set FLATLAND_FORCE_ASSET_SYNC=1 to pull remote.",
                            repo_dir.display()
                        );
                    }
                    return Ok(AssetSyncResult {
                        state: LocalAssetState {
                            publish_rev,
                            sprites_dir: repo_dir,
                        },
                        kind: AssetSyncKind::RepoDevNoRemote,
                    });
                }
            }
        }
    }

    let client = reqwest::Client::builder()
        .user_agent(format!("flatland-client-lib/{}", env!("CARGO_PKG_VERSION")))
        .connect_timeout(std::time::Duration::from_secs(5))
        .timeout(std::time::Duration::from_secs(30))
        .build()?;
    let response = client.get(&opts.index_url).send().await?;
    if response.status() == reqwest::StatusCode::NOT_FOUND {
        return sync_without_remote_index(opts);
    }
    let index: AssetBundleIndex = response.error_for_status()?.json().await?;
    sync_from_index(&client, opts, index).await
}

fn sync_without_remote_index(opts: AssetSyncOptions) -> anyhow::Result<AssetSyncResult> {
    if let Some(local) = read_local_state() {
        if local.sprites_dir.join("manifest.yaml").is_file() {
            if !opts.quiet {
                println!(
                    "Remote latest.json not found; using cached publish rev {}.",
                    local.publish_rev
                );
            }
            return Ok(AssetSyncResult {
                state: local,
                kind: AssetSyncKind::LocalCacheNoRemote,
            });
        }
    }
    if let Some(repo) = find_repo_sprites_dir() {
        let publish_rev = read_repo_publish_rev().unwrap_or(0);
        if !opts.quiet {
            println!(
                "Remote latest.json not found; using repo sprites at {} (rev {publish_rev}).",
                repo.display()
            );
        }
        return Ok(AssetSyncResult {
            state: LocalAssetState {
                publish_rev,
                sprites_dir: repo,
            },
            kind: AssetSyncKind::RepoDevNoRemote,
        });
    }
    anyhow::bail!(
        "Gfx asset bundle not published yet (HTTP 404 on latest.json). \
         Server admin: flatland-admin content publish"
    );
}

async fn sync_from_index(
    client: &reqwest::Client,
    opts: AssetSyncOptions,
    index: AssetBundleIndex,
) -> anyhow::Result<AssetSyncResult> {
    let publish_rev = opts.target_rev.unwrap_or(index.publish_rev);
    if publish_rev != index.publish_rev {
        anyhow::bail!(
            "requested publish rev {publish_rev} but remote latest is {}",
            index.publish_rev
        );
    }

    if let Some(local) = read_local_state() {
        if local.publish_rev == publish_rev && local.sprites_dir.join("manifest.yaml").is_file() {
            if verify_rev_dir(&local.sprites_dir, &index)? {
                if !opts.quiet {
                    println!("Assets up to date (publish rev {publish_rev}).");
                }
                return Ok(AssetSyncResult {
                    state: local,
                    kind: AssetSyncKind::Remote,
                });
            }
        }
    }

    let rev_dir = sprites_dir_for_rev(publish_rev)?;
    std::fs::create_dir_all(&rev_dir)?;

    let total = index.files.len();
    let mut done = 0usize;
    for (rel, entry) in &index.files {
        let dest = rev_dir.join(rel);
        if dest.is_file() {
            if sha256_file(&dest)? == entry.sha256 {
                done += 1;
                continue;
            }
        }
        if let Some(parent) = dest.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let url = firebase_download_url(
            DEFAULT_FIREBASE_BUCKET,
            &firebase_object_path_for_prefix(&opts.storage_prefix, publish_rev, rel),
        );
        let bytes = client.get(&url).send().await?.error_for_status()?.bytes().await?;
        if sha256_bytes(&bytes) != entry.sha256 {
            anyhow::bail!("hash mismatch for {rel} after download");
        }
        std::fs::write(&dest, &bytes)?;
        done += 1;
        if !opts.quiet {
            println!("  [{done}/{total}] {rel}");
        }
    }

    activate_rev_dir(&rev_dir)?;
    let state = LocalAssetState {
        publish_rev,
        sprites_dir: rev_dir,
    };
    write_local_state(&state)?;
    if !opts.quiet {
        println!(
            "Synced publish rev {publish_rev} → {}",
            state.sprites_dir.display()
        );
    }
    Ok(AssetSyncResult {
        state,
        kind: AssetSyncKind::Remote,
    })
}

pub fn needs_asset_sync(server_publish_rev: u64) -> bool {
    if server_publish_rev == 0 {
        return false;
    }
    if let Some(local) = read_local_state() {
        if local.publish_rev >= server_publish_rev
            && local.sprites_dir.join("manifest.yaml").is_file()
        {
            return false;
        }
    }
    if let Some(repo_rev) = read_repo_publish_rev() {
        if repo_rev >= server_publish_rev && find_repo_sprites_dir().is_some() {
            return false;
        }
    }
    resolve_sprites_dir().is_none()
}

/// Publish rev from `assets/.content-publish.json` when running from a dev checkout.
pub fn read_repo_publish_rev() -> Option<u64> {
    let sprites = find_repo_sprites_dir()?;
    let mut dir = sprites.as_path();
    for _ in 0..8 {
        let marker = dir.join(".content-publish.json");
        if marker.is_file() {
            #[derive(Deserialize)]
            struct Marker {
                rev: u64,
            }
            let text = std::fs::read_to_string(marker).ok()?;
            let m: Marker = serde_json::from_str(&text).ok()?;
            return Some(m.rev);
        }
        dir = dir.parent()?;
    }
    None
}

fn verify_rev_dir(dir: &Path, index: &AssetBundleIndex) -> anyhow::Result<bool> {
    for (rel, entry) in &index.files {
        let path = dir.join(rel);
        if !path.is_file() {
            return Ok(false);
        }
        if sha256_file(&path)? != entry.sha256 {
            return Ok(false);
        }
    }
    Ok(true)
}

fn sha256_bytes(bytes: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(bytes);
    hex::encode(hasher.finalize())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn firebase_url_encodes_slashes() {
        let url = firebase_download_url("bucket", "flatland3/client-assets/latest.json");
        assert!(url.contains("%2F"));
        assert!(url.contains("alt=media"));
    }

    #[test]
    fn build_index_hashes_files() {
        let dir = std::env::temp_dir().join(format!("flatland-assets-test-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("cells")).expect("dir");
        fs::write(dir.join("manifest.yaml"), "version: 1\n").expect("write");
        fs::write(dir.join("cells/a.png"), b"png").expect("write");
        let index = build_bundle_index(&dir, 3, "2026-01-01T00:00:00Z").expect("index");
        assert_eq!(index.publish_rev, 3);
        assert_eq!(index.files.len(), 2);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn needs_sync_false_when_repo_matches_server_rev() {
        if read_repo_publish_rev().is_some() && find_repo_sprites_dir().is_some() {
            let rev = read_repo_publish_rev().unwrap();
            assert!(!needs_asset_sync(rev));
        }
    }

    #[test]
    fn repo_outranks_stale_synced_cache() {
        assert!(repo_sprites_outrank_cache(Some(85), Some(75)));
        assert!(repo_sprites_outrank_cache(Some(75), Some(75)));
        assert!(!repo_sprites_outrank_cache(Some(70), Some(75)));
        assert!(repo_sprites_outrank_cache(Some(85), None));
        assert!(!repo_sprites_outrank_cache(None, Some(75)));
    }
}