use std::path::{Path, PathBuf};
use elemental_core::url::{OriginEndpoints, OriginPolicy};
use crate::error::Result;
pub const AUTHLIB_INJECTOR_ADDON_ID: &str = "authlib-injector";
pub const AUTHLIB_INJECTOR_OFFICIAL_URL: &str = nidhogg::authlib_injector::OFFICIAL_URL;
const KNOWN_ORIGINS: &[&str] = &[AUTHLIB_INJECTOR_OFFICIAL_URL];
#[derive(Debug, Clone)]
pub struct AuthlibInjectorEndpoints {
endpoints: OriginEndpoints,
}
impl Default for AuthlibInjectorEndpoints {
fn default() -> Self {
Self::official()
}
}
impl AuthlibInjectorEndpoints {
pub fn new(origin_policy: OriginPolicy) -> Self {
Self {
endpoints: OriginEndpoints::new(KNOWN_ORIGINS, origin_policy),
}
}
pub fn official() -> Self {
Self::new(OriginPolicy::default())
}
pub fn artifacts_url(&self) -> Result<String> {
Ok(self
.endpoints
.resolve(AUTHLIB_INJECTOR_OFFICIAL_URL, "artifacts.json")?)
}
pub fn latest_artifact_url(&self) -> Result<String> {
Ok(self
.endpoints
.resolve(AUTHLIB_INJECTOR_OFFICIAL_URL, "artifact/latest.json")?)
}
pub fn artifact_url(&self, build_number: u64) -> Result<String> {
Ok(self.endpoints.resolve(
AUTHLIB_INJECTOR_OFFICIAL_URL,
&format!("artifact/{build_number}.json"),
)?)
}
pub fn rewrite_download_url(&self, raw_url: &str) -> Result<String> {
Ok(self.endpoints.rewrite_known_or_keep(raw_url)?)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuthlibInjectorSource {
Remote,
#[cfg(feature = "authlib-injector-embed")]
Embedded,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuthlibInjector {
path: PathBuf,
version: String,
source: AuthlibInjectorSource,
fallback_reason: Option<String>,
}
impl AuthlibInjector {
pub(super) fn new(
path: PathBuf,
version: String,
source: AuthlibInjectorSource,
fallback_reason: Option<String>,
) -> Self {
Self {
path,
version,
source,
fallback_reason,
}
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn version(&self) -> &str {
&self.version
}
pub fn source(&self) -> AuthlibInjectorSource {
self.source
}
pub fn fallback_reason(&self) -> Option<&str> {
self.fallback_reason.as_deref()
}
pub fn jvm_arguments(&self, auth_server: &str) -> [String; 2] {
[
format!("-javaagent:{}={auth_server}", self.path.display()),
"-Dauthlibinjector.side=client".to_owned(),
]
}
}