2022-07-16 19:36:14 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
from typing import Dict, List, TextIO
|
2022-07-16 19:36:14 +00:00
|
|
|
|
|
|
|
from .enum import Language
|
|
|
|
from .model import assets
|
|
|
|
from .utils import create_ui_path
|
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
from typing import Union
|
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
PATH = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
class Assets:
|
|
|
|
DATA: Dict[str, dict] = {}
|
|
|
|
HASH_MAP: Dict[str, dict] = {}
|
2022-07-18 08:44:53 +00:00
|
|
|
LANGS: Language = Language.EN
|
2022-07-16 19:36:14 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
def __init__(self, lang: Union[str, Language] = Language.EN) -> None:
|
2022-07-16 19:36:14 +00:00
|
|
|
# Set language
|
2022-07-18 08:44:53 +00:00
|
|
|
self._set_language(lang)
|
2022-07-17 06:08:16 +00:00
|
|
|
self.reload_assets()
|
2022-07-16 19:36:14 +00:00
|
|
|
|
2022-07-17 06:08:16 +00:00
|
|
|
@classmethod
|
|
|
|
def reload_assets(cls) -> None:
|
2022-07-16 19:36:14 +00:00
|
|
|
# Load assets
|
2022-07-17 06:08:16 +00:00
|
|
|
cls.__load_assets_lang()
|
|
|
|
cls.__load_assets_data()
|
2022-07-16 19:36:14 +00:00
|
|
|
|
2022-08-02 05:29:35 +00:00
|
|
|
@property
|
|
|
|
def CHARACTERS_IDS(self) -> List[str]:
|
|
|
|
return [x for x in self.DATA["characters"]]
|
|
|
|
|
2022-08-03 07:50:15 +00:00
|
|
|
@property
|
|
|
|
def COSTUMES_IDS(self) -> List[str]:
|
|
|
|
return [x for x in self.DATA["costumes"]]
|
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
@classmethod
|
2022-08-03 07:50:15 +00:00
|
|
|
def character(cls, id: Union[int, str]) -> Union[assets.CharacterAsset, None]: # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
LOGGER.debug(f"Getting character assets with id: {id}")
|
2022-08-03 07:50:15 +00:00
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
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({
|
2022-08-03 07:50:15 +00:00
|
|
|
"id": id if id.isdigit() else id.split("-")[0],
|
|
|
|
"skill_id": str(id).split("-")[1] if not id.isdigit() else 0,
|
2022-07-16 19:36:14 +00:00
|
|
|
"images": cls.create_character_icon(data["sideIconName"]),
|
|
|
|
**data
|
|
|
|
})
|
|
|
|
|
2022-08-03 07:50:15 +00:00
|
|
|
@classmethod
|
|
|
|
def character_costume(cls, id: int): # noqa: E501
|
|
|
|
LOGGER.debug(f"Getting costume assets with id: {id}")
|
|
|
|
data = cls.DATA["costumes"].get(str(id))
|
|
|
|
if not data:
|
|
|
|
LOGGER.error(f"Costume not found with id: {id}")
|
|
|
|
return
|
|
|
|
|
|
|
|
return assets.CharacterCostume.parse_obj({
|
|
|
|
"id": id,
|
|
|
|
"images": cls.create_chractar_costume_icon(data["sideIconName"])
|
|
|
|
})
|
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
@classmethod
|
2022-07-29 04:31:21 +00:00
|
|
|
def constellations(cls, id: int) -> Union[assets.CharacterConstellationsAsset, None]: # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
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))
|
2022-08-03 07:50:15 +00:00
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
if not data:
|
|
|
|
LOGGER.error(f"Character skills not found with id: {id}")
|
2022-07-29 04:31:21 +00:00
|
|
|
return
|
2022-07-16 19:36:14 +00:00
|
|
|
|
|
|
|
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]:
|
2022-07-29 04:31:21 +00:00
|
|
|
LOGGER.debug(f"Getting nameTextMapHash {hash_id} with language: {cls.LANGS}") # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
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]
|
2022-07-29 04:31:21 +00:00
|
|
|
LOGGER.debug(f"Got nameTextMapHash {hash_id} with language: {key} (Value '{val}')") # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
return val
|
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
LOGGER.error(f"nameTextMapHash {hash_id} not found with language: {cls.LANGS}") # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
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),
|
2022-07-29 04:31:21 +00:00
|
|
|
banner=create_ui_path(path.replace("AvatarIcon_Side", "Gacha_AvatarImg")) # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
)
|
|
|
|
|
2022-08-03 07:50:15 +00:00
|
|
|
@classmethod
|
|
|
|
def create_chractar_costume_icon(cls, path: str) -> assets.CharacterIconAsset: # noqa: E501
|
|
|
|
_data = cls.create_character_icon(path)
|
|
|
|
_data.banner = _data.banner.replace("Gacha_AvatarImg", "Costume")
|
|
|
|
return _data
|
|
|
|
|
2022-07-16 19:36:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_icon_path(path: str) -> str:
|
|
|
|
return create_ui_path(path)
|
|
|
|
|
|
|
|
@classmethod
|
2022-07-18 08:44:53 +00:00
|
|
|
def _set_language(cls, lang: Language) -> None:
|
2022-07-16 19:36:14 +00:00
|
|
|
# Check language
|
2022-08-02 05:29:35 +00:00
|
|
|
if lang is None or not lang.split("-")[0].lower() in list(Language):
|
2022-07-29 04:31:21 +00:00
|
|
|
raise ValueError("Language not supported. Please check your language.") # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
|
|
|
|
LOGGER.debug(f"Set language to {lang}.")
|
|
|
|
cls.LANGS = lang.upper()
|
|
|
|
|
|
|
|
@classmethod
|
2022-07-17 06:08:16 +00:00
|
|
|
def _get_path_assets(cls) -> Dict[str, str]:
|
2022-07-16 19:36:14 +00:00
|
|
|
return {
|
2022-07-17 06:08:16 +00:00
|
|
|
"data": os.path.join(PATH, "assets", "data"),
|
|
|
|
"langs": os.path.join(PATH, "assets", "langs")
|
2022-07-16 19:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __load_assets_lang(cls) -> None:
|
2022-07-17 06:08:16 +00:00
|
|
|
_PATH = cls._get_path_assets()["langs"]
|
2022-07-16 19:36:14 +00:00
|
|
|
FILE_LANG = os.listdir(_PATH)
|
|
|
|
for FILENAME in FILE_LANG:
|
|
|
|
LOGGER.debug(f"Loading language file {FILENAME}...")
|
2022-07-29 04:31:21 +00:00
|
|
|
cls.HASH_MAP[FILENAME.split(".")[0]] = json.load(cls.__load(os.path.join(_PATH, FILENAME))) # noqa: E501
|
2022-07-16 19:36:14 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __load_assets_data(cls) -> None:
|
2022-07-17 06:08:16 +00:00
|
|
|
_PATH = cls._get_path_assets()["data"]
|
2022-07-16 19:36:14 +00:00
|
|
|
FILE_DATA = os.listdir(_PATH)
|
|
|
|
for FILENAME in FILE_DATA:
|
|
|
|
LOGGER.debug(f"Loading data file {FILENAME}...")
|
2022-08-04 13:56:23 +00:00
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
cls.DATA[FILENAME.split(".")[0]] = json.load(cls.__load(os.path.join(_PATH, FILENAME))) # noqa: E501
|
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def __load(path: str) -> TextIO:
|
2022-07-16 19:36:14 +00:00
|
|
|
return open(path, "r", encoding="utf-8")
|