mirror of
https://github.com/Yonokid/PyTaiko.git
synced 2026-02-04 03:30:13 +01:00
bare minimum to select and display in song select
This commit is contained in:
@@ -14,6 +14,7 @@ from raylib import SHADER_UNIFORM_VEC3
|
||||
from libs.animation import Animation, MoveAnimation
|
||||
from libs.audio import audio
|
||||
from libs.global_data import Crown, Difficulty, ScoreMethod
|
||||
from libs.parsers.osz import OsuParser
|
||||
from libs.texture import tex
|
||||
from libs.parsers.tja import TJAParser, test_encodings
|
||||
from libs.utils import OutlinedText, get_current_ms, global_data
|
||||
@@ -214,7 +215,7 @@ class SongBox(BaseBox):
|
||||
self.hash = dict()
|
||||
self.score_history = None
|
||||
self.history_wait = 0
|
||||
self.tja = tja
|
||||
self.parser = tja
|
||||
self.is_favorite = False
|
||||
self.yellow_box = None
|
||||
|
||||
@@ -226,8 +227,8 @@ class SongBox(BaseBox):
|
||||
with sqlite3.connect(global_data.score_db) as con:
|
||||
cursor = con.cursor()
|
||||
# Batch database query for all diffs at once
|
||||
if self.tja.metadata.course_data:
|
||||
hash_values = [self.hash[diff] for diff in self.tja.metadata.course_data if diff in self.hash]
|
||||
if self.parser.metadata.course_data:
|
||||
hash_values = [self.hash[diff] for diff in self.parser.metadata.course_data if diff in self.hash]
|
||||
placeholders = ','.join('?' * len(hash_values))
|
||||
|
||||
batch_query = f"""
|
||||
@@ -239,7 +240,7 @@ class SongBox(BaseBox):
|
||||
|
||||
hash_to_score = {row[0]: row[1:] for row in cursor.fetchall()}
|
||||
|
||||
for diff in self.tja.metadata.course_data:
|
||||
for diff in self.parser.metadata.course_data:
|
||||
if diff not in self.hash:
|
||||
continue
|
||||
diff_hash = self.hash[diff]
|
||||
@@ -261,7 +262,7 @@ class SongBox(BaseBox):
|
||||
self.score_history = ScoreHistory(self.scores, current_time)
|
||||
|
||||
if not is_open_prev and self.is_open:
|
||||
self.yellow_box = YellowBox(False, tja=self.tja)
|
||||
self.yellow_box = YellowBox(False, tja=self.parser)
|
||||
self.yellow_box.create_anim()
|
||||
self.wait = current_time
|
||||
if current_time >= self.history_wait + 3000:
|
||||
@@ -275,7 +276,7 @@ class SongBox(BaseBox):
|
||||
|
||||
self.name.draw(outline_color=self.fore_color, x=x + tex.skin_config["song_box_name"].x - int(self.name.texture.width / 2), y=y+tex.skin_config["song_box_name"].y, y2=min(self.name.texture.height, tex.skin_config["song_box_name"].height)-self.name.texture.height, fade=outer_fade_override)
|
||||
|
||||
if self.tja.ex_data.new:
|
||||
if self.parser.ex_data.new:
|
||||
tex.draw_texture('yellow_box', 'ex_data_new_song_balloon', x=x, y=y, fade=outer_fade_override)
|
||||
valid_scores = {k: v for k, v in self.scores.items() if v is not None}
|
||||
if valid_scores:
|
||||
@@ -297,6 +298,89 @@ class SongBox(BaseBox):
|
||||
if self.score_history is not None and get_current_ms() >= self.history_wait + 3000:
|
||||
self.score_history.draw()
|
||||
|
||||
class SongBoxOsu(BaseBox):
|
||||
def __init__(self, name: str, back_color: Optional[tuple[int, int, int]], fore_color: Optional[tuple[int, int, int]], texture_index: TextureIndex, parser: OsuParser):
|
||||
super().__init__(name, back_color, fore_color, texture_index)
|
||||
self.scores = dict()
|
||||
self.hash = dict()
|
||||
self.score_history = None
|
||||
self.history_wait = 0
|
||||
self.parser = parser
|
||||
self.is_favorite = False
|
||||
self.yellow_box = None
|
||||
|
||||
def load_text(self):
|
||||
super().load_text()
|
||||
self.text_loaded = True
|
||||
|
||||
def get_scores(self):
|
||||
with sqlite3.connect(global_data.score_db) as con:
|
||||
cursor = con.cursor()
|
||||
# Batch database query for all diffs at once
|
||||
if self.parser.metadata.course_data:
|
||||
hash_values = [self.hash[diff] for diff in self.parser.metadata.course_data if diff in self.hash]
|
||||
placeholders = ','.join('?' * len(hash_values))
|
||||
|
||||
batch_query = f"""
|
||||
SELECT hash, score, good, ok, bad, drumroll, clear
|
||||
FROM Scores
|
||||
WHERE hash IN ({placeholders})
|
||||
"""
|
||||
cursor.execute(batch_query, hash_values)
|
||||
|
||||
hash_to_score = {row[0]: row[1:] for row in cursor.fetchall()}
|
||||
|
||||
for diff in self.parser.metadata.course_data:
|
||||
if diff not in self.hash:
|
||||
continue
|
||||
diff_hash = self.hash[diff]
|
||||
self.scores[diff] = hash_to_score.get(diff_hash)
|
||||
self.score_history = None
|
||||
|
||||
def update(self, current_time: float, is_diff_select: bool):
|
||||
super().update(current_time, is_diff_select)
|
||||
is_open_prev = self.is_open
|
||||
self.is_open = self.position == BOX_CENTER
|
||||
|
||||
if self.yellow_box is not None:
|
||||
self.yellow_box.update(is_diff_select)
|
||||
|
||||
if self.history_wait == 0:
|
||||
self.history_wait = current_time
|
||||
|
||||
if self.score_history is None and {k: v for k, v in self.scores.items() if v is not None}:
|
||||
self.score_history = ScoreHistory(self.scores, current_time)
|
||||
|
||||
if not is_open_prev and self.is_open:
|
||||
self.yellow_box = YellowBox(False)
|
||||
self.yellow_box.create_anim()
|
||||
self.wait = current_time
|
||||
if current_time >= self.history_wait + 3000:
|
||||
self.history_wait = current_time
|
||||
|
||||
if self.score_history is not None:
|
||||
self.score_history.update(current_time)
|
||||
|
||||
def _draw_closed(self, x: float, y: float, outer_fade_override: float):
|
||||
super()._draw_closed(x, y, outer_fade_override)
|
||||
|
||||
self.name.draw(outline_color=self.fore_color, x=x + tex.skin_config["song_box_name"].x - int(self.name.texture.width / 2), y=y+tex.skin_config["song_box_name"].y, y2=min(self.name.texture.height, tex.skin_config["song_box_name"].height)-self.name.texture.height, fade=outer_fade_override)
|
||||
valid_scores = {k: v for k, v in self.scores.items() if v is not None}
|
||||
if valid_scores:
|
||||
highest_key = max(valid_scores.keys())
|
||||
score = self.scores[highest_key]
|
||||
if score and score[5] == Crown.DFC:
|
||||
tex.draw_texture('yellow_box', 'crown_dfc', x=x, y=y, frame=min(Difficulty.URA, highest_key), fade=outer_fade_override)
|
||||
elif score and score[5] == Crown.FC:
|
||||
tex.draw_texture('yellow_box', 'crown_fc', x=x, y=y, frame=min(Difficulty.URA, highest_key), fade=outer_fade_override)
|
||||
elif score and score[5] >= Crown.CLEAR:
|
||||
tex.draw_texture('yellow_box', 'crown_clear', x=x, y=y, frame=min(Difficulty.URA, highest_key), fade=outer_fade_override)
|
||||
|
||||
def draw_score_history(self):
|
||||
if self.is_open and get_current_ms() >= self.wait + 83.33:
|
||||
if self.score_history is not None and get_current_ms() >= self.history_wait + 3000:
|
||||
self.score_history.draw()
|
||||
|
||||
class FolderBox(BaseBox):
|
||||
def __init__(self, name: str, back_color: Optional[tuple[int, int, int]], fore_color: Optional[tuple[int, int, int]], texture_index: TextureIndex, genre_index: GenreIndex, tja_count: int = 0, box_texture: Optional[str] = None):
|
||||
super().__init__(name, back_color, fore_color, texture_index)
|
||||
@@ -393,6 +477,14 @@ class FolderBox(BaseBox):
|
||||
elif self.box_texture is not None:
|
||||
scaled_width = self.box_texture.width * tex.screen_scale
|
||||
scaled_height = self.box_texture.height * tex.screen_scale
|
||||
max_width = 344 * tex.screen_scale
|
||||
max_height = 424 * tex.screen_scale
|
||||
if scaled_width > max_width or scaled_height > max_height:
|
||||
width_scale = max_width / scaled_width
|
||||
height_scale = max_height / scaled_height
|
||||
scale_factor = min(width_scale, height_scale)
|
||||
scaled_width *= scale_factor
|
||||
scaled_height *= scale_factor
|
||||
x = int((x + tex.skin_config["box_texture"].x) - (scaled_width // 2))
|
||||
y = int((y + tex.skin_config["box_texture"].y) - (scaled_height // 2))
|
||||
src = ray.Rectangle(0, 0, self.box_texture.width, self.box_texture.height)
|
||||
@@ -1061,12 +1153,23 @@ class SongFile(FileSystemItem):
|
||||
def __init__(self, path: Path, name: str, back_color: Optional[tuple[int, int, int]], fore_color: Optional[tuple[int, int, int]], texture_index: TextureIndex):
|
||||
super().__init__(path, name)
|
||||
self.is_recent = (datetime.now() - datetime.fromtimestamp(path.stat().st_mtime)) <= timedelta(days=7)
|
||||
self.tja = TJAParser(path)
|
||||
self.parser = TJAParser(path)
|
||||
if self.is_recent:
|
||||
self.tja.ex_data.new = True
|
||||
title = self.tja.metadata.title.get(global_data.config['general']['language'].lower(), self.tja.metadata.title['en'])
|
||||
self.parser.ex_data.new = True
|
||||
title = self.parser.metadata.title.get(global_data.config['general']['language'].lower(), self.parser.metadata.title['en'])
|
||||
self.hash = global_data.song_paths[path]
|
||||
self.box = SongBox(title, back_color, fore_color, texture_index, self.tja)
|
||||
self.box = SongBox(title, back_color, fore_color, texture_index, self.parser)
|
||||
self.box.hash = global_data.song_hashes[self.hash][0]["diff_hashes"]
|
||||
self.box.get_scores()
|
||||
|
||||
class SongFileOsu(FileSystemItem):
|
||||
def __init__(self, path: Path, name: str, back_color: Optional[tuple[int, int, int]], fore_color: Optional[tuple[int, int, int]], texture_index: TextureIndex):
|
||||
super().__init__(path, name)
|
||||
self.is_recent = (datetime.now() - datetime.fromtimestamp(path.stat().st_mtime)) <= timedelta(days=7)
|
||||
self.parser = OsuParser(path)
|
||||
title = self.parser.osu_metadata["Version"]
|
||||
self.hash = global_data.song_paths[path]
|
||||
self.box = SongBoxOsu(title, back_color, fore_color, texture_index, self.parser)
|
||||
self.box.hash = global_data.song_hashes[self.hash][0]["diff_hashes"]
|
||||
self.box.get_scores()
|
||||
|
||||
@@ -1122,7 +1225,7 @@ class FileNavigator:
|
||||
|
||||
# Pre-generated objects storage
|
||||
self.all_directories: dict[str, Directory] = {} # path -> Directory
|
||||
self.all_song_files: dict[str, Union[SongFile, DanCourse]] = {} # path -> SongFile
|
||||
self.all_song_files: dict[str, Union[SongFile, DanCourse, SongFileOsu]] = {} # path -> SongFile
|
||||
self.directory_contents: dict[str, list[Union[Directory, SongFile]]] = {} # path -> list of items
|
||||
|
||||
# OPTION 2: Lazy crown calculation with caching
|
||||
@@ -1262,6 +1365,10 @@ class FileNavigator:
|
||||
child_dirs = []
|
||||
for item_path in dir_path.iterdir():
|
||||
if item_path.is_dir():
|
||||
child_has_osu = any(item_path.glob("*.osu"))
|
||||
if child_has_osu:
|
||||
child_dirs.append(item_path)
|
||||
self.process_osz(item_path)
|
||||
child_has_box_def = (item_path / "box.def").exists()
|
||||
if child_has_box_def:
|
||||
child_dirs.append(item_path)
|
||||
@@ -1289,8 +1396,8 @@ class FileNavigator:
|
||||
elif song_key not in self.all_song_files and tja_path in global_data.song_paths:
|
||||
song_obj = SongFile(tja_path, tja_path.name, back_color, fore_color, texture_index)
|
||||
song_obj.box.get_scores()
|
||||
for course in song_obj.tja.metadata.course_data:
|
||||
level = song_obj.tja.metadata.course_data[course].level
|
||||
for course in song_obj.parser.metadata.course_data:
|
||||
level = song_obj.parser.metadata.course_data[course].level
|
||||
|
||||
scores = song_obj.box.scores.get(course)
|
||||
if scores is not None:
|
||||
@@ -1342,6 +1449,59 @@ class FileNavigator:
|
||||
logger.error(f"Error creating SongFile for {tja_path}: {e}")
|
||||
continue
|
||||
|
||||
def process_osz(self, dir_path: Path):
|
||||
dir_key = str(dir_path)
|
||||
for file in dir_path.iterdir():
|
||||
if file.name.endswith('.osu'):
|
||||
with open(file, 'r', encoding='utf-8') as f:
|
||||
content = f.readlines()
|
||||
for line in content:
|
||||
if line.startswith('TitleUnicode:'):
|
||||
title_unicode = line.split(':', 1)[1].strip()
|
||||
name = title_unicode
|
||||
break
|
||||
else:
|
||||
name = dir_path.name if dir_path.name else str(dir_path)
|
||||
box_texture = None
|
||||
collection = None
|
||||
back_color = None
|
||||
fore_color = None
|
||||
texture_index = TextureIndex.DEFAULT
|
||||
genre_index = GenreIndex.DEFAULT
|
||||
|
||||
for file in dir_path.iterdir():
|
||||
if file.name.endswith('.jpg') or file.name.endswith('.png'):
|
||||
box_texture = str(file)
|
||||
|
||||
# Create Directory object
|
||||
file_count = len([file for file in dir_path.glob("*.osu")])
|
||||
directory_obj = Directory(
|
||||
dir_path, name, back_color, fore_color, texture_index, genre_index,
|
||||
tja_count=file_count,
|
||||
box_texture=box_texture,
|
||||
collection=collection,
|
||||
)
|
||||
self.all_directories[dir_key] = directory_obj
|
||||
|
||||
content_items = []
|
||||
|
||||
osu_files = [file for file in dir_path.glob("*.osu")]
|
||||
|
||||
# Create SongFile objects
|
||||
for osu_path in sorted(osu_files):
|
||||
song_key = str(osu_path)
|
||||
if song_key not in self.all_song_files and osu_path in global_data.song_paths:
|
||||
song_obj = SongFileOsu(osu_path, osu_path.name, back_color, fore_color, texture_index)
|
||||
song_obj.box.get_scores()
|
||||
self.song_count += 1
|
||||
global_data.song_progress = self.song_count / global_data.total_songs
|
||||
self.all_song_files[song_key] = song_obj
|
||||
|
||||
if song_key in self.all_song_files:
|
||||
content_items.append(self.all_song_files[song_key])
|
||||
|
||||
self.directory_contents[dir_key] = content_items
|
||||
|
||||
def is_at_root(self) -> bool:
|
||||
"""Check if currently at the virtual root"""
|
||||
return self.current_dir == Path()
|
||||
@@ -1377,7 +1537,7 @@ class FileNavigator:
|
||||
if sibling_key in self.directory_contents:
|
||||
for item in self.directory_contents[sibling_key]:
|
||||
if isinstance(item, SongFile) and item:
|
||||
if self.diff_sort_diff in item.tja.metadata.course_data and item.tja.metadata.course_data[self.diff_sort_diff].level == self.diff_sort_level:
|
||||
if self.diff_sort_diff in item.parser.metadata.course_data and item.parser.metadata.course_data[self.diff_sort_diff].level == self.diff_sort_level:
|
||||
if item not in content_items:
|
||||
content_items.append(item)
|
||||
return content_items
|
||||
@@ -1425,7 +1585,7 @@ class FileNavigator:
|
||||
if self._levenshtein_distance(song.name[:-4].lower(), search_name.lower()) < 2:
|
||||
items.append(song)
|
||||
if isinstance(song, SongFile):
|
||||
if self._levenshtein_distance(song.tja.metadata.subtitle["en"].lower(), search_name.lower()) < 2:
|
||||
if self._levenshtein_distance(song.parser.metadata.subtitle["en"].lower(), search_name.lower()) < 2:
|
||||
items.append(song)
|
||||
|
||||
return items
|
||||
@@ -1784,7 +1944,7 @@ class FileNavigator:
|
||||
else:
|
||||
box.draw(box.position + int(move_away_attribute), tex.skin_config["boxes"].y, is_ura, inner_fade_override=diff_fade_out_attribute, outer_fade_override=fade)
|
||||
|
||||
def mark_crowns_dirty_for_song(self, song_file: SongFile):
|
||||
def mark_crowns_dirty_for_song(self, song_file: SongFile | SongFileOsu):
|
||||
"""Mark directories as needing crown recalculation when a song's score changes"""
|
||||
song_path = song_file.path
|
||||
|
||||
@@ -1847,7 +2007,7 @@ class FileNavigator:
|
||||
return
|
||||
|
||||
recents_path = self.recent_folder.path / 'song_list.txt'
|
||||
new_entry = f'{song.hash}|{song.tja.metadata.title["en"]}|{song.tja.metadata.subtitle["en"]}\n'
|
||||
new_entry = f'{song.hash}|{song.parser.metadata.title["en"]}|{song.parser.metadata.subtitle["en"]}\n'
|
||||
existing_entries = []
|
||||
if recents_path.exists():
|
||||
with open(recents_path, 'r', encoding='utf-8-sig') as song_list:
|
||||
@@ -1858,7 +2018,7 @@ class FileNavigator:
|
||||
with open(recents_path, 'w', encoding='utf-8-sig') as song_list:
|
||||
song_list.writelines(recent_entries)
|
||||
|
||||
logger.info(f"Added Recent: {song.hash} {song.tja.metadata.title['en']} {song.tja.metadata.subtitle['en']}")
|
||||
logger.info(f"Added Recent: {song.hash} {song.parser.metadata.title['en']} {song.parser.metadata.subtitle['en']}")
|
||||
|
||||
def add_favorite(self) -> bool:
|
||||
"""Add the current song to the favorites list"""
|
||||
@@ -1877,7 +2037,7 @@ class FileNavigator:
|
||||
if not line: # Skip empty lines
|
||||
continue
|
||||
hash, title, subtitle = line.split('|')
|
||||
if song.hash == hash or (song.tja.metadata.title['en'] == title and song.tja.metadata.subtitle['en'] == subtitle):
|
||||
if song.hash == hash or (song.parser.metadata.title['en'] == title and song.parser.metadata.subtitle['en'] == subtitle):
|
||||
if not self.in_favorites:
|
||||
return False
|
||||
else:
|
||||
@@ -1886,11 +2046,11 @@ class FileNavigator:
|
||||
with open(favorites_path, 'w', encoding='utf-8-sig') as song_list:
|
||||
for line in lines:
|
||||
song_list.write(line + '\n')
|
||||
logger.info(f"Removed Favorite: {song.hash} {song.tja.metadata.title['en']} {song.tja.metadata.subtitle['en']}")
|
||||
logger.info(f"Removed Favorite: {song.hash} {song.parser.metadata.title['en']} {song.parser.metadata.subtitle['en']}")
|
||||
else:
|
||||
with open(favorites_path, 'a', encoding='utf-8-sig') as song_list:
|
||||
song_list.write(f'{song.hash}|{song.tja.metadata.title["en"]}|{song.tja.metadata.subtitle["en"]}\n')
|
||||
logger.info(f"Added Favorite: {song.hash} {song.tja.metadata.title['en']} {song.tja.metadata.subtitle['en']}")
|
||||
song_list.write(f'{song.hash}|{song.parser.metadata.title["en"]}|{song.parser.metadata.subtitle["en"]}\n')
|
||||
logger.info(f"Added Favorite: {song.hash} {song.parser.metadata.title['en']} {song.parser.metadata.subtitle['en']}")
|
||||
return True
|
||||
|
||||
navigator = FileNavigator()
|
||||
|
||||
@@ -1,24 +1,15 @@
|
||||
import hashlib
|
||||
import logging
|
||||
import math
|
||||
import random
|
||||
from collections import deque
|
||||
from dataclasses import dataclass, field, fields
|
||||
from enum import IntEnum
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from libs.global_data import Modifiers
|
||||
from libs.utils import strip_comments
|
||||
from libs.tja import TimelineObject, Note, NoteType, Drumroll, Balloon, NoteList, CourseData, ParserState
|
||||
from libs.parsers.tja import CourseData, Note, NoteType, Drumroll, Balloon, NoteList, TJAMetadata
|
||||
|
||||
import re
|
||||
|
||||
class OsuParser:
|
||||
general: dict[str, str]
|
||||
editor: dict[str, str]
|
||||
metadata: dict[str, str]
|
||||
osu_metadata: dict[str, str]
|
||||
difficulty: dict[str, str]
|
||||
events: list[int]
|
||||
timing_points: list[int]
|
||||
@@ -26,22 +17,24 @@ class OsuParser:
|
||||
|
||||
bpm: list[int]
|
||||
|
||||
def __init__(self, osu_file):
|
||||
def __init__(self, osu_file: Path):
|
||||
self.general = self.read_osu_data(osu_file, target_header="General", is_dict=True)
|
||||
self.editor = self.read_osu_data(osu_file, target_header="Editor", is_dict=True)
|
||||
self.metadata = self.read_osu_data(osu_file, target_header="Metadata", is_dict=True)
|
||||
self.osu_metadata = self.read_osu_data(osu_file, target_header="Metadata", is_dict=True)
|
||||
self.difficulty = self.read_osu_data(osu_file, target_header="Difficulty", is_dict=True)
|
||||
self.events = self.read_osu_data(osu_file, target_header="Events")
|
||||
self.timing_points = self.read_osu_data(osu_file, target_header="TimingPoints")
|
||||
#self.general = self.read_osu_data(osu_file, target_header="Colours", is_dict=True)
|
||||
self.hit_objects = self.read_osu_data(osu_file, target_header="HitObjects")
|
||||
self.bpm = []
|
||||
self.metadata = TJAMetadata()
|
||||
self.metadata.wave = osu_file.parent / self.general["AudioFilename"]
|
||||
self.metadata.course_data[0] = CourseData()
|
||||
for points in self.timing_points:
|
||||
self.bpm.append(math.floor(1 / points[1] * 1000 * 60))
|
||||
self.osu_NoteList = self.note_data_to_NoteList(self.hit_objects)
|
||||
|
||||
|
||||
def read_osu_data(self, file_path, target_header="HitObjects", is_dict = False):
|
||||
def read_osu_data(self, file_path: Path, target_header="HitObjects", is_dict = False):
|
||||
data = []
|
||||
if is_dict:
|
||||
data = {}
|
||||
@@ -72,7 +65,7 @@ class OsuParser:
|
||||
|
||||
return data
|
||||
|
||||
def note_data_to_NoteList(self, note_data):
|
||||
def note_data_to_NoteList(self, note_data) -> tuple[NoteList, list[NoteList], list[NoteList], list[NoteList]]:
|
||||
osu_NoteList = NoteList()
|
||||
counter = 0
|
||||
|
||||
@@ -238,6 +231,29 @@ class OsuParser:
|
||||
|
||||
osu_NoteList.draw_notes = osu_NoteList.play_notes.copy()
|
||||
|
||||
return osu_NoteList
|
||||
return osu_NoteList, [], [], []
|
||||
|
||||
def notes_to_position(self, difficulty):
|
||||
return self.osu_NoteList
|
||||
|
||||
def hash_note_data(self, notes: NoteList):
|
||||
"""Hashes the note data for the given NoteList."""
|
||||
n = hashlib.sha256()
|
||||
list1 = notes.play_notes
|
||||
list2 = notes.bars
|
||||
merged: list[Note | Drumroll | Balloon] = []
|
||||
i = 0
|
||||
j = 0
|
||||
while i < len(list1) and j < len(list2):
|
||||
if list1[i] <= list2[j]:
|
||||
merged.append(list1[i])
|
||||
i += 1
|
||||
else:
|
||||
merged.append(list2[j])
|
||||
j += 1
|
||||
merged.extend(list1[i:])
|
||||
merged.extend(list2[j:])
|
||||
for item in merged:
|
||||
n.update(item.get_hash().encode('utf-8'))
|
||||
|
||||
return n.hexdigest()
|
||||
|
||||
@@ -5,9 +5,11 @@ import logging
|
||||
import sqlite3
|
||||
import time
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
|
||||
from libs.config import get_config
|
||||
from libs.global_data import Crown
|
||||
from libs.parsers.osz import OsuParser
|
||||
from libs.parsers.tja import NoteList, TJAParser, test_encodings
|
||||
from libs.utils import global_data
|
||||
|
||||
@@ -105,12 +107,20 @@ def build_song_hashes(output_dir=Path("cache")):
|
||||
for root_dir in tja_paths:
|
||||
root_path = Path(root_dir)
|
||||
found_tja_files = root_path.rglob("*.tja", recurse_symlinks=True)
|
||||
found_osz_files = root_path.rglob("*.osz", recurse_symlinks=True)
|
||||
all_tja_files.extend(found_tja_files)
|
||||
all_tja_files.extend(found_osz_files)
|
||||
|
||||
global_data.total_songs = len(all_tja_files)
|
||||
files_to_process = []
|
||||
|
||||
for tja_path in all_tja_files:
|
||||
if tja_path.suffix == '.osz':
|
||||
with zipfile.ZipFile(tja_path, 'r') as zip_file:
|
||||
zip_file.extractall(tja_path.with_suffix(''))
|
||||
zip_path = Path(tja_path.with_suffix(''))
|
||||
for file in zip_path.glob('*.osu'):
|
||||
files_to_process.append(file)
|
||||
tja_path_str = str(tja_path)
|
||||
current_modified = tja_path.stat().st_mtime
|
||||
if current_modified <= saved_timestamp:
|
||||
@@ -133,56 +143,64 @@ def build_song_hashes(output_dir=Path("cache")):
|
||||
global_data.total_songs = total_songs
|
||||
|
||||
for tja_path in files_to_process:
|
||||
try:
|
||||
tja_path_str = str(tja_path)
|
||||
if tja_path.suffix == '.osu':
|
||||
parser = OsuParser(tja_path)
|
||||
path_str = str(tja_path)
|
||||
current_modified = tja_path.stat().st_mtime
|
||||
tja = TJAParser(tja_path)
|
||||
all_notes = NoteList()
|
||||
diff_hashes = dict()
|
||||
all_notes = parser.notes_to_position(0)[0]
|
||||
diff_hashes[0] = parser.hash_note_data(all_notes)
|
||||
else:
|
||||
try:
|
||||
path_str = str(tja_path)
|
||||
current_modified = tja_path.stat().st_mtime
|
||||
parser = TJAParser(tja_path)
|
||||
all_notes = NoteList()
|
||||
diff_hashes = dict()
|
||||
|
||||
for diff in tja.metadata.course_data:
|
||||
diff_notes, branch_m, branch_e, branch_n = TJAParser.notes_to_position(TJAParser(tja.file_path), diff)
|
||||
diff_hashes[diff] = tja.hash_note_data(diff_notes)
|
||||
all_notes.play_notes.extend(diff_notes.play_notes)
|
||||
if branch_m:
|
||||
for branch in branch_m:
|
||||
all_notes.play_notes.extend(branch.play_notes)
|
||||
all_notes.bars.extend(branch.bars)
|
||||
if branch_e:
|
||||
for branch in branch_e:
|
||||
all_notes.play_notes.extend(branch.play_notes)
|
||||
all_notes.bars.extend(branch.bars)
|
||||
if branch_n:
|
||||
for branch in branch_n:
|
||||
all_notes.play_notes.extend(branch.play_notes)
|
||||
all_notes.bars.extend(branch.bars)
|
||||
all_notes.bars.extend(diff_notes.bars)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to parse TJA {tja_path}: {e}")
|
||||
continue
|
||||
for diff in parser.metadata.course_data:
|
||||
diff_notes, branch_m, branch_e, branch_n = TJAParser.notes_to_position(TJAParser(parser.file_path), diff)
|
||||
diff_hashes[diff] = parser.hash_note_data(diff_notes)
|
||||
all_notes.play_notes.extend(diff_notes.play_notes)
|
||||
if branch_m:
|
||||
for branch in branch_m:
|
||||
all_notes.play_notes.extend(branch.play_notes)
|
||||
all_notes.bars.extend(branch.bars)
|
||||
if branch_e:
|
||||
for branch in branch_e:
|
||||
all_notes.play_notes.extend(branch.play_notes)
|
||||
all_notes.bars.extend(branch.bars)
|
||||
if branch_n:
|
||||
for branch in branch_n:
|
||||
all_notes.play_notes.extend(branch.play_notes)
|
||||
all_notes.bars.extend(branch.bars)
|
||||
all_notes.bars.extend(diff_notes.bars)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to parse TJA {tja_path}: {e}")
|
||||
continue
|
||||
|
||||
if all_notes == NoteList():
|
||||
continue
|
||||
|
||||
hash_val = tja.hash_note_data(all_notes)
|
||||
hash_val = parser.hash_note_data(all_notes)
|
||||
if hash_val not in song_hashes:
|
||||
song_hashes[hash_val] = []
|
||||
|
||||
song_hashes[hash_val].append({
|
||||
"file_path": tja_path_str,
|
||||
"file_path": path_str,
|
||||
"last_modified": current_modified,
|
||||
"title": tja.metadata.title,
|
||||
"subtitle": tja.metadata.subtitle,
|
||||
"title": parser.metadata.title,
|
||||
"subtitle": parser.metadata.subtitle,
|
||||
"diff_hashes": diff_hashes
|
||||
})
|
||||
|
||||
# Update both indexes
|
||||
path_to_hash[tja_path_str] = hash_val
|
||||
path_to_hash[path_str] = hash_val
|
||||
global_data.song_paths[tja_path] = hash_val
|
||||
|
||||
# Prepare database updates for each difficulty
|
||||
en_name = tja.metadata.title.get('en', '') if isinstance(tja.metadata.title, dict) else str(tja.metadata.title)
|
||||
jp_name = tja.metadata.title.get('ja', '') if isinstance(tja.metadata.title, dict) else ''
|
||||
en_name = parser.metadata.title.get('en', '') if isinstance(parser.metadata.title, dict) else str(parser.metadata.title)
|
||||
jp_name = parser.metadata.title.get('ja', '') if isinstance(parser.metadata.title, dict) else ''
|
||||
|
||||
score_ini_path = tja_path.with_suffix('.tja.score.ini')
|
||||
if score_ini_path.exists():
|
||||
|
||||
Reference in New Issue
Block a user