From eea2879b27c6a0b52cd4f4669e9763d9b86f9f44 Mon Sep 17 00:00:00 2001 From: GloriousEggroll Date: Tue, 15 Oct 2024 12:51:18 -0600 Subject: [PATCH] add umu-database.csv and package it, use it for looking up umu info instead of requiring internet connection --- .gitmodules | 3 +++ Makefile | 1 + fix.py | 52 +++++++++++++++++----------------------- subprojects/umu-database | 1 + umu-database.csv | 1 + 5 files changed, 28 insertions(+), 30 deletions(-) create mode 160000 subprojects/umu-database create mode 120000 umu-database.csv diff --git a/.gitmodules b/.gitmodules index 8d8b77cd..8349c500 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "subprojects/winetricks"] path = subprojects/winetricks url = https://github.com/Winetricks/winetricks.git +[submodule "subprojects/umu-database"] + path = subprojects/umu-database + url = https://github.com/Open-Wine-Components/umu-database diff --git a/Makefile b/Makefile index c1a7fc4f..af89c507 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ protonfixes-install: protonfixes cp -r verbs $(INSTALL_DIR) cp *.py $(INSTALL_DIR) cp winetricks $(INSTALL_DIR) + cp umu-database.csv $(INSTALL_DIR) rm $(INSTALL_DIR)/protonfixes_test.py # diff --git a/fix.py b/fix.py index 101485d5..f00835dc 100644 --- a/fix.py +++ b/fix.py @@ -3,19 +3,16 @@ import os import re import sys -import urllib -import json +import csv from functools import lru_cache from importlib import import_module try: from . import config - from .util import check_internet from .checks import run_checks from .logger import log except ImportError: import config - from util import check_internet from checks import run_checks from logger import log @@ -45,41 +42,36 @@ def get_game_id() -> str: def get_game_name() -> str: """Trys to return the game name from environment variables""" pfx = os.environ.get('WINEPREFIX') or protonmain.g_session.env.get('WINEPREFIX') + script_dir = os.path.dirname(os.path.abspath(__file__)) if os.environ.get('UMU_ID'): + if os.path.isfile(f'{pfx}/game_title'): with open(f'{pfx}/game_title', encoding='utf-8') as file: return file.readline() - if not check_internet(): - log.warn("No internet connection, can't fetch name") - return 'UNKNOWN' + umu_id = os.environ['UMU_ID'] + store = os.getenv('STORE', 'none') + csv_file_path = os.path.join(script_dir, 'umu-database.csv') try: - # Fallback to 'none', if STORE isn't set - store = os.getenv('STORE', 'none') - url = f'https://umu.openwinecomponents.org/umu_api.php?umu_id={os.environ["UMU_ID"]}&store={store}' - headers = {'User-Agent': 'Mozilla/5.0'} - req = urllib.request.Request(url, headers=headers) - with urllib.request.urlopen(req, timeout=5) as response: - data = response.read() - json_data = json.loads(data) - title = json_data[0]['title'] - with open( - pfx + '/game_title', 'w', encoding='utf-8' - ) as file: - file.write(title) - return title - except TimeoutError as ex: - log.info('umu.openwinecomponents.org timed out') - log.debug(f'TimeoutError occurred: {ex}') - except OSError as ex: - log.debug(f'OSError occurred: {ex}') - except IndexError as ex: - log.debug(f'IndexError occurred: {ex}') - except UnicodeDecodeError as ex: - log.debug(f'UnicodeDecodeError occurred: {ex}') + with open(csv_file_path, newline='', encoding='utf-8') as csvfile: + csvreader = csv.reader(csvfile) + for row in csvreader: + # Check if the row has enough columns and matches both UMU_ID and STORE + if len(row) > 3 and row[3] == umu_id and row[1] == store: + title = row[0] # Title is the first entry + with open(os.path.join(script_dir, 'game_title'), 'w', encoding='utf-8') as file: + file.write(title) + return title + except FileNotFoundError: + log.warn(f"CSV file not found: {csv_file_path}") + except Exception as ex: + log.debug(f"Error reading CSV file: {ex}") + + log.warn("Game title not found in CSV") return 'UNKNOWN' + try: log.debug('UMU_ID is not in environment') game_library = re.findall(r'.*/steamapps', os.environ['PWD'], re.IGNORECASE)[-1] diff --git a/subprojects/umu-database b/subprojects/umu-database new file mode 160000 index 00000000..ced4c791 --- /dev/null +++ b/subprojects/umu-database @@ -0,0 +1 @@ +Subproject commit ced4c791fb9527772cd1af431ab132051ec56453 diff --git a/umu-database.csv b/umu-database.csv new file mode 120000 index 00000000..76e0c198 --- /dev/null +++ b/umu-database.csv @@ -0,0 +1 @@ +subprojects/umu-database/umu-database.csv \ No newline at end of file