Add collaborative authored-file safety

This commit is contained in:
Rbanh committed 2026-07-12 11:27:57 -04:00
1 parent b4aa61e394
commit 5a82a9ee29
23 files changed
+2857 -89

No files matched your search

+29 -3
View File
@@ -227,17 +227,22 @@ impl Default for InputSettings {
/// Loads settings from disk, falling back to defaults.
pub fn load_project_settings_from_path(path: &str) -> ProjectSettings {
load_project_settings_with_source(path).0
}
/// Loads settings plus the exact source text used when the file was readable.
pub fn load_project_settings_with_source(path: &str) -> (ProjectSettings, Option<String>) {
match std::fs::read_to_string(path) {
Ok(contents) => match ron::from_str(&contents) {
Ok(settings) => settings,
Ok(settings) => (settings, Some(contents)),
Err(error) => {
warn!("Failed to parse {path}: {error}; using defaults");
ProjectSettings::default()
(ProjectSettings::default(), Some(contents))
}
},
Err(error) => {
warn!("Could not read {path}: {error}; using defaults");
ProjectSettings::default()
(ProjectSettings::default(), None)
}
}
}
@@ -266,6 +271,27 @@ mod tests {
assert_eq!(parsed.rendering.gi_mode, settings.rendering.gi_mode);
}
#[test]
fn settings_load_returns_the_exact_source_for_revision_guards() {
let root = std::env::temp_dir().join(format!(
"blacksite-settings-source-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let path = root.join("project.ron");
std::fs::create_dir_all(&root).unwrap();
let source = save_project_settings_to_string(&ProjectSettings::default()).unwrap();
std::fs::write(&path, &source).unwrap();
let (settings, loaded_source) = load_project_settings_with_source(path.to_str().unwrap());
assert_eq!(settings.name, ProjectSettings::default().name);
assert_eq!(loaded_source.as_deref(), Some(source.as_str()));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn legacy_manifest_defaults_project_identity_fields() {
let legacy = r#"(
+13 -5
View File
@@ -1,7 +1,7 @@
use bevy::prelude::*;
use crate::{
load_project_settings_from_path, save_project_settings_to_string, PhysicsSettings,
load_project_settings_with_source, save_project_settings_to_string, PhysicsSettings,
ProjectSettings, DEFAULT_PROJECT_PATH,
};
@@ -56,6 +56,8 @@ impl SimTuning {
pub struct ProjectSettingsIo {
pub path: String,
pub dirty: bool,
/// Exact source loaded or last saved by this process; collaboration guards hash it.
pub loaded_source: Option<String>,
}
impl Default for ProjectSettingsIo {
@@ -63,6 +65,7 @@ impl Default for ProjectSettingsIo {
Self {
path: DEFAULT_PROJECT_PATH.into(),
dirty: false,
loaded_source: None,
}
}
}
@@ -91,8 +94,9 @@ impl Plugin for ProjectSettingsPlugin {
}
}
fn load_settings(mut commands: Commands, io: Res<ProjectSettingsIo>) {
let settings = load_project_settings_from_path(&io.path);
fn load_settings(mut commands: Commands, mut io: ResMut<ProjectSettingsIo>) {
let (settings, source) = load_project_settings_with_source(&io.path);
io.loaded_source = source;
let tuning = SimTuning::from_physics(&settings.physics);
commands.insert_resource(settings);
commands.insert_resource(tuning);
@@ -103,13 +107,17 @@ pub fn sync_sim_tuning(settings: &ProjectSettings, mut tuning: ResMut<SimTuning>
*tuning = SimTuning::from_physics(&settings.physics);
}
/// Persists settings to the path in [`ProjectSettingsIo`].
/// Low-level unconditional persistence for non-editor callers.
///
/// The editor uses its guarded authored-file publication path instead so external revisions cannot
/// be overwritten.
pub fn save_project_settings(
settings: &ProjectSettings,
io: &mut ProjectSettingsIo,
) -> Result<(), String> {
let text = save_project_settings_to_string(settings).map_err(|error| error.to_string())?;
std::fs::write(&io.path, text).map_err(|error| error.to_string())?;
std::fs::write(&io.path, &text).map_err(|error| error.to_string())?;
io.loaded_source = Some(text);
io.dirty = false;
Ok(())
}