Compare commits

..

2 Commits

Author SHA1 Message Date
f8887a4baf feat: implement get_version
Thanks An Anime Team for the code xD
2023-06-17 03:33:37 +07:00
9126521f1b fix: remove abstractmethod so i can code 2023-06-17 03:33:05 +07:00
2 changed files with 48 additions and 8 deletions

View File

@ -10,14 +10,12 @@ class GameABC(ABC):
def __init__(self, path: PathLike = None): def __init__(self, path: PathLike = None):
pass pass
@abstractmethod
def is_installed(self) -> bool: def is_installed(self) -> bool:
""" """
Check if the game is installed Check if the game is installed
""" """
pass pass
@abstractmethod
def install_game(self, game_path: PathLike = None): def install_game(self, game_path: PathLike = None):
""" """
Install the game Install the game
@ -33,7 +31,6 @@ class GameABC(ABC):
""" """
pass pass
@abstractmethod
def install_game_from_archive( def install_game_from_archive(
self, archive: PathLike, game_path: PathLike = None self, archive: PathLike, game_path: PathLike = None
) -> None: ) -> None:
@ -48,7 +45,6 @@ class GameABC(ABC):
game_path (PathLike, optional): Path to install the game to. game_path (PathLike, optional): Path to install the game to.
""" """
@abstractmethod
def install_update_from_archive( def install_update_from_archive(
self, archive: PathLike, game_path: PathLike = None self, archive: PathLike, game_path: PathLike = None
) -> None: ) -> None:
@ -61,7 +57,6 @@ class GameABC(ABC):
""" """
pass pass
@abstractmethod
def get_version(self) -> tuple[int, int, int]: def get_version(self) -> tuple[int, int, int]:
""" """
Get the game version Get the game version
@ -70,21 +65,18 @@ class GameABC(ABC):
""" """
pass pass
@abstractmethod
def get_update(self): def get_update(self):
""" """
Get the game update Get the game update
""" """
pass pass
@abstractmethod
def get_voiceover_update(self, language: str): def get_voiceover_update(self, language: str):
""" """
Get the voiceover update Get the voiceover update
""" """
pass pass
@abstractmethod
def get_channel(self): def get_channel(self):
""" """
Get the game channel Get the game channel

View File

@ -19,6 +19,9 @@ class Game(GameABC):
def path(self) -> Path | None: def path(self) -> Path | None:
return self._path return self._path
def data_folder(self) -> Path:
return self._path.joinpath("StarRail_Data")
def is_installed(self) -> bool: def is_installed(self) -> bool:
if self._path is None: if self._path is None:
return False return False
@ -28,6 +31,49 @@ class Game(GameABC):
): ):
return False return False
def get_version(self) -> tuple[int, int, int]:
"""
Get the current installed game version.
Credits to An Anime Team for the code that does the magic:
https://github.com/an-anime-team/anime-game-core/blob/main/src/games/star_rail/game.rs#L49
"""
allowed = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
version_bytes: list[bytes] = [0, 0, 0]
version_ptr = 0
correct = True
with self.data_folder().joinpath("data.unity3d").open("rb") as f:
f.seek(0x7D0) # 2000 in decimal
for byte in f.read(10000):
match byte:
case 0:
version_bytes = [0, 0, 0]
version_ptr = 0
correct = True
case 46:
version_ptr += 1
if version_ptr > 2:
correct = False
case 38:
if (
correct
and version_bytes[0] > 0
and version_bytes[1] > 0
and version_bytes[2] > 0
):
# TODO: The below code is not correct.
return (
int(version_bytes[0]),
int(version_bytes[1]),
int(version_bytes[2]),
)
case _:
if correct and byte in allowed:
version_bytes[version_ptr] += byte
else:
correct = False
return (0, 0, 0)
def get_channel(self) -> GameChannel: def get_channel(self) -> GameChannel:
if self.get_version() == (1, 0, 5): if self.get_version() == (1, 0, 5):
for channel, v in md5sums["1.0.5"].values(): for channel, v in md5sums["1.0.5"].values():
@ -42,3 +88,5 @@ class Game(GameABC):
return GameChannel.China return GameChannel.China
case "os": case "os":
return GameChannel.Overseas return GameChannel.Overseas
else:
return