Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.4.5 #14

Merged
merged 6 commits into from
Jun 23, 2024
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "beans-rs"
version = "1.4.4"
version = "1.4.5"
edition = "2021"
authors = [
"Kate Ward <[email protected]>"
Expand Down Expand Up @@ -35,6 +35,7 @@ lazy_static = "1.4.0"
thread-id = "4.2.1"
colored = "2.1.0"
sentry-log = "0.34.0"
chrono = "0.4.38"

[target.'cfg(target_os = "windows")'.dependencies]
winconsole = { version = "0.11.1", features = ["window"] }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Requirements
- Rust Toolchain (nightly, only for building)
- Recommended to use [rustup](https://rustup.rs/) to install.
- x86-64/AMD64 Processor ([see notes](#notes-binaries))
- OpenSSL v3 (also required on deployments)
- **Following requirements are only required for testing**
- Steam Installed
- Source SDK Base 2013 Multiplayer ([install](steam://instal/243750))
Expand Down
42 changes: 42 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::backtrace::Backtrace;
use std::fmt::{Display, Formatter};
use std::num::ParseIntError;
use thiserror::Error;
use crate::appvar::AppVarData;
Expand Down Expand Up @@ -142,11 +143,17 @@ pub enum BeansError
location: String,
backtrace: Backtrace
},

#[error("Failed to set permissions on gameinfo.txt at {location} ({error:})")]
GameInfoPermissionSetFail {
error: std::io::Error,
permissions: std::fs::Permissions,
location: String
},

#[error("Failed to backup gameinfo.txt, {reason:}")]
GameinfoBackupFailure {
reason: GameinfoBackupFailureReason
}
}
#[derive(Debug)]
Expand All @@ -162,6 +169,41 @@ pub enum DownloadFailureReason
}
}
#[derive(Debug)]
pub enum GameinfoBackupFailureReason
{
ReadContentFail(GameinfoBackupReadContentFail),
BackupDirectoryCreateFailure(GameinfoBackupCreateDirectoryFail),
WriteFail(GameinfoBackupWriteFail)
}
#[derive(Debug)]
pub struct GameinfoBackupReadContentFail {
pub error: std::io::Error,
pub proposed_location: String,
pub current_location: String
}
#[derive(Debug)]
pub struct GameinfoBackupCreateDirectoryFail {
pub error: std::io::Error,
pub location: String
}
#[derive(Debug)]
pub struct GameinfoBackupWriteFail {
pub error: std::io::Error,
pub location: String
}
impl Display for GameinfoBackupFailureReason {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
GameinfoBackupFailureReason::ReadContentFail(v)
=> write!(f, "Couldn't read the content at {} ({:})", v.current_location, v.error),
GameinfoBackupFailureReason::BackupDirectoryCreateFailure(v)
=> write!(f, "Couldn't create backups directory {} ({:})", v.location, v.error),
GameinfoBackupFailureReason::WriteFail(v)
=> write!(f, "Failed to write content to {} ({:})", v.location, v.error)
}
}
}
#[derive(Debug)]
pub struct TarExtractFailureDetails {
pub source: String,
pub target: String,
Expand Down
72 changes: 69 additions & 3 deletions src/helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use std::io::Write;
use std::path::PathBuf;
use indicatif::{ProgressBar, ProgressStyle};
use futures::StreamExt;
use futures_util::SinkExt;
use log::{debug, error, trace, warn};
use crate::{BeansError, DownloadFailureReason, RunnerContext};
use crate::{BeansError, DownloadFailureReason, GameinfoBackupCreateDirectoryFail, GameinfoBackupFailureReason, GameinfoBackupReadContentFail, GameinfoBackupWriteFail, RunnerContext};
use rand::{distributions::Alphanumeric, Rng};
use reqwest::header::USER_AGENT;
use crate::appvar::AppVarData;

#[derive(Clone, Debug)]
pub enum InstallType
Expand Down Expand Up @@ -438,6 +438,72 @@ pub fn restore_gameinfo(ctx: &mut RunnerContext, data: Vec<u8>) -> Result<(), Be
}
return Ok(());
}
pub fn backup_gameinfo(ctx: &mut RunnerContext) -> Result<(), BeansError> {
let av = AppVarData::get();
let gamedir = join_path(ctx.clone().sourcemod_path, av.mod_info.sourcemod_name);
let backupdir = join_path(gamedir.clone(), String::from(GAMEINFO_BACKUP_DIRNAME));

let current_time = chrono::Local::now();
let current_time_formatted = current_time.format("%Y%m%d-%H%M%S").to_string();

if file_exists(backupdir.clone()) == false {
if let Err(e) = std::fs::create_dir(&backupdir) {
debug!("backupdir: {}", backupdir);
debug!("error: {:#?}", e);
error!("[helper::backup_gameinfo] Failed to create backup directory {:}", e);
return Err(BeansError::GameinfoBackupFailure {reason: GameinfoBackupFailureReason::BackupDirectoryCreateFailure(GameinfoBackupCreateDirectoryFail {
error: e,
location: backupdir
})});
}
}
let output_location = join_path(
backupdir,
format!("{}-{}.txt", ctx.current_version.unwrap_or(0), current_time_formatted));
let current_location = join_path(
gamedir,
String::from("gameinfo.txt"));

if file_exists(current_location.clone()) == false {
debug!("[helper::backup_gameinfo] can't backup since {} doesn't exist", current_location);
return Ok(());
}

let content = match std::fs::read_to_string(&current_location) {
Ok(v) => v,
Err(e) => {
debug!("location: {}", current_location);
debug!("error: {:#?}", e);
error!("[helper::backup_gameinfo] Failed to read content of gameinfo.txt {:}", e);
return Err(BeansError::GameinfoBackupFailure { reason: GameinfoBackupFailureReason::ReadContentFail(GameinfoBackupReadContentFail{
error: e,
proposed_location: output_location,
current_location: current_location.clone()
})})
}
};

if file_exists(output_location.clone()) {
if let Err(e) = std::fs::remove_file(&output_location) {
warn!("[helper::backup_gameinfo] Failed to delete existing file, lets hope things don't break. {:} {}", e, output_location.clone());
}
}

if let Err(e) = std::fs::write(&output_location, content) {
debug!("location: {}", output_location);
debug!("error: {:#?}", e);
error!("[helper::backup_gameinfo] Failed to write backup to {} ({:})", output_location, e);
return Err(BeansError::GameinfoBackupFailure { reason: GameinfoBackupFailureReason::WriteFail(GameinfoBackupWriteFail{
error: e,
location: output_location
})});
}

println!("[backup_gameinfo] Created backup at {}", output_location);

Ok(())
}
const GAMEINFO_BACKUP_DIRNAME: &str = "gameinfo_backup";
const GITHUB_RELEASES_URL: &str = "https://api.github.com/repositories/805393469/releases/latest";
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct GithubReleaseItem
Expand All @@ -450,4 +516,4 @@ pub struct GithubReleaseItem
pub html_url: String,
pub draft: bool,
pub prerelease: bool
}
}
6 changes: 2 additions & 4 deletions src/workflows/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ impl UpdateWorkflow
};

