fix: enforce asset naming convention
This commit is contained in:
+35
-1
@@ -3,6 +3,7 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
import { ASSET_EXTENSIONS, TEXTURE_EXTENSIONS } from '@/lib/constants'
|
||||
import { formatAssetFamilies, getAssetFamily } from '@/lib/asset-naming'
|
||||
import type { TextureFile } from '@/lib/client-types'
|
||||
|
||||
const SUPPORT_FILE_EXT_ARRAY = [...TEXTURE_EXTENSIONS, ...ASSET_EXTENSIONS]
|
||||
@@ -33,6 +34,32 @@ function getReferencedBufferNames(gltf: GltfJson) {
|
||||
.filter((filename): filename is string => Boolean(filename))
|
||||
}
|
||||
|
||||
function getFileExtension(filename: string) {
|
||||
return filename.slice(filename.lastIndexOf('.')).toLowerCase()
|
||||
}
|
||||
|
||||
function getFileStem(filename: string) {
|
||||
return filename.replace(/\.[^.]+$/, '')
|
||||
}
|
||||
|
||||
function getTextureNamingError(file: File) {
|
||||
const stem = getFileStem(file.name)
|
||||
const [prefix, ...targetParts] = stem.split('_')
|
||||
const family = getAssetFamily(prefix)
|
||||
|
||||
if (family && targetParts.every(Boolean)) return null
|
||||
|
||||
const reversedParts = stem.split('_')
|
||||
const reversedFamily = reversedParts.length > 1 ? getAssetFamily(reversedParts[reversedParts.length - 1]) : undefined
|
||||
|
||||
if (reversedFamily) {
|
||||
const target = reversedParts.slice(0, -1).join('_')
|
||||
return `Convention invalide : ${file.name}. Utilisez ${reversedFamily}_${target}.${file.name.split('.').pop()} pour cibler "${target}", ou ${reversedFamily}.${file.name.split('.').pop()} pour tout le modele.`
|
||||
}
|
||||
|
||||
return `Asset inconnu : ${file.name}. Familles autorisees : ${formatAssetFamilies()}. Utilisez asset.png pour tout le modele ou asset_objet.png pour cibler un objet.`
|
||||
}
|
||||
|
||||
async function getGltfWarnings(model: File, supportFiles: File[]) {
|
||||
const warnings: string[] = []
|
||||
let parsed: unknown
|
||||
@@ -82,10 +109,17 @@ export async function validateFolder(files: File[]): Promise<ValidationResult> {
|
||||
}
|
||||
|
||||
const supportFiles = files.filter((f) => {
|
||||
const ext = f.name.slice(f.name.lastIndexOf('.')).toLowerCase()
|
||||
const ext = getFileExtension(f.name)
|
||||
return SUPPORT_FILE_EXT_ARRAY.includes(ext)
|
||||
})
|
||||
|
||||
const textureNamingErrors = supportFiles
|
||||
.filter((file) => TEXTURE_EXTENSIONS.has(getFileExtension(file.name)))
|
||||
.map(getTextureNamingError)
|
||||
.filter((error): error is string => Boolean(error))
|
||||
|
||||
errors.push(...textureNamingErrors)
|
||||
|
||||
for (const tf of supportFiles) {
|
||||
textures.push({ name: tf.name, file: tf })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user