From 02c12bbe5184db2386df95fdc7a70a3b64efdb7e Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 5 Nov 2024 11:54:47 -0500 Subject: [PATCH] usdUtils: initial implementation of CompressionValidator --- pxr/usd/usdUtils/plugInfo.json | 6 ++++ pxr/usd/usdUtils/validatorTokens.h | 18 ++++++------ pxr/usd/usdUtils/validators.cpp | 45 +++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/pxr/usd/usdUtils/plugInfo.json b/pxr/usd/usdUtils/plugInfo.json index 1962a094a6..d50f64e106 100644 --- a/pxr/usd/usdUtils/plugInfo.json +++ b/pxr/usd/usdUtils/plugInfo.json @@ -9,6 +9,12 @@ "UsdzValidators" ] }, + "CompressionValidator": { + "doc": "Files within a usdz package should not be compressed or encrypted.", + "keywords": [ + "UsdzValidators" + ] + }, "keywords": [ "UsdUtilsValidators" ] diff --git a/pxr/usd/usdUtils/validatorTokens.h b/pxr/usd/usdUtils/validatorTokens.h index cc0b7ac1df..2128c516b4 100644 --- a/pxr/usd/usdUtils/validatorTokens.h +++ b/pxr/usd/usdUtils/validatorTokens.h @@ -16,17 +16,19 @@ PXR_NAMESPACE_OPEN_SCOPE -#define USD_UTILS_VALIDATOR_NAME_TOKENS \ - ((packageEncapsulationValidator, "usdUtils:PackageEncapsulationValidator")) +#define USD_UTILS_VALIDATOR_NAME_TOKENS \ + ((packageEncapsulationValidator, "usdUtils:PackageEncapsulationValidator")) \ + ((compressionValidator, "usdUtils:compressionValidator")) -#define USD_UTILS_VALIDATOR_KEYWORD_TOKENS \ - (UsdUtilsValidators) \ +#define USD_UTILS_VALIDATOR_KEYWORD_TOKENS \ + (UsdUtilsValidators) \ (UsdzValidators) -#define USD_UTILS_VALIDATION_ERROR_NAME_TOKENS \ - ((layerNotInPackage, "LayerNotInPackage")) \ - ((assetNotInPackage, "AssetNotInPackage")) \ - ((invalidLayerInPackage, "InvalidLayerInPackage")) +#define USD_UTILS_VALIDATION_ERROR_NAME_TOKENS \ + ((layerNotInPackage, "LayerNotInPackage")) \ + ((assetNotInPackage, "AssetNotInPackage")) \ + ((invalidLayerInPackage, "InvalidLayerInPackage")) \ + ((compressionDetected, "CompressionDetected")) ///\def /// Tokens representing validator names. Note that for plugin provided diff --git a/pxr/usd/usdUtils/validators.cpp b/pxr/usd/usdUtils/validators.cpp index 64e4cd33c3..5376af2b58 100644 --- a/pxr/usd/usdUtils/validators.cpp +++ b/pxr/usd/usdUtils/validators.cpp @@ -11,7 +11,7 @@ #include "pxr/usd/usdUtils/validatorTokens.h" #include "pxr/usd/ar/packageUtils.h" #include "pxr/usd/usdUtils/dependencies.h" -#include "pxr/usd/usdUtils/userProcessingFunc.h" +#include "pxr/usd/usd/zipFile.h" PXR_NAMESPACE_OPEN_SCOPE @@ -104,11 +104,54 @@ _PackageEncapsulationValidator(const UsdStagePtr& usdStage) { return errors; } +static +UsdValidationErrorVector +_CompressionValidator(const UsdStagePtr& usdStage) { + const SdfLayerHandle &rootLayer = usdStage->GetRootLayer(); + const UsdZipFile zipFile = UsdZipFile::Open(rootLayer->GetRealPath().c_str()); + + std::string packagePath = ArSplitPackageRelativePathOuter(rootLayer->GetIdentifier()).first; + if (!zipFile) + { + return {}; + } + + UsdValidationErrorVector errors; + for(auto it = zipFile.begin(); it != zipFile.end(); ++it) + { + const UsdZipFile::FileInfo &fileInfo = it.GetFileInfo(); + if (fileInfo.compressionMethod != 0) + { + const std::string &fileName = *it; + errors.emplace_back( + UsdUtilsValidationErrorNameTokens->compressionDetected, + UsdValidationErrorType::Error, + UsdValidationErrorSites { + UsdValidationErrorSite( + rootLayer, SdfPath(fileName)) + }, + TfStringPrintf( + ("File '%s' in package '%s' has " + "compression. Compression method is '%u', actual size " + "is %lu. Uncompressed size is %lu."), + fileName.c_str(), packagePath.c_str(), + fileInfo.compressionMethod, + fileInfo.size, fileInfo.uncompressedSize) + ); + } + } + + return errors; +} + TF_REGISTRY_FUNCTION(UsdValidationRegistry) { UsdValidationRegistry& registry = UsdValidationRegistry::GetInstance(); registry.RegisterPluginValidator( UsdUtilsValidatorNameTokens->packageEncapsulationValidator, _PackageEncapsulationValidator); + + registry.RegisterPluginValidator( + UsdUtilsValidatorNameTokens->compressionValidator, _CompressionValidator); } PXR_NAMESPACE_CLOSE_SCOPE