use elemental_driver::families::version_json::{
InstanceStorage, NamedMetadataPatch, PatchMode, PatchTrace, apply_metadata_patches_traced,
read_base_metadata,
};
use elemental_schema::mojang::piston::PistonMetaData;
use crate::{
addon::{AddonPatchMode, InstalledAddon},
error::{Error, Result},
};
use super::{AddonState, load_addon_state, save_addon_state, sync_instance_patches};
#[derive(Debug, Clone)]
pub struct AddonModify {
instance: InstanceStorage,
base: PistonMetaData,
original: AddonState,
draft: AddonState,
dirty: bool,
}
impl AddonModify {
pub async fn load(instance: InstanceStorage) -> Result<Self> {
let base = read_base_metadata(&instance)?;
let state = load_addon_state(&instance).await?;
Ok(Self {
instance,
base,
original: state.clone(),
draft: state,
dirty: false,
})
}
pub fn patch_mode(&self) -> PatchMode {
self.draft.patch_mode
}
pub fn addons(&self) -> Vec<InstalledAddon> {
self.draft
.addons
.iter()
.map(|record| record.to_installed(self.draft.patch_mode))
.collect()
}
pub fn is_dirty(&self) -> bool {
self.dirty
}
pub fn trace(&self) -> Result<PatchTrace> {
trace_state_with_extra(&self.base, &self.draft, &[])
}
pub fn trace_with_extra(&self, extra_patches: &[NamedMetadataPatch]) -> Result<PatchTrace> {
trace_state_with_extra(&self.base, &self.draft, extra_patches)
}
pub fn reorder(&mut self, ordered_ids: &[&str]) -> Result<PatchTrace> {
for id in ordered_ids {
if !self.draft.addons.iter().any(|record| record.id == *id) {
return Err(Error::UnknownAddonInOrder {
id: (*id).to_owned(),
});
}
}
let mut next = self.draft.clone();
let mut reordered = Vec::with_capacity(next.addons.len());
let mut remaining = std::mem::take(&mut next.addons);
for id in ordered_ids {
if let Some(pos) = remaining.iter().position(|record| record.id == *id) {
reordered.push(remaining.remove(pos));
}
}
reordered.extend(remaining);
next.addons = reordered;
let trace = trace_state_with_extra(&self.base, &next, &[])?;
self.draft = next;
self.dirty = true;
Ok(trace)
}
pub fn set_instance_patch_mode(&mut self, mode: PatchMode) -> Result<PatchTrace> {
let mut next = self.draft.clone();
next.patch_mode = mode;
next.validate_modes()?;
let trace = trace_state_with_extra(&self.base, &next, &[])?;
self.draft = next;
self.dirty = true;
Ok(trace)
}
pub fn set_addon_patch_mode(
&mut self,
addon_id: &str,
mode: AddonPatchMode,
) -> Result<PatchTrace> {
let mut next = self.draft.clone();
let Some(record) = next.addons.iter_mut().find(|record| record.id == addon_id) else {
return Err(Error::AddonNotInstalled {
id: addon_id.to_owned(),
});
};
record.patch_mode = mode;
next.validate_modes()?;
let trace = trace_state_with_extra(&self.base, &next, &[])?;
self.draft = next;
self.dirty = true;
Ok(trace)
}
pub fn discard(&mut self) -> Result<PatchTrace> {
self.draft = self.original.clone();
self.dirty = false;
self.trace()
}
pub async fn commit(&mut self) -> Result<PatchTrace> {
let trace = sync_instance_patches(&self.instance, &self.draft).await?;
save_addon_state(&self.instance, &self.draft).await?;
self.original = self.draft.clone();
self.dirty = false;
Ok(trace)
}
}
fn trace_state_with_extra(
base: &PistonMetaData,
state: &AddonState,
extra_patches: &[NamedMetadataPatch],
) -> Result<PatchTrace> {
let mut patches = state.resolve_named_patches()?;
patches.extend(extra_patches.iter().cloned());
Ok(apply_metadata_patches_traced(base.clone(), &patches)?)
}
pub async fn reorder_instance_addons(
instance: &InstanceStorage,
ordered_ids: &[&str],
) -> Result<PatchTrace> {
let mut modify = AddonModify::load(instance.clone()).await?;
modify.reorder(ordered_ids)?;
modify.commit().await
}
pub async fn set_instance_patch_mode(
instance: &InstanceStorage,
mode: PatchMode,
) -> Result<PatchTrace> {
let mut modify = AddonModify::load(instance.clone()).await?;
modify.set_instance_patch_mode(mode)?;
modify.commit().await
}
pub async fn set_addon_patch_mode(
instance: &InstanceStorage,
addon_id: &str,
mode: AddonPatchMode,
) -> Result<PatchTrace> {
let mut modify = AddonModify::load(instance.clone()).await?;
modify.set_addon_patch_mode(addon_id, mode)?;
modify.commit().await
}