2022-03-30 20:09:33 +01:00
|
|
|
from configparser import ConfigParser
|
2022-09-18 12:41:58 +01:00
|
|
|
from os import PathLike
|
2022-03-30 20:09:33 +01:00
|
|
|
from pathlib import Path
|
2022-09-28 06:24:24 +01:00
|
|
|
from worthless.game.gameenums import Variant
|
2022-06-24 19:13:47 +01:00
|
|
|
|
2022-03-30 20:09:33 +01:00
|
|
|
|
|
|
|
class LauncherConfig:
|
|
|
|
"""
|
|
|
|
Provides config.ini for official launcher compatibility
|
2022-09-20 10:35:56 +01:00
|
|
|
|
|
|
|
You should not use this class directly, since it's automatically used by Game when needed
|
2022-03-30 20:09:33 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
2022-09-20 10:35:56 +01:00
|
|
|
def create_config(game_version, variant: Variant):
|
2022-03-30 20:09:33 +01:00
|
|
|
"""
|
2022-09-18 12:41:58 +01:00
|
|
|
Creates `config.ini`
|
|
|
|
https://notabug.org/Krock/dawn/src/master/updater/update_gi.sh#L212
|
2022-03-30 20:09:33 +01:00
|
|
|
"""
|
2022-09-18 12:41:58 +01:00
|
|
|
sub_channel = 0
|
|
|
|
channel = 1
|
|
|
|
cps = "mihoyo"
|
|
|
|
match variant:
|
2022-09-20 10:35:56 +01:00
|
|
|
case Variant.INTERNATIONAL:
|
2022-09-18 12:41:58 +01:00
|
|
|
channel = 1
|
|
|
|
sub_channel = 0
|
|
|
|
cps = "mihoyo"
|
2022-09-20 10:35:56 +01:00
|
|
|
case Variant.CHINESE:
|
2022-09-18 12:41:58 +01:00
|
|
|
channel = 1
|
|
|
|
sub_channel = 1
|
|
|
|
cps = "mihoyo"
|
2022-09-20 10:35:56 +01:00
|
|
|
case Variant.BILIBILI:
|
2022-09-18 12:41:58 +01:00
|
|
|
channel = 14
|
|
|
|
sub_channel = 0
|
|
|
|
cps = "bilibili"
|
2022-03-30 20:09:33 +01:00
|
|
|
config = ConfigParser()
|
|
|
|
config.add_section("General")
|
2022-09-18 12:41:58 +01:00
|
|
|
config.set("General", "channel", str(channel))
|
|
|
|
config.set("General", "cps", cps)
|
2022-03-30 20:09:33 +01:00
|
|
|
config.set("General", "game_version", game_version)
|
|
|
|
config.set("General", "sdk_version", "")
|
2022-09-18 12:41:58 +01:00
|
|
|
config.set("General", "sub_channel", str(sub_channel))
|
2022-03-30 20:09:33 +01:00
|
|
|
return config
|
|
|
|
|
2022-09-18 12:41:58 +01:00
|
|
|
def __init__(
|
2022-09-20 10:35:56 +01:00
|
|
|
self, config_path: PathLike, game_version=None, variant: Variant = None
|
2022-09-18 12:41:58 +01:00
|
|
|
):
|
|
|
|
if not variant:
|
2022-09-20 10:35:56 +01:00
|
|
|
variant = Variant.INTERNATIONAL
|
2022-09-18 12:41:58 +01:00
|
|
|
if not isinstance(config_path, Path):
|
2022-06-24 19:13:47 +01:00
|
|
|
config_path = Path(config_path)
|
2022-03-30 20:09:33 +01:00
|
|
|
if not game_version:
|
|
|
|
game_version = "0.0.0"
|
2022-09-18 12:41:58 +01:00
|
|
|
self._config_path = config_path
|
|
|
|
self._config = ConfigParser()
|
|
|
|
if self._config_path.exists():
|
|
|
|
self._config.read(self._config_path)
|
2022-03-30 20:09:33 +01:00
|
|
|
else:
|
2022-09-18 12:41:58 +01:00
|
|
|
self._config = self.create_config(game_version, variant)
|
2022-03-30 20:09:33 +01:00
|
|
|
|
|
|
|
def set_game_version(self, game_version):
|
2022-09-18 12:41:58 +01:00
|
|
|
self._config.set("General", "game_version", game_version)
|
2022-03-30 20:09:33 +01:00
|
|
|
|
2022-09-20 10:35:56 +01:00
|
|
|
def set_variant(self, variant: Variant):
|
2022-09-18 12:41:58 +01:00
|
|
|
sub_channel = 0
|
|
|
|
match variant:
|
2022-09-20 10:35:56 +01:00
|
|
|
case Variant.INTERNATIONAL:
|
2022-09-18 12:41:58 +01:00
|
|
|
sub_channel = 0
|
2022-09-20 10:35:56 +01:00
|
|
|
case Variant.CHINESE:
|
2022-09-18 12:41:58 +01:00
|
|
|
sub_channel = 1
|
2022-09-20 10:35:56 +01:00
|
|
|
case Variant.BILIBILI:
|
2022-09-18 12:41:58 +01:00
|
|
|
sub_channel = 0
|
|
|
|
self._config.set("General", "sub_channel", str(sub_channel))
|
2022-03-30 20:09:33 +01:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
Saves config.ini
|
|
|
|
"""
|
2022-09-18 12:41:58 +01:00
|
|
|
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)
|