Created
July 11, 2018 13:48
-
-
Save Awlexus/a76e7d0aef1473bde138eb817bb7d684 to your computer and use it in GitHub Desktop.
Basic Python script to reverse a beatmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os.path | |
from multiprocessing import Pool | |
def wline(file_, text=""): | |
file_.write("%s\n" % text) | |
def _read_till_next_tag(file_, tag="", ignore_empty=True): | |
lines = [] | |
for line in file_: | |
# check if the line matches | |
if line.strip() == tag: | |
break | |
# skip empty lines | |
if line.strip() or not ignore_empty: | |
lines.append(line) | |
return lines | |
def _reverse_spinners(hitobjects): | |
for i in range(len(hitobjects)): | |
if int(hitobjects[i][3]) == 12: | |
hitobjects[i][5] = str(int(hitobjects[i][5]) - int(hitobjects[-1 -i][2]) + int(hitobjects[i][2])) | |
def _reverse_and_switch_field(pairs, field): | |
for i in range(len(pairs) // 2): | |
pairs[i][field], pairs[-1 - i][field] = pairs[-1 - i][field], pairs[i][field] | |
def _reverse_slidershapes(hitobjects): | |
for i in range(len(hitobjects)): | |
elem = hitobjects[i] | |
# Check whether object is a slider | |
if int(elem[3]) & 2: | |
# Reverse the points | |
elem[5] = elem[5].split('|') | |
points = elem[5][1:] | |
points.reverse() | |
elem[5] = [elem[5][0]] + points | |
# join the strings | |
elem[5] = join_list_elems([elem[5]], "|")[0] | |
hitobjects[i] = elem | |
def copy_until(from_, to, until): | |
for line in _read_till_next_tag(from_, until, False): | |
to.write(line) | |
to.write('') | |
wline(to, until) | |
def join_list_elems(list_, sep=","): | |
return [sep.join(elem) for elem in list_] | |
def reverse(bmap): | |
# File where the reversed map will be written to | |
reverse_file = bmap[:-4] + ' - reverse.osu' | |
with open(bmap, 'r', encoding="utf8") as mfile, open(reverse_file, 'w', encoding="utf8") as rfile: | |
# Copy the lines until we hit the timingpoints tag | |
copy_until(mfile, rfile, "[TimingPoints]") | |
# Read and reverse timingpoints | |
timingpoints = _read_till_next_tag(mfile, "[Colours]") | |
timingpoints = [t.split(',', 1) for t in timingpoints] | |
_reverse_and_switch_field(timingpoints, 0) | |
# Write timingpoints | |
for t in join_list_elems(timingpoints): | |
rfile.write(t) | |
# Write the colours tag into the file | |
wline(rfile) | |
wline(rfile, "[Colours]") | |
# Copy the colours | |
copy_until(mfile, rfile, "[HitObjects]") | |
# Read and reverse hitobjects | |
hitobjects = _read_till_next_tag(mfile) | |
hitobjects = [h.split(',') for h in hitobjects] | |
_reverse_and_switch_field(hitobjects, 2) | |
# Reverse the slider shapes | |
_reverse_slidershapes(hitobjects) | |
_reverse_spinners(hitobjects) | |
hitobjects = join_list_elems(hitobjects) | |
hitobjects.reverse() | |
# Write the hitobjects | |
for h in hitobjects: | |
rfile.write(h) | |
if __name__ == '__main__': | |
maps = sys.argv[1:] | |
p = Pool(len(maps)) | |
p.map(reverse, maps) | |
p.close() | |
p.join() | |
print('\nDone...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment