Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help: allow launching the game with a one-off gk executable #570

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src-tauri/src/commands/binaries.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use std::{
collections::HashMap,
io::ErrorKind,
path::{Path, PathBuf},
process::Stdio,
str::FromStr,
time::Instant,
};
use tokio::{io::AsyncWriteExt, process::Command};
Expand Down Expand Up @@ -591,7 +594,7 @@ pub async fn open_repl(
let mut command;
#[cfg(windows)]
{
command = Command::new("cmd");
command = std::process::Command::new("cmd");
command
.args([
"/K",
Expand All @@ -605,14 +608,14 @@ pub async fn open_repl(
}
#[cfg(target_os = "linux")]
{
command = Command::new("xdg-terminal-exec");
command = std::process::Command::new("xdg-terminal-exec");
command
.args(["./goalc", "--proj-path", &data_folder.to_string_lossy()])
.current_dir(exec_info.executable_dir);
}
#[cfg(target_os = "macos")]
{
command = Command::new("osascript");
command = std::process::Command::new("osascript");
command
.args([
"-e",
Expand Down Expand Up @@ -794,18 +797,44 @@ pub async fn launch_game(
app_handle: tauri::AppHandle,
game_name: String,
in_debug: bool,
executable_location: Option<String>,
) -> Result<(), CommandError> {
let config_lock = config.lock().await;
let config_info = common_prelude(&config_lock)?;

let exec_info = get_exec_location(&config_info, "gk")?;
let mut exec_info = get_exec_location(&config_info, "gk")?;
if let Some(custom_exec_location) = executable_location {
match PathBuf::from_str(custom_exec_location.as_str()) {
Ok(exec_path) => {
let path_copy = exec_path.clone();
if path_copy.parent().is_none() {
return Err(CommandError::BinaryExecution(format!(
"Failed to resolve custom binary parent directory"
)));
}
exec_info = ExecutableLocation {
executable_dir: exec_path.clone().parent().unwrap().to_path_buf(),
executable_path: exec_path.clone(),
};
}
Err(err) => {
return Err(CommandError::BinaryExecution(format!(
"Failed to resolve custom binary location {}",
err
)));
}
}
}

let args = generate_launch_game_string(&config_info, game_name.clone(), in_debug, false)?;

log::info!(
"Launching game version {:?} -> {:?} with args: {:?}",
"Launching game version {:?} -> {:?} with args: {:?}. Working Directory: {:?}, Path: {:?}",
&config_info.active_version,
&config_info.tooling_version,
args
args,
exec_info.executable_dir,
exec_info.executable_path,
);

let log_file = create_std_log_file(&app_handle, format!("game-{game_name}.log"), false)?;
Expand Down
12 changes: 11 additions & 1 deletion src/components/games/GameControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
} from "flowbite-svelte";
import { resetGameSettings, uninstallGame } from "$lib/rpc/game";
import { platform } from "@tauri-apps/api/os";
import { getLaunchGameString, launchGame, openREPL } from "$lib/rpc/binaries";
import {
getLaunchGameString,
launchGame,
launchGameWithCustomExecutable,
openREPL,
} from "$lib/rpc/binaries";
import {
doesActiveToolingVersionMeetMinimum,
getInstallationDirectory,
Expand Down Expand Up @@ -165,6 +170,11 @@
launchGame(getInternalName(activeGame), true);
}}>{$_("gameControls_button_playInDebug")}</DropdownItem
>
<DropdownItem
on:click={async () => {
launchGameWithCustomExecutable(getInternalName(activeGame));
}}>Launch with Custom Executable</DropdownItem
>
<DropdownItem
on:click={async () => {
openREPL(getInternalName(activeGame));
Expand Down
18 changes: 17 additions & 1 deletion src/lib/rpc/binaries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { filePrompt, filePromptNoFilters } from "$lib/utils/file-dialogs";
import { invoke_rpc } from "./rpc";

interface InstallationOutput {
Expand Down Expand Up @@ -66,12 +67,27 @@ export async function launchGame(
): Promise<void> {
return await invoke_rpc(
"launch_game",
{ gameName, inDebug },
{ gameName, inDebug, executableLocation: null },
() => {},
"_mirror_",
);
}

export async function launchGameWithCustomExecutable(
gameName: string,
): Promise<void> {
// Get custom executable location
const customExecutable = await filePromptNoFilters("Select custom 'gk'");
if (customExecutable !== null) {
return await invoke_rpc(
"launch_game",
{ gameName, inDebug: false, executableLocation: customExecutable },
() => {},
"_mirror_",
);
}
}

export async function openREPL(gameName: string): Promise<void> {
return await invoke_rpc(
"open_repl",
Expand Down
17 changes: 17 additions & 0 deletions src/lib/utils/file-dialogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { open, save } from "@tauri-apps/api/dialog";

export async function filePromptNoFilters(
title: string,
): Promise<string | null> {
const path = await open({
title: title,
multiple: false,
directory: false,
filters: undefined,
});

if (Array.isArray(path) || path === null) {
return null;
}

return path;
}

export async function filePrompt(
extensions: string[],
name: string,
Expand Down
Loading