From 364311023e899cd31d9fc2938365ae92dd4858c9 Mon Sep 17 00:00:00 2001 From: FalseIlyu <83550517+FalseIlyu@users.noreply.github.com> Date: Fri, 29 Dec 2023 10:31:33 +0100 Subject: [PATCH] Fixes for Black & White 2 plugins (#131) * Use BasicSaveGameInfo preview * Remove inheritance from interface --- games/game_blackandwhite2.py | 92 ++++++++++-------------------------- 1 file changed, 24 insertions(+), 68 deletions(-) diff --git a/games/game_blackandwhite2.py b/games/game_blackandwhite2.py index c81df30..768b6dc 100644 --- a/games/game_blackandwhite2.py +++ b/games/game_blackandwhite2.py @@ -2,17 +2,18 @@ import os import struct import time +from collections.abc import Mapping from pathlib import Path from typing import BinaryIO import mobase -from PyQt6.QtCore import QDateTime, QDir, QFile, QFileInfo, Qt -from PyQt6.QtGui import QPainter, QPixmap +from PyQt6.QtCore import QDateTime, QDir, QFile, QFileInfo from ..basic_features import BasicLocalSavegames from ..basic_features.basic_save_game_info import ( BasicGameSaveGame, BasicGameSaveGameInfo, + format_date, ) from ..basic_game import BasicGame @@ -188,12 +189,15 @@ def __init__(self, filepath: Path): self.elapsed = int.from_bytes(self.readInf(info, "elapsed"), "little") # Getting date in 100th of nanosecond need to convert NT time # to UNIX time and offset localtime - self.lastsave = ( + self.lastsave = int( ( - struct.unpack("q", self.readInf(info, "date"))[0] / 10000 - - 11644473600000 + ( + struct.unpack("q", self.readInf(info, "date"))[0] / 10000 + - 11644473600000 + ) ) - ) - (time.localtime().tm_gmtoff * 1000) + - (time.localtime().tm_gmtoff * 1000) + ) info.close() def readInf(self, inf: BinaryIO, key: str): @@ -206,8 +210,7 @@ def allFiles(self) -> list[str]: return files def getCreationTime(self) -> QDateTime: - time = QDateTime.fromMSecsSinceEpoch(self.lastsave) - return time + return QDateTime.fromMSecsSinceEpoch(self.lastsave) def getElapsed(self) -> str: return str(datetime.timedelta(seconds=self.elapsed)) @@ -222,60 +225,15 @@ def getSaveGroupIdentifier(self): return self._filepath.parent.parent.name -def _getPreview(savepath: Path): - save = BlackAndWhite2SaveGame(savepath) - lines = [ - [ - ("Name : " + save.getName(), Qt.AlignmentFlag.AlignLeft), - ( - "| Profile : " + save.getSaveGroupIdentifier()[1:], - Qt.AlignmentFlag.AlignLeft, - ), - ], - [("Land number : " + save.getLand(), Qt.AlignmentFlag.AlignLeft)], - [ - ( - "Saved at : " + save.getCreationTime().toString(), - Qt.AlignmentFlag.AlignLeft, - ) - ], - [("Elapsed time : " + save.getElapsed(), Qt.AlignmentFlag.AlignLeft)], - ] - - pixmap = QPixmap(320, 320) - pixmap.fill() - # rightBuffer = [] - - painter = QPainter() - painter.begin(pixmap) - fm = painter.fontMetrics() - margin = 5 - height = 0 - width = 0 - ln = 0 - for line in lines: - cHeight = 0 - cWidth = 0 - - for toPrint, align in line: - bRect = fm.boundingRect(toPrint) - cHeight = bRect.height() * (ln + 1) - bRect.moveTop(cHeight - bRect.height()) - if align != Qt.AlignmentFlag.AlignLeft: - continue - else: - bRect.moveLeft(cWidth + margin) - cWidth = cWidth + bRect.width() - painter.drawText(bRect, align, toPrint) - - height = max(height, cHeight) - width = max(width, cWidth + (2 * margin)) - ln = ln + 1 - # height = height + lh - - painter.end() - - return pixmap.copy(0, 0, width, height) +def getMetadata(savepath: Path, save: mobase.ISaveGame) -> Mapping[str, str]: + assert isinstance(save, BlackAndWhite2SaveGame) + return { + "Name": save.getName(), + "Profile": save.getSaveGroupIdentifier(), + "Land": save.getLand(), + "Saved at": format_date(save.getCreationTime()), + "Elapsed time": save.getElapsed(), + } PSTART_MENU = ( @@ -283,7 +241,7 @@ def _getPreview(savepath: Path): ) -class BlackAndWhite2Game(BasicGame, mobase.IPluginFileMapper): +class BlackAndWhite2Game(BasicGame): Name = "Black & White 2 Support Plugin" Author = "Ilyu" Version = "1.0.1" @@ -302,17 +260,15 @@ class BlackAndWhite2Game(BasicGame, mobase.IPluginFileMapper): _program_link = PSTART_MENU + "\\Black & White 2\\Black & White® 2.lnk" - def __init__(self): - BasicGame.__init__(self) - mobase.IPluginFileMapper.__init__(self) - def init(self, organizer: mobase.IOrganizer) -> bool: BasicGame.init(self, organizer) self._featureMap[mobase.ModDataChecker] = BlackAndWhite2ModDataChecker() self._featureMap[mobase.LocalSavegames] = BasicLocalSavegames( self.savesDirectory() ) - self._featureMap[mobase.SaveGameInfo] = BasicGameSaveGameInfo(_getPreview) + self._featureMap[mobase.SaveGameInfo] = BasicGameSaveGameInfo( + get_metadata=getMetadata, max_width=400 + ) return True def detectGame(self):