refactor: clean up

This commit is contained in:
Walid 2023-04-23 20:18:16 +01:00
parent a946baf1d7
commit b26459feeb
Signed by: Walidoux
GPG Key ID: CCF21881FE8BEBAF
9 changed files with 209 additions and 11 deletions

50
src/config/Endpoints.ts Normal file
View File

@ -0,0 +1,50 @@
import { getClient, ResponseType } from '@tauri-apps/api/http'
import type { DomainTypes, GameEndPointsTypes } from '../types'
const PROD_VERSION_REGEX = /(production-[^/]+)/im
const STABLE_PROD_VERSION = 'PRODUCTION-202304181630-471782382'
export let PROD_VERSION: string | undefined
const HABBO_URL = (domain: DomainTypes): string => {
return `https://www.habbo.${domain}`
}
export const HABBO_GORDON_URL = `https://images.habbo.com/gordon/${PROD_VERSION ?? STABLE_PROD_VERSION}`
export const client = await getClient()
await client
.get(`${HABBO_URL('com')}/gamedata/external_variables/0`, {
responseType: ResponseType.Text
})
.then(({ data }) => {
return (PROD_VERSION = (data as string).match(PROD_VERSION_REGEX)?.[0])
})
export const GAME_ENDPOINTS = (domain: DomainTypes): GameEndPointsTypes => {
return [
/* {
src: `${HABBO_URL(domain)}/gamedata/figuredata/0`, // check
convert: 'XML',
fileName: 'FigureData'
}, */
/* {
src: `${HABBO_GORDON_URL}/figuremap.xml`,
convert: 'XML',
fileName: 'FigureMap'
}, */
/* {
src: `${HABBO_URL(domain)}/gamedata/furnidata_json/0`, // check
fileName: 'FurniData'
}, */ {
src: `${HABBO_GORDON_URL}/effectmap.xml`,
convert: 'XML',
fileName: 'EffectMap'
}
]
}
export const ASSETS_ENDPOINTS = (domain: DomainTypes): string[] => {
return [`${HABBO_URL(domain)}/`]
}

View File

@ -1,4 +1,4 @@
import type { IDownloaderContent } from '../components' import type { IDownloaderContent } from '../components/layout/Downloaders/Downloader'
export const GameDataDownloader: IDownloaderContent = { export const GameDataDownloader: IDownloaderContent = {
title: 'Converts and bundles:', title: 'Converts and bundles:',

View File

@ -4,6 +4,8 @@
#root { #root {
font-family: 'Press Start 2P'; font-family: 'Press Start 2P';
overflow: hidden;
height: 100vh;
} }
@layer utilities { @layer utilities {

54
src/tools/convertTXT.ts Normal file
View File

@ -0,0 +1,54 @@
import { parseData } from './parseData'
interface IBadge {
code: string
name?: string
description?: string
}
const badgePattern = /^badge_(?:name|desc)_([^=]+)/gim
const descriptionPattern = /^badge_desc_(\s*\w+)/
const namePattern = /^badge_name_(\s*\w+)/
export const convertTXT = async (path: string, data: string): Promise<void> => {
const badges: IBadge[] = []
const lines = data.split(/\r?\n/)
for (const line of lines) {
const [key, value] = line.split('=')
const badge = key.match(badgePattern)
if (badge != null) {
if (key.match(namePattern) != null) {
const badgeCode = key.match(namePattern)?.[1] as string
const existingBadge = badges.filter((badge) => {
return badge.code === badgeCode
})[0]
if (Boolean(existingBadge)) {
const index = badges.indexOf(existingBadge)
badges[index].name = value
} else {
badges.push({ code: badgeCode, name: value })
}
} else if (key.match(descriptionPattern) != null) {
const badgeCode = key.match(descriptionPattern)?.[1] as string
const existingBadge = badges.filter((badge) => {
return badge.code === badgeCode
})[0]
if (Boolean(existingBadge)) {
const index = badges.indexOf(existingBadge)
badges[index].description = value
} else {
badges.push({ code: badgeCode, description: value })
}
}
lines.splice(lines.indexOf(line), 1)
}
}
return await parseData(path, 'Badges', badges)
}

View File

@ -0,0 +1,55 @@
import { downloadDir } from '@tauri-apps/api/path'
import { XMLParser } from 'fast-xml-parser'
import { writeTextFile } from '@tauri-apps/api/fs'
import { PROD_VERSION } from '../config/Endpoints'
import { FigureMap } from '../controllers/FigureMap'
import { EffectMap } from '../controllers/EffectMap'
import type { GameEndPointsTypes, IEffectMap, IFigureData, IFigureMap } from '../types'
import { parseData } from './parseData'
import { convertTXT } from './convertTXT'
import { FigureData } from '../controllers/FigureData'
import { FurniData } from '../controllers/FurniData'
export const fetchGamedata = async (data: string, endpoint: GameEndPointsTypes[number]): Promise<void> => {
const subDir = '/config'
const outputDir = async (path: string): Promise<string> => {
return (await downloadDir()).concat(String(PROD_VERSION) + path)
}
switch (endpoint.convert) {
case 'XML':
const convertedData = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '' }).parse(data)
const XML2JSON = () => {
if (endpoint.fileName === 'FigureData') {
return new FigureData(convertedData).data
} else if (endpoint.fileName === 'FigureMap') {
return new FigureMap(convertedData).data
} else if (endpoint.fileName === 'EffectMap') {
return new EffectMap(convertedData)
}
}
XML2JSON()
break
/* return await parseData(await outputDir(subDir), endpoint.fileName, XML2JSON) */
case 'TXT':
return await convertTXT(await outputDir(subDir), data)
default:
const handleJSON = () => {
if (endpoint.fileName === 'FurniData') {
return new FurniData(JSON.parse(data))
}
}
handleJSON()
break
/* return await parseData(await outputDir(subDir), endpoint.fileName, data) */
}
}

