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
19 changes: 19 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,31 @@ pub enum BeansError
old_location: String,
old_content: String
},
#[error("Failed to serialize JSON {instance:?} to location {location} ({error:})")]
VersionFileSerializeFailure
{
error: serde_json::Error,
instance: AdastralVersionFile,
location: String
},
#[error("Failed to read version file at {location}. ({error:})")]
VersionFileReadFailure
{
error: std::io::Error,
location: String
},
#[error("Failed to open version file at {location}. ({error:})")]
VersionFileOpenFailure
{
error: std::io::Error,
location: String
},
#[error("Failed to write version file at {location}. ({error:})")]
VersionFileWriteFailure
{
error: std::io::Error,
location: String
},

#[error("Failed to serialize provided AppVarData to JSON. ({error:})")]
AppVarDataSerializeFailure
Expand Down
69 changes: 67 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{backtrace::Backtrace,
collections::HashMap,
fs::read_to_string,
io::Write};
fs::{read_to_string,
File},
io::{BufWriter,
Write}};

use log::{debug,
error,
Expand Down Expand Up @@ -42,6 +44,69 @@ pub fn get_current_version(sourcemods_location: Option<String>) -> Option<usize>
}
}

/// set the version in the `.adastral` file in the sourcemod folder.
/// will silently fail when install_state is not InstallType::Adastral, or the
/// sourcemod isn't installed.
pub fn set_current_version(
sourcemods_location: Option<String>,
new_version: usize
) -> Result<(), BeansError>
{
let install_state = helper::install_state(sourcemods_location.clone());
if install_state != InstallType::Adastral
{
return Ok(());
}
match get_mod_location(sourcemods_location)
{
Some(smp_x) =>
{
// TODO generate BeansError instead of using panic
let location = format!("{}.adastral", smp_x);
let file = match helper::file_exists(location.clone())
{
false => match File::create(&location)
{
Ok(v) => v,
Err(e) =>
{
return Err(BeansError::VersionFileWriteFailure {
location: location.clone(),
error: e
});
}
},
true => match File::open(&location)
{
Ok(v) => v,
Err(e) =>
{
return Err(BeansError::VersionFileOpenFailure {
location: location.clone(),
error: e
});
}
}
};
let data = AdastralVersionFile {
version: format!("{new_version}")
};
debug!("[set_current_version] location: {location:}, content: {data:?}");
let mut writer = BufWriter::new(file);
match serde_json::to_writer(&mut writer, &data)
{
Ok(_) => Ok(()),
Err(e) => Err(BeansError::VersionFileSerializeFailure {
location: location.clone(),
instance: data.clone(),
error: e
})
}
}
None => Ok(()) // silently fail
}
}

fn get_version_location(sourcemods_location: Option<String>) -> Option<String>
{
get_mod_location(sourcemods_location).map(|v| format!("{}.adastral", v))
Expand Down
7 changes: 6 additions & 1 deletion src/workflows/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{BeansError,
RunnerContext,
appvar::AppVarData,
butler,
helper};
helper,
version};

pub struct UpdateWorkflow
{
Expand All @@ -30,6 +31,7 @@ impl UpdateWorkflow
};

let remote_version = ctx.current_remote_version()?;
let (remote_version_ident, _) = ctx.latest_remote_version();

ctx.prepare_symlink()?;
let patch = match ctx.has_patch_available()
Expand Down Expand Up @@ -112,6 +114,9 @@ impl UpdateWorkflow
return Err(e);
}

info!("[UpdateWorkflow] Updating version file (.adastral)");
version::set_current_version(Some(ctx.sourcemod_path.clone()), remote_version_ident)?;

ctx.gameinfo_perms()?;

Self::post_update_msg();
Expand Down
Loading