1
0
Fork 0
mirror of synced 2024-06-02 18:54:41 +12:00
Rare/rare/utils/utils.py

188 lines
8 KiB
Python
Raw Normal View History

2021-02-10 23:48:25 +13:00
import json
import os
2021-02-18 06:19:37 +13:00
import shutil
2021-04-14 02:56:44 +12:00
import sys
2021-02-10 23:48:25 +13:00
from logging import getLogger
import requests
2021-04-06 22:28:10 +12:00
from PIL import Image, UnidentifiedImageError
2021-02-20 00:57:55 +13:00
from PyQt5.QtCore import pyqtSignal, QLocale, QSettings
2021-02-10 23:48:25 +13:00
2021-04-14 02:56:44 +12:00
from rare import lang_path, __version__, style_path
2021-03-19 00:45:59 +13:00
from custom_legendary.core import LegendaryCore
2021-02-10 23:48:25 +13:00
2021-04-28 06:39:02 +12:00
import id
2021-02-10 23:48:25 +13:00
logger = getLogger("Utils")
s = QSettings("Rare", "Rare")
2021-04-08 05:53:07 +12:00
IMAGE_DIR = s.value("img_dir", os.path.expanduser("~/.cache/rare/images"), type=str)
2021-03-19 00:45:59 +13:00
logger.info("IMAGE DIRECTORY: " + IMAGE_DIR)
2021-02-10 23:48:25 +13:00
def download_images(signal: pyqtSignal, core: LegendaryCore):
if not os.path.isdir(IMAGE_DIR):
os.makedirs(IMAGE_DIR)
logger.info("Create Image dir")
# Download Images
2021-04-17 03:48:24 +12:00
games, dlcs = core.get_game_and_dlc_list()
dlc_list = []
for i in dlcs.values():
dlc_list.append(i[0])
2021-04-17 04:56:33 +12:00
game_list = games + dlc_list
for i, game in enumerate(game_list):
2021-02-20 00:57:55 +13:00
try:
download_image(game)
except json.decoder.JSONDecodeError:
shutil.rmtree(f"{IMAGE_DIR}/{game.app_name}")
download_image(game)
2021-04-17 04:56:33 +12:00
signal.emit(i/len(game_list)*100)
2021-02-10 23:48:25 +13:00
2021-02-18 06:19:37 +13:00
def download_image(game, force=False):
2021-04-17 03:48:24 +12:00
if force and os.path.exists(f"{IMAGE_DIR}/{game.app_name}"):
2021-02-18 06:19:37 +13:00
shutil.rmtree(f"{IMAGE_DIR}/{game.app_name}")
if not os.path.isdir(f"{IMAGE_DIR}/" + game.app_name):
os.mkdir(f"{IMAGE_DIR}/" + game.app_name)
2021-02-10 23:48:25 +13:00
2021-04-06 22:28:10 +12:00
# to git picture updates
2021-02-18 06:19:37 +13:00
if not os.path.isfile(f"{IMAGE_DIR}/{game.app_name}/image.json"):
2021-04-14 02:56:44 +12:00
json_data = {"DieselGameBoxTall": None, "DieselGameBoxLogo": None, "Thumbnail": None}
2021-02-18 06:19:37 +13:00
else:
json_data = json.load(open(f"{IMAGE_DIR}/{game.app_name}/image.json", "r"))
# Download
for image in game.metadata["keyImages"]:
2021-04-14 02:56:44 +12:00
if image["type"] == "DieselGameBoxTall" or image["type"] == "DieselGameBoxLogo" or image["type"] == "Thumbnail":
if image["type"] not in json_data.keys():
json_data[image["type"]] = None
2021-02-18 06:19:37 +13:00
if json_data[image["type"]] != image["md5"] or not os.path.isfile(
f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png"):
# Download
json_data[image["type"]] = image["md5"]
# os.remove(f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png")
json.dump(json_data, open(f"{IMAGE_DIR}/{game.app_name}/image.json", "w"))
logger.info(f"Download Image for Game: {game.app_title}")
url = image["url"]
with open(f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png", "wb") as f:
f.write(requests.get(url).content)
2021-04-06 22:28:10 +12:00
try:
img = Image.open(f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png")
img = img.resize((200, int(200*4/3)))
img.save(f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png")
except UnidentifiedImageError as e:
logger.warning(e)
2021-02-18 06:19:37 +13:00
# scale and grey
if not os.path.isfile(f'{IMAGE_DIR}/' + game.app_name + '/UninstalledArt.png'):
2021-02-10 23:48:25 +13:00
2021-02-18 06:19:37 +13:00
if os.path.isfile(f'{IMAGE_DIR}/' + game.app_name + '/DieselGameBoxTall.png'):
# finalArt = Image.open(f'{IMAGE_DIR}/' + game.app_name + '/DieselGameBoxTall.png')
# finalArt.save(f'{IMAGE_DIR}/{game.app_name}/FinalArt.png')
# And same with the grayscale one
2021-02-10 23:48:25 +13:00
2021-02-18 06:19:37 +13:00
bg = Image.open(f"{IMAGE_DIR}/{game.app_name}/DieselGameBoxTall.png")
uninstalledArt = bg.convert('L')
2021-04-06 22:28:10 +12:00
uninstalledArt = uninstalledArt.resize((200, int(200*4/3)))
2021-02-18 06:19:37 +13:00
uninstalledArt.save(f'{IMAGE_DIR}/{game.app_name}/UninstalledArt.png')
elif os.path.isfile(f"{IMAGE_DIR}/{game.app_name}/DieselGameBoxLogo.png"):
bg: Image.Image = Image.open(f"{IMAGE_DIR}/{game.app_name}/DieselGameBoxLogo.png")
bg = bg.resize((int(bg.size[1] * 3 / 4), bg.size[1]))
logo = Image.open(f'{IMAGE_DIR}/{game.app_name}/DieselGameBoxLogo.png').convert('RGBA')
wpercent = ((bg.size[0] * (3 / 4)) / float(logo.size[0]))
hsize = int((float(logo.size[1]) * float(wpercent)))
logo = logo.resize((int(bg.size[0] * (3 / 4)), hsize), Image.ANTIALIAS)
# Calculate where the image has to be placed
pasteX = int((bg.size[0] - logo.size[0]) / 2)
pasteY = int((bg.size[1] - logo.size[1]) / 2)
# And finally copy the background and paste in the image
# finalArt = bg.copy()
# finalArt.paste(logo, (pasteX, pasteY), logo)
# Write out the file
# finalArt.save(f'{IMAGE_DIR}/' + game.app_name + '/FinalArt.png')
logoCopy = logo.copy()
logoCopy.putalpha(int(256 * 3 / 4))
logo.paste(logoCopy, logo)
uninstalledArt = bg.copy()
uninstalledArt.paste(logo, (pasteX, pasteY), logo)
uninstalledArt = uninstalledArt.convert('L')
uninstalledArt.save(f'{IMAGE_DIR}/' + game.app_name + '/UninstalledArt.png')
else:
2021-04-06 22:28:10 +12:00
logger.warning(f"File {IMAGE_DIR}/{game.app_name}/DieselGameBoxTall.png doesn't exist")
2021-02-10 23:48:25 +13:00
def get_lang():
core = LegendaryCore()
if "Legendary" in core.lgd.config.sections() and "locale" in core.lgd.config["Legendary"]:
logger.info("Found locale in Legendary config: " + core.lgd.config.get("Legendary", "locale"))
return core.lgd.config.get("Legendary", "locale").split("-")[0]
2021-02-10 23:48:25 +13:00
else:
logger.info("Found locale in system config: " + QLocale.system().name().split("_")[0])
return QLocale.system().name().split("_")[0]
2021-02-20 00:57:55 +13:00
def get_possible_langs():
langs = ["en"]
for i in os.listdir(lang_path):
if i.endswith(".qm"):
langs.append(i.split(".")[0])
return langs
def get_latest_version():
2021-04-20 01:44:28 +12:00
try:
resp = requests.get("https://api.github.com/repos/Dummerle/Rare/releases/latest")
tag = json.loads(resp.content.decode("utf-8"))["tag_name"]
return tag
except requests.exceptions.ConnectionError:
return "0.0.0"
2021-04-12 07:02:56 +12:00
def get_size(b: int) -> str:
for i in ['', 'K', 'M', 'G', 'T', 'P', 'E']:
if b < 1024:
return f"{b:.2f}{i}B"
b /= 1024
2021-04-14 02:56:44 +12:00
2021-04-14 04:01:25 +12:00
def create_desktop_link(app_name, core: LegendaryCore, type_of_link="desktop"):
2021-04-14 02:56:44 +12:00
igame = core.get_installed_game(app_name)
if os.path.exists(
os.path.join(QSettings('Rare', 'Rare').value('img_dir', os.path.expanduser('~/.cache/rare/images'), str),
igame.app_name, 'Thumbnail.png')):
icon = os.path.join(QSettings('Rare', 'Rare').value('img_dir', os.path.expanduser('~/.cache/rare/images'), str),
igame.app_name, 'Thumbnail.png')
else:
icon = os.path.join(QSettings('Rare', 'Rare').value('img_dir', os.path.expanduser('~/.cache/rare/images'), str),
igame.app_name, 'DieselGameBoxTall.png')
2021-04-14 02:56:44 +12:00
# Linux
if os.name == "posix":
2021-04-14 04:01:25 +12:00
if type_of_link == "desktop":
path = os.path.expanduser(f"~/Desktop/")
elif type_of_link == "start_menu":
path = os.path.expanduser("~/.local/share/applications/")
else:
return
with open(f"{path}{igame.title}.desktop", "w") as desktop_file:
2021-04-14 02:56:44 +12:00
desktop_file.write("[Desktop Entry]\n"
f"Name={igame.title}\n"
f"Type=Application\n"
f"Icon={icon}\n"
f"Exec=rare launch {app_name}\n"
"Terminal=false\n"
"StartupWMClass=rare-game\n"
)
os.chmod(os.path.expanduser(f"~/Desktop/{igame.title}.desktop"), 0o755)
# Windows
2021-04-14 02:56:44 +12:00
elif os.name == "nt":
if type_of_link == "desktop":
path = os.path.expanduser(f"~/Desktop/")
elif type_of_link == "start_menu":
logger.info("Startmenu link is not supported on windows")
return
else:
return
with open(path+igame.title+".bat", "w") as desktop_file:
desktop_file.write(f"rare launch {app_name}")