Skip to content

Commit

Permalink
Properly handle shift-jis xml files, ...
Browse files Browse the repository at this point in the history
Add title and artist text clamping
Replace mdb search with xpath code
  • Loading branch information
camprevail committed May 9, 2021
1 parent b2137e4 commit e036505
Showing 1 changed file with 59 additions and 19 deletions.
78 changes: 59 additions & 19 deletions museca1_5/scorecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@

package_dir = Path(os.path.relpath(__file__)).parent

# I'm not going to include these two xml files, but they're easy enough to get.
# lxml doesn't like to parse these characters ( 隯, 霻, Ⅱ, 隯) , so you'll have to remove them manually.
mdb = etree.parse(str(package_dir / 'assets/music-info-b.xml')).getroot()
grafica_xml = etree.parse(str(package_dir / 'assets/grafica-info-b.xml')).getroot()

# Seek past the xml declaration since we're specifying encoding, cause lxml doesn't like it when we do that.
def seekXml(path):
with open(path, 'r', encoding='shift_jisx0213') as f:
if '<?xml' in f.readline():
return etree.parse(f)
else:
f.seek(0)
return etree.parse(f)

mdb = seekXml(str(package_dir / 'assets/music-info-b.xml'))

img_save_dir = package_dir / 'static'

Expand Down Expand Up @@ -72,14 +79,15 @@ def extract_info(self, call):
info['mission_grafica'] = etc[28]
info['mission_level'] = etc[29]
info['mission_percentage'] = etc[30]
for music in mdb.findall('music'):
id = music.find('info').find('label')
if id.text == info['music_id']:
info['title'] = music.find('info').find('title_name').text
info['artist'] = music.find('info').find('artist_name').text
diffmap = {'0': 'novice', '1': 'advanced', '2': 'exhaust'}
diff = diffmap.get(info['music_type'])
info['difficulty'] = music.find('difficulty').find(diff).find('difnum').text

music = mdb.find(f'music[@id="{info["music_id"]}"]')
if music is None :
raise Exception("This song isn't in the musicdb.")
info['title'] = music.find('info').find('title_name').text
info['artist'] = music.find('info').find('artist_name').text
diffmap = {'0': 'novice', '1': 'advanced', '2': 'exhaust'}
diff = diffmap.get(info['music_type'])
info['difficulty'] = music.find('difficulty').find(diff).find('difnum').text
try:
info['old_score'] = game_3.find('old_score').text
except:
Expand Down Expand Up @@ -108,14 +116,18 @@ def create_image(self, info):
record_font = ImageFont.truetype(str(package_dir / "assets/font/museca.ttf"), size=18)
record_font_shadow = ImageFont.truetype(str(package_dir / "assets/font/museca.ttf"), size=19)
record_font_2 = ImageFont.truetype(str(package_dir / "assets/font/museca.ttf"), size=15)

# ----- Name text -----
draw.text((161, 45), info['player_name'], (0, 0, 0), font=namefont)

# ----- Time text -----
now = datetime.now(timezone.utc).strftime("%Y/%m/%d - %I:%M%p UTC")
draw.text((161, 100), now, (133, 133, 133), font=dtfont)

# ----- Curator Rank -----
with Image.open(package_dir / 'assets/rank/rank_{}.png'.format(int(info['curator_rank']))) as rankimg:
base.paste(rankimg, (69, 26), mask=rankimg)

