quake 3 | find all shaders | , disassemble all QVMs | Search

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.

Run example

npm run import -- "search textures directory for paths"

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

What the code could have been:

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:

  1. Dependencies:

  2. findTextures Function:

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.