Skip to content

Commit

Permalink
feat(ArxMap): fill in metadata values from generator's and project's …
Browse files Browse the repository at this point in the history
…package.json
  • Loading branch information
meszaros-lajos-gyorgy committed Aug 24, 2023
1 parent 255eefc commit ab84256
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
15 changes: 11 additions & 4 deletions src/ArxMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { Vector3 } from '@src/Vector3.js'
import { Zone } from '@src/Zone.js'
import { MapFinalizedError, MapNotFinalizedError } from '@src/errors.js'
import { times } from '@src/faux-ramda.js'
import { getGeneratorPackageJSON, latin9ToLatin1 } from '@src/helpers.js'
import { getGeneratorPackageJSON, getProjectPackageJSON, latin9ToLatin1 } from '@src/helpers.js'
import { OriginalLevel } from '@src/types.js'
import { compile } from './compile.js'

Expand All @@ -57,7 +57,7 @@ type ToBeSortedLater = {
}

export class ArxMap {
meta: MetaData = new MetaData()
meta = new MetaData()
polygons = new Polygons()
lights = new Lights()
fogs: Fog[] = []
Expand Down Expand Up @@ -219,7 +219,7 @@ export class ArxMap {

private static async getGeneratorId() {
const generator = await getGeneratorPackageJSON()
return `Arx Level Generator - v.${generator.version}`
return `${generator.name} - v.${generator.version}`
}

finalize() {
Expand Down Expand Up @@ -348,11 +348,18 @@ export class ArxMap {
await Manifest.uninstall(settings)

const generator = await getGeneratorPackageJSON()
// const project = await getProjectPackageJSON()
this.meta.generator = generator.name
this.meta.generatorVersion = generator.version
this.meta.generatorUrl = generator.homepage
this.meta.seed = settings.seed

const project = await getProjectPackageJSON()
this.meta.name = project.name
this.meta.version = project.version
this.meta.description = project.description
this.meta.author = project.author
this.meta.url = project.homepage

// ------------------------

let textures = await this.polygons.exportTextures(settings)
Expand Down
2 changes: 1 addition & 1 deletion src/MetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class MetaData {
version = ''
author = ''
url = ''
generator = 'Arx Level Generator'
generator = ''
generatorUrl = ''
generatorVersion = ''
seed = ''
Expand Down
30 changes: 25 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ type PackageJsonProps = {
homepage: string
}

let cacheOfPackageJSON: PackageJsonProps
let cacheOfGeneratorPackageJSON: PackageJsonProps
let cacheOfProjectPackageJSON: PackageJsonProps

export const getGeneratorPackageJSON = async (): Promise<PackageJsonProps> => {
if (typeof cacheOfPackageJSON === 'undefined') {
if (typeof cacheOfGeneratorPackageJSON === 'undefined') {
try {
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const rawIn = await fs.promises.readFile(path.resolve(__dirname, '../../package.json'), 'utf-8')
cacheOfPackageJSON = JSON.parse(rawIn)
cacheOfGeneratorPackageJSON = JSON.parse(rawIn)
} catch (error) {
cacheOfPackageJSON = {
cacheOfGeneratorPackageJSON = {
name: '',
version: '',
description: '',
Expand All @@ -34,7 +35,26 @@ export const getGeneratorPackageJSON = async (): Promise<PackageJsonProps> => {
}
}

return cacheOfPackageJSON
return cacheOfGeneratorPackageJSON
}

export const getProjectPackageJSON = async (): Promise<PackageJsonProps> => {
if (typeof cacheOfProjectPackageJSON === 'undefined') {
try {
const rawIn = await fs.promises.readFile(path.resolve('./package.json'), 'utf-8')
cacheOfProjectPackageJSON = JSON.parse(rawIn)
} catch (error) {
cacheOfProjectPackageJSON = {
name: '',
version: '',
description: '',
author: '',
homepage: '',
}
}
}

return cacheOfProjectPackageJSON
}

export const evenAndRemainder = (divisor: number, n: number): [number, number] => {
Expand Down

0 comments on commit ab84256

Please sign in to comment.