worthless-launcher/worthless/launcherconfig.py

82 lines
2.6 KiB
Python

from configparser import ConfigParser
from os import PathLike
from pathlib import Path
from worthless.enums import GameVariant
class LauncherConfig:
"""
Provides config.ini for official launcher compatibility
"""
@staticmethod
def create_config(game_version, variant: GameVariant):
"""
Creates `config.ini`
https://notabug.org/Krock/dawn/src/master/updater/update_gi.sh#L212
"""
sub_channel = 0
channel = 1
cps = "mihoyo"
match variant:
case GameVariant.INTERNATIONAL:
channel = 1
sub_channel = 0
cps = "mihoyo"
case GameVariant.CHINESE:
channel = 1
sub_channel = 1
cps = "mihoyo"
case GameVariant.BILIBILI:
channel = 14
sub_channel = 0
cps = "bilibili"
config = ConfigParser()
config.add_section("General")
config.set("General", "channel", str(channel))
config.set("General", "cps", cps)
config.set("General", "game_version", game_version)
config.set("General", "sdk_version", "")
config.set("General", "sub_channel", str(sub_channel))
return config
def __init__(
self, config_path: PathLike, game_version=None, variant: GameVariant = None
):
if not variant:
variant = GameVariant.INTERNATIONAL
if not isinstance(config_path, Path):
config_path = Path(config_path)
if not game_version:
game_version = "0.0.0"
self._config_path = config_path
self._config = ConfigParser()
if self._config_path.exists():
self._config.read(self._config_path)
else:
self._config = self.create_config(game_version, variant)
def set_game_version(self, game_version):
self._config.set("General", "game_version", game_version)
def set_variant(self, variant: GameVariant):
sub_channel = 0
match variant:
case GameVariant.INTERNATIONAL:
sub_channel = 0
case GameVariant.CHINESE:
sub_channel = 1
case GameVariant.BILIBILI:
sub_channel = 0
self._config.set("General", "sub_channel", str(sub_channel))
def save(self):
"""
Saves config.ini
"""
with self._config_path.open("w") as config_file:
self._config.write(config_file)
def get(self, section: str, option: str, **kwargs) -> str:
return self._config.get(section=section, option=option, **kwargs)