-
Notifications
You must be signed in to change notification settings - Fork 5
/
osufilereader.cpp
340 lines (272 loc) · 10.8 KB
/
osufilereader.cpp
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
#include "osufilereader.h"
#include <prism/mugendefreader.h>
#include <prism/memoryhandler.h>
#include <prism/system.h>
#include <prism/log.h>
static OsuFile createEmptyOsuFile() {
OsuFile ret;
ret.mOsuTimingPoints = new_list();
ret.mOsuColors = new_list();
ret.mOsuHitObjects = new_list();
return ret;
}
static int isLinebreak(char p) {
return p == '\n' || p == 0xD || p == 0xA;
}
static Buffer removeOsuComments(Buffer& b) {
Buffer dstBuffer = makeBuffer(allocMemory(b.mLength), b.mLength);
char* src = (char*)b.mData;
char* dst = (char*)dstBuffer.mData;
int j = 0;
uint32_t i;
for (i = 0; i < b.mLength - 1; i++) {
if (src[i] == '/' && src[i + 1] == '/') {
while (i < b.mLength && !isLinebreak(src[i])) i++;
while (i < b.mLength && isLinebreak(src[i])) i++;
i--;
}
else {
dst[j] = src[i];
j++;
}
}
dstBuffer.mData = reallocMemory(dstBuffer.mData, j);
dstBuffer.mLength = j;
freeBuffer(b);
return dstBuffer;
}
static void loadOsuFileGeneral(OsuFileGeneral* tDst, MugenDefScript* tScipt) {
tDst->mAudioFileName = getAllocatedMugenDefStringOrDefault(tScipt, "general", "audiofilename", "music.wav");
tDst->mAudioLeadIn = getMugenDefIntegerOrDefault(tScipt, "general", "audioleadin", 0);
tDst->mPreviewTime = getMugenDefIntegerOrDefault(tScipt, "general", "previewtime", 0);
tDst->mCountdown = getMugenDefIntegerOrDefault(tScipt, "general", "countdown", 0);
tDst->mStackLeniency = getMugenDefFloatOrDefault(tScipt, "general", "stackleniency", 0);
}
static void loadOsuFileDifficulty(OsuFileDifficulty* tDst, MugenDefScript* tScipt) {
tDst->mHPDrainRate = getMugenDefIntegerOrDefault(tScipt, "difficulty", "hpdrainrate", 0);
tDst->mCircleSize = getMugenDefFloatOrDefault(tScipt, "difficulty", "circlesize", 5);
tDst->mOverallDifficulty = getMugenDefIntegerOrDefault(tScipt, "difficulty", "overalldifficulty", 5);
tDst->mApproachRate = getMugenDefFloatOrDefault(tScipt, "difficulty", "approachrate", 5);
tDst->mSliderMultiplier = getMugenDefFloatOrDefault(tScipt, "difficulty", "slidermultiplier", 0);
tDst->mSliderTickRate = getMugenDefIntegerOrDefault(tScipt, "difficulty", "slidertickrate", 0);
}
typedef struct {
OsuEvents* mDst;
} OsuEventLoadCaller;
static void loadSingleBreak(OsuEvents* tDst, MugenStringVector* tVector) {
if (tVector->mSize < 3) return;
OsuEventBreak* e = (OsuEventBreak*)allocMemory(sizeof(OsuEventBreak));
e->mStart = atoi(tVector->mElement[1]);
e->mEnd = atoi(tVector->mElement[2]);
list_push_back_owned(&tDst->mBreaks, e);
}
static void loadSingleOsuEvent(void* tCaller, void* tData) {
OsuEventLoadCaller* caller = (OsuEventLoadCaller*)tCaller;
MugenDefScriptGroupElement* element = (MugenDefScriptGroupElement*)tData;
if (element->mType != MUGEN_DEF_SCRIPT_GROUP_VECTOR_ELEMENT) return;
MugenDefScriptVectorElement* vectorElement = (MugenDefScriptVectorElement*)element->mData;
int type = atoi(vectorElement->mVector.mElement[0]);
if (type == 2) {
loadSingleBreak(caller->mDst, &vectorElement->mVector);
}
}
static void loadOsuFileEvent(OsuEvents* tDst, MugenDefScript* tScript) {
tDst->mBreaks = new_list();
MugenDefScriptGroup* group = &tScript->mGroups["events"];
OsuEventLoadCaller caller;
caller.mDst = tDst;
list_map(&group->mOrderedElementList, loadSingleOsuEvent, &caller);
}
typedef struct {
OsuFile* mDst;
} OsuFileTimingPointLoadCaller;
static void loadSingleOsuFileTimingPoint(void* tCaller, void* tData) {
OsuFileTimingPointLoadCaller* caller = (OsuFileTimingPointLoadCaller*)tCaller;
MugenDefScriptGroupElement* element = (MugenDefScriptGroupElement*)tData;
if (element->mType != MUGEN_DEF_SCRIPT_GROUP_VECTOR_ELEMENT) return;
MugenDefScriptVectorElement* vectorElement = (MugenDefScriptVectorElement*)element->mData;
if (vectorElement->mVector.mSize < 2) return;
OsuTimingPoint* e = (OsuTimingPoint*)allocMemory(sizeof(OsuTimingPoint));
e->mOffset = atoi(vectorElement->mVector.mElement[0]);
e->mMillisecondsPerBeat = atof(vectorElement->mVector.mElement[1]);
e->mMeter = (vectorElement->mVector.mSize >= 3) ? atoi(vectorElement->mVector.mElement[2]) : 4;
e->mSampleIndex = (vectorElement->mVector.mSize >= 5) ? atoi(vectorElement->mVector.mElement[4]) : 0;
e->mVolume = (vectorElement->mVector.mSize >= 6) ? atoi(vectorElement->mVector.mElement[5]) : 100;
e->mInherited = (vectorElement->mVector.mSize >= 7) ? atoi(vectorElement->mVector.mElement[6]) : 1;
e->mKiaiMode = (vectorElement->mVector.mSize >= 8) ? atoi(vectorElement->mVector.mElement[7]) : 0;
list_push_back_owned(&caller->mDst->mOsuTimingPoints, e);
}
static void loadOsuFileTimingPoints(OsuFile* tDst, MugenDefScript* tScript) {
MugenDefScriptGroup* group = &tScript->mGroups["timingpoints"];
OsuFileTimingPointLoadCaller caller;
caller.mDst = tDst;
list_map(&group->mOrderedElementList, loadSingleOsuFileTimingPoint, &caller);
}
typedef struct {
OsuFile* mDst;
} OsuFileColorLoadCaller;
static void createAndAddOsuColor(OsuFile* tFile, double r, double g, double b) {
OsuColor* e = (OsuColor*)allocMemory(sizeof(OsuColor));
e->mR = r;
e->mG = g;
e->mB = b;
list_push_back_owned(&tFile->mOsuColors, e);
}
static void loadSingleOsuFileColor(void* tCaller, void* tData) {
OsuFileColorLoadCaller* caller = (OsuFileColorLoadCaller*)tCaller;
MugenDefScriptGroupElement* element = (MugenDefScriptGroupElement*)tData;
if (element->mType != MUGEN_DEF_SCRIPT_GROUP_VECTOR_ELEMENT) return;
MugenDefScriptVectorElement* vectorElement = (MugenDefScriptVectorElement*)element->mData;
if (vectorElement->mVector.mSize != 3) return;
double r = atoi(vectorElement->mVector.mElement[0]) / 255.0;
double g = atoi(vectorElement->mVector.mElement[1]) / 255.0;
double b = atoi(vectorElement->mVector.mElement[2]) / 255.0;
createAndAddOsuColor(caller->mDst, r, g, b);
}
static void loadOsuFileColors(OsuFile* tDst, MugenDefScript* tScript) {
if (tScript->mGroups.find("colours") == tScript->mGroups.end()) {
createAndAddOsuColor(tDst, 1, 1, 1);
return;
}
MugenDefScriptGroup* group = &tScript->mGroups["colours"];
OsuFileColorLoadCaller caller;
caller.mDst = tDst;
list_map(&group->mOrderedElementList, loadSingleOsuFileColor, &caller);
}
typedef struct {
OsuFile* mDst;
} OsuFileHitObjectLoadCaller;
static MugenStringVector parseVectorString(char* tText, char tSeparator) {
MugenStringVector vector;
char* comma = tText;
vector.mSize = 0;
while (comma != NULL) {
vector.mSize++;
comma = strchr(comma + 1, tSeparator);
}
vector.mElement = (char**)allocMemory(sizeof(char*)*vector.mSize);
comma = tText - 1;
int i = 0;
while (comma != NULL) {
char temp[MUGEN_DEF_STRING_LENGTH];
strcpy(temp, comma + 1);
char* tempComma = strchr(temp, tSeparator);
if (tempComma != NULL) *tempComma = '\0';
comma = strchr(comma + 1, tSeparator);
int offset = 0;
while (temp[offset] == ' ') offset++;
char* start = temp + offset;
vector.mElement[i] = (char*)allocMemory(int(strlen(start)) + 3);
strcpy(vector.mElement[i], start);
i++;
}
if (i != vector.mSize) {
logWarningFormat("Unable to parse vector %s: %d vs %d sizes detected.", tText, i, vector.mSize);
recoverFromError();
}
return vector;
}
static void unloadMugenStringVector(MugenStringVector* e) {
int i;
for (i = 0; i < e->mSize; i++) {
freeMemory(e->mElement[i]);
}
freeMemory(e->mElement);
}
Vector3DI parseEndPositionFromPathString(char* tPathString) {
MugenStringVector vector = parseVectorString(tPathString, '|');
if (vector.mSize < 2) {
unloadMugenStringVector(&vector);
return Vector3DI(-1, -1, 0);
}
char* positionString = vector.mElement[vector.mSize - 1];
char* sepPosition = strchr(positionString, ':');
if (!sepPosition) {
unloadMugenStringVector(&vector);
return Vector3DI(-1, -1, 0);
}
*sepPosition = '\0';
char* posXString = positionString;
char* posYString = sepPosition + 1;
int posX = atoi(posXString);
int posY = atoi(posYString);
unloadMugenStringVector(&vector);
return Vector3DI(posX, posY, 0);
}
static void loadSingleOsuFileHitObject(void* tCaller, void* tData) {
OsuFileHitObjectLoadCaller* caller = (OsuFileHitObjectLoadCaller*)tCaller;
MugenDefScriptGroupElement* element = (MugenDefScriptGroupElement*)tData;
if (element->mType != MUGEN_DEF_SCRIPT_GROUP_STRING_ELEMENT) return;
MugenDefScriptStringElement* stringElement = (MugenDefScriptStringElement*)element->mData;
MugenStringVector vector = parseVectorString(stringElement->mString, ',');
if (vector.mSize < 5) {
unloadMugenStringVector(&vector);
return;
}
uint8_t type = (uint8_t)atoi(vector.mElement[3]);
if (type & OSU_TYPE_MASK_HIT_OBJECT) {
OsuHitObject* e = (OsuHitObject*)allocMemory(sizeof(OsuHitObject));
e->mX = atoi(vector.mElement[0]);
e->mY = atoi(vector.mElement[1]);
e->mTime = atoi(vector.mElement[2]);
e->mType = (uint8_t)atoi(vector.mElement[3]);
e->mHitSound = (uint8_t)atoi(vector.mElement[4]);
list_push_back_owned(&caller->mDst->mOsuHitObjects, e);
}
else if (type & OSU_TYPE_MASK_SLIDER) {
if (vector.mSize < 8) {
unloadMugenStringVector(&vector);
return;
}
OsuSliderObject* e = (OsuSliderObject*)allocMemory(sizeof(OsuSliderObject));
e->mX = atoi(vector.mElement[0]);
e->mY = atoi(vector.mElement[1]);
e->mTime = atoi(vector.mElement[2]);
e->mType = (uint8_t)atoi(vector.mElement[3]);
e->mHitSound = (uint8_t)atoi(vector.mElement[4]);
e->mEndPosition = parseEndPositionFromPathString(vector.mElement[5]);
e->mRepeat = atoi(vector.mElement[6]);
e->mPixelLength = atof(vector.mElement[7]);
list_push_back_owned(&caller->mDst->mOsuHitObjects, e);
}
else if (type & OSU_TYPE_MASK_SPINNER) {
if (vector.mSize < 6) {
unloadMugenStringVector(&vector);
return;
}
OsuSpinnerObject* e = (OsuSpinnerObject*)allocMemory(sizeof(OsuSpinnerObject));
e->mX = atoi(vector.mElement[0]);
e->mY = atoi(vector.mElement[1]);
e->mTime = atoi(vector.mElement[2]);
e->mType = (uint8_t)atoi(vector.mElement[3]);
e->mHitSound = (uint8_t)atoi(vector.mElement[4]);
e->mEndTime = atoi(vector.mElement[5]);
list_push_back_owned(&caller->mDst->mOsuHitObjects, e);
}
unloadMugenStringVector(&vector);
}
static void loadOsuFileHitObjects(OsuFile* tDst, MugenDefScript* tScript) {
MugenDefScriptGroup* group = &tScript->mGroups["hitobjects"];
OsuFileHitObjectLoadCaller caller;
caller.mDst = tDst;
list_map(&group->mOrderedElementList, loadSingleOsuFileHitObject, &caller);
}
static void loadOsuFileFromScript(OsuFile * tDst, MugenDefScript* tScript) {
loadOsuFileGeneral(&tDst->mGeneral, tScript);
loadOsuFileDifficulty(&tDst->mDifficulty, tScript);
loadOsuFileEvent(&tDst->mEvents, tScript);
loadOsuFileTimingPoints(tDst, tScript);
loadOsuFileColors(tDst, tScript);
loadOsuFileHitObjects(tDst, tScript);
}
OsuFile loadOsuFile(char * tPath)
{
OsuFile ret = createEmptyOsuFile();
Buffer b = fileToBuffer(tPath);
b = removeOsuComments(b);
MugenDefScript script;
loadMugenDefScriptFromBufferAndFreeBuffer(&script, b);
loadOsuFileFromScript(&ret, &script);
unloadMugenDefScript(&script);
return ret;
}