-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload.ts
470 lines (441 loc) · 15.1 KB
/
load.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import { ILoadInfoData } from './types/bsmap/infoDiff.ts';
import { GenericFileName } from './types/beatmap/shared/filename.ts';
import { Info as V1Info, InfoDifficulty } from './beatmap/v1/info.ts';
import { Info as V2Info } from './beatmap/v2/info.ts';
import { Difficulty as V1Difficulty } from './beatmap/v1/difficulty.ts';
import { Difficulty as V2Difficulty } from './beatmap/v2/difficulty.ts';
import { Difficulty as V3Difficulty } from './beatmap/v3/difficulty.ts';
import {
parseDifficulty as parseV1Difficulty,
parseInfo as parseV1Info,
} from './beatmap/v1/parse.ts';
import {
parseDifficulty as parseV2Difficulty,
parseInfo as parseV2Info,
} from './beatmap/v2/parse.ts';
import { parseDifficulty as parseV3Difficulty } from './beatmap/v3/parse.ts';
import globals from './globals.ts';
import logger from './logger.ts';
import { LooseAutocomplete } from './types/utils.ts';
import { ILoadOptionsDifficulty, ILoadOptionsInfo } from './types/bsmap/load.ts';
import { resolve } from './deps.ts';
import { toV1Difficulty, toV1Info } from './converter/toV1.ts';
import { toV2Difficulty, toV2Info } from './converter/toV2.ts';
import { toV3Difficulty } from './converter/toV3.ts';
import { IWrapDifficulty } from './types/beatmap/wrapper/difficulty.ts';
import { IWrapInfo } from './types/beatmap/wrapper/info.ts';
function tag(name: string): string[] {
return ['load', name];
}
const optionsInfo: Required<ILoadOptionsInfo> = {
directory: '',
filePath: 'Info.dat',
forceConvert: true,
dataCheck: {
enabled: true,
throwError: true,
},
sort: true,
};
const optionsDifficulty: Required<ILoadOptionsDifficulty> = {
directory: '',
forceConvert: true,
dataCheck: {
enabled: true,
throwError: true,
},
sort: true,
};
const optionsDifficultyList: Required<ILoadOptionsDifficulty> = {
directory: '',
forceConvert: false,
dataCheck: {
enabled: true,
throwError: true,
},
sort: true,
};
/** Set default option value for load function. */
export const defaultOptions = {
info: optionsInfo,
difficulty: optionsDifficulty,
difficultyList: optionsDifficultyList,
};
function _readJSONFile(path: string) {
logger.tInfo(tag('_readJSONFile'), `Async reading JSON file from ${path}`);
return Deno.readTextFile(path).then(JSON.parse) as Promise<Record<string, unknown>>;
}
function _readJSONFileSync(path: string) {
logger.tInfo(tag('_readJSONFileSync'), `Sync reading JSON file from ${path}`);
return JSON.parse(Deno.readTextFileSync(path)) as Record<string, unknown>;
}
function _info(
json: Record<string, unknown>,
filePath: string,
targetVer: number | null | undefined,
options: ILoadOptionsInfo,
) {
const opt: Required<ILoadOptionsInfo> = {
filePath: '',
directory: '',
forceConvert: options.forceConvert ?? defaultOptions.info.forceConvert,
dataCheck: options.dataCheck ?? defaultOptions.info.dataCheck,
sort: options.sort ?? defaultOptions.info.sort,
};
const jsonVerStr = typeof json._version === 'string'
? json._version.at(0)
: typeof json.version === 'string'
? json.version.at(0)
: null;
let jsonVer: number;
if (jsonVerStr) {
jsonVer = parseInt(jsonVerStr);
} else {
jsonVer = json.songName ? 1 : 2;
logger.tWarn(
tag('_info'),
'Could not identify info version from JSON, assume implicit version',
jsonVer,
);
}
let data: IWrapInfo;
switch (jsonVer) {
case 1: {
data = parseV1Info(json, opt.dataCheck).setFileName(filePath);
break;
}
case 2: {
data = parseV2Info(json, opt.dataCheck).setFileName(filePath);
break;
}
default: {
throw new Error(
`Info version ${jsonVer} is not supported, this may be an error in JSON or is newer than currently supported.`,
);
}
}
if (targetVer && jsonVer !== targetVer) {
if (!opt.forceConvert) {
throw new Error(`Info version unmatched, expected ${targetVer} but received ${jsonVer}`);
}
logger.tWarn(
tag('_info'),
'Info version unmatched, expected',
targetVer,
'but received',
jsonVer,
'for version; Converting to info version',
targetVer,
);
if (targetVer === 1) data = toV1Info(data);
if (targetVer === 2) data = toV2Info(data);
}
if (opt.sort) data.sort();
return data;
}
/**
* Asynchronously load beatmap info file.
* ```ts
* load.info().then((data) => console.log(data));
* ```
*
* Mismatched beatmap version will be automatically converted, unspecified will leave the version as is but not known.
*/
export function info(version?: null, options?: ILoadOptionsInfo): Promise<IWrapInfo>;
export function info(version: 2, options?: ILoadOptionsInfo): Promise<V2Info>;
export function info(version: 1, options?: ILoadOptionsInfo): Promise<V1Info>;
export function info(version?: number | null, options: ILoadOptionsInfo = {}) {
logger.tInfo(tag('info'), 'Async loading info');
const filePath = options.filePath ?? defaultOptions.info.filePath;
const path = resolve(
options.directory ?? (defaultOptions.info.directory || globals.directory),
options.filePath ?? defaultOptions.info.filePath,
);
return _readJSONFile(path).then((data) => _info(data, filePath, version, options));
}
/**
* Synchronously load beatmap info file.
* ```ts
* const info = load.infoSync();
* console.log(info);
* ```
*
* Mismatched beatmap version will be automatically converted, unspecified will leave the version as is but not known.
*/
export function infoSync(version?: null, options?: ILoadOptionsInfo): IWrapInfo;
export function infoSync(version: 2, options?: ILoadOptionsInfo): V2Info;
export function infoSync(version: 1, options?: ILoadOptionsInfo): V1Info;
export function infoSync(version?: number | null, options: ILoadOptionsInfo = {}) {
logger.tInfo(tag('infoSync'), 'Sync loading info');
const filePath = options.filePath ?? defaultOptions.info.filePath;
const path = resolve(
options.directory ?? (defaultOptions.info.directory || globals.directory),
options.filePath ?? defaultOptions.info.filePath,
);
return _info(_readJSONFileSync(path), filePath, version, options);
}
function _difficulty(
json: Record<string, unknown>,
filePath: string,
targetVer: number | null | undefined,
options: ILoadOptionsDifficulty,
): IWrapDifficulty {
const opt: Required<ILoadOptionsDifficulty> = {
directory: '',
forceConvert: options.forceConvert ?? defaultOptions.difficulty.forceConvert,
dataCheck: options.dataCheck ?? defaultOptions.difficulty.dataCheck,
sort: options.sort ?? defaultOptions.difficulty.sort,
};
const jsonVerStr = typeof json._version === 'string'
? json._version.at(0)
: typeof json.version === 'string'
? json.version.at(0)
: null;
let jsonVer: number;
if (jsonVerStr) {
jsonVer = parseInt(jsonVerStr);
} else {
logger.tWarn(
tag('_difficulty'),
'Could not identify beatmap version from JSON, assume implicit version',
2,
);
jsonVer = 2;
}
let data: IWrapDifficulty;
switch (jsonVer) {
case 1: {
data = parseV1Difficulty(json, opt.dataCheck).setFileName(filePath);
break;
}
case 2: {
data = parseV2Difficulty(json, opt.dataCheck).setFileName(filePath);
break;
}
case 3: {
data = parseV3Difficulty(json, opt.dataCheck).setFileName(filePath);
break;
}
default: {
throw new Error(
`Beatmap version ${jsonVer} is not supported, this may be an error in JSON or is newer than currently supported.`,
);
}
}
if (targetVer && jsonVer !== targetVer) {
if (!opt.forceConvert) {
throw new Error(
`Beatmap version unmatched, expected ${targetVer} but received ${jsonVer}`,
);
}
logger.tWarn(
tag('_difficulty'),
'Beatmap version unmatched, expected',
targetVer,
'but received',
jsonVer,
'for version; Converting to beatmap version',
targetVer,
);
if (targetVer === 1) {
data = toV1Difficulty(data, new V1Info(), new InfoDifficulty({ jsonPath: filePath }));
}
if (targetVer === 2) data = toV2Difficulty(data);
if (targetVer === 3) data = toV3Difficulty(data);
}
if (opt.sort) data.sort();
return data;
}
/**
* Asynchronously load beatmap difficulty file.
* ```ts
* load.difficulty('EasyStandard.dat', 3).then((data) => console.log(data));
* ```
*
* Mismatched beatmap version will be automatically converted, unspecified will leave the version as is but not known.
*/
export function difficulty(
filePath: LooseAutocomplete<GenericFileName>,
version?: null,
options?: ILoadOptionsDifficulty,
): Promise<IWrapDifficulty>;
export function difficulty(
filePath: LooseAutocomplete<GenericFileName>,
version: 3,
options?: ILoadOptionsDifficulty,
): Promise<V3Difficulty>;
export function difficulty(
filePath: LooseAutocomplete<GenericFileName>,
version: 2,
options?: ILoadOptionsDifficulty,
): Promise<V2Difficulty>;
export function difficulty(
filePath: LooseAutocomplete<GenericFileName>,
version: 1,
options?: ILoadOptionsDifficulty,
): Promise<V1Difficulty>;
export function difficulty(
filePath: LooseAutocomplete<GenericFileName>,
version?: number | null,
options: ILoadOptionsDifficulty = {},
) {
logger.tInfo(tag('difficulty'), 'Async loading difficulty');
const path = resolve(
options.directory ?? (defaultOptions.difficulty.directory || globals.directory),
filePath,
);
return _readJSONFile(path).then((data) => _difficulty(data, filePath, version!, options));
}
/**
* Synchronously load beatmap difficulty file.
* ```ts
* const difficulty = load.difficultySync('EasyStandard.dat', 3);
* console.log(difficulty);
* ```
*
* Mismatched beatmap version will be automatically converted, unspecified will leave the version as is but not known.
*/
export function difficultySync(
filePath: LooseAutocomplete<GenericFileName>,
version?: null,
options?: ILoadOptionsDifficulty,
): IWrapDifficulty;
export function difficultySync(
filePath: LooseAutocomplete<GenericFileName>,
version: 3,
options?: ILoadOptionsDifficulty,
): V3Difficulty;
export function difficultySync(
filePath: LooseAutocomplete<GenericFileName>,
version: 2,
options?: ILoadOptionsDifficulty,
): V2Difficulty;
export function difficultySync(
filePath: LooseAutocomplete<GenericFileName>,
version: 1,
options?: ILoadOptionsDifficulty,
): V1Difficulty;
export function difficultySync(
filePath: LooseAutocomplete<GenericFileName>,
version?: number | null,
options: ILoadOptionsDifficulty = {},
) {
logger.tInfo(tag('difficultySync'), 'Sync loading difficulty');
const path = resolve(
options.directory ?? (defaultOptions.difficulty.directory || globals.directory),
filePath,
);
return _difficulty(_readJSONFileSync(path), filePath, version!, options);
}
/**
* Asynchronously load multiple beatmap difficulties given beatmap info.
*
* Automatically omits difficulty that could not be loaded or does not exist.
* ```ts
* load.difficultyFromInfo().then((data) => data.forEach((d) => console.log(d)));
* ```
*
* Info difficulty reference is also given to allow further control.
*/
export function difficultyFromInfo(
info: IWrapInfo,
options: ILoadOptionsDifficulty = {},
): Promise<ILoadInfoData[]> {
logger.tInfo(tag('difficultyFromInfo'), 'Async loading difficulty from info');
const opt: Required<ILoadOptionsDifficulty> = {
directory: options.directory ??
(globals.directory || defaultOptions.difficultyList.directory),
forceConvert: options.forceConvert ?? defaultOptions.difficultyList.forceConvert,
dataCheck: options.dataCheck ?? defaultOptions.difficultyList.dataCheck,
sort: options.sort ?? defaultOptions.difficultyList.sort,
};
return Promise.all(
info.listMap().map(async ([mode, beatmap]) => {
let p;
try {
p = resolve(opt.directory, beatmap.filename);
const json = await _readJSONFile(p);
const jsonVerStr = typeof json._version === 'string'
? json._version.at(0)
: typeof json.version === 'string'
? json.version.at(0)
: null;
let jsonVer: number;
if (jsonVerStr) {
jsonVer = parseInt(jsonVerStr);
} else {
jsonVer = 2;
}
return {
characteristic: mode,
difficulty: beatmap.difficulty,
settings: beatmap,
version: jsonVer,
data: _difficulty(json, beatmap.filename, jsonVer, opt),
};
} catch {
logger.tWarn(
tag('difficultyFromInfo'),
`Could not load difficulty from ${p}, skipping...`,
);
}
}),
).then((d) => d.filter((e) => e) as ILoadInfoData[]);
}
/**
* Synchronously load multiple beatmap difficulties given beatmap info.
*
* Automatically omits difficulty that could not be loaded or does not exist.
* ```ts
* const difficultyList = load.difficultyFromInfoSync();
* difficultyList.forEach((d) => console.log(d));
* ```
*
* Info difficulty reference is also given to allow further control.
*/
export function difficultyFromInfoSync(
info: IWrapInfo,
options: ILoadOptionsDifficulty = {},
): ILoadInfoData[] {
logger.tInfo(tag('difficultyFromInfoSync'), 'Sync loading difficulty from info');
const opt: Required<ILoadOptionsDifficulty> = {
directory: options.directory ??
(globals.directory || defaultOptions.difficultyList.directory),
forceConvert: options.forceConvert ?? defaultOptions.difficultyList.forceConvert,
dataCheck: options.dataCheck ?? defaultOptions.difficultyList.dataCheck,
sort: options.sort ?? defaultOptions.difficultyList.sort,
};
return info
.listMap()
.map(([mode, beatmap]) => {
let p;
try {
p = resolve(opt.directory, beatmap.filename);
const json = _readJSONFileSync(p);
const jsonVerStr = typeof json._version === 'string'
? json._version.at(0)
: typeof json.version === 'string'
? json.version.at(0)
: null;
let jsonVer: number;
if (jsonVerStr) {
jsonVer = parseInt(jsonVerStr);
} else {
jsonVer = 2;
}
return {
characteristic: mode,
difficulty: beatmap.difficulty,
settings: beatmap,
version: jsonVer,
data: _difficulty(json, beatmap.filename, jsonVer, opt),
};
} catch {
logger.tWarn(
tag('difficultyFromInfoSync'),
`Could not load difficulty from ${p}, skipping...`,
);
}
})
.filter((e) => e) as ILoadInfoData[];
}