-
Notifications
You must be signed in to change notification settings - Fork 7
/
BeatmapDecoder.ts
206 lines (167 loc) · 7.07 KB
/
BeatmapDecoder.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Beatmap } from 'osu-classes';
import {
BeatmapGeneralDecoder,
BeatmapEditorDecoder,
BeatmapMetadataDecoder,
BeatmapDifficultyDecoder,
BeatmapEventDecoder,
BeatmapHitObjectDecoder,
BeatmapTimingPointDecoder,
} from './Handlers';
import { SectionDecoder } from './SectionDecoder';
import { StoryboardDecoder } from './StoryboardDecoder';
import { IBeatmapParsingOptions } from '../Interfaces';
import { BufferLike, stringifyBuffer } from '../Utils/Buffer';
import { FileFormat, LineType, Section } from '../Enums';
/**
* A beatmap decoder.
*/
export class BeatmapDecoder extends SectionDecoder<Beatmap> {
/**
* An offset which needs to be applied to old beatmaps (v4 and lower)
* to correct timing changes that were applied at a game client level.
*/
static readonly EARLY_VERSION_TIMING_OFFSET = 24;
/**
* Current offset for all time values.
*/
protected _offset = 0;
/**
* Current storyboard lines.
*/
protected _sbLines: string[] | null = null;
/**
* Performs beatmap decoding from the specified .osu file.
* @param path A path to the .osu file.
* @param options Beatmap parsing options.
* Setting this to boolean will only affect storyboard parsing.
* All sections that weren't specified will be enabled by default.
* @throws If file doesn't exist or can't be decoded.
* @returns A decoded beatmap.
*/
async decodeFromPath(path: string, options?: boolean | IBeatmapParsingOptions): Promise<Beatmap> {
if (!path.endsWith(FileFormat.Beatmap)) {
throw new Error(`Wrong file format! Only ${FileFormat.Beatmap} files are supported!`);
}
try {
const data = await this._getFileBuffer(path);
const beatmap = this.decodeFromBuffer(data, options);
beatmap.fileUpdateDate = await this._getFileUpdateDate(path);
return beatmap;
}
catch (err: unknown) {
const reason = (err as Error).message || err;
throw new Error(`Failed to decode a beatmap: ${reason}`);
}
}
/**
* Performs beatmap decoding from a data buffer.
* @param data The buffer with beatmap data.
* @param options Beatmap parsing options.
* Setting this to boolean will only affect storyboard parsing.
* All sections that weren't specified will be enabled by default.
* @throws If beatmap data can't be decoded.
* @returns A decoded beatmap.
*/
decodeFromBuffer(data: BufferLike, options?: boolean | IBeatmapParsingOptions): Beatmap {
return this.decodeFromString(stringifyBuffer(data), options);
}
/**
* Performs beatmap decoding from a string.
* @param str The string with beatmap data.
* @param options Beatmap parsing options.
* Setting this to boolean will only affect storyboard parsing.
* All sections that weren't specified will be enabled by default.
* @throws If beatmap data can't be decoded.
* @returns A decoded beatmap.
*/
decodeFromString(str: string, options?: boolean | IBeatmapParsingOptions): Beatmap {
str = typeof str !== 'string' ? String(str) : str;
return this.decodeFromLines(str.split(/\r?\n/), options);
}
/**
* Performs beatmap decoding from a string array.
* @param data The array of split lines.
* @param options Beatmap parsing options.
* Setting this to boolean will only affect storyboard parsing.
* @throws If beatmap data can't be decoded.
* @returns A decoded beatmap.
*/
decodeFromLines(data: string[], options?: boolean | IBeatmapParsingOptions): Beatmap {
const beatmap = new Beatmap();
this._reset();
this._lines = this._getLines(data);
this._setEnabledSections(typeof options !== 'boolean' ? options : {});
// This array isn't needed if we don't parse a storyboard.
this._sbLines = this._shouldParseStoryboard(options) ? [] : null;
// Parse beatmap lines.
for (let i = 0; i < this._lines.length; ++i) {
const type = this._parseLine(this._lines[i], beatmap);
// Break early if we parsed all enabled sections.
if (type === LineType.Break) break;
}
// Flush last control point group.
BeatmapTimingPointDecoder.flushPendingPoints();
// Apply default values to the all hit objects.
for (let i = 0; i < beatmap.hitObjects.length; ++i) {
beatmap.hitObjects[i].applyDefaults(beatmap.controlPoints, beatmap.difficulty);
}
// Use stable sorting to keep objects in the right order.
beatmap.hitObjects.sort((a, b) => a.startTime - b.startTime);
// Storyboard
if (this._sbLines && this._sbLines.length) {
const storyboardDecoder = new StoryboardDecoder();
beatmap.events.storyboard = storyboardDecoder.decodeFromLines(this._sbLines);
/**
* Because we parsed storyboard through beatmap decoder
* we need to copy these properties from beatmap directly.
*/
beatmap.events.storyboard.useSkinSprites = beatmap.general.useSkinSprites;
beatmap.events.storyboard.colors = beatmap.colors;
beatmap.events.storyboard.fileFormat = beatmap.fileFormat;
}
return beatmap;
}
protected _parseSectionData(line: string, beatmap: Beatmap): void {
switch (this._sectionMap.currentSection) {
case Section.General:
return BeatmapGeneralDecoder.handleLine(line, beatmap, this._offset);
case Section.Editor:
return BeatmapEditorDecoder.handleLine(line, beatmap);
case Section.Metadata:
return BeatmapMetadataDecoder.handleLine(line, beatmap);
case Section.Difficulty:
return BeatmapDifficultyDecoder.handleLine(line, beatmap);
case Section.Events:
return BeatmapEventDecoder.handleLine(line, beatmap, this._sbLines, this._offset);
case Section.TimingPoints:
return BeatmapTimingPointDecoder.handleLine(line, beatmap, this._offset);
case Section.HitObjects:
return BeatmapHitObjectDecoder.handleLine(line, beatmap, this._offset);
}
super._parseSectionData(line, beatmap);
}
/**
* Sets current enabled sections.
* All known sections are enabled by default.
* @param options Parsing options.
*/
protected _setEnabledSections(options?: IBeatmapParsingOptions): void {
super._setEnabledSections(options);
this._sectionMap.set(Section.General, options?.parseGeneral);
this._sectionMap.set(Section.Editor, options?.parseEditor);
this._sectionMap.set(Section.Metadata, options?.parseMetadata);
this._sectionMap.set(Section.Difficulty, options?.parseDifficulty);
this._sectionMap.set(Section.Events, options?.parseEvents);
this._sectionMap.set(Section.TimingPoints, options?.parseTimingPoints);
this._sectionMap.set(Section.HitObjects, options?.parseHitObjects);
}
protected _shouldParseStoryboard(options?: boolean | IBeatmapParsingOptions): boolean {
const parsingOptions = options as IBeatmapParsingOptions;
const storyboardFlag = parsingOptions?.parseStoryboard ?? options as boolean;
const parseSb = typeof storyboardFlag === 'boolean' ? storyboardFlag : true;
const parseEvents = parsingOptions?.parseEvents ?? true;
// Storyboard should be parsed only if both events and storyboard flags are enabled.
return parseEvents && parseSb;
}
}