Skip to content

Commit eff6e2d

Browse files
authored
Merge pull request #48 from ktwrd/feature/44
[Feature] Make beans-rs update the .adastral file after an upgrade
2 parents 744f1cf + 8a90f5e commit eff6e2d

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

src/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,31 @@ pub enum BeansError
175175
old_location: String,
176176
old_content: String
177177
},
178+
#[error("Failed to serialize JSON {instance:?} to location {location} ({error:})")]
179+
VersionFileSerializeFailure
180+
{
181+
error: serde_json::Error,
182+
instance: AdastralVersionFile,
183+
location: String
184+
},
178185
#[error("Failed to read version file at {location}. ({error:})")]
179186
VersionFileReadFailure
180187
{
181188
error: std::io::Error,
182189
location: String
183190
},
191+
#[error("Failed to open version file at {location}. ({error:})")]
192+
VersionFileOpenFailure
193+
{
194+
error: std::io::Error,
195+
location: String
196+
},
197+
#[error("Failed to write version file at {location}. ({error:})")]
198+
VersionFileWriteFailure
199+
{
200+
error: std::io::Error,
201+
location: String
202+
},
184203

185204
#[error("Failed to serialize provided AppVarData to JSON. ({error:})")]
186205
AppVarDataSerializeFailure

src/version.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::{backtrace::Backtrace,
22
collections::HashMap,
3-
fs::read_to_string,
4-
io::Write};
3+
fs::{read_to_string,
4+
File},
5+
io::{BufWriter,
6+
Write}};
57

68
use log::{debug,
79
error,
@@ -42,6 +44,69 @@ pub fn get_current_version(sourcemods_location: Option<String>) -> Option<usize>
4244
}
4345
}
4446

47+
/// set the version in the `.adastral` file in the sourcemod folder.
48+
/// will silently fail when install_state is not InstallType::Adastral, or the
49+
/// sourcemod isn't installed.
50+
pub fn set_current_version(
51+
sourcemods_location: Option<String>,
52+
new_version: usize
53+
) -> Result<(), BeansError>
54+
{
55+
let install_state = helper::install_state(sourcemods_location.clone());
56+
if install_state != InstallType::Adastral
57+
{
58+
return Ok(());
59+
}
60+
match get_mod_location(sourcemods_location)
61+
{
62+
Some(smp_x) =>
63+
{
64+
// TODO generate BeansError instead of using panic
65+
let location = format!("{}.adastral", smp_x);
66+
let file = match helper::file_exists(location.clone())
67+
{
68+
false => match File::create(&location)
69+
{
70+
Ok(v) => v,
71+
Err(e) =>
72+
{
73+
return Err(BeansError::VersionFileWriteFailure {
74+
location: location.clone(),
75+
error: e
76+
});
77+
}
78+
},
79+
true => match File::open(&location)
80+
{
81+
Ok(v) => v,
82+
Err(e) =>
83+
{
84+
return Err(BeansError::VersionFileOpenFailure {
85+
location: location.clone(),
86+
error: e
87+
});
88+
}
89+
}
90+
};
91+
let data = AdastralVersionFile {
92+
version: format!("{new_version}")
93+
};
94+
debug!("[set_current_version] location: {location:}, content: {data:?}");
95+
let mut writer = BufWriter::new(file);
96+
match serde_json::to_writer(&mut writer, &data)
97+
{
98+
Ok(_) => Ok(()),
99+
Err(e) => Err(BeansError::VersionFileSerializeFailure {
100+
location: location.clone(),
101+
instance: data.clone(),
102+
error: e
103+
})
104+
}
105+
}
106+
None => Ok(()) // silently fail
107+
}
108+
}
109+
45110
fn get_version_location(sourcemods_location: Option<String>) -> Option<String>
46111
{
47112
get_mod_location(sourcemods_location).map(|v| format!("{}.adastral", v))

src/workflows/update.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::{BeansError,
55
RunnerContext,
66
appvar::AppVarData,
77
butler,
8-
helper};
8+
helper,
9+
version};
910

1011
pub struct UpdateWorkflow
1112
{
@@ -30,6 +31,7 @@ impl UpdateWorkflow
3031
};
3132

3233
let remote_version = ctx.current_remote_version()?;
34+
let (remote_version_ident, _) = ctx.latest_remote_version();
3335

3436
ctx.prepare_symlink()?;
3537
let patch = match ctx.has_patch_available()
@@ -112,6 +114,9 @@ impl UpdateWorkflow
112114
return Err(e);
113115
}
114116

117+
info!("[UpdateWorkflow] Updating version file (.adastral)");
118+
version::set_current_version(Some(ctx.sourcemod_path.clone()), remote_version_ident)?;
119+
115120
ctx.gameinfo_perms()?;
116121

117122
Self::post_update_msg();

0 commit comments

Comments
 (0)