This code analyzes a Quake 3 map file, identifies used textures, locates them within the mod directory, and optionally organizes them into a specified output directory.
npm run import -- "search textures directory for paths"
var path = require('path')
var importer = require('../Core')
var {glob} = importer.import("glob files")
var {chext, chroot} = importer.import("changing file name")
var listTextures = importer.import("list textures in quake 3 map")
var mkdirpSync = importer.import("mkdirp")
//var findShaders = importer.import("list shaders in quake 3 shader")
async function findTextures(file, modDir, outDir) {
var textures = listTextures(file)
var shaders = require(path.join(modDir, 'all_shaders.json'))
var textureCount = textures.length
var shaded = []
for(var i = 0; i < textureCount; i++) {
var realTexture = textures[i].includes('textures') ? textures[i] : ('textures/' + textures[i])
if(typeof shaders[realTexture] != 'undefined') {
console.log('shaded ' + realTexture)
shaded.push(textures[i])
var shader = shaders[realTexture]
var extraTextures = importer
.regexToArray(/map\s([^\s]+?\/[^\s]+?$)/img, shader, 1)
.map(t => chext(t, ''))
textures.push.apply(textures, extraTextures)
}
}
var defaults = textures
.filter(t => !shaded.includes(t))
var matched = await glob(defaults.map(t => '**/' + t + '*'), modDir)
var listStr = matched.join(';')
var unmatched = defaults
.filter(t => !listStr.includes(path.basename(t)))
if(outDir) {
for(var i = 0; i < matched.length; i++) {
var outFile = chroot(matched[i], modDir, outDir)
mkdirpSync(path.dirname(outFile))
fs.copyFileSync(matched[i], outFile)
}
}
return {
matched: matched,
unmatched: unmatched
}
}
module.exports = findTextures
const path = require('path');
const { globSync } = require('glob');
const mkdirpSync = require('mkdirp').sync;
const { regexToArray } = require('../Core/utils');
const importer = Object.freeze({
glob: require('../Core/globFiles'),
changingFileName: require('../Core/changingFileName'),
listTextures: require('../Core/listTexturesInQuake3Map'),
// listShaders: require('../Core/listShadersInQuake3Shader') // Commented out as not used
});
class TextureFinder {
async findTextures(file, modDir, outDir) {
const {
glob: { files: globFiles },
changingFileName: { chext, chroot },
listTextures: { textures },
} = importer;
const shaders = require(path.join(modDir, 'all_shaders.json'));
const defaultTextures = textures(file);
const shadedTextures = [];
// Identify textures with shaders
for (const texture of defaultTextures) {
const realTexture = texture.includes('textures')? texture : `textures/${texture}`;
if (Object.prototype.hasOwnProperty.call(shaders, realTexture)) {
console.log(`Shaded: ${realTexture}`);
shadedTextures.push(texture);
const shader = shaders[realTexture];
const extraTextures = regexToArray(/map\s([^\s]+?\/[^\s]+?$)/gim, shader, 1).map((t) => chext(t, ''));
defaultTextures.push(...extraTextures);
}
}
// Find unshaded textures in modDir
const defaults = defaultTextures.filter((t) =>!shadedTextures.includes(t));
const matched = globFiles(defaults.map((t) => `**/${t}*`), modDir);
const unmatched = defaults.filter((t) =>!matched.join(';').includes(path.basename(t)));
// Copy matched files to outDir
if (outDir) {
for (const file of matched) {
const outFile = chroot(file, modDir, outDir);
mkdirpSync(path.dirname(outFile));
const fs = require('fs');
fs.copyFileSync(file, outFile);
}
}
return {
matched,
unmatched,
};
}
}
module.exports = TextureFinder;
This code snippet analyzes a Quake 3 map file to identify textures used within it and then attempts to locate and organize those textures within a specified output directory.
Here's a breakdown:
Dependencies:
path
: For working with file paths.Core
: A custom module likely containing utility functions and imported modules.glob
: A module for finding files matching a pattern.chext
: A function (imported from Core
) for extracting file extensions.chroot
: A function (imported from Core
) for changing the root directory of a file path.listTextures
: A custom function (imported from Core
) that extracts texture names from a Quake 3 map file.mkdirp
: A module for creating directories recursively.findTextures
Function:
file
: The path to the Quake 3 map file.modDir
: The directory containing the Quake 3 mod.outDir
: The directory to output the found textures to (optional).listTextures
to get a list of textures used in the map.all_shaders.json
containing shader definitions from the modDir
.glob
to find matching files for the textures without shaders.outDir
is provided:
chroot
to adjust the root directory.In essence, this code snippet helps analyze Quake 3 map files, identify textures used, locate them within the mod directory, and optionally organize them into a separate output directory.