Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Writes synced settings and launcher overrides before Minecraft starts.

use super::options_file::{
GameOptionsDocument, options_path, read_document, sha1_bytes,
validate_raw_key_value,
GameOptionsDocument, options_path, read_document, validate_raw_key_value,
};
use super::write_shared_settings::{
apply_shared_settings_to_instance, remember_processed_sha,
apply_shared_settings_to_instance, remember_synced_file,
sync_is_active_for_instance,
};
use crate::ErrorKind;
Expand Down Expand Up @@ -68,10 +67,9 @@ pub async fn apply_launcher_overrides(
document.set(key, value, true)?;
}
let output_bytes = document.serialize()?;
let output_sha1 = sha1_bytes(&output_bytes);
if output_bytes != input_bytes {
io::write(&path, output_bytes).await?;
io::write(&path, &output_bytes).await?;
}
remember_processed_sha(instance_id, &output_sha1, &state).await?;
remember_synced_file(instance_id, &output_bytes, &state).await?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::fullscreen::update_app_fullscreen_setting;
use super::launch_overrides::currently_launcher_owned_keys;
use super::options_file::{GameOptionsDocument, validate_raw_key_value};
use crate::state::{
CanonicalValue, GameOptionKind, InstanceMetadata, State,
CanonicalValue, GameOptionKind, InstanceMetadata, State, StoredOption,
load_game_option_preferences, load_game_options_sync_state,
load_shared_game_options,
};
Expand All @@ -18,11 +18,69 @@ pub(super) fn custom_setting_id(key: &str) -> String {
format!("external:{key}")
}

#[derive(Clone, Copy)]
pub(super) enum InstanceEdits<'a> {
Live(Option<&'a GameOptionsDocument>),
Offline(&'a GameOptionsDocument),
}

impl<'a> InstanceEdits<'a> {
fn last_synced(self) -> Option<&'a GameOptionsDocument> {
match self {
Self::Live(last_synced) => last_synced,
Self::Offline(last_synced) => Some(last_synced),
}
}
}

fn decode_instance_value(
definition: Option<&SupportedSetting>,
stored: &StoredOption,
key: &str,
raw: &str,
game_version: &str,
) -> Option<CanonicalValue> {
if let Some(definition) = definition {
decode_value_for_version(definition, key, raw, game_version)
} else if matches!(stored.kind, GameOptionKind::External)
&& validate_raw_key_value(key, raw).is_ok()
{
Some(CanonicalValue::ExternalRaw(raw.to_string()))
} else {
None
}
}

fn shared_value_unchanged_since(
last_synced: &GameOptionsDocument,
definition: Option<&SupportedSetting>,
stored: &StoredOption,
game_version: &str,
) -> bool {
let key = match definition {
Some(definition) => {
observed_physical_key(definition, last_synced, game_version)
}
None => stored.raw_key.clone(),
};
let synced = key.and_then(|key| {
let raw = last_synced.value(&key)?;
decode_instance_value(definition, stored, &key, raw, game_version)
});
synced.is_some_and(|synced| {
stored
.value
.as_ref()
.is_some_and(|current| canonical_values_equal(current, &synced))
})
}

