Skip to content

Commit

Permalink
Merge pull request #292 from GabuTheDev/fix/non-ascii-paths
Browse files Browse the repository at this point in the history
Fix: Ensure `utf-8` encoding & path cleanup.
  • Loading branch information
KotRikD authored Feb 5, 2025
2 parents 52f333e + 5cc517a commit 70ed261
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/tosu/src/instances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ResultScreen } from '@/states/resultScreen';
import { Settings } from '@/states/settings';
import { TourneyManager } from '@/states/tourney';
import { User } from '@/states/user';
import { cleanPath } from '@/utils/converters';

export interface DataRepoList {
settings: Settings;
Expand Down Expand Up @@ -63,7 +64,7 @@ export abstract class AbstractInstance {
this.pid = pid;

this.process = new Process(this.pid, bitness);
this.path = this.process.path;
this.path = cleanPath(this.process.path);
this.bitness = bitness;

this.client =
Expand Down
4 changes: 2 additions & 2 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 @@ -68,7 +68,7 @@ export class OsuInstance extends AbstractInstance {
global.setSongsFolder(global.memorySongsFolder);
} else {
global.setSongsFolder(
path.join(this.path, global.memorySongsFolder)
cleanPath(this.path, global.memorySongsFolder)
);
}
}
Expand Down
18 changes: 9 additions & 9 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 @@ -339,10 +338,10 @@ export class BeatmapPP extends AbstractState {
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 Expand Up @@ -467,12 +466,13 @@ export class BeatmapPP extends AbstractState {
const { bpm, bpmMin, bpmMax } = this.lazerBeatmap;

if (
this.lazerBeatmap.events.backgroundPath !==
cleanPath(this.lazerBeatmap.events.backgroundPath || '') !==
menu.backgroundFilename &&
!lazerByPass
) {
menu.backgroundFilename =
this.lazerBeatmap.events.backgroundPath || '';
menu.backgroundFilename = cleanPath(
this.lazerBeatmap.events.backgroundPath || ''
);
}

this.previewtime = this.lazerBeatmap.general.previewTime;
Expand Down
5 changes: 3 additions & 2 deletions packages/tosu/src/states/global.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClientType, wLogger } from '@tosu/common';

import { AbstractState } from '@/states';
import { cleanPath } from '@/utils/converters';
import { defaultCalculatedMods } from '@/utils/osuMods';
import { CalculateMods } from '@/utils/osuMods.types';

Expand Down Expand Up @@ -60,8 +61,8 @@ export class Global extends AbstractState {
this.gameTime = result.gameTime;
this.menuMods = result.menuMods;

this.skinFolder = result.skinFolder;
this.memorySongsFolder = result.memorySongsFolder;
this.skinFolder = cleanPath(result.skinFolder);
this.memorySongsFolder = cleanPath(result.memorySongsFolder);

this.resetReportCount('global updateState');
} catch (exc) {
Expand Down
9 changes: 5 additions & 4 deletions packages/tosu/src/states/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClientType, wLogger } from '@tosu/common';

import { AbstractState } from '@/states';
import { cleanPath } from '@/utils/converters';

export class Menu extends AbstractState {
gamemode: number;
Expand Down Expand Up @@ -75,7 +76,7 @@ export class Menu extends AbstractState {

// MD5 hasn't changed in over NEW_MAP_COMMIT_DELAY, commit to new map
this.checksum = result.checksum;
this.filename = result.filename;
this.filename = cleanPath(result.filename);

this.plays = result.plays;
this.artist = result.artist;
Expand All @@ -87,9 +88,9 @@ export class Menu extends AbstractState {
this.cs = result.cs;
this.hp = result.hp;
this.od = result.od;
this.audioFilename = result.audioFilename;
this.backgroundFilename = result.backgroundFilename;
this.folder = result.folder;
this.audioFilename = cleanPath(result.audioFilename);
this.backgroundFilename = cleanPath(result.backgroundFilename);
this.folder = cleanPath(result.folder);
this.creator = result.creator;
this.difficulty = result.difficulty;
this.mapID = result.mapID;
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);
};

0 comments on commit 70ed261

Please sign in to comment.