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.
npm run import -- "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
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:
Dependencies:
fs
: For file system operations (reading and writing files).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 (used to locate shader files).findShaders
: A custom function (imported from Core
) that extracts shader definitions from a single shader file.findAllShaders
Function:
modDir
(directory path of the Quake 3 mod) as input.allShaders
to store the collected shader definitions.glob
to find all files with the .shader
extension within the modDir
.findShaders
to extract shader definitions from the current file.Object.assign
to merge the extracted definitions into the allShaders
object.allShaders
object as a JSON file named all_shaders.json
in the modDir
.allShaders
object).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.