Skip to content

Commit

Permalink
refactor(ArxMap): generate metadata in one go when saving to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Aug 24, 2023
1 parent 11a930c commit 35a9956
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 63 deletions.
20 changes: 4 additions & 16 deletions src/ArxMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { LevelLoader } from '@src/LevelLoader.js'
import { Light } from '@src/Light.js'
import { Lights } from '@src/Lights.js'
import { Manifest } from '@src/Manifest.js'
import { MetaData } from '@src/MetaData.js'
import { generateMetadata } from '@src/MetaData.js'
import { Path } from '@src/Path.js'
import { Player } from '@src/Player.js'
import { Polygon } from '@src/Polygon.js'
Expand Down Expand Up @@ -57,7 +57,6 @@ type ToBeSortedLater = {
}

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

await Manifest.uninstall(settings)

const generator = await getGeneratorPackageJSON()
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
const meta = await generateMetadata(settings)

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

Expand Down Expand Up @@ -508,7 +496,7 @@ export class ArxMap {
for (const [filename, translation] of Object.entries(translations)) {
await fs.promises.writeFile(
filename,
`// ${this.meta.name} v.${this.meta.version} - ${generatorId}
`// ${meta.name} v.${meta.version} - ${generatorId}
${translation}`,
'utf8',
Expand Down Expand Up @@ -547,7 +535,7 @@ export class ArxMap {

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

await Manifest.write(settings, this, pathsOfTheFiles)
await Manifest.write(settings, pathsOfTheFiles)
}

adjustOffsetTo(map: ArxMap) {
Expand Down
24 changes: 10 additions & 14 deletions src/Manifest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'node:fs'
import path from 'node:path'
import { ArxMap } from '@src/ArxMap.js'
import { MetaData } from '@src/MetaData.js'
import { MetaData, generateMetadata } from '@src/MetaData.js'
import { Settings } from '@src/Settings.js'
import { fileExists } from './helpers.js'

export type ManifestData = ReturnType<MetaData['toData']> & {
export type ManifestData = MetaData & {
files: string[]
}

Expand All @@ -21,31 +21,27 @@ export class Manifest {
return await fileExists(filename)
}

static async read(settings: Settings): Promise<ManifestData> {
static async read(settings: Settings): Promise<ManifestData | undefined> {
const filename = Manifest.getPathToFilename(settings)

const emptyMetadata = new MetaData()
const emptyManifest = {
...emptyMetadata.toData(),
files: [],
}

if (!(await Manifest.exists(settings))) {
return emptyManifest
return undefined
}

try {
const rawIn = await fs.promises.readFile(filename, 'utf-8')
return JSON.parse(rawIn)
} catch (e: unknown) {
console.error(`[error] Manifest: failed to read or parse "${Manifest.filename}" in "${settings.outputDir}"`)
return emptyManifest
return undefined
}
}

static async write(settings: Settings, map: ArxMap, files: string[], prettify: boolean = false) {
static async write(settings: Settings, files: string[], prettify: boolean = false) {
const metaData = await generateMetadata(settings)

const manifest: ManifestData = {
...map.meta.toData(),
...metaData,
files: files.map((file) => {
return file.replace(settings.outputDir, '')
}),
Expand All @@ -62,7 +58,7 @@ export class Manifest {
return
}

const manifest = await Manifest.read(settings)
const manifest = (await Manifest.read(settings)) ?? { files: [] }

for (let file of manifest.files) {
try {
Expand Down
56 changes: 34 additions & 22 deletions src/MetaData.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
export class MetaData {
name = ''
description = ''
version = ''
author = ''
url = ''
generator = ''
generatorUrl = ''
generatorVersion = ''
seed = ''
import { Settings, Variant } from './Settings.js'
import { getGeneratorPackageJSON, getProjectPackageJSON } from './helpers.js'

toData() {
return {
name: this.name,
description: this.description,
version: this.version,
author: this.author,
url: this.url,
generator: this.generator,
generatorVersion: this.generatorVersion,
generatorUrl: this.generatorUrl,
seed: this.seed,
}
export type MetaData = {
seed: string
variant: Variant

generator: string
generatorVersion: string
generatorUrl: string

name: string
version: string
description: string
author: string
url: string
}

export const generateMetadata = async (settings: Settings): Promise<MetaData> => {
const generator = await getGeneratorPackageJSON()
const project = await getProjectPackageJSON()

return {
seed: settings.seed,
variant: settings.variant,

generator: generator.name,
generatorVersion: generator.version,
generatorUrl: generator.homepage,

name: project.name,
version: project.version,
description: project.description,
author: project.author,
url: project.homepage,
}
}
14 changes: 7 additions & 7 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type LightingCalculatorModes =
| 'DistanceAngleShadowNoTransparency'
| 'GI'

type Versions = 'normal' | 'premium'
export type Variant = 'normal' | 'premium'

type Modes = 'development' | 'production'

Expand Down Expand Up @@ -57,11 +57,11 @@ type SettingsConstructorProps = {
*/
seed?: string
/**
* This field allows branching between normal and premium versions
* This field allows branching between "normal" and "premium" variants
*
* default value is Versions.Normal
* default value is "normal"
*/
version?: Versions
variant?: Variant
/**
* This field allows branching the code based on what phase the project
* is in. For example a cutscene in the beginning of a map can be turned
Expand Down Expand Up @@ -115,11 +115,11 @@ export class Settings {
*/
readonly seed: string
/**
* This field allows branching between "normal" and "premium" versions
* This field allows branching between "normal" and "premium" variants
*
* default value is "normal"
*/
readonly version: Versions
readonly variant: Variant
/**
* This field allows branching the code based on what phase the project
* is in. For example a cutscene in the beginning of a map can be turned
Expand All @@ -142,7 +142,7 @@ export class Settings {
this.calculateLighting = props.calculateLighting ?? true
this.lightingCalculatorMode = props.lightingCalculatorMode ?? 'DistanceAngleShadowNoTransparency'
this.seed = props.seed ?? randomIntBetween(100_000_000, 999_999_999).toString()
this.version = props.version ?? 'normal'
this.variant = props.variant ?? 'normal'
this.mode = props.mode ?? 'production'

seedrandom(this.seed, { global: true })
Expand Down
4 changes: 2 additions & 2 deletions src/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class Texture extends ThreeJsTextue {
const image = await getSharpInstance(originalSource)

let quality = 100
if (settings.version !== 'premium' && !this.filename.endsWith('[icon].bmp')) {
if (settings.variant !== 'premium' && !this.filename.endsWith('[icon].bmp')) {
image.resize(Math.floor(this._width / 2), Math.floor(this._height / 2), { fit: 'cover' })
quality = 70
}
Expand Down Expand Up @@ -195,7 +195,7 @@ export class Texture extends ThreeJsTextue {

let newSize = powerOfTwo
let quality = 100
if (settings.version !== 'premium' && !this.filename.endsWith('[icon].bmp')) {
if (settings.variant !== 'premium' && !this.filename.endsWith('[icon].bmp')) {
newSize = newSize / 2
quality = 70
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Mesh, Object3D } from 'three'
import { Vector3 } from '@src/Vector3.js'
import { mean } from '@src/faux-ramda.js'

type PackageJsonProps = {
export type PackageJsonProps = {
name: string
version: string
description: string
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export { LevelLoader } from '@src/LevelLoader.js'
export { Light } from '@src/Light.js'
export { Lights } from '@src/Lights.js'
export { Material } from '@src/Material.js'
export { MetaData } from '@src/MetaData.js'
export { Path } from '@src/Path.js'
export { Player } from '@src/Player.js'
export { Polygon } from '@src/Polygon.js'
Expand Down

0 comments on commit 35a9956

Please sign in to comment.