Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix path issue for config.ini and static folder #172

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions packages/common/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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`);
Expand Down
21 changes: 15 additions & 6 deletions packages/common/utils/directories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
5 changes: 3 additions & 2 deletions packages/common/utils/gosu.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
21 changes: 7 additions & 14 deletions packages/game-overlay/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
checkGameOverlayConfig,
downloadFile,
getProgramPath,
unzip,
wLogger
} from '@tosu/common';
Expand All @@ -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;

Expand Down Expand Up @@ -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'
Expand All @@ -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!'
);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/server/utils/counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^(\\\\\\|\\\\|\\|\/|\/\/)/, '')
Expand Down
7 changes: 4 additions & 3 deletions packages/updater/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
config,
downloadFile,
getProgramPath,
platformResolver,
sleep,
unzip,
Expand All @@ -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 {
Expand Down Expand Up @@ -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');

Expand Down
Loading