Skip to content

Commit

Permalink
add umu-database.csv and package it, use it for looking up umu info i…
Browse files Browse the repository at this point in the history
…nstead of requiring internet connection
  • Loading branch information
GloriousEggroll committed Oct 15, 2024
1 parent 5936c55 commit deae774
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
55 changes: 21 additions & 34 deletions fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -45,41 +42,31 @@ 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]
Expand Down
1 change: 1 addition & 0 deletions subprojects/umu-database
Submodule umu-database added at ced4c7
1 change: 1 addition & 0 deletions umu-database.csv

0 comments on commit deae774

Please sign in to comment.