-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.js
218 lines (199 loc) · 4.55 KB
/
parser.js
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
var keyMap = {
'TITLE': 'title',
'SUBTITLE': 'subTitle',
'ARTIST': 'artist',
'TITLETRANSLIT': 'titleTranslit',
'SUBTITLETRANSLIT': 'subTitleTransit',
'ARTISTTRANSLIT': 'artistTranslit',
'GENRE': 'genre',
'CREDIT': 'credit',
'BANNER': 'banner',
'BACKGROUND': 'background',
'LYRICSPATH': 'lyricsPath',
'CDTITLE': 'CDTitle',
'MUSIC': 'music',
'OFFSET': ['offset', parseOffset],
'SAMPLESTART': ['sampleStart', parseFloat],
'SAMPLELENGTH': ['sampleLength', parseFloat],
'SELECTABLE': ['selectable', parseBoolean],
'BPMS': ['bpms', parseBPMs],
'STOPS': ['stops', parseStops],
'DELAYS': ['delays', parseDelays],
'TIMESIGNATURES': ['timeSignatures', parseTimeSignatures],
'TICKCOUNTS': ['tickCounts', parseTickCounts],
'BGCHANGES': ['bgChanges', parseBgChanges],
'KEYSOUNDS': ['keySounds', parseKeySounds],
'ATTACKS': ['attacks', parseAttacks]
};
var sizes = {
'dance': {
'threepanel': 3,
'single': 4,
'solo': 6,
'double': 8,
'couple': 8
},
'pump': {
'single': 5,
'halfdouble': 6,
'double': 10,
'couple': 10
},
'ez2': {
'single': 5,
'double': 10,
'real': 7,
},
'para':{
'single': 5
},
'ds3ddx': {
'single': 8
},
'maniax': {
'single': 4,
'double': 8,
},
'techno': {
'single4': 4,
'single5': 5,
'single8': 8,
'double4': 8,
'double5': 10,
},
'pnm': {
'five': 5,
'nine': 9
}
};
function parseOffset(v) {
return parseFloat(v) * 1000;
}
function parseBoolean(v) {
return v === 'YES' ? true : false;
}
function parseBPMs(v) {
return v.split(',').map(function(str) {
return str.split('=').map(function(flt) {
return parseFloat(flt);
});
});
}
function parseStops(v) {
return v;
}
function parseDelays(v) {
return v;
}
function parseTimeSignatures(v) {
return v;
}
function parseTickCounts(v) {
return v;
}
function parseBgChanges(v) {
return v;
}
function parseKeySounds(v) {
return v;
}
function parseAttacks(v) {
return v;
}
function parseNotes(mode, what, difficulty, steps, what2, notes) {
var modeDesc = mode.split('-');
var upperLimit = sizes[modeDesc[0]][modeDesc[1]];
return {
mode: mode,
difficulty: difficulty,
steps: parseInt(steps),
rawNotes: notes.split(',').map(function(measure) {
var re = new RegExp(".{1," + upperLimit + "}","g");
return measure.match(re);
})
};
}
function parseMeasures(data) {
var baseBPM = data.bpms[0][1];
var measureLength = 60000 / baseBPM * 4;
data.notes.forEach(function(difficulty) {
var offset = 0;
var notes = difficulty.notes = [];
difficulty.rawNotes.forEach(function(measure, measureId) {
if(!measure){return}
var len = measure.length;
var noteTime = measureLength / len;
measure.forEach(function(note, i) {
// add mine handling
if (parseInt(note)) {
notes.push({
offset: offset,
steps: note,
measure: measureId,
type: getStepType(i, len)
});
}
offset += noteTime;
});
});
});
}
// TODO: add support for triples
function getStepType(offset, parts) {
var pow = 1, part;
// type 1 if there are 4 parts or less
if (parts / 4 <= 1) {
return 1;
} else {
// reduce offset by quads
parts = parts / 4;
while (offset >= parts) {
offset -= parts;
}
}
// type 1
if (offset === 0) {
return 1;
}
for (var i = 2; i < 5; i++) {
pow *= 2;
part = parts / pow;
if (part !== parseInt(part)) break;
if (offset - part === 0) {
return i;
} else if (offset - part > 0) {
offset -= part;
}
}
return 8;
}
var fs = require('fs');
var path = require('path');
var filepath = process.argv[2];
var dir = path.dirname(filepath);
var filename = path.basename(filepath, '.sm');
var outFile = path.join(dir, filename + '.json');
var content = fs.readFileSync(filepath, 'utf-8');
var inData = content
.replace(/(\/\*([\s\S]*?)\*\/)|(\/\/(.*)$)/gm, '')
.replace(/[\r\n\t\s]/g, '')
.split(';')
.map(function(field) {
return field.split(':');
});
var outData = {
notes: []
};
inData.forEach(function(field) {
var fieldName = field[0].slice(1);
var map = keyMap[fieldName];
if (fieldName === 'NOTES') {
outData.notes.push(parseNotes.apply(undefined, field.slice(1)));
} else if (typeof map === 'string') {
outData[map] = field[1];
} else if (map) {
outData[map[0]] = map[1].apply(undefined, field.slice(1));
}
});
parseMeasures(outData);
fs.writeFileSync(outFile, JSON.stringify(outData, undefined, '\t'), 'utf-8');