Skip to content

Commit

Permalink
feat: command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Toaaa committed Feb 2, 2024
1 parent 27ea910 commit 31361a1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pa-updater"
version = "0.1.1"
version = "0.1.2"
authors = ["Toa Toastrejn <toa@toaaa.de>"]
edition = "2021"
description = "Official Installer and Updater for Project-Apparatus, a Lethal Company cheat based on Infinite Company."
Expand All @@ -13,4 +13,6 @@ license-file = "LICENSE.md"
reqwest = { version = "0.11", features = ["blocking", "json", "stream"] }
tokio = { version = "1", features = ["full"] }
indicatif = "0.17.7"
zip = "0.6.6"
zip = "0.6.6"
clap = { version = "4.4.18", features = ["cargo", "derive"] }
serde_json = "1.0.113"
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# PA-Updater

Official Installer and Updater for Project-Apparatus, a Lethal Company cheat based on Infinite Company.

## How do i use it?

1. Download the [latest release](https://github.com/Toaaa/PA-Updater/releases/latest)
2. Move the `pa-updater.exe` to a new/empty folder (optional)
3. Run the `pa-updater.exe`. This will download and extract the latest version of [Project-Apparatus](https://github.com/KaylinOwO/Project-Apparatus) into the current folder.
4. Run the `inject.bat` file while your game is running.
5. Enjoy!

> Note: Every time you run the `inject.bat` file, it will automatically run the `pa-updater.exe` so you always have the latest version when you inject.
> [!NOTE]\
> Every time you run the `inject.bat`, it will automatically run the `pa-updater.exe` so you always have the latest version when you inject.

## To-Do

- [ ] Add a some way to update PA-Updater itself
52 changes: 40 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ use std::fs::{self, File};
use std::io::{self, Write};
use std::path::Path;
use reqwest::Client;
use clap::Parser;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Sets the path where the zip file should be downloaded and extracted to (default: current directory)
#[clap(short, long)]
path: Option<String>,

/// Downloads a specific version (default: latest)
#[clap(short, long)]
download_version: Option<String>,
}

async fn download_file(url: &str, file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
Expand All @@ -14,13 +27,13 @@ async fn download_file(url: &str, file_path: &Path) -> Result<(), Box<dyn std::e
}
println!("File downloaded successfully");
} else {
println!("Failed to download the file: {}", response.status());
println!("Failed to download the file from '{}': {}", url, response.status());
}

Ok(())
}

async fn extract_zip(zip_file: &Path) -> Result<(), Box<dyn std::error::Error>> {
async fn extract_zip(zip_file: &Path, target_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
let file = File::open(zip_file)?;
let mut archive = zip::ZipArchive::new(file)?;

Expand All @@ -29,9 +42,10 @@ async fn extract_zip(zip_file: &Path) -> Result<(), Box<dyn std::error::Error>>
let file_path = file.name();
println!("Extracting {}...", file_path);

let dest_path = file_path;
// Modify the destination path to use the target directory
let dest_path = target_dir.join(file_path);

if let Some(parent_dir) = Path::new(dest_path).parent() {
if let Some(parent_dir) = dest_path.parent() {
fs::create_dir_all(parent_dir)?;
}

Expand All @@ -44,26 +58,40 @@ async fn extract_zip(zip_file: &Path) -> Result<(), Box<dyn std::error::Error>>
}

if zip_file.exists() {
fs::remove_file(&zip_file).unwrap();
fs::remove_file(&zip_file)?;
}

println!("ZIP file extracted successfully");
println!("ZIP file extracted successfully to: {}", target_dir.display());
Ok(())
}

#[tokio::main]
async fn main() -> io::Result<()> {
let zip_url = "https://pa.toaaa.de/latest/Project-Apparatus-latest.zip";
let zip_name = "Project-Apparatus-latest.zip";
let zip_path = Path::new(zip_name);
let args = Args::parse();

let zip_url = match args.download_version {
Some(version) => format!("https://pa.toaaa.de/{}/Project-Apparatus.zip", version),
None => "https://pa.toaaa.de/latest/Project-Apparatus-latest.zip".to_string(),
};

let zip_name = "Project-Apparatus.zip";
let zip_path = Path::new(&zip_name);

if zip_path.exists() {
println!("ZIP file already exists, overwrite...");
fs::remove_file(&zip_path).unwrap();
}
download_file(zip_url, zip_path).await.unwrap();

extract_zip(zip_path).await.unwrap();

let mut path = args.path.as_deref().map_or_else(|| std::env::current_dir().unwrap(), |s| Path::new(s).to_path_buf());
path.push(zip_path);

let target_dir = args.path.map_or_else(|| std::env::current_dir().unwrap(), |s| Path::new(&s).to_path_buf());

download_file(&zip_url, &path).await.unwrap();
extract_zip(&path, &target_dir).await.unwrap();

Ok(())
}

// println!("ZIP file extracted successfully to: {}", path.display());
}

0 comments on commit 31361a1

Please sign in to comment.