diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index c5a3037..ce639d6 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -52,6 +52,6 @@ pub fn set_auto_login_user(user: &str) -> String { } #[tauri::command] -pub fn check_file_exists(file: &str) -> bool { - std::path::Path::new(&file).exists() +pub fn check_path(path: &str) -> bool { + std::path::Path::new(&path).exists() } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3873d56..2487fae 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -82,7 +82,7 @@ fn main() { cmds::kill_steam, cmds::get_steam_path, cmds::set_auto_login_user, - cmds::check_file_exists, + cmds::check_path, on_button_clicked ]) .run(ctx) diff --git a/src/components/cstb/Prepare.tsx b/src/components/cstb/Prepare.tsx index 32cc0c2..771c60e 100644 --- a/src/components/cstb/Prepare.tsx +++ b/src/components/cstb/Prepare.tsx @@ -1,8 +1,9 @@ -import { Button, Spinner } from "@heroui/react" +import { addToast, Button, Spinner } from "@heroui/react" import { useRouter } from "next/navigation" import { useEffect, useState } from "react" import { useSteamStore } from "@/store/steam" import { open } from "@tauri-apps/plugin-dialog" +import { invoke } from "@tauri-apps/api/core" export function Prepare() { const steam = useSteamStore() @@ -10,13 +11,17 @@ export function Prepare() { const [loading, setLoading] = useState(true) const [steamDir, setSteamDir] = useState(steam.state.dir) const [cs2Dir, setCs2Dir] = useState(steam.state.csDir) + const [inited, setInited] = useState(false) useEffect(() => { - const checkPaths = () => { - if (steam.state.dir && steam.state.csDir) { - // router.push("/home") + const checkPaths = async () => { + if ( + (await invoke("check_path", { path: steam.state.dir })) && + (await invoke("check_path", { path: steam.state.csDir })) + ) { + setInited(true) } - + setLoading(false) } checkPaths() @@ -25,10 +30,15 @@ export function Prepare() { const handleSelectSteamDir = async () => { const selected = await open({ title: "选择 Steam.exe 文件", - filters: [{ name: "Steam", extensions: ["exe"] }], + filters: [{ name: "steam", extensions: ["exe"] }], }) if (selected) { const dir = selected.replace(/\\[^\\]+$/, "") + const pathExist = await invoke("check_path", { path: dir }) + if (!pathExist) { + addToast({ title: "路径不存在", color: "warning" }) + return + } setSteamDir(dir) steam.setDir(dir) } @@ -37,9 +47,15 @@ export function Prepare() { const handleSelectCs2Dir = async () => { const selected = await open({ title: "选择 CS2.exe 文件", - filters: [{ name: "Cs2", extensions: ["exe"] }], + filters: [{ name: "cs2", extensions: ["exe"] }], }) if (selected) { + const dir = selected.replace(/\\[^\\]+$/, "") + const pathExist = await invoke("check_path", { path: dir }) + if (!pathExist) { + addToast({ title: "路径不存在", color: "warning" }) + return + } setCs2Dir(selected) steam.setCsDir(selected) } @@ -86,12 +102,7 @@ export function Prepare() {
-