ctx.gameinfo_perms()?;
let gameinfo_backup = ctx.read_gameinfo_file()?;

if helper::has_free_space(ctx.sourcemod_path.clone(), patch.clone().tempreq)? == false {
println!("[UpdateWorkflow::wizard] Not enough free space! Requires {}", helper::format_size(patch.tempreq));
Expand All @@ -51,6 +50,8 @@ impl UpdateWorkflow
let mod_dir_location = ctx.get_mod_location();
let staging_dir_location = ctx.get_staging_location();

helper::backup_gameinfo(ctx)?;

ctx.gameinfo_perms()?;
info!("[UpdateWorkflow] Verifying game");
if let Err(e) = butler::verify(
Expand All @@ -72,9 +73,6 @@ impl UpdateWorkflow
}

ctx.gameinfo_perms()?;
if let Some(gi) = gameinfo_backup {
helper::restore_gameinfo(ctx, gi)?;
}

println!("Game has been updated!");
Ok(())
Expand Down
8 changes: 2 additions & 6 deletions src/workflows/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ impl VerifyWorkflow {
return Ok(());
}

ctx.gameinfo_perms()?;
let gameinfo_backup = ctx.read_gameinfo_file()?;
helper::backup_gameinfo(ctx)?;
let mod_dir_location = ctx.get_mod_location();
butler::verify(
format!("{}{}", &av.remote_info.base_url, remote.signature_url.unwrap()),
mod_dir_location.clone(),
format!("{}{}", &av.remote_info.base_url, remote.heal_url.unwrap()))?;
ctx.gameinfo_perms()?;
if let Some(gi) = gameinfo_backup {
helper::restore_gameinfo(ctx, gi)?;
}
println!("[VerifyWorkflow::wizard] The verification process has completed, and any corruption has been repaired.");
ctx.gameinfo_perms()?;
Ok(())
}
}