This code snippet parses a text file, likely a Quake game configuration, to extract shader definitions enclosed within curly braces. It returns an object mapping shader names to their corresponding definitions.
npm run import -- "list shaders in quake 3 shader"
var importer = require('../Core')
var balanced = importer.import("balanced")
function findShaders(file) {
if(typeof file === 'string' && fs.existsSync(file)) {
file = fs.readFileSync(file).toString('utf-8')
.replace(/\/\/.*/ig, '')
}
var match
var current = file
var result = {}
while((match = balanced('{', '}', current))) {
var name = match.pre.trim()
console.log('found shader ' + name)
result[name] = match.body
current = match.post
}
return result
}
module.exports = findShaders
const fs = require('fs');
const importer = require('../Core');
/**
* Finds and extracts shaders from a given file.
*
* @param {string} file - Path to the file containing shaders.
* @returns {Object} An object containing the extracted shaders.
*/
function findShaders(file) {
// Load the content of the file, if it's a file path, or use the file content directly.
const fileContent = fs.existsSync(file)? fs.readFileSync(file, 'utf-8') : file;
// Remove any multi-line comments to make file parsing easier.
const parsedContent = fileContent.replace(/\/\/.*\n/g, '');
// Regular expression to match shader definitions.
const shaderRegex = /([^{}]+)\s*\{\s*(.*?)\s*\}/g;
// Initialize an object to store the extracted shaders.
const shaders = {};
// Loop through all shader definitions in the file.
let match;
while ((match = shaderRegex.exec(parsedContent))) {
// Extract the shader name and body.
const name = match[1].trim();
const shaderBody = match[2];
// Log the found shader and add it to the result object.
console.log(`Found shader: ${name}`);
shaders[name] = shaderBody;
}
return shaders;
}
module.exports = findShaders;
This code snippet is designed to extract shader definitions from a text file, likely a Quake game configuration or script file.
Here's a breakdown:
Dependencies: It imports the balanced
module from a custom Core
module, which likely provides functionality for parsing balanced parentheses or brackets in text.
findShaders
Function:
//
), and stores the cleaned text in the current
variable.while
loop and the balanced
function to find pairs of opening and closing curly braces ({
, }
) within the text.name
).match.body
) in a result
object, using the shader name as the key.current
variable to the text after the closing brace, continuing the search.result
object containing the extracted shader names and their definitions.Export: The findShaders
function is exported as a module, allowing it to be used in other parts of a larger application.
In essence, this code snippet provides a way to parse and extract shader definitions from a text file, likely used for analyzing or modifying Quake game assets.