Skip to content

Commit

Permalink
feat: multi-platform vencordorion updater
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Oct 9, 2023
1 parent f4edc0c commit 12b6593
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/injection/injection_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{

static mut TAURI_INJECTED: bool = false;

fn injection_dir(win: tauri::Window) -> PathBuf {
pub fn injection_dir(win: tauri::Window) -> PathBuf {
let local_config_dir = std::env::current_exe()
.unwrap()
.parent()
Expand Down
18 changes: 7 additions & 11 deletions src-tauri/src/release.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io::BufRead;

use crate::injection::{injection_runner::injection_dir, self};

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Release {
pub tag_name: String,
Expand Down Expand Up @@ -37,10 +39,6 @@ pub async fn update_check() -> Vec<String> {

println!("Checking for updates...");

// For now, non-windows should just return
#[cfg(not(target_os = "windows"))]
return to_update;

if maybe_latest_injection_release().await {
println!("Available update for Vencordorion!");
to_update.push("vencordorion".to_string());
Expand All @@ -52,12 +50,15 @@ pub async fn update_check() -> Vec<String> {
}

#[tauri::command]
pub async fn do_update(to_update: Vec<String>) -> () {
pub async fn do_update(win: tauri::Window, to_update: Vec<String>) -> () {
let mut args = vec![];

if to_update.contains(&"vencordorion".to_string()) {
let injection_path = injection_dir(win);
let injection_path = format!("{}", injection_path.to_str().unwrap());
println!("Updating Vencordorion...");
args.push("--vencord");
args.push(String::from("--vencord"));
args.push(injection_path);
}

if args.len() > 0 {
Expand All @@ -79,12 +80,7 @@ pub async fn do_update(to_update: Vec<String>) -> () {
}
}

// TODO: Test platforms other than Windows
#[cfg(not(target_os = "windows"))]
pub async fn maybe_latest_injection_release() -> bool { false }

#[tauri::command]
#[cfg(target_os = "windows")]
pub async fn maybe_latest_injection_release() -> bool {
// See if there is a new release in Vencordorion
let url = "https://api.github.com/repos/SpikeHD/Vencordorion/releases/latest";
Expand Down
113 changes: 113 additions & 0 deletions updater/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions updater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
serde_json = "1.0"
reqwest = { version = "0.11.14", features = ["blocking"] }
clap = { version = "4.3.21", features = ["derive"] }

#[cfg(not(target_os = "windows"))]
sudo = "0.6.0"
Expand Down
51 changes: 27 additions & 24 deletions updater/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
use std::process::{Command, exit};
use std::path::PathBuf;
use clap::{command, Parser};

/// If you are reading this, you probably don't need to be. Dorion updates on it's own, silly!
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
/// Update Dorion
#[arg(short = 'm', long)]
main: Option<String>,

/// Path to injection folder
#[arg(short = 'v', long)]
vencord: Option<String>,
}

pub fn main() {
let args = Args::parse();

// This should always be run by Dorion itself, which means it will likely not have admin perms, so we request them before anything else
#[cfg(target_os = "windows")]
if is_elevated::is_elevated() == false {
Expand All @@ -9,32 +25,21 @@ pub fn main() {

#[cfg(not(target_os = "windows"))]
sudo::escalate_if_needed().expect("Failed to escalate as root");

// Two args, --main and --vencord, since we may need to update one or both
let args: Vec<String> = std::env::args().collect();

if args.len() < 2 {
println!("No arguments provided, exiting");
return;
}

let main = args.contains(&String::from("--main"));
let vencord = args.contains(&String::from("--vencord"));

if main {

if args.main.is_some() {
update_main();
}

if vencord {
update_vencordorion();
if args.vencord.is_some() {
update_vencordorion(PathBuf::from(args.vencord.unwrap()));
}
}

#[cfg(target_os = "windows")]
pub fn reopen_as_elevated() {
let install = std::env::current_exe().unwrap();

Command::new("powershell.exe")
std::process::Command::new("powershell.exe")
.arg("powershell")
.arg(format!(
"-command \"&{{Start-Process -filepath '{}' -verb runas -ArgumentList \"{}\"}}\"",
Expand All @@ -48,7 +53,7 @@ pub fn reopen_as_elevated() {
exit(0);
}

pub fn update_vencordorion() {
pub fn update_vencordorion(path: PathBuf) {
let url = "https://api.github.com/repos/SpikeHD/Vencordorion/releases/latest";
let client = reqwest::blocking::Client::new();
let response = client
Expand Down Expand Up @@ -87,13 +92,11 @@ pub fn update_vencordorion() {
.unwrap();

// Write both to disk
let mut css_path = std::env::current_exe().unwrap();
css_path.pop();
css_path.push("injection/browser.css");
let mut css_path = path.clone();
css_path.push("browser.css");

let mut js_path = std::env::current_exe().unwrap();
js_path.pop();
js_path.push("injection/browser.js");
let mut js_path = path.clone();
js_path.push("browser.js");

std::fs::write(css_path, css_response.text().unwrap()).unwrap();
std::fs::write(js_path, js_response.text().unwrap()).unwrap();
Expand Down

0 comments on commit 12b6593

Please sign in to comment.