EnkaNetwork.py/enkanetwork/assets.py

158 lines
5.1 KiB
Python
Raw Normal View History

import json
import os
import logging
from typing import Dict, Union
from io import TextIOWrapper
from .enum import Language
from .model import assets
from .utils import create_ui_path
PATH = os.path.dirname(os.path.abspath(__file__))
LOGGER = logging.getLogger(__name__)
class Assets:
# Characters & Skills
DATA: Dict[str, dict] = {}
# Hash Map
HASH_MAP: Dict[str, dict] = {}
# Language
LANGS: str = Language.EN
def __init__(self, lang: Language = Language.EN) -> None:
# Set language
self.__set_languege(lang)
# Load assets
self.__load_assets_lang()
self.__load_assets_data()
@classmethod
def character(cls, id: int) -> Union[assets.CharacterAsset, None]:
LOGGER.debug(f"Getting character assets with id: {id}")
data = cls.DATA["characters"].get(str(id))
if not data:
LOGGER.error(f"Character not found with id: {id}")
return
return assets.CharacterAsset.parse_obj({
"id": id,
"images": cls.create_character_icon(data["sideIconName"]),
**data
})
@classmethod
def constellations(cls, id: int) -> Union[assets.CharacterConstellationsAsset, None]:
LOGGER.debug(f"Getting character constellations assets with id: {id}")
data = cls.DATA["constellations"].get(str(id))
if not data:
LOGGER.error(f"Character constellations not found with id: {id}")
return
return assets.CharacterConstellationsAsset.parse_obj({
"id": id,
**data,
"icon": cls.create_icon_path(data["icon"])
})
@classmethod
def skills(cls, id: int) -> Union[assets.CharacterSkillAsset, None]:
LOGGER.debug(f"Getting character skills assets with id: {id}")
data = cls.DATA["skills"].get(str(id))
if not data:
LOGGER.error(f"Character skills not found with id: {id}")
return
return assets.CharacterSkillAsset.parse_obj({
"id": id,
**data,
"skillIcon": cls.create_icon_path(data["skillIcon"])
})
@classmethod
def namecards(cls, id: int) -> Union[assets.NamecardAsset, None]:
LOGGER.debug(f"Getting namecards assets with id: {id}")
data = cls.DATA["namecards"].get(str(id))
if not data:
LOGGER.error(f"Namecards not found with id: {id}")
return
return assets.NamecardAsset.parse_obj({
"id": id,
**data,
"icon": cls.create_icon_path(data["icon"]),
"banner": cls.create_icon_path(data["picPath"][1]),
"navbar": cls.create_icon_path(data["picPath"][0])
})
@classmethod
def get_hash_map(cls, hash_id: str) -> Union[str, None]:
LOGGER.debug(f"Getting nameTextMapHash {hash_id} with language: {cls.LANGS}")
for key in cls.HASH_MAP:
if str(hash_id) in cls.HASH_MAP[key]:
val = cls.HASH_MAP[key][str(hash_id)][cls.LANGS]
LOGGER.debug(f"Got nameTextMapHash {hash_id} with language: {key} (Value '{val}')")
return val
LOGGER.error(f"nameTextMapHash {hash_id} not found with language: {cls.LANGS}")
return
@classmethod
def character_icon(cls, id: int) -> Union[assets.CharacterIconAsset, None]:
data = cls.character(id)
if not data:
return
return data.images
@staticmethod
def create_character_icon(path: str) -> assets.CharacterIconAsset:
return assets.CharacterIconAsset(
icon=create_ui_path(path.replace("_Side", "")),
side=create_ui_path(path),
banner=create_ui_path(path.replace("AvatarIcon_Side", "Gacha_AvatarImg"))
)
@staticmethod
def create_icon_path(path: str) -> str:
return create_ui_path(path)
@classmethod
def __set_languege(cls, lang: Language) -> None:
# Check language
if not lang.split("-")[0].lower() in list(Language):
raise ValueError("Language not supported. Please check your language.")
LOGGER.debug(f"Set language to {lang}.")
cls.LANGS = lang.upper()
@classmethod
def __get_path_assets(cls) -> Dict[str, str]:
return {
"data": os.path.join(PATH, "json", "data"),
"langs": os.path.join(PATH, "json", "langs")
}
@classmethod
def __load_assets_lang(cls) -> None:
_PATH = cls.__get_path_assets()["langs"]
FILE_LANG = os.listdir(_PATH)
for FILENAME in FILE_LANG:
LOGGER.debug(f"Loading language file {FILENAME}...")
cls.HASH_MAP[FILENAME.split(".")[0]] = json.load(cls.__load(os.path.join(_PATH, FILENAME)))
@classmethod
def __load_assets_data(cls) -> None:
_PATH = cls.__get_path_assets()["data"]
FILE_DATA = os.listdir(_PATH)
for FILENAME in FILE_DATA:
LOGGER.debug(f"Loading data file {FILENAME}...")
cls.DATA[FILENAME.split(".")[0]] = json.load(cls.__load(os.path.join(_PATH, FILENAME)))
def __load(path: str) -> TextIOWrapper:
return open(path, "r", encoding="utf-8")