-
Notifications
You must be signed in to change notification settings - Fork 7
/
BeatmapEncoder.ts
72 lines (62 loc) · 2.08 KB
/
BeatmapEncoder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { IBeatmap } from 'osu-classes';
import { FileFormat } from '../Enums';
import { mkdir, writeFile, dirname } from '../Utils/FileSystem';
import {
BeatmapGeneralEncoder,
BeatmapEditorEncoder,
BeatmapMetadataEncoder,
BeatmapDifficultyEncoder,
BeatmapEventEncoder,
BeatmapTimingPointEncoder,
BeatmapColorEncoder,
BeatmapHitObjectEncoder,
} from './Handlers';
/**
* A beatmap encoder.
*/
export class BeatmapEncoder {
/**
* First playable lazer version.
*/
static readonly FIRST_LAZER_VERSION = 128;
/**
* Performs beatmap encoding to the specified path.
* @param path The path for writing the .osu file.
* @param beatmap The beatmap for encoding.
* @throws If beatmap can't be encoded or file can't be written.
*/
async encodeToPath(path: string, beatmap?: IBeatmap): Promise<void> {
if (!path.endsWith(FileFormat.Beatmap)) {
path += FileFormat.Beatmap;
}
try {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, await this.encodeToString(beatmap));
}
catch (err: unknown) {
const reason = (err as Error).message || err;
throw new Error(`Failed to encode a beatmap: ${reason}`);
}
}
/**
* Performs beatmap encoding to a string.
* @param beatmap The beatmap for encoding.
* @returns The string with encoded beatmap data.
*/
encodeToString(beatmap?: IBeatmap): string {
if (!beatmap?.fileFormat) return '';
const fileFormat = beatmap.fileFormat ?? BeatmapEncoder.FIRST_LAZER_VERSION;
const encoded: string[] = [
`osu file format v${fileFormat}`,
BeatmapGeneralEncoder.encodeGeneralSection(beatmap),
BeatmapEditorEncoder.encodeEditorSection(beatmap),
BeatmapMetadataEncoder.encodeMetadataSection(beatmap),
BeatmapDifficultyEncoder.encodeDifficultySection(beatmap),
BeatmapEventEncoder.encodeEventSection(beatmap),
BeatmapTimingPointEncoder.encodeControlPoints(beatmap),
BeatmapColorEncoder.encodeColors(beatmap),
BeatmapHitObjectEncoder.encodeHitObjects(beatmap),
];
return encoded.join('\n\n') + '\n';
}
}