Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ User-facing changes only. The release workflow publishes the matching

### Fixed

- Mods: an outdated HUD can no longer be installed as a custom pack. The file review greys it out
and says to get an updated copy from the author, as Import HUD does.
- Crosshair and Sounds: when another pack replaces the same files, Show in Mods now opens Custom
packs with that pack marked, instead of only opening Mods.
- HUD and Viewmodels: importing something that belongs in Mods now says so and offers Open Mods,
Expand Down
33 changes: 26 additions & 7 deletions apps/desktop/src-tauri/core/src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,33 @@ fn validate_hud_info(tree: &HudTree) -> Result<(), ProfileError> {
let info = tree
.info_vdf()
.ok_or_else(|| ProfileError::Io(NO_HUD_FOUND.into()))?;
match std::str::from_utf8(info).ok().and_then(parse_ui_version) {
Some(CURRENT_HUD_UI_VERSION) => Ok(()),
Some(version) => Err(ProfileError::Io(format!(
"This HUD declares UI version {version}; TF2 requires {CURRENT_HUD_UI_VERSION}. Get an updated copy from the author. Your current HUD has not changed."
match outdated_hud_info(info) {
None => Ok(()),
Some(reason) => Err(ProfileError::Io(format!(
"{reason} Your current HUD has not changed."
))),
None => Err(ProfileError::Io(
"This HUD's info.vdf is invalid or has no ui_version. Get a complete, updated copy from the author. Your current HUD has not changed.".into(),
)),
}
}

/// The refusal for an `info.vdf` that declares a UI version other than the
/// current one. `None` when it declares the current version or none at all,
/// which is how an `info.vdf` that is not HUD metadata reads.
pub(crate) fn other_ui_version_reason(info: &[u8]) -> Option<String> {
let version = std::str::from_utf8(info).ok().and_then(parse_ui_version)?;
(version != CURRENT_HUD_UI_VERSION).then(|| format!(
"This HUD declares UI version {version}; TF2 requires {CURRENT_HUD_UI_VERSION}. Get an updated copy from the author."
))
}

/// Why an `info.vdf` does not describe a HUD TF2 will load, or `None` when it
/// declares the current UI version.
pub(crate) fn outdated_hud_info(info: &[u8]) -> Option<String> {
match std::str::from_utf8(info).ok().and_then(parse_ui_version) {
Some(CURRENT_HUD_UI_VERSION) => None,
Some(_) => other_ui_version_reason(info),
None => Some(
"This HUD's info.vdf is invalid or has no ui_version. Get a complete, updated copy from the author.".into(),
),
}
}

Expand Down
10 changes: 10 additions & 0 deletions apps/desktop/src-tauri/core/src/mod_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ impl PreparedModImport {
}
None => disabled_metadata[&path],
};
// An outdated HUD would install as a pack TF2 ignores.
if disabled_reason.is_none() {
disabled_reason = content
.as_ref()
.and_then(|content| crate::mods::outdated_hud_reason(content).ok().flatten());
}
if disabled_reason.is_some() {
content = None;
}
Expand Down Expand Up @@ -602,6 +608,10 @@ mod tests {
)
.unwrap();
assert!(old.choices.iter().all(|choice| choice.hud.is_none()));
// It cannot be picked as a pack either, and says why.
let reason = old.choices[0].disabled_reason.as_deref().unwrap();
assert!(reason.contains("UI version 1; TF2 requires 3"), "{reason}");
assert!(old.select(&["0".into()]).is_err());
}

#[test]
Expand Down
90 changes: 75 additions & 15 deletions apps/desktop/src-tauri/core/src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ where
for (name, content) in packs {
selection_budget.add(&content)?;
refuse_hud_mod(&content)?;
refuse_outdated_hud_mod(&content)?;
refuse_crosshair_script_collision(&content, &crosshair_scripts)?;

let display = display_name(&name);
Expand Down Expand Up @@ -576,27 +577,64 @@ where
detail_from_manifest(profiles_dir, &manifest)
}

/// Whether a pack is a HUD: it carries an `info.vdf` declaring the current UI
/// version. Detection requires real compatibility data.
pub fn is_hud_content(content: &ModContent) -> Result<bool, ProfileError> {
/// Every `info.vdf` a pack carries: any depth in loose files, the root of a VPK.
fn hud_info_files(content: &ModContent) -> Result<Vec<Vec<u8>>, ProfileError> {
Ok(match content {
ModContent::Tree(entries) => entries.iter().any(|(path, bytes)| {
path.rsplit('/')
.next()
.is_some_and(|name| name.eq_ignore_ascii_case("info.vdf"))
&& crate::hud::is_current_hud_info(bytes)
}),
ModContent::Tree(entries) => entries
.iter()
.filter(|(path, _)| {
path.rsplit('/')
.next()
.is_some_and(|name| name.eq_ignore_ascii_case("info.vdf"))
})
.map(|(_, bytes)| bytes.clone())
.collect(),
ModContent::Vpk(bytes) => {
let info =
read_vpk_dir_bytes_filtered(bytes, &|path| path.eq_ignore_ascii_case("info.vdf"))
.map_err(|err| ProfileError::Io(err.message()))?;
info.files
.values()
.any(|bytes| crate::hud::is_current_hud_info(bytes))
read_vpk_dir_bytes_filtered(bytes, &|path| path.eq_ignore_ascii_case("info.vdf"))
.map_err(|err| ProfileError::Io(err.message()))?
.files
.into_values()
.collect()
}
})
}

/// Whether a pack is a HUD: it carries an `info.vdf` declaring the current UI
/// version. Detection requires real compatibility data.
pub fn is_hud_content(content: &ModContent) -> Result<bool, ProfileError> {
Ok(hud_info_files(content)?
.iter()
.any(|info| crate::hud::is_current_hud_info(info)))
}

/// Why a pack is a HUD TF2 no longer loads: an `info.vdf` declares an older or
/// newer UI version and none declares the current one. `None` for a current
/// HUD, and for a pack whose `info.vdf` is missing or declares no UI version,
/// which is an ordinary mod.
pub fn outdated_hud_reason(content: &ModContent) -> Result<Option<String>, ProfileError> {
let infos = hud_info_files(content)?;
if infos
.iter()
.any(|info| crate::hud::is_current_hud_info(info))
{
return Ok(None);
}
Ok(infos
.iter()
.find_map(|info| crate::hud::other_ui_version_reason(info)))
}

/// An outdated HUD would install as a pack TF2 ignores, so it is refused with
/// the reason the HUD importer gives.
fn refuse_outdated_hud_mod(content: &ModContent) -> Result<(), ProfileError> {
match outdated_hud_reason(content)? {
Some(reason) => Err(ProfileError::OutdatedHud(format!(
"{reason} No selected files were installed."
))),
None => Ok(()),
}
}

/// HUDs need the one-HUD replacement review, including when a mod picker or
/// GameBanana supplied their bytes.
fn refuse_hud_mod(content: &ModContent) -> Result<(), ProfileError> {
Expand Down Expand Up @@ -1285,6 +1323,28 @@ pub fn read_mod_pcf(

#[cfg(test)]
mod tests {
#[test]
fn an_outdated_hud_is_named_and_an_ordinary_or_current_pack_is_not() {
let pack = |info: Option<&[u8]>| {
let mut files = vec![("resource/ui/hudlayout.res".to_string(), vec![1])];
files.extend(info.map(|bytes| ("info.vdf".to_string(), bytes.to_vec())));
ModContent::Tree(files)
};
let old = pack(Some(b"\"hud\" { \"ui_version\" \"2\" }"));
let reason = outdated_hud_reason(&old).unwrap().unwrap();
assert!(reason.contains("UI version 2; TF2 requires 3"), "{reason}");
let error = refuse_outdated_hud_mod(&old).unwrap_err();
assert_eq!(error.code(), "OutdatedHud");
assert!(error.message().contains("Get an updated copy"));
// An `info.vdf` that declares no UI version is not HUD metadata.
let addon = pack(Some(b"\"Addon\" { \"version\" \"3\" }"));
assert_eq!(outdated_hud_reason(&addon).unwrap(), None);
assert_eq!(outdated_hud_reason(&pack(None)).unwrap(), None);
let current = pack(Some(b"\"hud\" { \"ui_version\" \"3\" }"));
assert_eq!(outdated_hud_reason(&current).unwrap(), None);
assert!(refuse_outdated_hud_mod(&current).is_ok());
}

#[test]
fn oversized_mods_name_their_size_the_limit_and_the_manual_route() {
let message = oversized_mod_message(Some(1_503_238_554));
Expand Down
5 changes: 4 additions & 1 deletion apps/desktop/src-tauri/core/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub enum ProfileError {
HudReviewRequired,
HudLiveReviewRequired,
HudImportRequired(String),
/// A pack carries HUD metadata TF2 no longer accepts.
OutdatedHud(String),
KeptPackHandoff(Vec<String>),
PendingLiveHandoff,
/// A drive lacks room for a write; the message names the drive and sizes.
Expand Down Expand Up @@ -154,6 +156,7 @@ impl ProfileError {
Self::HudReviewRequired => "HudReviewRequired",
Self::HudLiveReviewRequired => "HudLiveReviewRequired",
Self::HudImportRequired(_) => "HudImportRequired",
Self::OutdatedHud(_) => "OutdatedHud",
Self::KeptPackHandoff(_) => "KeptPackHandoff",
Self::PendingLiveHandoff => "PendingLiveHandoff",
Self::NotEnoughSpace(_) => "NotEnoughSpace",
Expand Down Expand Up @@ -200,7 +203,7 @@ impl ProfileError {
"{name} is still saved as a Casual particle source on this profile. Deselect it and choose Apply mods, or Restore stock files, before removing its source pack."
),
Self::HudReviewRequired | Self::HudLiveReviewRequired => "This setup contains more than one HUD. Review which HUD to keep before continuing. Original files will be preserved outside mounted HUD folders.".into(),
Self::HudImportRequired(message) => message.clone(),
Self::HudImportRequired(message) | Self::OutdatedHud(message) => message.clone(),
Self::KeptPackHandoff(packs) => format!(
"These kept packs are still installed: {}. Capture them in this profile before switching, or remove them from TF2 yourself.",
packs.join(", ")
Expand Down
Loading
Loading