/// Imports every enabled setting present in `options.txt`. The caller skips file
/// hashes produced by app writes.
pub(super) async fn read_instance_changes_into_shared_settings(
metadata: &InstanceMetadata,
document: &GameOptionsDocument,
edits: InstanceEdits<'_>,
state: &State,
) -> crate::Result<bool> {
let values = load_shared_game_options(&state.pool).await?;
Expand Down Expand Up @@ -75,22 +133,29 @@ pub(super) async fn read_instance_changes_into_shared_settings(
let Some(current_raw) = document.value(&current_key) else {
continue;
};
let candidate = if let Some(definition) = definition {
decode_value_for_version(
if edits
.last_synced()
.is_some_and(|base| base.value(&current_key) == Some(current_raw))
{
continue;
}
if let InstanceEdits::Offline(base) = edits
&& !shared_value_unchanged_since(
base,
definition,
&current_key,
current_raw,
stored,
&metadata.applied_content_set.game_version,
)
} else if matches!(stored.kind, GameOptionKind::External) {
if validate_raw_key_value(&current_key, current_raw).is_err() {
continue;
}
Some(CanonicalValue::ExternalRaw(current_raw.to_string()))
} else {
None
};
let Some(candidate) = candidate else {
{
continue;
}
let Some(candidate) = decode_instance_value(
definition,
stored,
&current_key,
current_raw,
&metadata.applied_content_set.game_version,
) else {
continue;
};
if stored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use super::options_file::{
GameOptionsDocument, options_path, read_document, sha1_bytes,
};
use super::pack_updates::materialize_pack_options_if_missing;
use super::read_instance_changes::read_instance_changes_into_shared_settings;
use super::read_instance_changes::{
InstanceEdits, read_instance_changes_into_shared_settings,
};
use super::source_selection::initialize_from_source_instance;
use crate::state::{
CanonicalValue, InstanceMetadata, State, SyncedOption,
Expand Down Expand Up @@ -38,9 +40,9 @@ pub(super) async fn sync_is_active_for_instance(
&& game_options_sync_is_enabled(&state.pool).await?)
}

pub(super) async fn remember_processed_sha(
pub(super) async fn remember_synced_file(
instance_id: &str,
sha1: &str,
contents: &[u8],
state: &State,
) -> crate::Result<()> {
let (canonical_revision, _) =
Expand All @@ -49,8 +51,8 @@ pub(super) async fn remember_processed_sha(
instance_id,
SyncedOption::GameOptions,
"default",
sha1,
None,
&sha1_bytes(contents),
Some(contents),
canonical_revision as i64,
state,
)
Expand Down Expand Up @@ -175,6 +177,65 @@ async fn apply_shared_settings_to_document(
})
}

async fn record_unsynced_changes(
metadata: &InstanceMetadata,
document: &GameOptionsDocument,
contents: &[u8],
state: &State,
) -> crate::Result<bool> {
let Some(checkpoint) = synced_options::checkpoint(
&metadata.instance.id,
SyncedOption::GameOptions,
"default",
state,
)
.await?
else {
return Ok(false);
};
if checkpoint.expected_sha1 == sha1_bytes(contents)
|| !crate::state::shared_game_options_exist(&state.pool).await?
{
return Ok(false);
}
let Some(last_synced) =
last_synced_document(checkpoint.merge_base.as_deref())
else {
return Ok(false);
};
read_instance_changes_into_shared_settings(
metadata,
document,
InstanceEdits::Offline(&last_synced),
state,
)
.await
}

async fn import_unsynced_instance_changes(
metadata: &InstanceMetadata,
state: &State,
) -> crate::Result<bool> {
if synced_options::sync_files_are_protected(metadata)
|| !sync_is_active_for_instance(metadata, state).await?
{
return Ok(false);
}
let path = options_path(metadata, state);
if !path.exists() {
return Ok(false);
}
let (document, contents) = read_document(&path).await?;
if document.value("version").is_none()
&& !supported_settings_cover_game_version(
&metadata.applied_content_set.game_version,
)
{
return Ok(false);
}
record_unsynced_changes(metadata, &document, &contents, state).await
}

pub(super) async fn apply_shared_settings_to_instance(
metadata: &InstanceMetadata,
state: &State,
Expand Down Expand Up @@ -209,6 +270,10 @@ pub(super) async fn apply_shared_settings_to_instance(
{
return Ok(SyncOutcome::Deferred);
}
if !file_was_missing {
record_unsynced_changes(metadata, &document, &input_bytes, state)
.await?;
Comment thread
IMB11 marked this conversation as resolved.
Resolved
}
let applied =
apply_shared_settings_to_document(metadata, &mut document, state)
.await?;
Expand All @@ -217,12 +282,11 @@ pub(super) async fn apply_shared_settings_to_instance(
}

let output_bytes = document.serialize()?;
let output_sha1 = sha1_bytes(&output_bytes);
let changed = file_was_missing || output_bytes != input_bytes;
if changed {
io::write(&path, output_bytes).await?;
io::write(&path, &output_bytes).await?;
}
remember_processed_sha(&metadata.instance.id, &output_sha1, state).await?;
remember_synced_file(&metadata.instance.id, &output_bytes, state).await?;

Ok(if changed && applied.used_migration {
SyncOutcome::Migrated
Expand Down Expand Up @@ -268,14 +332,22 @@ pub(super) async fn capture_instance_options(
let canonical_changed =
if crate::state::shared_game_options_exist(&state.pool).await? {
read_instance_changes_into_shared_settings(
metadata, &document, state,
metadata,
&document,
InstanceEdits::Live(
last_synced_document(checkpoint.as_ref().and_then(
|checkpoint| checkpoint.merge_base.as_deref(),
))
.as_ref(),
),
state,
)
.await?
} else {
initialize_from_source_instance(metadata, state).await?;
true
};
remember_processed_sha(&metadata.instance.id, &input_sha1, state).await?;
remember_synced_file(&metadata.instance.id, &input_bytes, state).await?;

if canonical_changed {
let _ = sync_other_participating_instances(
Expand All @@ -287,6 +359,12 @@ pub(super) async fn capture_instance_options(
Ok(SyncOutcome::Unchanged)
}

fn last_synced_document(
contents: Option<&[u8]>,
) -> Option<GameOptionsDocument> {
contents.and_then(|contents| GameOptionsDocument::parse(contents).ok())
}

pub(in crate::api::instance) async fn capture_instance_file_change(
metadata: &InstanceMetadata,
state: &State,
Expand Down Expand Up @@ -337,13 +415,30 @@ pub(super) async fn sync_other_participating_instances(
return result;
}
};
for metadata in instances {
if excluded_instance_id == Some(metadata.instance.id.as_str())
|| !metadata.synced_options.game_options
let instances = instances
.into_iter()
.filter(|metadata| metadata.synced_options.game_options)
.collect::<Vec<_>>();
let mut imported = false;
for metadata in &instances {
if excluded_instance_id == Some(metadata.instance.id.as_str()) {
continue;
}
match import_unsynced_instance_changes(metadata, state).await {
Ok(changed) => imported |= changed,
Err(error) => tracing::warn!(
"Failed to read unsynced game options from {}: {error}",
metadata.instance.id
),
}
}
for metadata in &instances {
if !imported
&& excluded_instance_id == Some(metadata.instance.id.as_str())
{
continue;
}
match apply_shared_settings_to_instance(&metadata, state, false).await {
match apply_shared_settings_to_instance(metadata, state, false).await {
Ok(SyncOutcome::Applied | SyncOutcome::Unchanged) => {
result.applied += 1
}
Expand Down
Loading