use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use serde::Deserialize;
use crate::device::CachedTicketFile;
use crate::error::SdkError;
pub(crate) const DRM_ROOT_ENV: &str = "ARCANE_DRM_ROOT";
pub(crate) fn drm_data_root() -> Result<PathBuf, SdkError> {
if let Some(raw) = env::var_os(DRM_ROOT_ENV) {
if !raw.is_empty() {
return Ok(PathBuf::from(raw));
}
}
let base = dirs::data_dir().ok_or_else(|| {
SdkError::internal("Could not resolve the OS application data directory.")
.with_hint(format!(
"Set {DRM_ROOT_ENV} to an explicit directory if this platform has no standard app-data path."
))
})?;
Ok(base.join("Arcane Powered").join("drm"))
}
pub(crate) fn machine_id_path() -> Result<PathBuf, SdkError> {
Ok(drm_data_root()?.join("machine_id"))
}
pub(crate) fn jwks_path() -> Result<PathBuf, SdkError> {
Ok(drm_data_root()?.join("jwks.json"))
}
pub(crate) fn session_path() -> Result<PathBuf, SdkError> {
Ok(drm_data_root()?.join("session.json"))
}
pub(crate) fn tickets_root() -> Result<PathBuf, SdkError> {
Ok(drm_data_root()?.join("tickets"))
}
pub(crate) fn ticket_path(user_id: &str, public_key: &str) -> Result<PathBuf, SdkError> {
Ok(tickets_root()?
.join(user_id)
.join(format!("{public_key}.ticket")))
}
#[derive(Debug, Deserialize)]
struct SessionFile {
#[serde(default)]
user_id: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SessionState {
SignedIn(String),
SignedOut,
Unknown,
}
pub(crate) fn load_session() -> SessionState {
let Ok(path) = session_path() else {
return SessionState::Unknown;
};
let Ok(raw) = fs::read_to_string(&path) else {
return SessionState::Unknown;
};
let Ok(session) = serde_json::from_str::<SessionFile>(&raw) else {
return SessionState::Unknown;
};
match session.user_id {
Some(user_id) if !user_id.trim().is_empty() => {
SessionState::SignedIn(user_id.trim().to_string())
}
_ => SessionState::SignedOut,
}
}
pub(crate) fn load_cached_drm_flag(public_key: &str) -> Option<bool> {
let path = drm_data_root()
.ok()?
.join("flags")
.join(format!("{public_key}.json"));
let raw = fs::read_to_string(path).ok()?;
let value: serde_json::Value = serde_json::from_str(&raw).ok()?;
value.get("drm_enabled")?.as_bool()
}
#[derive(Debug, Clone)]
pub(crate) struct ResolvedTicket {
pub file: CachedTicketFile,
pub path: PathBuf,
}
fn read_ticket_at(path: &Path) -> Result<ResolvedTicket, SdkError> {
let raw = fs::read_to_string(path).map_err(|e| {
SdkError::internal(format!("Could not read the cached ownership ticket: {e}"))
.with_hint("Check read permissions on the Arcane DRM directory.")
.with_context("path", path.display())
})?;
let file: CachedTicketFile = serde_json::from_str(&raw).map_err(|e| {
SdkError::ticket_invalid(format!("The cached ownership ticket file is corrupt: {e}"))
.with_hint("Delete the file and let Arcane desktop mint a fresh ticket.")
.with_context("path", path.display())
})?;
Ok(ResolvedTicket {
file,
path: path.to_path_buf(),
})
}
pub(crate) fn resolve_ticket(public_key: &str) -> Result<ResolvedTicket, SdkError> {
match load_session() {
SessionState::SignedIn(user_id) => {
let path = ticket_path(&user_id, public_key)?;
if !path.exists() {
return Err(SdkError::ticket_missing(
"No ownership ticket is cached for this title on the signed-in account.",
)
.with_hint(
"Open the Arcane desktop app once while online so it can mint a ticket \
for this account.",
)
.with_context("public_key", public_key)
.with_context("user_id", &user_id)
.with_context("expected_path", path.display()));
}
read_ticket_at(&path)
}
SessionState::SignedOut => Err(SdkError::not_authenticated(
"Nobody is signed in to Arcane on this machine.",
)
.with_hint("Sign in to the Arcane desktop app, then retry.")
.with_context("public_key", public_key)
.with_context(
"session_path",
session_path()
.map(|p| p.display().to_string())
.unwrap_or_else(|_| "<unresolved>".into()),
)),
SessionState::Unknown => resolve_ticket_by_scan(public_key),
}
}
fn resolve_ticket_by_scan(public_key: &str) -> Result<ResolvedTicket, SdkError> {
let root = tickets_root()?;
if !root.exists() {
return Err(
SdkError::ticket_missing("No ownership tickets are cached on this machine.")
.with_hint("Sign in to the Arcane desktop app once while online, then retry.")
.with_context("public_key", public_key)
.with_context("tickets_root", root.display()),
);
}
let entries = fs::read_dir(&root).map_err(|e| {
SdkError::internal(format!("Could not list cached ownership tickets: {e}"))
.with_hint("Check read permissions on the Arcane DRM directory.")
.with_context("tickets_root", root.display())
})?;
let mut matches: Vec<PathBuf> = Vec::new();
for entry in entries.flatten() {
let candidate = entry.path().join(format!("{public_key}.ticket"));
if candidate.exists() {
matches.push(candidate);
}
}
match matches.len() {
0 => Err(SdkError::ticket_missing(format!(
"No ownership ticket is cached for `{public_key}`."
))
.with_hint("Open the Arcane desktop app once while online, then retry.")
.with_context("public_key", public_key)
.with_context("tickets_root", root.display())),
1 => read_ticket_at(&matches[0]),
n => Err(SdkError::ambiguous_session(format!(
"{n} accounts hold a ticket for this title on this machine, and Arcane has not \
recorded which one is signed in."
))
.with_hint(
"Open the Arcane desktop app once so it records the active session, then retry. \
This needs an Arcane build that writes session.json.",
)
.with_context("public_key", public_key)
.with_context("tickets_root", root.display())
.with_context("candidates", n)),
}
}