Improved game executable launcher, and added argument validation
Now uses proper shell escaping and parsing for all executable arguments, for more robust game launching. All existing game JSON files with the old string values will automatically be converted to the new format on app launch. The executable parsing uses the "shlex" library, which guarantees accurate parsing. We now also use direct process launching (without any intermediary shell) by default, but the old "shell"-based launch method still exists in the code via an alternative flag in `run_command.py` (if we ever need to restore it for some reason). Furthermore, if the user attempts to manually write an improperly escaped argument into the game's details (such as missing closing quotation marks), the GUI will now alert the user that their executable argument is invalid, along with telling them the exact reason why it's invalid.
This commit is contained in:
@@ -19,6 +19,9 @@
|
||||
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
|
||||
from .game_data_to_json import game_data_to_json
|
||||
|
||||
|
||||
def get_games(game_ids=None):
|
||||
@@ -39,8 +42,30 @@ def get_games(game_ids=None):
|
||||
game_files = os.listdir(games_dir)
|
||||
|
||||
for game in game_files:
|
||||
with open(os.path.join(games_dir, game), "r") as open_file:
|
||||
with open(os.path.join(games_dir, game), "r+") as open_file:
|
||||
data = json.loads(open_file.read())
|
||||
|
||||
# Convert any outdated JSON values to our newest data format.
|
||||
needs_rewrite = False
|
||||
if "executable" in data and isinstance(data["executable"], str):
|
||||
needs_rewrite = True
|
||||
try:
|
||||
# Use shell parsing to determine what the individual components are.
|
||||
executable_split = shlex.split(
|
||||
data["executable"], comments=False, posix=True
|
||||
)
|
||||
except:
|
||||
# Fallback: Split once at earliest space (1 part if no spaces, else 2 parts).
|
||||
executable_split = data["executable"].split(" ", 1)
|
||||
data["executable"] = executable_split
|
||||
|
||||
if needs_rewrite:
|
||||
open_file.seek(0)
|
||||
open_file.truncate()
|
||||
open_file.write(game_data_to_json(data))
|
||||
|
||||
open_file.close()
|
||||
games[data["game_id"]] = data
|
||||
|
||||
games[data["game_id"]] = data
|
||||
|
||||
return games
|
||||
|
||||
Reference in New Issue
Block a user