View File

@ -0,0 +1,36 @@
import { ResponseType } from '@tauri-apps/api/http'
import { GAME_ENDPOINTS, client } from '../config/Endpoints'
import type { DomainTypes } from '../types/Domain'
import { fetchGamedata } from './fetchGamedata'
import type { StateTypes } from '../types/global'
export const handleConvertion = async (
domain: DomainTypes,
callback: (message: string, state: StateTypes) => void,
assetsOption = false
): Promise<void> => {
if (!assetsOption) {
callback('Converting Gamedata configuration...', 'loading')
await Promise.all(
GAME_ENDPOINTS(domain).map(async (endpoint) => {
await client
.get(endpoint.src, { responseType: ResponseType.Text })
.then(async ({ data }) => {
return await fetchGamedata(data as string, endpoint)
})
.catch((error) => {
console.error(error)
return callback(error, 'error')
})
})
)
callback('Converting shockwave files...', 'loading')
} else {
/* ASSETS_ENDPOINTS(domain).map((endpoint) => {
client.get()
}) */
}
}

9
src/tools/parseData.ts Normal file
View File

@ -0,0 +1,9 @@
import { createDir, exists, writeTextFile } from '@tauri-apps/api/fs'
export const parseData = async (path: string, fileName: string, fileContent: string | object): Promise<void> => {
const fileDir = path.concat(`/${fileName}.json`)
if (!(await exists(path))) await createDir(path)
await writeTextFile(fileDir, typeof fileContent === 'object' ? JSON.stringify(fileContent) : fileContent)
}

11
src/types/Domain.d.ts vendored
View File

@ -1,10 +1 @@
export type DomainTypes = export type DomainTypes = 'com.br' | 'com.tr' | 'com' | 'de' | 'es' | 'fi' | 'fr' | 'it' | 'nl'
| 'com.br'
| 'com.tr'
| 'com'
| 'de'
| 'es'
| 'fi'
| 'fr'
| 'it'
| 'nl'

View File

@ -2,3 +2,4 @@ export * from './Converters'
export * from './SubConverters' export * from './SubConverters'
export * from './Endpoint' export * from './Endpoint'
export * from './Domain' export * from './Domain'
export * from './global'