# ----- Jacket -----
try:
base.paste(Image.open(
Expand All @@ -134,20 +146,39 @@ def create_image(self, info):
try:
base.paste(Image.open(package_dir / 'assets/jackets/jk_01_0000_0_b.png'), (471, 124))
if int(info['music_id']) > 226:
log.error("Jacket(s) don't exist, using default jacket.")
with Image.open(package_dir / 'assets/misc/mplus.png') as mplus:
base.paste(mplus, (425, 127), mask=mplus)
log.error("Jacket(s) don't exist, using default jacket.")
except FileNotFoundError:
log.error("Jacket(s) don't exist, did you fuck something up?")

# ----- Title/Artist text -----
# ----- Title text -----
title = self.fixBrokenChars(info['title'])
titleW, titleH = draw.textsize(title, font=title_font)
if titleW > 247:
titleW, titleH = draw.textsize(title, font=title_font_s)
titlecanvas = Image.new('RGBA', (titleW, titleH), color=(255, 255, 255, 0))
titledraw = ImageDraw.Draw(titlecanvas)
titledraw.text((0, 0), title, (30, 30, 30), font=title_font_s)
if titlecanvas.size[0] > 247: # If the smaller font size still doesn't fit, resize the text image to fit.
titlecanvas = titlecanvas.resize((247, titleH), resample=Image.LANCZOS)
base.paste(titlecanvas, (694 - titlecanvas.size[0], 359), mask=titlecanvas)
else:
draw.text((694 - draw.textsize(title, font=title_font)[0], 359), title, (30, 30, 30),
font=title_font)

# ------ Artist text ------
artist = self.fixBrokenChars(info['artist'])
draw.text((694 - draw.textsize(artist, font=artist_font)[0], 382), artist, (120, 120, 120), font=artist_font)
if draw.textsize(title, font=title_font)[0] > 248:
draw.text((694 - draw.textsize(title, font=title_font_s)[0], 359), title, (30, 30, 30), font=title_font_s)
artistW, artistH = draw.textsize(artist, font=artist_font)
if artistW > 247:
artistcanvas = Image.new('RGBA', (artistW, artistH), color=(255, 255, 255, 0))
artistdraw = ImageDraw.Draw(artistcanvas)
artistdraw.text((0, 0), artist, (120, 120, 120), font=artist_font)
artistcanvas = artistcanvas.resize((247, artistH), resample=Image.LANCZOS)
base.paste(artistcanvas, (694 - artistcanvas.size[0], 382), mask=artistcanvas)
else:
draw.text((694 - draw.textsize(title, font=title_font)[0], 359), title, (30, 30, 30), font=title_font)
draw.text((694 - draw.textsize(artist, font=artist_font)[0], 382), artist, (120, 120, 120), font=artist_font)

# ----- Score text -----
draw.text((694 - draw.textsize(info['new_score'], font=scorefont)[0], 431), info['new_score'], (0, 0, 0),
font=scorefont)
Expand All @@ -161,11 +192,13 @@ def create_image(self, info):
font=subscorefont)
draw.text((694 - draw.textsize(info['max_combo'], font=subscorefont)[0], 583), info['max_combo'], (0, 0, 0),
font=subscorefont)

# ----- Level -----
with Image.open(package_dir / 'assets/numbers/lv_{}.png'.format(info['difficulty'])) as levelimg:
base.paste(levelimg, (609, 41), mask=levelimg)
with Image.open(package_dir / 'assets/misc/difficulty_{}.png'.format(int(info['music_type']))) as levelicon:
base.paste(levelicon, (542, 89), mask=levelicon)

# ----- Grade -----
with Image.open(package_dir / 'assets/grade/grade_{}.png'.format(info['score_grade'])) as grade:
base.paste(grade, (467, 682), mask=grade)
Expand All @@ -176,9 +209,11 @@ def create_image(self, info):
else:
with Image.open(package_dir / 'assets/misc/grade_index_0.png') as pointer:
base.paste(pointer, (pointer_x_map.get(info['score_grade']), 650), mask=pointer)

# ----- Track number -----
with Image.open(package_dir / 'assets/misc/track_{}.png'.format(info['track_no'])) as layer:
base.paste(layer, (0, 221), mask=layer)

# ----- GRAFICA -----
if info['grafica_1'] != '0':
base.paste(Image.open(package_dir / 'assets/grafica/{}.png'.format(info['grafica_1'])), (126, 134))
Expand All @@ -198,9 +233,11 @@ def create_image(self, info):
base.paste(medel, (186, 854), mask=medel)
with Image.open(package_dir / 'assets/misc/frame_3.png') as frame:
base.paste(frame, (126, 668), mask=frame)

# ----- Connect All -----
if info['clear_type'] == '4':
base.paste(Image.open(package_dir / 'assets/misc/ca_icon_big.png'), (475, 501))

# ----- Score difference -----
if 'old_score' in info.keys():
old_score, new_score = int(info['old_score']), int(info['new_score'])
Expand All @@ -225,6 +262,7 @@ def create_image(self, info):
with Image.open(package_dir / 'assets/misc/minus_record_bg.png') as minus_record:
base.paste(minus_record, (493, 470), mask=minus_record)
draw.text((692 - draw.textsize(diff, font=record_font_2)[0], 471), diff, (0, 0, 0), font=record_font_2)

# ----- Object Placement -----
if info['object_placement'] == '1':
with Image.open(package_dir / 'assets/misc/option_mirror.png') as mirror:
Expand Down Expand Up @@ -267,6 +305,8 @@ def saveImage(self, base):
saveName = img_save_dir / f'{newNum}.png'
log.info("Saving imgscore %s", saveName)
base.save(saveName)
if __name__ == '__main__':
base.show()
return saveName

def fixBrokenChars(self, name): # thanks mon
Expand Down Expand Up @@ -308,7 +348,7 @@ def fixBrokenChars(self, name): # thanks mon


if __name__ == '__main__':
tree = etree.parse('req-game_3-save_m.xml') # File not included
tree = etree.parse('assets/req-game_3-save_m.xml') # File not included
data = etree.tostring(tree)
scorecard = ScoreCard(data)
scorecard.generate()

0 comments on commit e036505

Please sign in to comment.