Update NSLGameScanner.py

This commit is contained in:
Roy 2024-11-04 23:59:31 -08:00 committed by GitHub
parent 59eaa5e32d
commit 2cf3f336db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1183,6 +1183,7 @@ if amazon_games:
#Itchio Scanner #Itchio Scanner
def get_itch_games(itch_db_location): def get_itch_games(itch_db_location):
print(f"Checking if {itch_db_location} exists...") print(f"Checking if {itch_db_location} exists...")
if not os.path.exists(itch_db_location): if not os.path.exists(itch_db_location):
@ -1190,64 +1191,69 @@ def get_itch_games(itch_db_location):
return [] return []
print("Opening and reading the database file...") print("Opening and reading the database file...")
with open(itch_db_location, 'rb') as f: conn = sqlite3.connect(itch_db_location)
shortcut_bytes = f.read() cursor = conn.cursor()
print("Parsing the database file...") # Parse the 'caves' table
paths = parse_butler_db(shortcut_bytes) cursor.execute("SELECT * FROM caves;")
caves = cursor.fetchall()
print("Converting paths to games...") # Parse the 'games' table
games = [dbpath_to_game(path) for path in paths if dbpath_to_game(path) is not None] cursor.execute("SELECT * FROM games;")
# Remove duplicates games = cursor.fetchall()
games = list(set(games))
print(f"Found {len(games)} unique games.")
return games
def parse_butler_db(content): # Create a dictionary to store game information by game_id
print("Finding matches in the database content...") games_dict = {game[0]: game for game in games}
pattern = rb'\{"basePath":"(.*?)","totalSize".*?"candidates":\[(.*?)\]\}'
matches = re.findall(pattern, content)
print(f"Found {len(matches)} matches.")
print("Converting matches to database paths...") print("Matching games between 'caves' and 'games' tables...")
db_paths = [] games_list = []
for match in matches: for cave in caves:
base_path = match[0].decode(errors='ignore') game_id = cave[1]
candidates_json = b'[' + match[1] + b']' if game_id in games_dict:
print(f"Candidates JSON: {candidates_json}") game_info = games_dict[game_id]
try: base_path = json.loads(cave[11])['basePath']
candidates = json.loads(candidates_json.decode(errors='ignore')) candidates = json.loads(cave[11])['candidates']
paths = [candidate['path'] for candidate in candidates] executable_path = candidates[0]['path']
db_paths.append((base_path, paths))
except json.JSONDecodeError as e: # Skip browser games (HTML files)
print(f"JSON decoding error: {e}. Skipping this entry and continuing...") if executable_path.endswith('.html'):
print(f"Skipping browser game: {game_info[2]}")
continue continue
print(f"Converted {len(matches)} matches to {len(db_paths)} database paths.")
return db_paths game_title = game_info[2]
games_list.append((base_path, executable_path, game_title))
# Remove duplicates
games_list = list(set(games_list))
print(f"Found {len(games_list)} unique games.")
return games_list
def dbpath_to_game(paths): def dbpath_to_game(paths):
# Convert the Windows-style path from the database to a Unix-style path # Convert the base path to a Unix-style path
db_path = paths[0].replace("\\\\", "/").replace("C:", "") base_path = paths[0].replace("\\\\", "/").replace("C:", "")
linux_path = f"{logged_in_home}/.local/share/Steam/steamapps/compatdata/{itchio_launcher}/pfx/drive_c" + db_path linux_path = f"{logged_in_home}/.local/share/Steam/steamapps/compatdata/{itchio_launcher}/pfx/drive_c" + base_path
receipt_path = os.path.join(linux_path, ".itch", "receipt.json.gz") receipt_path = os.path.join(linux_path, ".itch", "receipt.json.gz")
if not os.path.exists(receipt_path): if not os.path.exists(receipt_path):
return None return None
for executable in paths[1]: for executable in paths[1:]:
exe_path = os.path.join(linux_path, executable) exe_path = os.path.join(linux_path, executable)
if os.access(exe_path, os.X_OK): # check if file is executable if os.access(exe_path, os.X_OK): # Check if executable
with gzip.open(receipt_path, 'rb') as f: with gzip.open(receipt_path, 'rb') as f:
receipt_str = f.read().decode() receipt_str = f.read().decode()
receipt = json.loads(receipt_str) receipt = json.loads(receipt_str)
return (linux_path, executable, receipt['game']['title']) return (linux_path, executable, receipt['game']['title'])
# Usage:
itch_db_location = f"{logged_in_home}/.local/share/Steam/steamapps/compatdata/{itchio_launcher}/pfx/drive_c/users/steamuser/AppData/Roaming/itch/db/butler.db-wal" # Usage
itch_db_location = f"{logged_in_home}/.local/share/Steam/steamapps/compatdata/{itchio_launcher}/pfx/drive_c/users/steamuser/AppData/Roaming/itch/db/butler.db"
print(f"Getting games from {itch_db_location}...") print(f"Getting games from {itch_db_location}...")
games = get_itch_games(itch_db_location) games = get_itch_games(itch_db_location)
print("Printing games...") print("Printing games...")
for game in games: for game in games:
print(game) print(game)
# For each game, prepare to create a new entry for launch
for game in games: for game in games:
linux_path, executable, game_title = game linux_path, executable, game_title = game
exe_path = f"\"{os.path.join(linux_path, executable)}\"" exe_path = f"\"{os.path.join(linux_path, executable)}\""
@ -1255,6 +1261,7 @@ for game in games:
launchoptions = f"STEAM_COMPAT_DATA_PATH=\"{logged_in_home}/.local/share/Steam/steamapps/compatdata/{itchio_launcher}/\" %command%" launchoptions = f"STEAM_COMPAT_DATA_PATH=\"{logged_in_home}/.local/share/Steam/steamapps/compatdata/{itchio_launcher}/\" %command%"
create_new_entry(exe_path, game_title, launchoptions, start_dir) create_new_entry(exe_path, game_title, launchoptions, start_dir)
# End of Itchio Scanner
#End of Itchio Scanner #End of Itchio Scanner