Created
October 26, 2022 17:22
-
-
Save Kattoor/202701587395730c2d3348fcbd249465 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {globby} from 'globby'; | |
import fs from 'fs'; | |
import {execSync} from 'child_process'; | |
const path = 'C:\\Users\\jaspe\\out\\\\'; | |
const filePaths = (await globby('**/*.dds', {cwd: path})).map((pathSuffix) => path + pathSuffix.replace(/\//g, '\\')); | |
let count = 0; | |
for (let filePath of filePaths) { | |
const folder = filePath.split('\\').slice(0, -1).join('\\') + '\\'; | |
const fileName = filePath.split('\\').slice(-1)[0].slice(0, -4); | |
const allGroupFiles = (await globby(`**/${fileName}\.[!png]*`, {cwd: folder})).map((pathSuffix) => folder + pathSuffix); | |
if (allGroupFiles.length > 1) { | |
const relevantGroupFiles = [...allGroupFiles].filter((path) => !path.endsWith('a')); | |
relevantGroupFiles.sort((file1, file2) => file1.localeCompare(file2)); | |
relevantGroupFiles.splice(1, relevantGroupFiles.length - 2); | |
const [headerFilePath, dataFilePath] = relevantGroupFiles; | |
const headerFileBuffer = fs.readFileSync(headerFilePath); | |
const dataFileBuffer = fs.readFileSync(dataFilePath); | |
headerFileBuffer[0x1c] = 0; // set mip count | |
const fullFileBuffer = Buffer.concat([headerFileBuffer.slice(0, 0x94), dataFileBuffer]); | |
allGroupFiles.forEach((filePath) => fs.rmSync(filePath)); | |
fs.writeFileSync(`${folder}${fileName}.dds`, fullFileBuffer); | |
} | |
/*const buffers = allGroupFiles.map((path) => fs.readFileSync(path)); | |
const all = Buffer.concat(buffers); | |
allGroupFiles.forEach((path) => fs.rmSync(path)); | |
fs.writeFileSync(`${folder}${fileName}.dds`, all);*/ | |
try { | |
execSync(`texconv.exe "${fileName}.dds" -ft png -y`, {env: process.env, cwd: folder}); | |
fs.rmSync(filePath); | |
} catch (a) { | |
try { | |
execSync(`texconv.exe "${fileName}.dds" -ft png -f rgba -y`, {env: process.env, cwd: folder}); | |
fs.rmSync(filePath); | |
} catch (b) { | |
console.log(`Couldn't convert ${folder}${fileName}`); | |
} | |
} | |
console.log(`Assembled and converted ${++count} of ${filePaths.length}\t${folder}${fileName}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment