Skip to content

Commit

Permalink
Fixes for Black & White 2 plugins (#131)
Browse files Browse the repository at this point in the history
* Use BasicSaveGameInfo preview
* Remove inheritance from interface
  • Loading branch information
FalseIlyu authored Dec 29, 2023
1 parent 4d7a7cc commit 3643110
Showing 1 changed file with 24 additions and 68 deletions.
92 changes: 24 additions & 68 deletions games/game_blackandwhite2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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))
Expand All @@ -222,68 +225,23 @@ 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 = (
str(os.getenv("ProgramData")) + "\\Microsoft\\Windows\\Start Menu\\Programs"
)


class BlackAndWhite2Game(BasicGame, mobase.IPluginFileMapper):
class BlackAndWhite2Game(BasicGame):
Name = "Black & White 2 Support Plugin"
Author = "Ilyu"
Version = "1.0.1"
Expand All @@ -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):
Expand Down

0 comments on commit 3643110

Please sign in to comment.