From a2754259437789ec271800e26b217cbef2bac180 Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Tue, 22 Mar 2022 23:06:08 +0000 Subject: [PATCH] Fix atlas and palette scripts pathing --- src/ui/layout.ts | 6 ++---- src/util.ts | 1 + tools/build-atlas.ts | 29 ++++++++++++++++------------- tools/build-palette.ts | 5 +++-- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/ui/layout.ts b/src/ui/layout.ts index ec1bb73..1043cc1 100644 --- a/src/ui/layout.ts +++ b/src/ui/layout.ts @@ -5,10 +5,9 @@ import { FileInputElement } from './elements/file_input'; import { ButtonElement } from './elements/button'; import { OutputElement } from './elements/output'; import { Action, AppContext } from '../app_context'; -import { ASSERT, ATLASES_DIR, LOG } from '../util'; +import { ASSERT, ATLASES_DIR, LOG, PALETTES_DIR } from '../util'; import fs from 'fs'; -import path from 'path'; import { ToolbarItemElement } from './elements/toolbar_item'; import { EAppEvent } from '../event'; import { MeshType, Renderer } from '../renderer'; @@ -458,9 +457,8 @@ export class UI { private _getBlockPalettes(): ComboBoxItem[] { const blockPalettes: ComboBoxItem[] = []; - const palettesDir = path.join(__dirname, '../../../resources/palettes'); - fs.readdirSync(palettesDir).forEach((file) => { + fs.readdirSync(PALETTES_DIR).forEach((file) => { if (file.endsWith('.palette')) { const paletteID = file.split('.')[0]; let paletteName = paletteID.replace('-', ' ').toLowerCase(); diff --git a/src/util.ts b/src/util.ts index 2d993b5..acf1b9f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -294,6 +294,7 @@ export const ATLASES_DIR = path.join(RESOURCES_DIR, './atlases'); export const PALETTES_DIR = path.join(RESOURCES_DIR, './palettes/'); export const STATIC_DIR = path.join(RESOURCES_DIR, './static/'); export const SHADERS_DIR = path.join(RESOURCES_DIR, './shaders/'); +export const TOOLS_DIR = path.join(BASE_DIR, './tools/'); export function getRandomID(): string { return (Math.random() + 1).toString(36).substring(7); diff --git a/tools/build-atlas.ts b/tools/build-atlas.ts index 045cc4e..10e3ff0 100644 --- a/tools/build-atlas.ts +++ b/tools/build-atlas.ts @@ -1,4 +1,4 @@ -import { RGB, UV } from '../src/util'; +import { ATLASES_DIR, RGB, TOOLS_DIR, UV } from '../src/util'; import { log, logBreak, LogStyle } from './logging'; import { isDirSetup, ASSERT, getAverageColour, getPermission } from './misc'; @@ -21,8 +21,8 @@ void async function main() { }(); function cleanupDirectories() { - fs.rmSync(path.join(__dirname, '/blocks'), { recursive: true, force: true }); - fs.rmSync(path.join(__dirname, '/models'), { recursive: true, force: true }); + fs.rmSync(path.join(TOOLS_DIR, '/blocks'), { recursive: true, force: true }); + fs.rmSync(path.join(TOOLS_DIR, '/models'), { recursive: true, force: true }); } async function getResourcePack() { @@ -85,9 +85,9 @@ function fetchVanillModelsAndTextures(fetchTextures: boolean) { const zipEntries = zip.getEntries(); zipEntries.forEach((zipEntry: any) => { if (fetchTextures && zipEntry.entryName.startsWith('assets/minecraft/textures/block')) { - zip.extractEntryTo(zipEntry.entryName, path.join(__dirname, './blocks'), false, true); + zip.extractEntryTo(zipEntry.entryName, path.join(TOOLS_DIR, './blocks'), false, true); } else if (zipEntry.entryName.startsWith('assets/minecraft/models/block')) { - zip.extractEntryTo(zipEntry.entryName, path.join(__dirname, './models'), false, true); + zip.extractEntryTo(zipEntry.entryName, path.join(TOOLS_DIR, './models'), false, true); } }); log(LogStyle.Success, `Extracted textures and models successfully\n`); @@ -109,7 +109,7 @@ async function fetchModelsAndTextures() { if (fs.lstatSync(resourcePackDir).isDirectory()) { log(LogStyle.Info, `Resource pack '${resourcePack}' is a directory`); const blockTexturesSrc = path.join(resourcePackDir, 'assets/minecraft/textures/block'); - const blockTexturesDst = path.join(__dirname, '../tools/blocks'); + const blockTexturesDst = path.join(TOOLS_DIR, './blocks'); log(LogStyle.Info, `Copying ${blockTexturesSrc} to ${blockTexturesDst}`); copydir(blockTexturesSrc, blockTexturesDst, { utimes: true, @@ -124,7 +124,7 @@ async function fetchModelsAndTextures() { const zipEntries = zip.getEntries(); zipEntries.forEach((zipEntry: any) => { if (zipEntry.entryName.startsWith('assets/minecraft/textures/block')) { - zip.extractEntryTo(zipEntry.entryName, path.join(__dirname, './blocks'), false, true); + zip.extractEntryTo(zipEntry.entryName, path.join(TOOLS_DIR, './blocks'), false, true); } }); log(LogStyle.Success, `Copied block textures successfully`); @@ -149,7 +149,7 @@ async function buildAtlas() { logBreak(); log(LogStyle.Info, 'Loading ignore list...'); let ignoreList: Array = []; - const ignoreListPath = path.join(__dirname, './ignore-list.txt'); + const ignoreListPath = path.join(TOOLS_DIR, './ignore-list.txt'); if (fs.existsSync(ignoreListPath)) { log(LogStyle.Success, 'Found ignore list'); ignoreList = fs.readFileSync(ignoreListPath, 'utf-8').replace(/\r/g, '').split('\n'); @@ -186,13 +186,14 @@ async function buildAtlas() { log(LogStyle.Info, 'Loading block models...'); const faces = ['north', 'south', 'up', 'down', 'east', 'west']; const allModels: Array = []; + const allBlockNames: Set = new Set(); const usedTextures: Set = new Set(); - fs.readdirSync(path.join(__dirname, './models')).forEach((filename) => { + fs.readdirSync(path.join(TOOLS_DIR, './models')).forEach((filename) => { if (path.extname(filename) !== '.json') { return; }; - const filePath = path.join(__dirname, './models', filename); + const filePath = path.join(TOOLS_DIR, './models', filename); const fileData = fs.readFileSync(filePath, 'utf8'); const modelData = JSON.parse(fileData); const parsedPath = path.parse(filePath); @@ -266,6 +267,7 @@ async function buildAtlas() { name: modelName, faces: faceData, }); + allBlockNames.add(modelName); }); if (allModels.length === 0) { log(LogStyle.Failure, 'No blocks loaded'); @@ -296,7 +298,7 @@ async function buildAtlas() { log(LogStyle.Info, `Building ${atlasName}.png...`); usedTextures.forEach((textureName) => { const shortName = textureName.split('/')[1]; // Eww - const absolutePath = path.join(__dirname, './blocks', shortName + '.png'); + const absolutePath = path.join(TOOLS_DIR, './blocks', shortName + '.png'); const fileData = fs.readFileSync(absolutePath); const pngData = PNG.sync.read(fileData); const image = images(absolutePath); @@ -343,14 +345,15 @@ async function buildAtlas() { log(LogStyle.Info, 'Exporting...'); - const atlasDir = path.join(__dirname, `../../resources/atlases/${atlasName}.png`); + const atlasDir = path.join(ATLASES_DIR, `./${atlasName}.png`); outputImage.save(atlasDir); log(LogStyle.Success, `${atlasName}.png exported to /resources/atlases/`); const outputJSON = { atlasSize: atlasSize, blocks: allModels, + supportedBlockNames: Array.from(allBlockNames), }; - fs.writeFileSync(path.join(__dirname, `../../resources/atlases/${atlasName}.atlas`), JSON.stringify(outputJSON, null, 4)); + fs.writeFileSync(path.join(ATLASES_DIR, `./${atlasName}.atlas`), JSON.stringify(outputJSON, null, 4)); log(LogStyle.Success, `${atlasName}.atlas exported to /resources/atlases/\n`); /* eslint-disable */ diff --git a/tools/build-palette.ts b/tools/build-palette.ts index a540c92..cd8131d 100644 --- a/tools/build-palette.ts +++ b/tools/build-palette.ts @@ -1,4 +1,5 @@ import { log, LogStyle } from './logging'; +import { TOOLS_DIR, PALETTES_DIR } from '../src/util'; import fs from 'fs'; import path from 'path'; @@ -7,7 +8,7 @@ import prompt from 'prompt'; void async function main() { log(LogStyle.Info, 'Creating a new palette...'); - const paletteBlocksDir = path.join(__dirname, '../../tools/new-palette-blocks.txt'); + const paletteBlocksDir = path.join(TOOLS_DIR, './new-palette-blocks.txt'); if (!fs.existsSync(paletteBlocksDir)) { log(LogStyle.Failure, 'Could not find /tools/new-palette-blocks.txt'); return; @@ -42,6 +43,6 @@ void async function main() { blocks: blocksToUse, }; - fs.writeFileSync(path.join(__dirname, `../resources/palettes/${promptUser.paletteName}.palette`), JSON.stringify(paletteJSON, null, 4)); + fs.writeFileSync(path.join(PALETTES_DIR, `./${promptUser.paletteName}.palette`), JSON.stringify(paletteJSON, null, 4)); log(LogStyle.Success, `Successfully created ${promptUser.paletteName}.palette in /resources/palettes/`); }();