quake 3 | list shaders in quake 3 shader | search textures directory for paths | Search

This code snippet automates the collection of all shader definitions from a Quake 3 mod directory, storing them in a JSON file for convenient access.

Run example

npm run import -- "find all shaders"

find all shaders

var fs = require('fs')
var path = require('path')
var importer = require('../Core')
var {glob} = importer.import("glob files")
var findShaders = importer.import("list shaders in quake 3 shader")

async function findAllShaders(modDir) {
    var allShaders = {}
    var shaders = await glob('**/*.shader', modDir)
    for(var i = 0; i < shaders.length; i++) {
        console.log(shaders[i])
        Object.assign(allShaders, findShaders(shaders[i]))
    }
    fs.writeFileSync(path.join(modDir, 'all_shaders.json'),
                     JSON.stringify(allShaders, null, 2))
    return Object.keys(allShaders)
}

module.exports = findAllShaders

What the code could have been:

const fs = require('fs');
const path = require('path');
const { glob } = require('glob');
const importer = require('../Core');

/**
 * Finds all shaders in the given directory and its subdirectories.
 * 
 * @param {string} modDir - The directory to search for shaders.
 * @returns {Promise} An array of shader names.
 */
async function findAllShaders(modDir) {
    // Initialize an object to store all shaders
    const allShaders = {};

    try {
        // Get all shader files in the directory
        const shaderFiles = await glob(path.join(modDir, '**/*.shader'));

        // Iterate over each shader file
        for (const shaderFile of shaderFiles) {
            // Get the shader definition and add it to the allShaders object
            const shaders = await findShaders(shaderFile);
            Object.assign(allShaders, shaders);
        }

        // Write the allShaders object to a JSON file
        fs.writeFileSync(path.join(modDir, 'all_shaders.json'), JSON.stringify(allShaders, null, 2));

        // Return the names of all shaders
        return Object.keys(allShaders);
    } catch (error) {
        // Log any errors that occur during the process
        console.error(`Error finding shaders in ${modDir}:`, error);
    }
}

module.exports = findAllShaders;

// TODO: Consider using a more robust logging mechanism
// TODO: Add input validation for the modDir parameter
// TODO: Consider using a more efficient data structure than an object for allShaders

This code snippet is designed to find and collect all shader definitions from a Quake 3 mod directory.

Here's a breakdown:

  1. Dependencies:

  2. findAllShaders Function:

  3. Export: The findAllShaders function is exported as a module, allowing it to be used in other parts of a larger application.

In essence, this code snippet automates the process of collecting all shader definitions from a Quake 3 mod directory, organizing them into a JSON file for easier access and analysis.