diff --git a/src/tools/fetchGamedataConfig.ts b/src/tools/fetchGamedataConfig.ts index 9687e75..676d47d 100644 --- a/src/tools/fetchGamedataConfig.ts +++ b/src/tools/fetchGamedataConfig.ts @@ -1,33 +1,44 @@ import { XMLParser } from 'fast-xml-parser' +import type { GameEndPointsTypes } from '../types' import { FigureMap } from '../controllers/FigureMap' import { EffectMap } from '../controllers/EffectMap' -import type { GameEndPointsTypes } from '../types' import { convertTXT } from './convertTXT' import { FigureData } from '../controllers/FigureData' import { FurniData } from '../controllers/FurniData' import { Convertion } from '../config/Convertion' +import { parseData } from './parseData' export const fetchGamedataConfig = async (data: string, endpoint: GameEndPointsTypes[number]): Promise => { switch (endpoint.convert) { case 'XML': const convertedData = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '' }).parse(data) + let parsedData: FigureData | FigureMap | EffectMap | undefined if (endpoint.fileName === 'FigureData') { - return new FigureData(convertedData, endpoint.fileName) + parsedData = new FigureData(convertedData, endpoint.fileName) } else if (endpoint.fileName === 'FigureMap') { - return new FigureMap(convertedData, endpoint.fileName) + parsedData = new FigureMap(convertedData, endpoint.fileName) } 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': return await convertTXT(Convertion.gamedataDir, data) - default: + default: { + let parsedData: FurniData | undefined + 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) + }) + } } } diff --git a/src/tools/handleConvertion.ts b/src/tools/handleConvertion.ts index f03fd34..f2a46d6 100644 --- a/src/tools/handleConvertion.ts +++ b/src/tools/handleConvertion.ts @@ -1,9 +1,9 @@ import { ResponseType } from '@tauri-apps/api/http' import { GAME_ENDPOINTS, client } from '../config/Endpoints' +import type { ConvertionHandler } from '../types' import type { DomainTypes } from '../types/Domain' import { fetchGamedataConfig } from './fetchGamedataConfig' -import type { ConvertionHandler } from '../types/global' export const handleConvertion = async ( 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 // write the files into a seperate folder diff --git a/src/tools/parseData.ts b/src/tools/parseData.ts index 91b3659..fcb90c0 100644 --- a/src/tools/parseData.ts +++ b/src/tools/parseData.ts @@ -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' -export const parseData = async (path: string, fileName: string, fileContent: string | object): Promise => { +export const parseData = async ( + path: string, + fileName: string | undefined, + fileContent: string | object | undefined +): Promise => { + if (fileName == null || fileContent == null) return + const fileDir = path.concat(`/${fileName}.json`) - if (!(await exists(Convertion.gamedataDir))) await createDir(Convertion.gamedataDir) - if (!(await exists(Convertion.gamedataConfigDir))) await createDir(Convertion.gamedataConfigDir) + // By default, output files will be overwritten, and I cannot recursively remove the entire output folder + // 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) }