3
2
forked from Dniel97/artemis
artemis/titles/cm/read.py

156 lines
5.8 KiB
Python
Raw Normal View History

2023-03-03 22:46:29 +00:00
from decimal import Decimal
import logging
import os
import re
import csv
import xml.etree.ElementTree as ET
from typing import Any, Dict, List, Optional
from read import BaseReader
from core.config import CoreConfig
from titles.ongeki.database import OngekiData
from titles.cm.const import CardMakerConstants
from titles.ongeki.const import OngekiConstants
from titles.ongeki.config import OngekiConfig
class CardMakerReader(BaseReader):
2023-03-09 16:38:58 +00:00
def __init__(
self,
config: CoreConfig,
version: int,
bin_dir: Optional[str],
opt_dir: Optional[str],
extra: Optional[str],
) -> None:
2023-03-03 22:46:29 +00:00
super().__init__(config, version, bin_dir, opt_dir, extra)
self.ongeki_data = OngekiData(config)
try:
self.logger.info(
2023-03-09 16:38:58 +00:00
f"Start importer for {CardMakerConstants.game_ver_to_string(version)}"
)
2023-03-03 22:46:29 +00:00
except IndexError:
self.logger.error(f"Invalid Card Maker version {version}")
2023-03-03 22:46:29 +00:00
exit(1)
def read(self) -> None:
static_datas = {
"static_gachas.csv": "read_ongeki_gacha_csv",
2023-03-09 16:38:58 +00:00
"static_gacha_cards.csv": "read_ongeki_gacha_card_csv",
2023-03-03 22:46:29 +00:00
}
data_dirs = []
2023-03-03 22:46:29 +00:00
if self.bin_dir is not None:
for file, func in static_datas.items():
if os.path.exists(f"{self.bin_dir}/MU3/{file}"):
read_csv = getattr(CardMakerReader, func)
read_csv(self, f"{self.bin_dir}/MU3/{file}")
else:
2023-03-09 16:38:58 +00:00
self.logger.warn(
f"Couldn't find {file} file in {self.bin_dir}, skipping"
)
2023-03-03 22:46:29 +00:00
if self.opt_dir is not None:
data_dirs += self.get_data_directories(self.opt_dir)
2023-03-03 22:46:29 +00:00
# ONGEKI (MU3) cnnot easily access the bin data(A000.pac)
# so only opt_dir will work for now
for dir in data_dirs:
self.read_ongeki_gacha(f"{dir}/MU3/gacha")
2023-03-03 22:46:29 +00:00
def read_ongeki_gacha_csv(self, file_path: str) -> None:
self.logger.info(f"Reading gachas from {file_path}...")
with open(file_path, encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
self.ongeki_data.static.put_gacha(
row["version"],
row["gachaId"],
row["gachaName"],
row["kind"],
type=row["type"],
isCeiling=True if row["isCeiling"] == "1" else False,
2023-03-09 16:38:58 +00:00
maxSelectPoint=row["maxSelectPoint"],
2023-03-03 22:46:29 +00:00
)
self.logger.info(f"Added gacha {row['gachaId']}")
def read_ongeki_gacha_card_csv(self, file_path: str) -> None:
self.logger.info(f"Reading gacha cards from {file_path}...")
with open(file_path, encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
self.ongeki_data.static.put_gacha_card(
row["gachaId"],
row["cardId"],
rarity=row["rarity"],
weight=row["weight"],
isPickup=True if row["isPickup"] == "1" else False,
2023-03-09 16:38:58 +00:00
isSelect=True if row["isSelect"] == "1" else False,
2023-03-03 22:46:29 +00:00
)
self.logger.info(f"Added card {row['cardId']} to gacha")
def read_ongeki_gacha(self, base_dir: str) -> None:
self.logger.info(f"Reading gachas from {base_dir}...")
# assuming some GachaKinds based on the GachaType
type_to_kind = {
"Normal": "Normal",
"Pickup": "Pickup",
"RecoverFiveShotFlag": "BonusRestored",
"Free": "Free",
2023-03-09 16:38:58 +00:00
"FreeSR": "Free",
2023-03-03 22:46:29 +00:00
}
for root, dirs, files in os.walk(base_dir):
for dir in dirs:
if os.path.exists(f"{root}/{dir}/Gacha.xml"):
with open(f"{root}/{dir}/Gacha.xml", "r", encoding="utf-8") as f:
troot = ET.fromstring(f.read())
2023-03-09 16:38:58 +00:00
name = troot.find("Name").find("str").text
gacha_id = int(troot.find("Name").find("id").text)
# skip already existing gachas
2023-03-09 16:38:58 +00:00
if (
self.ongeki_data.static.get_gacha(
OngekiConstants.VER_ONGEKI_BRIGHT_MEMORY, gacha_id
)
is not None
):
self.logger.info(
f"Gacha {gacha_id} already added, skipping"
)
continue
# 1140 is the first bright memory gacha
if gacha_id < 1140:
version = OngekiConstants.VER_ONGEKI_BRIGHT
else:
version = OngekiConstants.VER_ONGEKI_BRIGHT_MEMORY
2023-03-03 22:46:29 +00:00
gacha_kind = OngekiConstants.CM_GACHA_KINDS[
2023-03-09 16:38:58 +00:00
type_to_kind[troot.find("Type").text]
].value
2023-03-03 22:46:29 +00:00
# hardcode which gachas get "Select Gacha" with 33 points
is_ceiling, max_select_point = 0, 0
if gacha_id in {1163, 1164, 1165, 1166, 1167, 1168}:
is_ceiling = 1
max_select_point = 33
2023-03-03 22:46:29 +00:00
self.ongeki_data.static.put_gacha(
version,
gacha_id,
name,
gacha_kind,
isCeiling=is_ceiling,
2023-03-09 16:38:58 +00:00
maxSelectPoint=max_select_point,
)
self.logger.info(f"Added gacha {gacha_id}")