-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.py
351 lines (309 loc) · 12.2 KB
/
convert.py
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
import os
import json
import sys
import zipfile
from shutil import rmtree
from msvcrt import getch
import requests
import webbrowser
from tqdm import tqdm
import string
import random
if getattr(sys, 'frozen', False):
os.chdir(os.path.split(sys.executable)[0])
#https://codeday.me/ko/qa/20190316/78831.html
print("Malody to osu!mania Converter v1.2")
print("October 16th, 2019")
print("by Jakads\n\n")
version = "1.2"
if '--:update' in sys.argv: #added ":" to disallow user to view this message by dragging in files
print(f"[O] Successfully updated to v{version}! :D\n\n")
sys.argv.remove('--:update')
print("(i) Checking for updates . . .")
try:
latest = requests.get('https://github.com/jakads/Malody-to-Osumania/raw/master/version.txt')
latest.raise_for_status()
print(f"\n(i) Latest Version = v{latest.text}")
if latest.text != version:
print("\n[!] New update is available! Would you like to download? (Y/N)")
choice = getch()
while choice != (b'y' or b'Y' or b'n' or b'N'):
choice = getch()
if choice == (b'n' or b'N'):
print("(i) Skipping the update.")
else:
print("(i) Downloading . . .")
exe = requests.get("https://github.com/jakads/Malody-to-Osumania/raw/master/convert.exe", stream=True)
exe.raise_for_status()
total = int(exe.headers.get('content-length'))
progress = tqdm(total=total, unit='B', unit_scale=True, unit_divisor=1024, ncols=80) #https://github.com/tqdm/tqdm/wiki/How-to-make-a-great-Progress-Bar
rand=''.join(random.choice(string.ascii_letters + string.digits) for i in range(16)) #https://pynative.com/python-generate-random-string/
filename=os.path.split(sys.executable)[1]
with open(f"{rand}.exe", 'wb') as f: #https://stackoverflow.com/questions/37573483
for chunk in exe.iter_content(chunk_size=4096):
if chunk:
progress.update(4096)
f.write(chunk)
with open(f'{rand}.bat', 'w') as f:
f.write('\n'.join(['@echo off',
'echo (i) Restarting . . .',
"echo (i) All the files you've dragged in will be converted very soon!",
'timeout /t 5 /nobreak >nul',
f'del {filename}',
f'rename {rand}.exe {filename}',
f'start {filename} --:update {" ".join(sys.argv[1:]) if len(sys.argv)>1 else ""}',
f'del {rand}.bat']))
os.startfile(f'{rand}.bat')
sys.exit()
#webbrowser.open('https://github.com/jakads/Malody-to-Osumania#changelog')
else:
print("\n(i) Your program is as good as new! We're good to go.\n\n")
except Exception as e:
print("\n[!] Fatal Error while connecting:", e)
print("\n[!] Connection to GitHub failed. Will just continue...\n\n")
def recursive_file_gen(mydir):
for root, dirs, files in os.walk(mydir):
for file in files:
yield os.path.join(root, file)
#https://stackoverflow.com/questions/2865278
def convert(i, bgtmp, soundtmp):
try:
with open(f'{i}',encoding='utf-8') as mc:
mcFile = json.loads(mc.read())
mcFile['meta']['mode']
except:
print(f"[!] FileWarning: {os.path.split(i)[1]} is not a valid .mc file. Ignoring...")
return 1
if not mcFile['meta']['mode'] == 0:
print(f"[!] KeyWarning: {os.path.split(i)[1]} is not a Key difficulty. Ignoring...")
return 1
else:
line = mcFile['time']
lineset = set()
for x in line:
lineset.add(x["bpm"])
if len(lineset)>1:
bpmname.append(os.path.split(i)[1])
print(f"[!] BPMWarning: {os.path.split(i)[1]} contains one or more BPM changes. Ignoring...")
MultiBPM = True
return 1
meta = mcFile['meta']
note = mcFile['note']
keys = meta["mode_ext"]["column"]
bpm = int(line[0]["bpm"])
try:
offset = -int(note[-1]["offset"])
except:
offset = 0
try:
preview = meta["preview"]
except:
preview = -1
try:
titleorg = meta["song"]["titleorg"]
except:
titleorg = meta["song"]["title"]
try:
artistorg = meta["song"]["artistorg"]
except:
artistorg = meta["song"]["artist"]
global title
title = meta["song"]["title"]
global artist
artist = meta["song"]["artist"]
background = meta["background"]
if not background=="": bgtmp.append(f'{os.path.split(i)[0]}\\{background}')
sound = note[-1]["sound"]
if not sound=="": soundtmp.append(f'{os.path.split(i)[0]}\\{sound}')
creator = meta["creator"]
version = meta["version"]
with open(f'{os.path.splitext(i)[0]}.osu',mode='at',encoding='utf-8') as osu:
osuformat = ['osu file format v14',
'',
'[General]',
f'AudioFilename: {sound}',
'AudioLeadIn: 0',
f'PreviewTime: {preview}',
'Countdown: 0',
'SampleSet: Soft',
'StackLeniency: 0.7',
'Mode: 3',
'LetterboxInBreaks: 0',
'SpecialStyle: 0',
'WidescreenStoryboard: 0',
'',
'[Editor]',
'DistanceSpacing: 1.2',
'BeatDivisor: 4',
'GridSize: 8',
'TimelineZoom: 2.4',
'',
'[Metadata]',
f'Title:{title}',
f'TitleUnicode:{titleorg}',
f'Artist:{artist}',
f'ArtistUnicode:{artistorg}',
f'Creator:{creator}',
f'Version:{version}',
'Source:Malody',
'Tags:Malody Convert by Jakads',
'BeatmapID:0',
'BeatmapSetID:-1',
'',
'[Difficulty]',
'HPDrainRate:8',
f'CircleSize:{keys}',
'OverallDifficulty:8',
'ApproachRate:5',
'SliderMultiplier:1.4',
'SliderTickRate:1',
'',
'[Events]',
'//Background and Video events',
f'0,0,\"{background}\",0,0',
'',
'[TimingPoints]',
f'{offset},{60000/bpm},4,1,0,0,1,0',
'',
'[HitObjects]\n']
osu.write('\n'.join(osuformat))
#https://thrillfighter.tistory.com/310
for n in note[:-1]:
if ms(n["beat"], bpm, offset)+offset >= 0:
if len(n) == 2: #Regular Note
osu.write(f'{col(n["column"], keys)},192,{ms(n["beat"], bpm, offset)},1,0\n')
else: #Long Note
osu.write(f'{col(n["column"], keys)},192,{ms(n["beat"], bpm, offset)},128,0,{ms(n["endbeat"], bpm, offset)}\n')
return 0
def ms(beats, bpm, offset): #beats = [measure, nth beat, divisor]
return int(1000*(60/bpm)*(beats[0]+beats[1]/beats[2]))+offset
def col(column, keys):
return int(512*(2*column+1)/(2*keys))
def compress(compressname, name, bglist, soundlist):
osz = zipfile.ZipFile(f'{compressname}.osz','w')
for i in name:
osz.write(f'{os.path.splitext(i)[0]}.osu')
os.remove(f'{os.path.splitext(i)[0]}.osu')
print(f'[O] Compressed: {os.path.split(i)[1]}.osu')
if not len(bglist)==0:
for i in bglist:
osz.write(f'{i}')
print(f'[O] Compressed: {os.path.split(i)[1]}')
if not len(soundlist)==0:
for i in soundlist:
osz.write(f'{i}')
print(f'[O] Compressed: {os.path.split(i)[1]}\n')
osz.close()
oszname.append(f'{compressname}.osz')
if len(sys.argv)<=1:
print("(i) Drag .mc or .mcz/.zip files into this program to convert them to .osu or .osz!")
print("(i) Press any key to turn off the program.")
getch()
sys.exit()
MCDragged = False
MCValid = False
ZIPDragged = False
MultiBPM = False
mcname = []
zipname = []
bpmname = []
foldername = []
oszname = []
mctmp = []
for x in sys.argv[1:]:
isMCZ = False
if os.path.isdir(x):
print(f"[!] FileWarning: {os.path.split(x)[1]} is a directory, not a file. Ignoring...")
elif not os.path.isfile(x):
print(f"[!] FileWarning: {os.path.split(x)[1]} does not exist. I recommend dragging the file into the program, not with a command prompt. Ignoring...")
elif os.path.splitext(x)[1].lower() == ".mc":
mctmp.append(x)
MCDragged = True
elif os.path.splitext(x)[1].lower() == (".mcz" or ".zip"):
if os.path.splitext(x)[1].lower() == ".mcz":
isMCZ = True
os.rename(x, f'{os.path.splitext(x)[0]}.zip')
x = f'{os.path.splitext(x)[0]}.zip'
mcz = zipfile.ZipFile(x)
mcz.extractall(os.path.splitext(x)[0])
mcz.close()
if isMCZ:
os.rename(x, f'{os.path.splitext(x)[0]}.mcz')
zipname.append(os.path.splitext(x)[0])
ZIPDragged = True
else:
print(f"[!] FileWarning: The file type of {os.path.split(x)[1]} is not supported. Ignoring...")
if MCDragged:
mcname.append(mctmp)
if not MCDragged and not ZIPDragged:
print("\n[X] FILEERROR: None of the files you've dragged in are supported. This program only accepts .mc, .mcz, or .zip files.")
print("(i) Press any key to turn off the program.")
getch()
sys.exit()
title = ""
artist = ""
mctitle = ""
mcartist = ""
bglist = []
soundlist = []
print("\n\n(i) Converting . . .\n")
#Converting to .osu (dragged .mc files)
if MCDragged:
bgtmp = []
soundtmp = []
for i in mcname[0]:
if not convert(i, bgtmp, soundtmp):
print(f'[O] Converted: {os.path.split(i)[1]}')
MCValid = True
mctitle = title
mcartist = artist
bglist.append(bgtmp)
soundlist.append(soundtmp)
#Converting to .osu (dragged .mcz/.zip files)
if ZIPDragged:
for folder in zipname:
print(f'\n(i) Converting {os.path.split(folder)[1]} . . .\n')
c=0
bgtmp = []
soundtmp = []
mctmp = []
filelist = list(recursive_file_gen(folder))
for files in filelist:
if os.path.splitext(files)[1] == ".mc":
if not convert(files, bgtmp, soundtmp):
print(f'[O] Converted: {os.path.split(files)[1]}')
c+=1
MCValid = True
mctmp.append(files)
if c>0:
foldername.append(folder)
bglist.append(bgtmp)
soundlist.append(soundtmp)
mcname.append(mctmp)
if MultiBPM:
print("\n(i) This program does not support difficulties with multiple BPMs yet, thus the following files has not been converted.\n(i) Sorry for the inconvenience, and please try again in the future updates.")
for i in bpmname:
print(f'* {i}')
if not MCValid:
print("\n[X] FILEERROR: None of the files you've dragged are supported.")
print("(i) Press any key to turn off the program.")
getch()
sys.exit()
print('\n\n(i) All the supported .mc files have been converted to .osu!\n(i) Either close the program now and move the files manually,\n(i) or press Enter to compress all into .osz.')
getch()
print('\n(i) Compressing . . .\n')
#Compress to .osz (dragged .mc files as single mapset)
if MCDragged:
compress(f'{mcartist} - {mctitle}', mcname[0], set(bglist[0]), set(soundlist[0]))
if ZIPDragged:
i = 1 if MCDragged else 0
for folder in zipname:
print(f'\n(i) Compressing {os.path.split(folder)[1]} . . .\n')
compress(f'{os.path.split(folder)[1]}', mcname[i], set(bglist[i]), set(soundlist[i]))
i+=1
rmtree(folder)
print('\n(i) The following .osz files have been created! Run the files to add the maps to osu! automatically.\n')
for i in oszname:
print(f'* {i}')
print('\n(i) Press any key to turn off the program.')
getch()