1
0
mirror of synced 2024-11-28 16:00:48 +01:00
artemis/titles/chuni/read.py

189 lines
8.0 KiB
Python
Raw Normal View History

from typing import Optional
from os import walk, path
import xml.etree.ElementTree as ET
from read import BaseReader
from core.config import CoreConfig
from titles.chuni.database import ChuniData
from titles.chuni.const import ChuniConstants
2023-03-09 17:38:58 +01:00
class ChuniReader(BaseReader):
2023-03-09 17:38:58 +01:00
def __init__(
self,
config: CoreConfig,
version: int,
bin_dir: Optional[str],
opt_dir: Optional[str],
extra: Optional[str],
) -> None:
super().__init__(config, version, bin_dir, opt_dir, extra)
self.data = ChuniData(config)
try:
2023-03-09 17:38:58 +01:00
self.logger.info(
f"Start importer for {ChuniConstants.game_ver_to_string(version)}"
)
except IndexError:
self.logger.error(f"Invalid chunithm version {version}")
exit(1)
2023-03-09 17:38:58 +01:00
def read(self) -> None:
data_dirs = []
if self.bin_dir is not None:
data_dirs += self.get_data_directories(self.bin_dir)
2023-03-09 17:38:58 +01:00
if self.opt_dir is not None:
2023-03-09 17:38:58 +01:00
data_dirs += self.get_data_directories(self.opt_dir)
for dir in data_dirs:
self.logger.info(f"Read from {dir}")
self.read_events(f"{dir}/event")
self.read_music(f"{dir}/music")
self.read_charges(f"{dir}/chargeItem")
self.read_avatar(f"{dir}/avatarAccessory")
2023-03-09 17:38:58 +01:00
def read_events(self, evt_dir: str) -> None:
for root, dirs, files in walk(evt_dir):
for dir in dirs:
if path.exists(f"{root}/{dir}/Event.xml"):
2023-03-09 17:38:58 +01:00
with open(f"{root}/{dir}/Event.xml", "rb") as fp:
bytedata = fp.read()
2023-03-09 17:38:58 +01:00
strdata = bytedata.decode("UTF-8")
xml_root = ET.fromstring(strdata)
2023-03-09 17:38:58 +01:00
for name in xml_root.findall("name"):
id = name.find("id").text
name = name.find("str").text
for substances in xml_root.findall("substances"):
event_type = substances.find("type").text
result = self.data.static.put_event(
self.version, id, event_type, name
)
if result is not None:
self.logger.info(f"Inserted event {id}")
else:
self.logger.warn(f"Failed to insert event {id}")
def read_music(self, music_dir: str) -> None:
for root, dirs, files in walk(music_dir):
for dir in dirs:
if path.exists(f"{root}/{dir}/Music.xml"):
2023-03-09 17:38:58 +01:00
with open(f"{root}/{dir}/Music.xml", "rb") as fp:
bytedata = fp.read()
2023-03-09 17:38:58 +01:00
strdata = bytedata.decode("UTF-8")
xml_root = ET.fromstring(strdata)
2023-03-09 17:38:58 +01:00
for name in xml_root.findall("name"):
song_id = name.find("id").text
title = name.find("str").text
for artistName in xml_root.findall("artistName"):
artist = artistName.find("str").text
2023-03-09 17:38:58 +01:00
for genreNames in xml_root.findall("genreNames"):
for list_ in genreNames.findall("list"):
for StringID in list_.findall("StringID"):
genre = StringID.find("str").text
2023-03-09 17:38:58 +01:00
for jaketFile in xml_root.findall("jaketFile"): # nice typo, SEGA
jacket_path = jaketFile.find("path").text
2023-03-09 17:38:58 +01:00
for fumens in xml_root.findall("fumens"):
for MusicFumenData in fumens.findall("MusicFumenData"):
fumen_path = MusicFumenData.find("file").find("path")
if fumen_path is not None:
2023-03-09 17:38:58 +01:00
chart_id = MusicFumenData.find("type").find("id").text
if chart_id == "4":
level = float(xml_root.find("starDifType").text)
2023-03-09 17:38:58 +01:00
we_chara = (
xml_root.find("worldsEndTagName")
.find("str")
.text
)
else:
2023-03-09 17:38:58 +01:00
level = float(
f"{MusicFumenData.find('level').text}.{MusicFumenData.find('levelDecimal').text}"
)
we_chara = None
2023-03-09 17:38:58 +01:00
result = self.data.static.put_music(
self.version,
song_id,
2023-03-09 17:38:58 +01:00
chart_id,
title,
artist,
level,
genre,
jacket_path,
2023-03-09 17:38:58 +01:00
we_chara,
)
if result is not None:
2023-03-09 17:38:58 +01:00
self.logger.info(
f"Inserted music {song_id} chart {chart_id}"
)
else:
2023-03-09 17:38:58 +01:00
self.logger.warn(
f"Failed to insert music {song_id} chart {chart_id}"
)
def read_charges(self, charge_dir: str) -> None:
for root, dirs, files in walk(charge_dir):
for dir in dirs:
if path.exists(f"{root}/{dir}/ChargeItem.xml"):
2023-03-09 17:38:58 +01:00
with open(f"{root}/{dir}/ChargeItem.xml", "rb") as fp:
bytedata = fp.read()
2023-03-09 17:38:58 +01:00
strdata = bytedata.decode("UTF-8")
xml_root = ET.fromstring(strdata)
2023-03-09 17:38:58 +01:00
for name in xml_root.findall("name"):
id = name.find("id").text
name = name.find("str").text
expirationDays = xml_root.find("expirationDays").text
consumeType = xml_root.find("consumeType").text
sellingAppeal = bool(xml_root.find("sellingAppeal").text)
result = self.data.static.put_charge(
self.version,
id,
name,
expirationDays,
consumeType,
sellingAppeal,
)
if result is not None:
self.logger.info(f"Inserted charge {id}")
else:
self.logger.warn(f"Failed to insert charge {id}")
def read_avatar(self, avatar_dir: str) -> None:
for root, dirs, files in walk(avatar_dir):
for dir in dirs:
if path.exists(f"{root}/{dir}/AvatarAccessory.xml"):
2023-03-09 17:38:58 +01:00
with open(f"{root}/{dir}/AvatarAccessory.xml", "rb") as fp:
bytedata = fp.read()
2023-03-09 17:38:58 +01:00
strdata = bytedata.decode("UTF-8")
xml_root = ET.fromstring(strdata)
2023-03-09 17:38:58 +01:00
for name in xml_root.findall("name"):
id = name.find("id").text
name = name.find("str").text
category = xml_root.find("category").text
for image in xml_root.findall("image"):
iconPath = image.find("path").text
for texture in xml_root.findall("texture"):
texturePath = texture.find("path").text
result = self.data.static.put_avatar(
self.version, id, name, category, iconPath, texturePath
)
if result is not None:
self.logger.info(f"Inserted avatarAccessory {id}")
else:
self.logger.warn(f"Failed to insert avatarAccessory {id}")