Skip to content

Commit

Permalink
feat(compile): execute Fredlllll's lighting calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Aug 9, 2023
1 parent d2ef2a5 commit 3198cd7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

type LightingCalculatorModes =
| 'Distance'
| 'Danae'
| 'DistanceAngle'
| 'DistanceAngleShadow'
| 'DistanceAngleShadowNoTransparency'
| 'GI'

type SettingsConstructorProps = {
/**
* default value is "../pkware-test-files" relative to the project root
Expand All @@ -27,6 +35,10 @@ type SettingsConstructorProps = {
* if there are no lights, then this gets set to false
*/
calculateLighting?: boolean
/**
* default value is "DistanceAngleShadowNoTransparency"
*/
lightingCalculatorMode?: LightingCalculatorModes
}

export class Settings {
Expand All @@ -36,6 +48,7 @@ export class Settings {
levelIdx: number
assetsDir: string
calculateLighting: boolean
lightingCalculatorMode: LightingCalculatorModes
/**
* arx-level-generator comes with its own assets folder
*/
Expand All @@ -48,6 +61,7 @@ export class Settings {
this.levelIdx = props.levelIdx ?? 1
this.assetsDir = props.assetsDir ?? path.resolve('./assets')
this.calculateLighting = props.calculateLighting ?? true
this.lightingCalculatorMode = props.lightingCalculatorMode ?? 'DistanceAngleShadowNoTransparency'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand Down
50 changes: 48 additions & 2 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { exec } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { Readable } from 'node:stream'
import { fileURLToPath } from 'node:url'
import { promisify } from 'node:util'
import { DLF, FTS, LLF } from 'arx-convert'
import { ArxDLF, ArxFTS, ArxLLF } from 'arx-convert/types'
import { getHeaderSize } from 'arx-header-size'
Expand Down Expand Up @@ -72,8 +76,50 @@ const compileDLF = async (settings: Settings) => {
.pipe(fs.createWriteStream(path.join(dlfPath, `level${settings.levelIdx}.dlf`)))
}

const hasLights = async (settings: Settings) => {
const llfPath = path.join(settings.outputDir, `graph/levels/level${settings.levelIdx}`)
const llfJSONRaw = await fs.promises.readFile(path.join(llfPath, `level${settings.levelIdx}.llf.json`), 'utf-8')
const llfJSON = JSON.parse(llfJSONRaw) as ArxLLF

return llfJSON.lights.length > 0
}

export const compile = async (settings: Settings) => {
await Promise.all([compileFTS(settings), compileLLF(settings), compileDLF(settings)])
await Promise.allSettled([compileFTS(settings), compileLLF(settings), compileDLF(settings)])

if (settings.calculateLighting && (await hasLights(settings))) {
const operatingSystem = os.platform()

if (operatingSystem !== 'win32' && operatingSystem !== 'linux') {
console.error('ArxLibertatisLightingCalculator: unsupported platform')
return
}

const args = [
`--level "level${settings.levelIdx}"`,
`--arx-data-dir "${settings.outputDir}"`,
`--lighting-profile "${settings.lightingCalculatorMode}"`,
]

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const libPath = path.resolve(__dirname, '../../lib')

let exeFile: string
switch (operatingSystem) {
case 'win32':
exeFile = path.resolve(libPath, `fredlllll-lighting-calculator/win/ArxLibertatisLightingCalculator.exe`)
break
case 'linux':
exeFile = path.resolve(libPath, `fredlllll-lighting-calculator/linux/ArxLibertatisLightingCalculator`)
break
}

// calculate lighting
const { stdout, stderr } = await promisify(exec)(`${exeFile} ${args.join(' ')}`)
console.log(stdout)
if (stderr !== '') {
console.error(stderr)
}
}
}

0 comments on commit 3198cd7

Please sign in to comment.