Skip to content

Commit eb8e516

Browse files
authored
Merge branch 'develop' into vpk
2 parents 46aee7c + eff6e2d commit eb8e516

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
**When creating a PR, you must branch off the `develop` branch.** When merging back into this repo remember to select the `develop` as the branch to merge into. After the **7th of June 2024** any PRs that **do not** use `develop` as the base branch will be closed.
44

5+
**Before submitting a PR** make sure to format your code using the `cargo fmt` command. Code that is not properly formatted will need to do so before being merged.
6+
57
When adding a new feature (that a user will interact with), create a new file in `src/workflows/` with the name of the feature (for example, `launch.rs`). Inside of `launch.rs` you would have a struct with the name of `LaunchWorkflow`. It would look something like this;
68
```rust
79
use crate::{RunnerContext, BeansError};

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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{backtrace::Backtrace,
33
fs::{File,
44
read_to_string},
55
io::{Read,
6+
BufWriter,
67
Write}};
78

89
use log::{debug,
@@ -319,6 +320,68 @@ async fn generate_version_file(
319320
log::info!("Generated .adastral file at location {}", mod_path);
320321

321322
Ok(mod_version_translation)
323+
324+
/// set the version in the `.adastral` file in the sourcemod folder.
325+
/// will silently fail when install_state is not InstallType::Adastral, or the
326+
/// sourcemod isn't installed.
327+
pub fn set_current_version(
328+
sourcemods_location: Option<String>,
329+
new_version: usize
330+
) -> Result<(), BeansError>
331+
{
332+
let install_state = helper::install_state(sourcemods_location.clone());
333+
if install_state != InstallType::Adastral
334+
{
335+
return Ok(());
336+
}
337+
match get_mod_location(sourcemods_location)
338+
{
339+
Some(smp_x) =>
340+
{
341+
// TODO generate BeansError instead of using panic
342+
let location = format!("{}.adastral", smp_x);
343+
let file = match helper::file_exists(location.clone())
344+
{
345+
false => match File::create(&location)
346+
{
347+
Ok(v) => v,
348+
Err(e) =>
349+
{
350+
return Err(BeansError::VersionFileWriteFailure {
351+
location: location.clone(),
352+
error: e
353+
});
354+
}
355+
},
356+
true => match File::open(&location)
357+
{
358+
Ok(v) => v,
359+
Err(e) =>
360+
{
361+
return Err(BeansError::VersionFileOpenFailure {
362+
location: location.clone(),
363+
error: e
364+
});
365+
}
366+
}
367+
};
368+
let data = AdastralVersionFile {
369+
version: format!("{new_version}")
370+
};
371+
debug!("[set_current_version] location: {location:}, content: {data:?}");
372+
let mut writer = BufWriter::new(file);
373+
match serde_json::to_writer(&mut writer, &data)
374+
{
375+
Ok(_) => Ok(()),
376+
Err(e) => Err(BeansError::VersionFileSerializeFailure {
377+
location: location.clone(),
378+
instance: data.clone(),
379+
error: e
380+
})
381+
}
382+
}
383+
None => Ok(()) // silently fail
384+
}
322385
}
323386

324387
fn get_version_location(sourcemods_location: Option<String>) -> Option<String>

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)