refactor(tools): parsing - writing games data

This commit refactors the code to parse and write data from WoW game files. It creates a new function `parseData` which correctly parses the data and writes the output files to a new folder called `output`. The code now handles both `JSON` and `XML` data from game files. It also makes use of optional chaining to prevent null pointers while parsing the data. Finally, it removes an unused `callback` message without any effect.
This commit is contained in:
Walid 2023-04-28 14:40:45 +01:00
parent 868e675f8e
commit a25d278115
Signed by: Walidoux
GPG Key ID: CCF21881FE8BEBAF
3 changed files with 34 additions and 14 deletions

View File

@ -1,33 +1,44 @@
import { XMLParser } from 'fast-xml-parser' import { XMLParser } from 'fast-xml-parser'
import type { GameEndPointsTypes } from '../types'
import { FigureMap } from '../controllers/FigureMap' import { FigureMap } from '../controllers/FigureMap'
import { EffectMap } from '../controllers/EffectMap' import { EffectMap } from '../controllers/EffectMap'
import type { GameEndPointsTypes } from '../types'
import { convertTXT } from './convertTXT' import { convertTXT } from './convertTXT'
import { FigureData } from '../controllers/FigureData' import { FigureData } from '../controllers/FigureData'
import { FurniData } from '../controllers/FurniData' import { FurniData } from '../controllers/FurniData'
import { Convertion } from '../config/Convertion' import { Convertion } from '../config/Convertion'
import { parseData } from './parseData'
export const fetchGamedataConfig = async (data: string, endpoint: GameEndPointsTypes[number]): Promise<unknown> => { export const fetchGamedataConfig = async (data: string, endpoint: GameEndPointsTypes[number]): Promise<unknown> => {
switch (endpoint.convert) { switch (endpoint.convert) {
case 'XML': case 'XML':
const convertedData = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '' }).parse(data) const convertedData = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '' }).parse(data)
let parsedData: FigureData | FigureMap | EffectMap | undefined
if (endpoint.fileName === 'FigureData') { if (endpoint.fileName === 'FigureData') {
return new FigureData(convertedData, endpoint.fileName) parsedData = new FigureData(convertedData, endpoint.fileName)
} else if (endpoint.fileName === 'FigureMap') { } else if (endpoint.fileName === 'FigureMap') {
return new FigureMap(convertedData, endpoint.fileName) parsedData = new FigureMap(convertedData, endpoint.fileName)
} else if (endpoint.fileName === 'EffectMap') { } else if (endpoint.fileName === 'EffectMap') {
return new EffectMap(convertedData, endpoint.fileName) parsedData = new EffectMap(convertedData, endpoint.fileName)
} }
break return await parseData(Convertion.gamedataDir, parsedData?.fileName, parsedData?.data).catch((error) => {
return console.error(error)
})
case 'TXT': case 'TXT':
return await convertTXT(Convertion.gamedataDir, data) return await convertTXT(Convertion.gamedataDir, data)
default: default: {
let parsedData: FurniData | undefined
if (endpoint.fileName === 'FurniData') { if (endpoint.fileName === 'FurniData') {
return new FurniData(JSON.parse(data), endpoint.fileName) parsedData = new FurniData(JSON.parse(data), endpoint.fileName)
}
return await parseData(Convertion.gamedataDir, parsedData?.fileName, parsedData?.data).catch((error) => {
return console.error(error)
})
} }
} }
} }

View File

@ -1,9 +1,9 @@
import { ResponseType } from '@tauri-apps/api/http' import { ResponseType } from '@tauri-apps/api/http'
import { GAME_ENDPOINTS, client } from '../config/Endpoints' import { GAME_ENDPOINTS, client } from '../config/Endpoints'
import type { ConvertionHandler } from '../types'
import type { DomainTypes } from '../types/Domain' import type { DomainTypes } from '../types/Domain'
import { fetchGamedataConfig } from './fetchGamedataConfig' import { fetchGamedataConfig } from './fetchGamedataConfig'
import type { ConvertionHandler } from '../types/global'
export const handleConvertion = async ( export const handleConvertion = async (
domain: DomainTypes, domain: DomainTypes,
@ -27,7 +27,7 @@ export const handleConvertion = async (
}) })
) )
callback('Converting shockwave files...', 'loading') // callback('Converting shockwave files...', 'loading')
// fetch, read and convert the files from the production folder in the user downloads' folder // fetch, read and convert the files from the production folder in the user downloads' folder
// write the files into a seperate folder // write the files into a seperate folder

View File

@ -1,12 +1,21 @@
import { createDir, exists, writeTextFile } from '@tauri-apps/api/fs' import { createDir, exists, writeFile } from '@tauri-apps/api/fs'
import { Convertion } from '../config/Convertion' import { Convertion } from '../config/Convertion'
export const parseData = async (path: string, fileName: string, fileContent: string | object): Promise<void> => { export const parseData = async (
path: string,
fileName: string | undefined,
fileContent: string | object | undefined
): Promise<void> => {
if (fileName == null || fileContent == null) return
const fileDir = path.concat(`/${fileName}.json`) const fileDir = path.concat(`/${fileName}.json`)
if (!(await exists(Convertion.gamedataDir))) await createDir(Convertion.gamedataDir) // By default, output files will be overwritten, and I cannot recursively remove the entire output folder
if (!(await exists(Convertion.gamedataConfigDir))) await createDir(Convertion.gamedataConfigDir) // and create it again because it just won't parse files' contents for some reason
await writeTextFile(fileDir, typeof fileContent === 'object' ? JSON.stringify(fileContent) : fileContent) if (!(await exists(Convertion.outputDir))) await createDir(Convertion.outputDir)
if (!(await exists(Convertion.gamedataDir))) await createDir(Convertion.gamedataDir)
return await writeFile(fileDir, typeof fileContent === 'object' ? JSON.stringify(fileContent) : fileContent)
} }