worthless-launcher/worthless/game/launcherconfig.py

84 lines
2.7 KiB
Python

from configparser import ConfigParser
from os import PathLike
from pathlib import Path
from worthless.game.gameenums import Variant
class LauncherConfig:
"""
Provides config.ini for official launcher compatibility
You should not use this class directly, since it's automatically used by Game when needed
"""
@staticmethod
def create_config(game_version, variant: Variant):
"""
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 Variant.INTERNATIONAL:
channel = 1
sub_channel = 0
cps = "mihoyo"
case Variant.CHINESE:
channel = 1
sub_channel = 1
cps = "mihoyo"
case Variant.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: Variant = None
):
if not variant:
variant = Variant.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: Variant):
sub_channel = 0
match variant:
case Variant.INTERNATIONAL:
sub_channel = 0
case Variant.CHINESE:
sub_channel = 1
case Variant.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)