Skip to main content

concinnity_host/store/paths/
mod.rs

1//! Project state root: where the engine's state tree is anchored, and the names
2//! of the directories hanging off it.
3//!
4//! Everything the engine writes for a project lives under one state directory:
5//! the compiled blobs (`data/`), the regenerable cache (`cache/`: `0` for the
6//! running application, `1` for a build, the baked asset thumbnails included),
7//! fetched source assets (`assets/`),
8//! named worlds (`worlds/`), the runtime save files (`saves/`), and the
9//! mutable settings file (`settings`).
10//!
11//! Nothing here has a default. A host installs the state directory via
12//! [`set_state_dir`] before anything reads the tree, and until it does every
13//! path below resolves to `None`. The naming of that directory is the host's
14//! business, not this crate's: the dev CLI hides it inside the project, a
15//! shipped application puts it beside its executable, and an embedder points
16//! it wherever its own layout implies. Reads that cannot proceed without a
17//! state tree report [`CnResult::NoStateRoot`](concinnity_core::result::CnResult);
18//! the caches and the settings file simply do nothing.
19//!
20//! The read-only content of the tree (`data/`) and the runtime-writable state
21//! (`saves/` + `settings`) usually share one root, but a shipped application
22//! installed in a read-only location (Program Files) cannot write beside its
23//! data. Such an application installs a separate writable root via
24//! [`set_writable_state_dir`] so only `saves/` and `settings` relocate to a
25//! per-user directory while `data/` stays beside the executable.
26//!
27//! Resolution touches no files: these functions compute paths. Reading the tree
28//! is `super::source` (finding a source asset) and `super::blob` (the compiled
29//! blob).
30
31use std::path::{Path, PathBuf};
32
33mod root;
34
35pub use root::{
36    clear_state_dir, clear_writable_state_dir, set_state_dir, set_writable_state_dir, state_dir,
37    writable_state_dir,
38};
39
40/// The state root's `assets/` directory.
41pub fn assets_dir() -> Option<PathBuf> {
42    state_dir().map(|d| d.join("assets"))
43}
44
45/// The state root's `data/` directory.
46pub fn data_dir() -> Option<PathBuf> {
47    state_dir().map(|d| d.join("data"))
48}
49
50/// Directory holding the runtime save files (`auto`, `save1` ..). Created on
51/// first write by the running application, never by a build. Resolves under the
52/// writable-state dir, which is the content root unless an application redirected it.
53pub fn saves_dir() -> Option<PathBuf> {
54    writable_state_dir().map(|d| d.join("saves"))
55}
56
57/// Sandboxed sibling of [saves_dir] for preview sessions (see the
58/// `TransientSaves` protocol resource): the save UI keeps working against this
59/// directory, but the real saves are never touched and the sandbox is wiped at
60/// each session start.
61pub fn preview_saves_dir() -> Option<PathBuf> {
62    writable_state_dir().map(|d| d.join("preview-saves"))
63}
64
65/// The mutable settings file (CBOR). Written by the in-engine settings menu,
66/// never by a build. A sibling of `data/` in the common case, or under the
67/// writable-state dir when a read-only install redirected it.
68pub fn settings_path() -> Option<PathBuf> {
69    writable_state_dir().map(|d| d.join("settings"))
70}
71
72/// Directory holding crash reports (and minidumps) written by the crash
73/// reporting machinery. Resolves under the writable-state dir like `saves/`,
74/// since a shipped install's content root may be read-only. Created on first
75/// write; capped by the writer's retention pruning, never by a build.
76pub fn crashes_dir() -> Option<PathBuf> {
77    writable_state_dir().map(|d| d.join("crashes"))
78}
79
80/// The state root's `worlds/` directory.
81pub fn worlds_dir() -> Option<PathBuf> {
82    state_dir().map(|d| d.join("worlds"))
83}
84
85/// The runtime cache segment inside `state_dir`, for a caller naming a state
86/// tree other than the installed one: `cn export` warms the segment it writes
87/// into a bundle before that bundle is ever launched.
88pub fn runtime_cache_in(state_dir: &Path) -> PathBuf {
89    state_dir.join("cache").join("0")
90}
91
92/// The runtime cache segment, `cache/0`: one container holding every
93/// regenerable artifact the running application produces for its own later
94/// launches, indexed by producer and key. Resolves under the writable-state
95/// dir, since a shipped install's content root may be read-only.
96///
97/// Deletable at any time; whatever is missing is recomputed. The running
98/// application writes this file and no other, so a concurrent build writing a
99/// segment of its own never shares a file with it.
100pub fn runtime_cache_path() -> Option<PathBuf> {
101    writable_state_dir().map(|d| runtime_cache_in(&d))
102}
103
104/// The runtime cache segment a bundle ships, read-only. `cn export` warms it
105/// with the shader binaries a first launch would otherwise compile; because
106/// those artifacts are backend IR (DXBC / SPIR-V) rather than machine code, one
107/// warmed at package time is valid on any machine.
108///
109/// Resolves against the content root, so it stays readable on a read-only
110/// install. That is also the only layout where this differs from
111/// [`runtime_cache_path`]: a bundle the player can write to has one segment
112/// serving both roles.
113pub fn bundled_runtime_cache_path() -> Option<PathBuf> {
114    state_dir().map(|d| runtime_cache_in(&d))
115}
116
117/// The build cache segment, `cache/1`: one container holding every payload,
118/// expansion, and baked thumbnail a cook produced, indexed by producer and
119/// key. A build writes this
120/// file and no other, so a cook running against a live application never
121/// shares a file with the segment that application writes.
122///
123/// Resolves against the content root rather than the writable one: a build
124/// writes the `data/` beside it, so a tree it cannot write is a tree it cannot
125/// cook into either.
126///
127/// Deletable at any time; whatever is missing is recompiled.
128pub fn build_cache_path() -> Option<PathBuf> {
129    state_dir().map(|d| d.join("cache").join("1"))
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135
136    // Exercises the process-global roots end to end. The single test that drives
137    // the globals, so its mutations never race another test that reads them.
138    #[test]
139    fn installed_roots_redirect_every_state_dir() {
140        let flat = Path::new("/tmp/flat-probe");
141        set_state_dir(flat);
142        assert_eq!(state_dir().as_deref(), Some(flat));
143        assert_eq!(data_dir().unwrap(), flat.join("data"));
144        assert_eq!(runtime_cache_path().unwrap(), flat.join("cache").join("0"));
145        assert_eq!(build_cache_path().unwrap(), flat.join("cache").join("1"));
146        assert_eq!(assets_dir().unwrap(), flat.join("assets"));
147        assert_eq!(worlds_dir().unwrap(), flat.join("worlds"));
148        // With no writable override, writable state stays beside the data.
149        assert_eq!(writable_state_dir().as_deref(), Some(flat));
150        assert_eq!(saves_dir().unwrap(), flat.join("saves"));
151        assert_eq!(settings_path().unwrap(), flat.join("settings"));
152        assert_eq!(crashes_dir().unwrap(), flat.join("crashes"));
153
154        // A writable override relocates only the runtime-writable state
155        // (`saves/`, `settings`, `crashes/`); `data/` (and assets/worlds) stay
156        // at the content root, and so does the segment a build writes.
157        let writable = Path::new("/tmp/per-user-probe");
158        set_writable_state_dir(writable);
159        assert_eq!(writable_state_dir().as_deref(), Some(writable));
160        assert_eq!(saves_dir().unwrap(), writable.join("saves"));
161        assert_eq!(settings_path().unwrap(), writable.join("settings"));
162        assert_eq!(crashes_dir().unwrap(), writable.join("crashes"));
163        assert_eq!(
164            runtime_cache_path().unwrap(),
165            writable.join("cache").join("0")
166        );
167        assert_eq!(build_cache_path().unwrap(), flat.join("cache").join("1"));
168        // The bundle's warmed segment stays with the content, which is what
169        // makes a read-only install's shipped artifacts still readable.
170        assert_eq!(
171            bundled_runtime_cache_path().unwrap(),
172            flat.join("cache").join("0")
173        );
174        assert_eq!(data_dir().unwrap(), flat.join("data"));
175        clear_writable_state_dir();
176        assert_eq!(saves_dir().unwrap(), flat.join("saves"));
177
178        // With nothing installed there is no state tree at all: no guess
179        // against the working directory, so a library writes nowhere.
180        clear_state_dir();
181        assert_eq!(state_dir(), None);
182        for path in [
183            data_dir(),
184            assets_dir(),
185            worlds_dir(),
186            saves_dir(),
187            preview_saves_dir(),
188            settings_path(),
189            crashes_dir(),
190            runtime_cache_path(),
191            bundled_runtime_cache_path(),
192            build_cache_path(),
193            writable_state_dir(),
194        ] {
195            assert_eq!(path, None);
196        }
197    }
198}