#![warn(missing_docs)]
use rbx_dom_weak::{WeakDom, types::Ref};
use rbx_types::Variant;
use reqwest::{blocking::Client, header};
use std::{
collections::{BTreeSet, HashMap},
fs,
io::Cursor,
path::{Path, PathBuf},
sync::{
atomic::{AtomicUsize, Ordering},
mpsc,
},
thread,
time::Duration,
};
use thiserror::Error;
pub const DEFAULT_OUTPUT_DIR: &str = "mhif";
pub const DEFAULT_DOWNLOAD_JOBS: usize = 4;
const ASSET_DELIVERY_URL: &str = "https://assetdelivery.roblox.com/v1/asset/?id=";
const EXTRACTION_CACHE_DIRECTORY: &str = ".fingerprint";
const EXTRACTION_CACHE_VERSION: &str = "1";
#[derive(Debug, Error)]
pub enum MhifError {
#[error("failed to read input file {path}: {source}")]
ReadInput {
path: String,
source: std::io::Error,
},
#[error("failed to read input directory {path}: {source}")]
ReadDirectory {
path: String,
source: std::io::Error,
},
#[error("unsupported input extension for {path}; expected .rbxm, .rbxmx, .rbxl, or .rbxlx")]
UnsupportedInput {
path: String,
},
#[error("failed to parse Roblox file {path}: {message}")]
ParseInput {
path: String,
message: String,
},
#[error("failed to read .ROBLOSECURITY file {path}: {source}")]
ReadCookie {
path: String,
source: std::io::Error,
},
#[error(".ROBLOSECURITY file {path} is empty")]
EmptyCookie {
path: String,
},
#[error("failed to create output directory {path}: {source}")]
CreateOutput {
path: String,
source: std::io::Error,
},
}
#[derive(Debug, Clone, Default)]
pub struct ExtractionOptions {
cache_directory: Option<PathBuf>,
}
impl ExtractionOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_cache_directory(mut self, output_directory: impl Into<PathBuf>) -> Self {
self.cache_directory = Some(output_directory.into());
self
}
fn cache_directory(&self) -> Option<&Path> {
self.cache_directory.as_deref()
}
}
#[derive(Debug)]
pub struct DownloadReport {
pub discovered: usize,
pub downloaded: usize,
pub cached: usize,
pub output_directory: PathBuf,
pub failed: Vec<DownloadFailure>,
pub assets: Vec<DownloadedAsset>,
}
#[derive(Debug)]
pub struct ExtractionReport {
pub asset_ids: Vec<String>,
pub files: usize,
pub parsed: usize,
pub cached: usize,
}
#[derive(Debug)]
pub struct DownloadedAsset {
pub asset_id: String,
pub path: PathBuf,
pub cached: bool,
}
#[derive(Debug)]
pub struct DownloadFailure {
pub asset_id: String,
pub error: DownloadError,
}
#[derive(Debug, Clone)]
pub struct DownloadOptions {
output_directory: PathBuf,
roblosecurity_file: Option<PathBuf>,
jobs: usize,
}
impl DownloadOptions {
pub fn new(output_directory: impl Into<PathBuf>) -> Self {
Self {
output_directory: output_directory.into(),
roblosecurity_file: None,
jobs: DEFAULT_DOWNLOAD_JOBS,
}
}
pub fn with_roblosecurity_file(mut self, path: impl Into<PathBuf>) -> Self {
self.roblosecurity_file = Some(path.into());
self
}
pub fn with_jobs(mut self, jobs: usize) -> Self {
self.jobs = jobs.max(1);
self
}
pub fn output_directory(&self) -> &Path {
&self.output_directory
}
pub fn roblosecurity_file(&self) -> Option<&Path> {
self.roblosecurity_file.as_deref()
}
pub fn jobs(&self) -> usize {
self.jobs
}
}
#[derive(Debug, Error)]
pub enum DownloadError {
#[error("invalid Roblox asset ID")]
InvalidAssetId,
#[error("request failed: {0}")]
Request(#[source] reqwest::Error),
#[error("cookie error: {0}")]
Cookie(#[source] MhifError),
#[error("HTTP {status}: {body}")]
HttpStatus {
status: u16,
body: String,
},
#[error("failed to create asset directory {path}: {source}")]
CreateDirectory {
path: String,
source: std::io::Error,
},
#[error("failed to write {path}: {source}")]
Write {
path: String,
source: std::io::Error,
},
}
pub fn extract_asset_ids(path: impl AsRef<Path>) -> Result<Vec<String>, MhifError> {
Ok(extract_asset_ids_with_options(path, &ExtractionOptions::new())?.asset_ids)
}
pub fn extract_asset_ids_cached(
path: impl AsRef<Path>,
output_directory: impl AsRef<Path>,
) -> Result<ExtractionReport, MhifError> {
extract_asset_ids_with_options(
path,
&ExtractionOptions::new().with_cache_directory(output_directory.as_ref()),
)
}
pub fn extract_asset_ids_with_options(
path: impl AsRef<Path>,
options: &ExtractionOptions,
) -> Result<ExtractionReport, MhifError> {
extract_asset_ids_with_cache(path.as_ref(), options.cache_directory())
}
fn extract_asset_ids_with_cache(
path: &Path,
output_directory: Option<&Path>,
) -> Result<ExtractionReport, MhifError> {
let mut asset_ids = BTreeSet::new();
let mut cache = output_directory.map(ExtractionCache::new);
let mut files = 0;
let mut parsed = 0;
let mut cached = 0;
if fs::metadata(path)
.map_err(|source| MhifError::ReadInput {
path: path.display().to_string(),
source,
})?
.is_dir()
{
let mut directories = vec![path.to_owned()];
while let Some(directory) = directories.pop() {
let entries = fs::read_dir(&directory).map_err(|source| MhifError::ReadDirectory {
path: directory.display().to_string(),
source,
})?;
for entry in entries {
let entry = entry.map_err(|source| MhifError::ReadDirectory {
path: directory.display().to_string(),
source,
})?;
let file_type = entry
.file_type()
.map_err(|source| MhifError::ReadDirectory {
path: directory.display().to_string(),
source,
})?;
let entry_path = entry.path();
if file_type.is_dir() {
directories.push(entry_path);
} else if file_type.is_file() && is_supported_input(&entry_path) {
files += 1;
if extract_asset_ids_from_file(&entry_path, &mut asset_ids, cache.as_mut())? {
cached += 1;
} else {
parsed += 1;
}
}
}
}
} else {
files = 1;
if extract_asset_ids_from_file(path, &mut asset_ids, cache.as_mut())? {
cached = 1;
} else {
parsed = 1;
}
}
Ok(ExtractionReport {
asset_ids: asset_ids.into_iter().collect(),
files,
parsed,
cached,
})
}
fn extract_asset_ids_from_file(
path: &Path,
asset_ids: &mut BTreeSet<String>,
mut cache: Option<&mut ExtractionCache>,
) -> Result<bool, MhifError> {
let bytes = fs::read(path).map_err(|source| MhifError::ReadInput {
path: path.display().to_string(),
source,
})?;
let cache_key = input_cache_key(path, &bytes);
if let Some(cache) = cache.as_mut()
&& let Some(cached_ids) = cache.get(&cache_key)
{
asset_ids.extend(cached_ids);
return Ok(true);
}
let ids = extract_asset_ids_from_bytes(path, &bytes)?;
asset_ids.extend(ids.iter().cloned());
if let Some(cache) = cache {
cache.store(&cache_key, &ids);
}
Ok(false)
}
fn extract_asset_ids_from_bytes(path: &Path, bytes: &[u8]) -> Result<BTreeSet<String>, MhifError> {
let dom = decode_dom(path, bytes)?;
let mut asset_ids = BTreeSet::new();
for referent in dom.root().children() {
collect_instance_assets(&dom, *referent, &mut asset_ids);
}
Ok(asset_ids)
}
struct ExtractionCache {
directory: PathBuf,
entries: HashMap<String, Vec<String>>,
}
impl ExtractionCache {
fn new(output_directory: &Path) -> Self {
Self {
directory: output_directory.join(EXTRACTION_CACHE_DIRECTORY),
entries: HashMap::new(),
}
}
fn get(&mut self, key: &str) -> Option<Vec<String>> {
if let Some(entry) = self.entries.get(key) {
return Some(entry.clone());
}
let contents = fs::read_to_string(self.path_for(key)).ok()?;
let mut ids = BTreeSet::new();
for line in contents.lines() {
ids.insert(numeric_id(line.trim())?);
}
let ids: Vec<_> = ids.into_iter().collect();
self.entries.insert(key.to_owned(), ids.clone());
Some(ids)
}
fn store(&mut self, key: &str, asset_ids: &BTreeSet<String>) {
let ids: Vec<_> = asset_ids.iter().cloned().collect();
self.entries.insert(key.to_owned(), ids.clone());
if fs::create_dir_all(&self.directory).is_err() {
return;
}
let path = self.path_for(key);
let temporary_path = self
.directory
.join(format!(".{key}.{}.tmp", std::process::id()));
if fs::write(&temporary_path, ids.join("\n")).is_ok()
&& fs::rename(&temporary_path, path).is_err()
{
let _ = fs::remove_file(temporary_path);
}
}
fn path_for(&self, key: &str) -> PathBuf {
self.directory.join(key)
}
}
fn input_cache_key(path: &Path, bytes: &[u8]) -> String {
let mut hasher = blake3::Hasher::new();
hasher.update(EXTRACTION_CACHE_VERSION.as_bytes());
hasher.update(&[0]);
if let Some(extension) = path.extension().and_then(|value| value.to_str()) {
hasher.update(extension.to_ascii_lowercase().as_bytes());
}
hasher.update(&[0]);
hasher.update(bytes);
hasher.finalize().to_hex().to_string()
}
fn is_supported_input(path: &Path) -> bool {
matches!(
path.extension()
.and_then(|value| value.to_str())
.map(|value| value.to_ascii_lowercase())
.as_deref(),
Some("rbxm" | "rbxmx" | "rbxl" | "rbxlx")
)
}
pub fn download_assets<I, S>(
asset_ids: I,
options: &DownloadOptions,
) -> Result<DownloadReport, MhifError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let output_directory = options.output_directory();
fs::create_dir_all(output_directory).map_err(|source| MhifError::CreateOutput {
path: output_directory.display().to_string(),
source,
})?;
let asset_ids: Vec<_> = asset_ids
.into_iter()
.map(|asset_id| asset_id.as_ref().to_owned())
.collect();
let discovered = asset_ids.len();
let downloader = Downloader::new(options.roblosecurity_file());
let mut downloaded = 0;
let mut cached = 0;
let mut failed = Vec::new();
let mut assets = Vec::new();
for (asset_id, result) in download_in_parallel(&asset_ids, options.jobs(), |asset_id| {
downloader.download_asset(asset_id, output_directory)
}) {
match result {
Ok(AssetResult::Downloaded(path)) => {
downloaded += 1;
assets.push(DownloadedAsset {
asset_id,
path,
cached: false,
});
}
Ok(AssetResult::Cached(path)) => {
cached += 1;
assets.push(DownloadedAsset {
asset_id,
path,
cached: true,
});
}
Err(error) => {
failed.push(DownloadFailure { asset_id, error });
}
}
}
Ok(DownloadReport {
discovered,
downloaded,
cached,
output_directory: output_directory.to_owned(),
failed,
assets,
})
}
fn download_in_parallel<T, F>(asset_ids: &[String], jobs: usize, download: F) -> Vec<(String, T)>
where
T: Send,
F: Fn(&str) -> T + Sync,
{
if asset_ids.is_empty() {
return Vec::new();
}
let worker_count = jobs.max(1).min(asset_ids.len());
let next_index = AtomicUsize::new(0);
let (sender, receiver) = mpsc::channel();
thread::scope(|scope| {
let download = &download;
let next_index = &next_index;
for _ in 0..worker_count {
let sender = sender.clone();
scope.spawn(move || {
loop {
let index = next_index.fetch_add(1, Ordering::Relaxed);
let Some(asset_id) = asset_ids.get(index) else {
break;
};
let result = download(asset_id);
if sender.send((index, asset_id.clone(), result)).is_err() {
break;
}
}
});
}
drop(sender);
let mut results = receiver.into_iter().collect::<Vec<_>>();
results.sort_unstable_by_key(|(index, _, _)| *index);
results
.into_iter()
.map(|(_, asset_id, result)| (asset_id, result))
.collect()
})
}
fn decode_dom(path: &Path, bytes: &[u8]) -> Result<WeakDom, MhifError> {
let extension = path
.extension()
.and_then(|value| value.to_str())
.map(|value| value.to_ascii_lowercase());
let result = match extension.as_deref() {
Some("rbxmx" | "rbxlx") => {
rbx_xml::from_reader_default(Cursor::new(bytes)).map_err(|source| source.to_string())
}
Some("rbxm" | "rbxl") => {
rbx_binary::from_reader(Cursor::new(bytes)).map_err(|source| source.to_string())
}
_ => {
return Err(MhifError::UnsupportedInput {
path: path.display().to_string(),
});
}
};
result.map_err(|message| MhifError::ParseInput {
path: path.display().to_string(),
message,
})
}
fn collect_instance_assets(dom: &WeakDom, referent: Ref, asset_ids: &mut BTreeSet<String>) {
let Some(instance) = dom.get_by_ref(referent) else {
return;
};
for (name, value) in &instance.properties {
collect_variant_assets(name.as_str(), value, asset_ids);
}
for child in instance.children() {
collect_instance_assets(dom, *child, asset_ids);
}
}
fn collect_variant_assets(name: &str, value: &Variant, asset_ids: &mut BTreeSet<String>) {
match value {
Variant::Font(value) => {
collect_candidate_assets(&value.family, false, asset_ids);
if let Some(cached_face_id) = &value.cached_face_id {
collect_candidate_assets(cached_face_id, true, asset_ids);
}
}
Variant::String(value) => {
collect_candidate_assets(value, is_asset_property(name), asset_ids);
}
Variant::ContentId(value) => {
collect_candidate_assets(value.as_str(), false, asset_ids);
}
Variant::Content(value) => {
if let Some(uri) = value.as_uri() {
collect_candidate_assets(uri, false, asset_ids);
}
}
_ => {}
}
}
fn collect_candidate_assets(candidate: &str, allow_raw_id: bool, asset_ids: &mut BTreeSet<String>) {
if let Some(asset_id) = parse_asset_id(candidate, allow_raw_id) {
asset_ids.insert(asset_id);
}
}
fn is_asset_property(name: &str) -> bool {
let name = name.to_ascii_lowercase();
name.contains("asset")
|| name.contains("content")
|| matches!(
name.as_str(),
"animationid"
| "fontface"
| "image"
| "meshid"
| "soundid"
| "texture"
| "textureid"
| "video"
)
}
fn parse_asset_id(value: &str, allow_raw_id: bool) -> Option<String> {
let value = value.trim();
if allow_raw_id && let Some(asset_id) = numeric_id(value) {
return Some(asset_id);
}
if let Some(asset_id) = value.strip_prefix("rbxassetid://") {
return numeric_id(asset_id);
}
if !value.starts_with("http://")
&& !value.starts_with("https://")
&& !value.starts_with("rbxthumb://")
{
return None;
}
let (_, query) = value.split_once('?')?;
query.split('&').find_map(|part| {
let (name, value) = part.split_once('=')?;
name.eq_ignore_ascii_case("id")
.then(|| numeric_id(value))
.flatten()
})
}
fn numeric_id(value: &str) -> Option<String> {
let value = value.trim();
(!value.is_empty() && value.chars().all(|character| character.is_ascii_digit()))
.then(|| value.to_owned())
}
struct Downloader {
client: Client,
cookie_file: Option<PathBuf>,
}
struct DownloadPayload {
bytes: Vec<u8>,
content_type: Option<String>,
}
enum AssetResult {
Downloaded(PathBuf),
Cached(PathBuf),
}
impl Downloader {
fn new(cookie_file: Option<&Path>) -> Self {
let client = Client::builder()
.user_agent("mhif/0.1")
.timeout(Duration::from_secs(60))
.build()
.expect("static HTTP client configuration is valid");
Self {
client,
cookie_file: cookie_file.map(Path::to_owned),
}
}
fn download_asset(
&self,
asset_id: &str,
output_directory: &Path,
) -> Result<AssetResult, DownloadError> {
if numeric_id(asset_id).is_none() {
return Err(DownloadError::InvalidAssetId);
}
let asset_directory = output_directory.join(asset_id);
if let Some(path) = find_cached_asset(&asset_directory) {
return Ok(AssetResult::Cached(path));
}
let payload = self.download_public_or_authenticated(asset_id)?;
let extension = artifact_extension(&payload.bytes, payload.content_type.as_deref());
fs::create_dir_all(&asset_directory).map_err(|source| DownloadError::CreateDirectory {
path: asset_directory.display().to_string(),
source,
})?;
let path = asset_directory.join(format!("asset.{extension}"));
let temporary_path = asset_directory.join(format!("asset.{extension}.part"));
fs::write(&temporary_path, payload.bytes).map_err(|source| DownloadError::Write {
path: temporary_path.display().to_string(),
source,
})?;
fs::rename(&temporary_path, &path).map_err(|source| DownloadError::Write {
path: path.display().to_string(),
source,
})?;
Ok(AssetResult::Downloaded(path))
}
fn download_public_or_authenticated(
&self,
asset_id: &str,
) -> Result<DownloadPayload, DownloadError> {
match self.download(asset_id, None) {
Ok(payload) => Ok(payload),
Err(
error @ DownloadError::HttpStatus {
status: 401 | 403, ..
},
) => {
let cookie =
load_cookie(self.cookie_file.as_deref()).map_err(DownloadError::Cookie)?;
match cookie {
Some(cookie) => self.download(asset_id, Some(&cookie)),
None => Err(error),
}
}
Err(error) => Err(error),
}
}
fn download(
&self,
asset_id: &str,
cookie: Option<&str>,
) -> Result<DownloadPayload, DownloadError> {
let mut request = self.client.get(format!("{ASSET_DELIVERY_URL}{asset_id}"));
if let Some(cookie) = cookie {
request = request.header(header::COOKIE, cookie);
}
let response = request.send().map_err(DownloadError::Request)?;
let status = response.status();
let content_type = response
.headers()
.get(header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.map(str::to_owned);
let bytes = response.bytes().map_err(DownloadError::Request)?.to_vec();
if !status.is_success() {
return Err(DownloadError::HttpStatus {
status: status.as_u16(),
body: String::from_utf8_lossy(&bytes).chars().take(400).collect(),
});
}
Ok(DownloadPayload {
bytes,
content_type,
})
}
}
fn find_cached_asset(asset_directory: &Path) -> Option<PathBuf> {
fs::read_dir(asset_directory)
.ok()?
.filter_map(Result::ok)
.map(|entry| entry.path())
.find(|path| {
path.is_file()
&& fs::metadata(path).is_ok_and(|metadata| metadata.len() > 0)
&& path.file_stem().and_then(|value| value.to_str()) == Some("asset")
})
}
fn read_cookie(path: &Path) -> Result<String, MhifError> {
let value = fs::read_to_string(path).map_err(|source| MhifError::ReadCookie {
path: path.display().to_string(),
source,
})?;
normalize_cookie(&value, path.display().to_string())
}
fn load_cookie(path: Option<&Path>) -> Result<Option<String>, MhifError> {
match path {
Some(path) => read_cookie(path).map(Some),
None => rbx_cookies::get_value()
.map(|value| normalize_cookie(&value, "rbx_cookies"))
.transpose(),
}
}
fn normalize_cookie(value: &str, path: impl Into<String>) -> Result<String, MhifError> {
let trimmed = value.trim();
let value = trimmed
.strip_prefix(".ROBLOSECURITY=")
.or_else(|| trimmed.strip_prefix("ROBLOSECURITY="))
.unwrap_or(trimmed)
.trim();
if value.is_empty() {
return Err(MhifError::EmptyCookie { path: path.into() });
}
Ok(format!(".ROBLOSECURITY={value}"))
}
fn artifact_extension(bytes: &[u8], content_type: Option<&str>) -> &'static str {
if bytes.starts_with(b"\x89PNG\r\n\x1a\n")
|| content_type.is_some_and(|value| value.starts_with("image/png"))
{
"png"
} else if bytes.starts_with(b"\xff\xd8\xff")
|| content_type.is_some_and(|value| value.starts_with("image/jpeg"))
{
"jpg"
} else if bytes.starts_with(b"GIF8")
|| content_type.is_some_and(|value| value.starts_with("image/gif"))
{
"gif"
} else if bytes.starts_with(b"RIFF") && bytes.get(8..12) == Some(b"WEBP") {
"webp"
} else if bytes.starts_with(b"BM") {
"bmp"
} else if bytes.starts_with(b"OggS") {
"ogg"
} else if bytes.starts_with(b"RIFF") && bytes.get(8..12) == Some(b"WAVE") {
"wav"
} else if bytes.starts_with(b"ID3") || bytes.starts_with(&[0xff, 0xfb]) {
"mp3"
} else if bytes.starts_with(b"glTF") {
"glb"
} else if bytes.starts_with(b"<roblox!") {
"rbxm"
} else if bytes.iter().find(|byte| !byte.is_ascii_whitespace()) == Some(&b'<') {
"rbxmx"
} else if bytes.iter().find(|byte| !byte.is_ascii_whitespace()) == Some(&b'{')
|| bytes.iter().find(|byte| !byte.is_ascii_whitespace()) == Some(&b'[')
{
"json"
} else {
"bin"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_deduplicated_ids_from_xml_content_and_urls() {
let xml = br#"<roblox version="4"><Item class="MeshPart" referent="RBXmesh"><Properties><Content name="MeshId"><url>rbxassetid://123</url></Content><Content name="TextureID"><url>https://www.roblox.com/asset/?id=456</url></Content><string name="SoundId">123</string><string name="Name">asset/?id=999</string></Properties></Item></roblox>"#;
let path = Path::new("fixture.rbxmx");
let dom = decode_dom(path, xml).unwrap();
let mut ids = BTreeSet::new();
for referent in dom.root().children() {
collect_instance_assets(&dom, *referent, &mut ids);
}
assert_eq!(ids.into_iter().collect::<Vec<_>>(), ["123", "456"]);
}
#[test]
fn extracts_asset_ids_recursively_from_directory() {
let root = std::env::temp_dir().join(format!("mhif-directory-{}", std::process::id()));
let nested = root.join("nested");
fs::create_dir_all(&nested).unwrap();
fs::write(
root.join("model.RBXMX"),
br#"<roblox version="4"><Item class="MeshPart" referent="RBXmesh"><Properties><Content name="MeshId"><url>rbxassetid://123</url></Content></Properties></Item></roblox>"#,
)
.unwrap();
fs::write(
nested.join("place.rbxmx"),
br#"<roblox version="4"><Item class="MeshPart" referent="RBXmesh"><Properties><Content name="MeshId"><url>rbxassetid://456</url></Content></Properties></Item></roblox>"#,
)
.unwrap();
fs::write(root.join("notes.txt"), b"not a Roblox file").unwrap();
assert_eq!(extract_asset_ids(&root).unwrap(), ["123", "456"]);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn reuses_extracted_ids_for_duplicate_and_unchanged_files() {
let root =
std::env::temp_dir().join(format!("mhif-extraction-cache-{}", std::process::id()));
let input = root.join("input");
let output = root.join("output");
let model = |asset_id: &str| {
format!(
r#"<roblox version="4"><Item class="MeshPart" referent="RBXmesh"><Properties><Content name="MeshId"><url>rbxassetid://{asset_id}</url></Content></Properties></Item></roblox>"#
)
};
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(&input).unwrap();
fs::write(input.join("first.rbxmx"), model("123")).unwrap();
fs::write(input.join("second.rbxmx"), model("123")).unwrap();
let first = extract_asset_ids_cached(&input, &output).unwrap();
assert_eq!(first.asset_ids, ["123"]);
assert_eq!((first.files, first.parsed, first.cached), (2, 1, 1));
let second = extract_asset_ids_cached(&input, &output).unwrap();
assert_eq!(second.asset_ids, ["123"]);
assert_eq!((second.files, second.parsed, second.cached), (2, 0, 2));
fs::write(input.join("second.rbxmx"), model("456")).unwrap();
let third = extract_asset_ids_cached(&input, &output).unwrap();
assert_eq!(third.asset_ids, ["123", "456"]);
assert_eq!((third.files, third.parsed, third.cached), (2, 1, 1));
fs::remove_dir_all(root).unwrap();
}
#[test]
fn identifies_common_artifact_extensions() {
assert_eq!(artifact_extension(b"\x89PNG\r\n\x1a\n", None), "png");
assert_eq!(artifact_extension(b"<roblox!", None), "rbxm");
assert_eq!(artifact_extension(b"<roblox version=\"4\">", None), "rbxmx");
assert_eq!(artifact_extension(b"opaque bytes", None), "bin");
}
#[test]
fn normalizes_explicit_and_raw_cookie_values() {
assert_eq!(
normalize_cookie("raw-cookie\n", "test").unwrap(),
".ROBLOSECURITY=raw-cookie"
);
assert_eq!(
normalize_cookie(".ROBLOSECURITY=assigned-cookie", "test").unwrap(),
".ROBLOSECURITY=assigned-cookie"
);
}
#[test]
fn finds_non_empty_cached_asset_files_only() {
let root = std::env::temp_dir().join(format!("mhif-cache-{}", std::process::id()));
let asset_directory = root.join("123");
fs::create_dir_all(&asset_directory).unwrap();
fs::write(asset_directory.join("asset.bin.part"), b"partial").unwrap();
assert!(find_cached_asset(&asset_directory).is_none());
fs::write(asset_directory.join("asset.bin"), b"complete").unwrap();
assert_eq!(
find_cached_asset(&asset_directory),
Some(asset_directory.join("asset.bin"))
);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn limits_parallel_downloads_and_preserves_input_order() {
use std::sync::{Arc, Barrier};
let asset_ids = (0..8).map(|id| id.to_string()).collect::<Vec<_>>();
let active = Arc::new(AtomicUsize::new(0));
let maximum_active = Arc::new(AtomicUsize::new(0));
let calls = Arc::new(AtomicUsize::new(0));
let barrier = Arc::new(Barrier::new(3));
let results = download_in_parallel(&asset_ids, 3, {
let active = Arc::clone(&active);
let maximum_active = Arc::clone(&maximum_active);
let calls = Arc::clone(&calls);
let barrier = Arc::clone(&barrier);
move |asset_id| {
let active_now = active.fetch_add(1, Ordering::SeqCst) + 1;
maximum_active.fetch_max(active_now, Ordering::SeqCst);
if calls.fetch_add(1, Ordering::SeqCst) < 3 {
barrier.wait();
}
thread::sleep(Duration::from_millis(10));
active.fetch_sub(1, Ordering::SeqCst);
asset_id.to_owned()
}
});
assert_eq!(maximum_active.load(Ordering::SeqCst), 3);
assert_eq!(
results.into_iter().map(|(_, id)| id).collect::<Vec<_>>(),
asset_ids
);
}
#[test]
fn extracts_the_shop_fixture_asset_ids() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/shop.rbxmx");
let asset_ids = extract_asset_ids(&path).unwrap();
assert_eq!(asset_ids.len(), 1);
assert!(asset_ids.iter().any(|asset_id| asset_id == "16301645002"));
}
#[test]
fn extracts_the_diversity_fixture_asset_ids() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/diversity.rbxmx");
let asset_ids = extract_asset_ids(&path).unwrap();
assert_eq!(asset_ids.len(), 3);
}
}