feat: add new version detection

This commit is contained in:
Adamaq01 2023-09-06 22:49:15 +02:00
parent b197baeee0
commit f750adc485
4 changed files with 74 additions and 6 deletions

View File

@ -5,6 +5,7 @@ authors = ["Adam Thibert <adamthibert01@gmail.com>"]
edition = "2021"
license = "MIT"
readme = "README.md"
build = "build.rs"
[lib]
crate-type = ["cdylib"]
@ -16,6 +17,9 @@ lto = true
codegen-units = 1
panic = "abort"
[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "cargo", "git", "gitcl"] }
[dependencies]
winapi = { version = "0.3", features = ["minwindef", "windef", "winuser", "libloaderapi", "processthreadsapi", "winbase", "consoleapi"] }
crochet = "0.2"

11
build.rs Normal file
View File

@ -0,0 +1,11 @@
use std::error::Error;
use vergen::EmitBuilder;
fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder()
.git_describe(false, false, None)
.git_sha(false)
.build_date()
.emit()?;
Ok(())
}

View File

@ -6,6 +6,15 @@ use log::{debug, error};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
pub fn request_agent() -> ureq::Agent {
let timeout = CONFIGURATION.general.timeout;
let timeout = if timeout > 10000 { 10000 } else { timeout };
ureq::builder()
.timeout(std::time::Duration::from_millis(timeout))
.build()
}
fn request<T>(
method: impl AsRef<str>,
url: impl AsRef<str>,
@ -14,11 +23,7 @@ fn request<T>(
where
T: Serialize + Debug,
{
let timeout = CONFIGURATION.general.timeout;
let timeout = if timeout > 10000 { 10000 } else { timeout };
let agent = ureq::builder()
.timeout(std::time::Duration::from_millis(timeout))
.build();
let agent = request_agent();
let method = method.as_ref();
let url = url.as_ref();

View File

@ -9,7 +9,7 @@ mod types;
use crate::log::Logger;
use crate::mikado::{hook_init, hook_release};
use ::log::error;
use ::log::{error, info};
use configuration::Configuration;
use lazy_static::lazy_static;
use url::Url;
@ -99,6 +99,49 @@ fn init_logger() {
.init();
}
fn print_infos() {
info!(
"Starting Mikado v{}-{} by adamaq01",
env!("CARGO_PKG_VERSION"),
option_env!("VERGEN_GIT_DESCRIBE").unwrap_or("unknown")
);
if let Some(build_date) = option_env!("VERGEN_BUILD_DATE") {
info!("Build date: {}", build_date);
}
}
fn check_for_update() -> anyhow::Result<()> {
let commit_hash = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
let latest_commit_hash = helpers::request_agent()
.get("https://api.github.com/repos/adamaq01/mikado/releases/latest")
.call()?
.into_json::<serde_json::Value>()?
.get("tag_name")
.and_then(|value| value.as_str())
.ok_or(anyhow::anyhow!("Could not get latest release tag name"))
.and_then(|tag| {
helpers::request_agent()
.get(&format!(
"https://api.github.com/repos/adamaq01/mikado/git/refs/tags/{}",
tag
))
.call()?
.into_json::<serde_json::Value>()?
.get("object")
.and_then(|value| value.get("sha"))
.and_then(|value| value.as_str())
.map(|value| value.to_string())
.ok_or(anyhow::anyhow!("Could not get latest release commit hash"))
})?;
if commit_hash != latest_commit_hash && !cfg!(debug_assertions) {
info!("A newer version of Mikado is available at https://github.com/adamaq01/mikado/releases/latest");
}
Ok(())
}
#[crochet::hook("avs2-ea3.dll", "XEmdwapa000024")]
unsafe fn avs_ea3_boot_startup_hook(node: *const ()) -> i32 {
if let Err(err) = hook_init(node) {
@ -116,6 +159,11 @@ extern "system" fn DllMain(dll_module: HINSTANCE, call_reason: DWORD, reserved:
unsafe { AllocConsole() };
init_logger();
print_infos();
if let Err(err) = check_for_update() {
error!("Unable to get update informations {:#}", err);
}
if let Err(err) = crochet::enable!(avs_ea3_boot_startup_hook) {
error!("{:#}", err);
}