Skip to content

Commit

Permalink
use "proper" args argparsing and clean up uri handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysintreble committed Jul 29, 2024
1 parent aea33ae commit 910964d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
4 changes: 2 additions & 2 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ def get_base_parser(description: typing.Optional[str] = None):
return parser


def run_as_textclient():
def run_as_textclient(*args):
class TextContext(CommonContext):
# Text Mode to use !hint and such with games that have no text entry
tags = CommonContext.tags | {"TextOnly"}
Expand Down Expand Up @@ -1033,7 +1033,7 @@ async def main(args):
parser = get_base_parser(description="Gameless Archipelago Client, for text interfacing.")
parser.add_argument('--name', default=None, help="Slot Name to connect as.")
parser.add_argument("url", nargs="?", help="Archipelago connection url")
args = parser.parse_args()
args = parser.parse_args(args)

if args.url:
url = urllib.parse.urlparse(args.url)
Expand Down
32 changes: 13 additions & 19 deletions Launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def update_settings():
])


def handle_uri(path: str) -> Tuple[Union[None, str], Union[None, Component]]:
def handle_uri(path: str, launch_args: Tuple[str, ...]) -> None:
url = urllib.parse.urlparse(path)
queries = urllib.parse.parse_qs(url.query)
client_component = None
Expand Down Expand Up @@ -138,20 +138,14 @@ def build(self):

text_client_button = Button(
text=text_client_component.display_name,
on_release=lambda *args: launch(get_exe(text_client_component), True)
on_release=lambda *args: run_component(text_client_component, *launch_args)
)
button_row.add_widget(text_client_button)

if client_component is not None:
def launch_component(*args) -> None:
if client_component.func:
client_component.func()
else:
launch(get_exe(client_component), True)

game_client_button = Button(
text=client_component.display_name,
on_release=launch_component
on_release=lambda *args: run_component(client_component, *launch_args)
)
button_row.add_widget(game_client_button)

Expand All @@ -161,15 +155,10 @@ def launch_component(*args) -> None:

Popup().run()

# prevents launcher from trying to start up its gui
return path, None


def identify(path: Union[None, str]) -> Tuple[Union[None, str], Union[None, Component]]:
if path is None:
return None, None
if path.startswith("archipelago://"):
return handle_uri(path)
for component in components:
if component.handles_file(path):
return path, component
Expand Down Expand Up @@ -359,14 +348,18 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None):
elif not args:
args = {}

if args.get("Patch|Game|Component", None) is not None:
file, component = identify(args["Patch|Game|Component"])
path = args.get("Patch|Game|Component|url", None)
if path is not None:
if path.startswith("archipelago://"):
handle_uri(path, args.get("args", ()))
return
file, component = identify(path)
if file:
args['file'] = file
if component:
args['component'] = component
if not component:
logging.warning(f"Could not identify Component responsible for {args['Patch|Game|Component']}")
logging.warning(f"Could not identify Component responsible for {path}")

if args["update_settings"]:
update_settings()
Expand All @@ -386,8 +379,9 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None):
run_group = parser.add_argument_group("Run")
run_group.add_argument("--update_settings", action="store_true",
help="Update host.yaml and exit.")
run_group.add_argument("Patch|Game|Component", type=str, nargs="?",
help="Pass either a patch file, a generated game or the name of a component to run.")
run_group.add_argument("Patch|Game|Component|url", type=str, nargs="?",
help="Pass either a patch file, a generated game, the component name to run, or a url to "
"connect with.")
run_group.add_argument("args", nargs="*",
help="Arguments to pass to component.")
main(parser.parse_args())
Expand Down
8 changes: 4 additions & 4 deletions worlds/LauncherComponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def __repr__(self):
processes = weakref.WeakSet()


def launch_subprocess(func: Callable, name: str = None):
def launch_subprocess(func: Callable, name: str = None, args: Tuple[str, ...] = ()):
global processes
import multiprocessing
process = multiprocessing.Process(target=func, name=name)
process = multiprocessing.Process(target=func, name=name, args=args)
process.start()
processes.add(process)

Expand All @@ -83,9 +83,9 @@ def __call__(self, path: str) -> bool:
return False


def launch_textclient():
def launch_textclient(*args):
import CommonClient
launch_subprocess(CommonClient.run_as_textclient, "TextClient")
launch_subprocess(CommonClient.run_as_textclient, "TextClient", args)


def _install_apworld(apworld_src: str = "") -> Optional[Tuple[pathlib.Path, pathlib.Path]]:
Expand Down
4 changes: 2 additions & 2 deletions worlds/messenger/client_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
MOD_URL = "https://api.github.com/repos/alwaysintreble/TheMessengerRandomizerModAP/releases/latest"


def launch_game() -> None:
def launch_game(*args) -> None:
"""Check the game installation, then launch it"""
def courier_installed() -> bool:
"""Check if Courier is installed"""
Expand Down Expand Up @@ -154,7 +154,7 @@ def available_mod_update(latest_version: str) -> bool:

parser = argparse.ArgumentParser(description="Messenger Client Launcher")
parser.add_argument("url", type=str, nargs="?", help="Archipelago Webhost uri to auto connect to.")
args = parser.parse_args()
args = parser.parse_args(args)
if not is_windows:
if args.url:
open_file(f"steam://rungameid/764790//{args.url}/")
Expand Down

0 comments on commit 910964d

Please sign in to comment.