-
Notifications
You must be signed in to change notification settings - Fork 3
/
SMUtils.hx
174 lines (157 loc) · 4.22 KB
/
SMUtils.hx
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
using StringTools;
typedef ParsedEntry = {
shouldParse:Bool,
?tag:String,
?value:String
};
// Ye olde format:
// typedef SwagSection = {
// sectionNotes:Array<Dynamic>,
// sectionBeats:Int,
// typeOfSection:Int,
// mustHitSection: Bool,
// gfSection:Bool,
// bpm:Float,
// changeBPM:Bool,
// lengthInSteps:Int
// };
typedef SwagSection = {
var sectionNotes:Array<Dynamic>;
var sectionBeats:Float;
var typeOfSection:Int;
var mustHitSection:Bool;
var gfSection:Bool;
var bpm:Float;
var changeBPM:Bool;
var altAnim:Bool;
};
class SMUtils
{
public static inline final TAG_START = '#';
public static inline final TAG_END = ';';
public static inline final TAG_SEP = ':';
public static final TAGS_TO_INCLUDE = [
'TITLE',
'OFFSET',
'BPMS',
'STOPS',
'NOTES',
'ARTIST',
'CREDIT'
];
public static inline final NOTE_NONE = '0';
public static inline final NOTE_STEP = '1';
public static inline final NOTE_HOLD_HEAD = '2';
public static inline final NOTE_TAIL = '3';
public static inline final NOTE_ROLL_HEAD = '4';
public static inline final NOTE_MINE = 'M';
public static inline final BEATS_PER_MEASURE = 4;
public static function parseBPMStr(bpmstr:String)
{
var bpmmap:Array<Array<Float>> = [];
bpmstr = bpmstr.trim();
var bpmpairs = bpmstr.split(',');
for(pair in bpmpairs)
{
var splitpar = pair.split('=');
bpmmap.push([ Std.parseFloat(splitpar[0]), Std.parseFloat(splitpar[1]) ]);
}
bpmmap.sort((a, b)->{
if(a[0] > b[0]) return 1;
else if(a[0] < b[0]) return -1;
else return 0;
});
return bpmmap;
}
public static function parseEntry(entry:String):ParsedEntry
{
var pair = entry.trim().split(TAG_SEP);
var tag = pair[0];
if(!TAGS_TO_INCLUDE.contains(tag))
{
return {
shouldParse: false,
tag: tag
};
}
var value:String;
if(pair.length > 2)
{
value = pair.slice(1).join(TAG_SEP);
}
else
{
value = pair[1];
}
if(value.endsWith(';'))
value = value.substring(0, value.length-1);
return {
shouldParse: true,
tag: tag,
value: value
};
}
public static inline function getBeatsPerRow(measure:String)
{
return BEATS_PER_MEASURE/measure.trim().split('\n').length;
}
public static function bpmFromMap(bpmmap:Array<Array<Float>>, beatn:Float)
{
for(i in 0...bpmmap.length)
{
if(bpmmap[i][0] > beatn)
return bpmmap[i-1][1];
}
return bpmmap[bpmmap.length - 1][1];
}
public static inline function makeSwagSection(bpm:Float, changebpm:Bool, bfsection=false):SwagSection
{
// return {
// sectionNotes: [],
// sectionBeats: 4,
// typeOfSection: 0,
// mustHitSection: bfsection,
// gfSection: false,
// bpm: bpm,
// changeBPM: changebpm,
// lengthInSteps: 16
// };
return {
sectionNotes: [],
sectionBeats: 4,
typeOfSection: 0,
mustHitSection: bfsection,
gfSection: false,
bpm: bpm,
changeBPM: changebpm,
altAnim: false
};
}
public static function cleanChart(chartstr:String)
{
// removes comments from the simfiles
var newchartstr = '';
var valid = true;
for(i in 0...chartstr.length)
{
var ch = chartstr.charAt(i);
if(ch == '/' && (i+1 < chartstr.length && chartstr.charAt(i+1) == '/'))
{
valid = false;
}
else if(ch == '\n')
{
newchartstr += ch;
valid = true;
}
else
{
if(valid)
{
newchartstr += ch;
}
}
}
return newchartstr;
}
}