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: Ensure utf-8 encoding & path cleanup. #292

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion packages/tosu/src/instances/lazerInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { LazerMemory } from '@/memory/lazer';
import { Gameplay } from '@/states/gameplay';
import { Global } from '@/states/global';
import { cleanPath } from '@/utils/converters';

import { AbstractInstance } from '.';

Expand Down Expand Up @@ -58,7 +59,7 @@ export class LazerInstance extends AbstractInstance {

if (!global.gameFolder) {
global.setGameFolder(this.path);
global.setSongsFolder(global.memorySongsFolder);
global.setSongsFolder(cleanPath(global.memorySongsFolder));
}

// update important data before doing rest
Expand Down
12 changes: 7 additions & 5 deletions packages/tosu/src/instances/osuInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
wLogger
} from '@tosu/common';
import fs from 'fs';
import path from 'path';

import { AbstractInstance } from '@/instances/index';
import { StableMemory } from '@/memory/stable';
import { Gameplay } from '@/states/gameplay';
import { Global } from '@/states/global';
import { cleanPath } from '@/utils/converters';

export class OsuInstance extends AbstractInstance {
gameOverlayAllowed = true;
Expand Down Expand Up @@ -61,14 +61,16 @@ export class OsuInstance extends AbstractInstance {
}

if (!global.gameFolder) {
global.setGameFolder(this.path);
global.setGameFolder(cleanPath(this.path));

// condition when user have different BeatmapDirectory in osu! config
if (fs.existsSync(global.memorySongsFolder)) {
global.setSongsFolder(global.memorySongsFolder);
if (fs.existsSync(cleanPath(global.memorySongsFolder))) {
global.setSongsFolder(
cleanPath(global.memorySongsFolder)
);
} else {
global.setSongsFolder(
path.join(this.path, global.memorySongsFolder)
cleanPath(this.path, global.memorySongsFolder)
);
}
}
Expand Down
23 changes: 11 additions & 12 deletions packages/tosu/src/states/beatmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { ClientType, config, wLogger } from '@tosu/common';
import fs from 'fs';
import { Beatmap as ParsedBeatmap, TimingPoint } from 'osu-classes';
import { BeatmapDecoder } from 'osu-parsers';
import path from 'path';

import { BeatmapStrains } from '@/api/types/v1';
import { AbstractInstance } from '@/instances';
import { AbstractState } from '@/states';
import { fixDecimals } from '@/utils/converters';
import { cleanPath, fixDecimals } from '@/utils/converters';
import { removeDebuffMods } from '@/utils/osuMods';
import { CalculateMods, ModsLazer } from '@/utils/osuMods.types';

Expand Down Expand Up @@ -316,9 +315,9 @@ export class BeatmapPP extends AbstractState {
`beatmapPP updateMapMetadata`,
`Skip osu! music theme file`,
{
SongsFolder: global.songsFolder,
Folder: menu.folder,
Path: menu.filename
SongsFolder: cleanPath(global.songsFolder),
Folder: cleanPath(menu.folder),
Path: cleanPath(menu.filename)
}
);
return;
Expand All @@ -331,18 +330,18 @@ export class BeatmapPP extends AbstractState {
`beatmapPP updateMapMetadata`,
`Skip new map creation`,
{
SongsFolder: global.songsFolder,
Folder: menu.folder,
Path: menu.filename
SongsFolder: cleanPath(global.songsFolder),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't you clean it in osuInstance/lazerInstance ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't you clean it in osuInstance/lazerInstance ?

wanted to be sure.

Folder: cleanPath(menu.folder),
Path: cleanPath(menu.filename)
}
);
return;
}

const mapPath = path.join(
global.songsFolder.trim(),
menu.folder.trim(),
menu.filename.trim()
const mapPath = cleanPath(
global.songsFolder,
menu.folder,
menu.filename
);

try {
Expand Down
16 changes: 16 additions & 0 deletions packages/tosu/src/utils/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ export const numberFromDecimal = (

return value;
};

/**
* Joins multiple paths into a single path, replacing invalid path characters in the process.
*
* @param {...string[]} paths Paths to join
* @returns {string} Joined path
*/
export const cleanPath = (...paths: string[]): string => {
paths = paths.map((path) =>
Buffer.from(path.trim())
.toString('utf8')
.replace(process.platform === 'win32' ? /[<>:"|?*]/g : /\//g, '')
);

return paths.join(...paths);
};