From 41d0093431066e598ca1016cc2d6acb5f5de9974 Mon Sep 17 00:00:00 2001 From: ck <21735205+cyperdark@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:30:58 +0300 Subject: [PATCH] fix: Fix path issue for `config.ini` and `static` folder --- packages/common/utils/config.ts | 14 +++++--------- packages/common/utils/directories.ts | 21 +++++++++++++++------ packages/common/utils/gosu.ts | 5 +++-- packages/game-overlay/src/index.ts | 21 +++++++-------------- packages/server/utils/counters.ts | 2 +- packages/updater/index.ts | 7 ++++--- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/packages/common/utils/config.ts b/packages/common/utils/config.ts index 6dd3b446..e6cc0427 100644 --- a/packages/common/utils/config.ts +++ b/packages/common/utils/config.ts @@ -2,13 +2,11 @@ import * as dotenv from 'dotenv'; import fs from 'fs'; import path from 'path'; +import { getProgramPath } from './directories'; import { checkGameOverlayConfig } from './gosu'; import { wLogger } from './logger'; -const configPath = path.join( - 'pkg' in process ? path.dirname(process.execPath) : process.cwd(), - 'tsosu.env' -); +const configPath = path.join(getProgramPath(), 'tsosu.env'); const createConfig = () => { if (!fs.existsSync(configPath)) { @@ -260,11 +258,9 @@ export const refreshConfig = (httpServer: any, refresh: boolean) => { config.showMpCommands = showMpCommands; config.staticFolderPath = staticFolderPath; - if ( - config.staticFolderPath === './static' && - !fs.existsSync(path.join(process.cwd(), 'static')) - ) { - fs.mkdirSync(path.join(process.cwd(), 'static')); + const staticPath = path.join(getProgramPath(), 'static'); + if (config.staticFolderPath === './static' && !fs.existsSync(staticPath)) { + fs.mkdirSync(staticPath); } if (updated) wLogger.info(`Config ${status}ed`); diff --git a/packages/common/utils/directories.ts b/packages/common/utils/directories.ts index 5d49dcd2..a2d8834a 100644 --- a/packages/common/utils/directories.ts +++ b/packages/common/utils/directories.ts @@ -3,9 +3,6 @@ import path from 'path'; import { config } from './config'; -const pkgRunningFolder = - 'pkg' in process ? path.dirname(process.execPath) : process.cwd(); - export function recursiveFilesSearch({ _ignoreFileName, dir, @@ -43,12 +40,24 @@ export function recursiveFilesSearch({ } export function getStaticPath() { - const staticPath = - config.staticFolderPath || path.join(pkgRunningFolder, 'static'); + let staticPath = + config.staticFolderPath || path.join(getProgramPath(), 'static'); + + // replace ./static with normal path to the static with program path + if ( + staticPath.toLowerCase() === './static' || + staticPath.toLowerCase() === '.\\static' + ) + staticPath = path.join(getProgramPath(), 'static'); return path.resolve(staticPath); } export function getCachePath() { - return path.join(pkgRunningFolder, '.cache'); + return path.join(getProgramPath(), '.cache'); +} + +export function getProgramPath() { + if ('pkg' in process) return path.dirname(process.execPath); + return process.cwd(); } diff --git a/packages/common/utils/gosu.ts b/packages/common/utils/gosu.ts index a150136c..90eba536 100644 --- a/packages/common/utils/gosu.ts +++ b/packages/common/utils/gosu.ts @@ -1,9 +1,10 @@ import fs from 'fs'; import path from 'path'; -export const checkGameOverlayConfig = () => { - const configPath = path.join(process.cwd(), 'config.ini'); +import { getProgramPath } from './directories'; +export const checkGameOverlayConfig = () => { + const configPath = path.join(getProgramPath(), 'config.ini'); if (fs.existsSync(configPath)) return; fs.writeFileSync( configPath, diff --git a/packages/game-overlay/src/index.ts b/packages/game-overlay/src/index.ts index 484d7b51..ea7daf93 100644 --- a/packages/game-overlay/src/index.ts +++ b/packages/game-overlay/src/index.ts @@ -1,6 +1,7 @@ import { checkGameOverlayConfig, downloadFile, + getProgramPath, unzip, wLogger } from '@tosu/common'; @@ -10,7 +11,7 @@ import { mkdir, rm } from 'node:fs/promises'; import path from 'node:path'; import { Process } from 'tsprocess/dist/process'; -const configPath = path.join(process.cwd(), 'config.ini'); +const configPath = path.join(getProgramPath(), 'config.ini'); const checkGosuConfig = (p: Process, checking?: boolean) => { if (!existsSync(configPath)) return null; @@ -39,8 +40,8 @@ export const injectGameOverlay = async (p: Process) => { // Check for DEPRECATED GOSU CONFIG, due its needed to read [GameOverlay] section from original configuration checkGameOverlayConfig(); - if (!existsSync(path.join(process.cwd(), 'gameOverlay'))) { - const gameOverlayPath = path.join(process.cwd(), 'gameOverlay'); + const gameOverlayPath = path.join(getProgramPath(), 'gameOverlay'); + if (!existsSync(gameOverlayPath)) { const archivePath = path.join( gameOverlayPath, 'gosu-gameoverlay.zip' @@ -56,11 +57,7 @@ export const injectGameOverlay = async (p: Process) => { await rm(archivePath); } - if ( - !existsSync( - path.join(process.cwd(), 'gameOverlay', 'gosumemoryoverlay.dll') - ) - ) { + if (!existsSync(path.join(gameOverlayPath, 'gosumemoryoverlay.dll'))) { wLogger.info( '[gosu-overlay] Please delete gameOverlay folder, and restart program!' ); @@ -77,14 +74,10 @@ export const injectGameOverlay = async (p: Process) => { return await new Promise((resolve, reject) => { const child = execFile( - path.join(process.cwd(), 'gameOverlay', 'a.exe'), + path.join(gameOverlayPath, 'a.exe'), [ p.id.toString(), - path.join( - process.cwd(), - 'gameOverlay', - 'gosumemoryoverlay.dll' - ) + path.join(gameOverlayPath, 'gosumemoryoverlay.dll') ], { windowsHide: true diff --git a/packages/server/utils/counters.ts b/packages/server/utils/counters.ts index b9d424c1..b3b4b684 100644 --- a/packages/server/utils/counters.ts +++ b/packages/server/utils/counters.ts @@ -81,7 +81,7 @@ export function parseTXT(filePath: string) { filePath = path.resolve(filePath); - const staticPath = path.resolve(config.staticFolderPath); + const staticPath = getStaticPath(); object.folderName = path .dirname(filePath.replace(staticPath, '')) .replace(/^(\\\\\\|\\\\|\\|\/|\/\/)/, '') diff --git a/packages/updater/index.ts b/packages/updater/index.ts index 130ac3bc..f529efb9 100644 --- a/packages/updater/index.ts +++ b/packages/updater/index.ts @@ -1,6 +1,7 @@ import { config, downloadFile, + getProgramPath, platformResolver, sleep, unzip, @@ -14,8 +15,8 @@ import path from 'path'; const currentVersion = require(process.cwd() + '/_version.js'); const repositoryName = 'tosu'; -const fileDestination = path.join(process.cwd(), 'update.zip'); -const backupExecutablePath = path.join(process.cwd(), 'tosu_old.exe'); +const fileDestination = path.join(getProgramPath(), 'update.zip'); +const backupExecutablePath = path.join(getProgramPath(), 'tosu_old.exe'); const deleteNotLocked = async (filePath: string) => { try { @@ -119,7 +120,7 @@ export const autoUpdater = async () => { const currentExecutablePath = process.argv[0]; // Path to the current executable await fs.promises.rename(currentExecutablePath, backupExecutablePath); - await unzip(downloadAsset, process.cwd()); + await unzip(downloadAsset, getProgramPath()); wLogger.info('Restarting program');