-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptimize.ts
223 lines (210 loc) · 7.37 KB
/
optimize.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
import { IInfo } from './types/beatmap/v2/info.ts';
import {
IOptimizeOptions,
IOptimizeOptionsDifficulty,
IOptimizeOptionsInfo,
} from './types/bsmap/optimize.ts';
import { round } from './utils/math.ts';
import logger from './logger.ts';
import { IDifficulty as IDifficultyV1 } from './types/beatmap/v1/difficulty.ts';
import { IDifficulty as IDifficultyV2 } from './types/beatmap/v2/difficulty.ts';
import { IDifficulty as IDifficultyV3 } from './types/beatmap/v3/difficulty.ts';
import { isV1, isV2, isV3 } from './beatmap/version.ts';
import {
sortV2NoteFn,
sortV2ObjectFn,
sortV3NoteFn,
sortV3ObjectFn,
} from './beatmap/shared/helpers.ts';
function tag(name: string): string[] {
return ['optimize', name];
}
const optionsInfo: Required<IOptimizeOptionsInfo> = {
enabled: true,
floatTrim: 4,
stringTrim: true,
throwError: true,
removeDuplicate: true,
};
const optionsDifficulty: Required<IOptimizeOptionsDifficulty> = {
enabled: true,
floatTrim: 4,
stringTrim: true,
throwError: true,
sort: true,
};
/** Set default option value for optimize function. */
export const defaultOptions = {
info: optionsInfo,
difficulty: optionsDifficulty,
};
const ignoreObjectRemove = [
'_notes',
'_sliders',
'_events',
'_obstacles',
'_waypoints',
'_difficultyBeatmapSets',
'bpmEvents',
'rotationEvents',
'colorNotes',
'bombNotes',
'obstacles',
'sliders',
'burstSliders',
'waypoints',
'basicBeatmapEvents',
'colorBoostBeatmapEvents',
'lightColorEventBoxGroups',
'lightRotationEventBoxGroups',
'lightTranslationEventBoxGroups',
'basicEventTypesWithKeywords',
'useNormalEventsAsCompatibleEvents',
'd',
'e',
'l',
'_environmentNames',
'_colorSchemes',
'_difficultyBeatmapSets',
'_difficultyBeatmaps',
'difficultyLevels',
];
export function deepClean(
// deno-lint-ignore no-explicit-any
obj: { [key: string | number]: any } | any[],
options: IOptimizeOptions,
name = '',
) {
for (const k in obj) {
// shorten number
if (typeof obj[k] === 'number') {
obj[k] = round(obj[k], options.floatTrim);
}
// trim that string space
if (typeof obj[k] === 'string' && options.stringTrim) {
obj[k] = obj[k].trim();
}
// recursion
if ((typeof obj[k] === 'object' || Array.isArray(obj[k])) && obj[k] !== null) {
deepClean(obj[k], options, `${name}.${k}`);
// if it's lightID array, sort it
if (
(k === '_lightID' || k === 'lightID') &&
Array.isArray(obj[k]) &&
// deno-lint-ignore no-explicit-any
obj[k].every((x: any) => typeof x === 'number')
) {
obj[k] = obj[k].sort((a: number, b: number) => a - b);
}
}
// remove empty array/object property
if (
!ignoreObjectRemove.includes(k) &&
((Array.isArray(obj[k]) && !obj[k].length) ||
(typeof obj[k] === 'object' &&
!Array.isArray(obj[k]) &&
JSON.stringify(obj[k]) === '{}'))
) {
delete obj[k];
continue;
}
// throw or remove null
if (obj[k] === null) {
if (options.throwError) {
throw new Error(`null value found in object key ${name}.${k}.`);
} else {
if (Array.isArray(obj)) {
logger.tError(
tag('deepClean'),
`null value found in array ${name}[${k}], defaulting to 0...`,
);
obj[k] = 0;
} else {
logger.tError(
tag('deepClean'),
`null value found in object key ${name}.${k}, deleting property...`,
);
delete obj[k];
}
}
}
}
}
export function info(info: IInfo, options: IOptimizeOptionsInfo = { enabled: true }) {
const opt: Required<IOptimizeOptionsInfo> = {
enabled: options.enabled,
floatTrim: options.floatTrim ?? defaultOptions.info.floatTrim,
stringTrim: options.stringTrim ?? defaultOptions.info.stringTrim,
throwError: options.throwError ?? defaultOptions.info.throwError,
removeDuplicate: options.removeDuplicate ?? defaultOptions.info.removeDuplicate,
};
if (!opt.enabled) {
return info;
}
logger.tInfo(tag('info'), `Optimising info data`);
logger.tDebug(tag('info'), 'Applying deep clean');
deepClean(info, opt);
return info;
}
export function difficulty<T extends IDifficultyV1 | IDifficultyV2 | IDifficultyV3>(
difficulty: T,
options: IOptimizeOptionsDifficulty = { enabled: true },
): T {
const opt: Required<IOptimizeOptionsDifficulty> = {
enabled: options.enabled,
floatTrim: options.floatTrim ?? defaultOptions.difficulty.floatTrim,
stringTrim: options.stringTrim ?? defaultOptions.difficulty.stringTrim,
throwError: options.throwError ?? defaultOptions.difficulty.throwError,
sort: options.sort ?? defaultOptions.difficulty.sort,
};
if (!opt.enabled) {
return difficulty;
}
logger.tInfo(tag('difficulty'), `Optimising difficulty data`);
logger.tDebug(tag('difficulty'), 'Applying deep clean');
deepClean(difficulty, opt);
if (opt.sort) {
logger.tDebug(tag('difficulty'), 'Sorting objects');
if (isV1(difficulty)) {
difficulty._notes.sort(sortV2NoteFn);
difficulty._obstacles.sort(sortV2ObjectFn);
difficulty._events.sort(sortV2ObjectFn);
difficulty._bookmarks?.sort(sortV2ObjectFn);
difficulty._BPMChanges?.sort(sortV2ObjectFn);
}
if (isV2(difficulty)) {
difficulty._notes.sort(sortV2NoteFn);
difficulty._obstacles.sort(sortV2ObjectFn);
difficulty._events.sort(sortV2ObjectFn);
difficulty._sliders.sort((a, b) => a._headTime - b._headTime);
difficulty._waypoints.sort(sortV2ObjectFn);
difficulty._customData?._customEvents?.sort(sortV2ObjectFn);
difficulty._customData?._bookmarks?.sort(sortV2ObjectFn);
difficulty._customData?._bpmChanges?.sort(sortV2ObjectFn);
difficulty._customData?._BPMChanges?.sort(sortV2ObjectFn);
}
if (isV3(difficulty)) {
difficulty.colorNotes.sort(sortV3NoteFn);
difficulty.bombNotes.sort(sortV3NoteFn);
difficulty.obstacles.sort(sortV3ObjectFn);
difficulty.bpmEvents.sort(sortV3ObjectFn);
difficulty.rotationEvents.sort(sortV3ObjectFn);
difficulty.colorBoostBeatmapEvents.sort(sortV3ObjectFn);
difficulty.basicBeatmapEvents.sort(sortV3ObjectFn);
difficulty.sliders.sort(sortV3NoteFn);
difficulty.burstSliders.sort(sortV3NoteFn);
difficulty.lightColorEventBoxGroups.sort(sortV3ObjectFn);
difficulty.lightRotationEventBoxGroups.sort(sortV3ObjectFn);
difficulty.lightTranslationEventBoxGroups.sort(sortV3ObjectFn);
difficulty.waypoints.sort(sortV3NoteFn);
difficulty.customData?.customEvents?.sort(sortV3ObjectFn);
difficulty.customData?.bookmarks?.sort(sortV3ObjectFn);
difficulty.customData?.BPMChanges?.sort(sortV3ObjectFn);
difficulty.customData?.fakeColorNotes?.sort(sortV3NoteFn);
difficulty.customData?.fakeBombNotes?.sort(sortV3NoteFn);
difficulty.customData?.fakeBurstSliders?.sort(sortV3NoteFn);
difficulty.customData?.fakeObstacles?.sort(sortV3ObjectFn);
}
}
return difficulty;
}