2024-11-11 10:04:00 +08:00
|
|
|
import type { SteamUser } from "@/types/steam"
|
2025-03-20 01:46:54 +08:00
|
|
|
import { store } from "@tauri-store/valtio"
|
|
|
|
|
import { DEFAULT_STORE_CONFIG } from "./config"
|
2025-03-17 11:48:30 +08:00
|
|
|
import { useSnapshot } from "valtio"
|
2025-03-21 02:42:49 +08:00
|
|
|
import { invoke } from "@tauri-apps/api/core"
|
2025-03-21 18:00:16 +08:00
|
|
|
import path from "path"
|
2024-09-21 02:25:23 +08:00
|
|
|
|
2025-03-12 11:22:32 +08:00
|
|
|
const defaultValue = {
|
2025-03-21 02:42:49 +08:00
|
|
|
steamDir: "C:\\Program Files (x86)\\Steam",
|
|
|
|
|
cs2Dir: "",
|
2025-03-21 02:52:09 +08:00
|
|
|
steamDirValid: false,
|
|
|
|
|
cs2DirValid: false,
|
|
|
|
|
steamDirChecking: false,
|
|
|
|
|
cs2DirChecking: false,
|
2025-03-12 22:20:06 +08:00
|
|
|
users: [
|
|
|
|
|
{
|
|
|
|
|
steamID64: "76561198052315353",
|
|
|
|
|
steamID32: "STEAM_0:0:46157676",
|
|
|
|
|
accountName: "wrr",
|
|
|
|
|
personaName: "wrr",
|
|
|
|
|
recent: 0,
|
|
|
|
|
avatar: "",
|
|
|
|
|
},
|
|
|
|
|
] as SteamUser[],
|
2024-09-21 02:25:23 +08:00
|
|
|
}
|
2025-03-17 11:48:30 +08:00
|
|
|
|
2025-03-12 22:20:06 +08:00
|
|
|
export const steamStore = store(
|
|
|
|
|
"steam",
|
|
|
|
|
{ ...defaultValue },
|
|
|
|
|
DEFAULT_STORE_CONFIG,
|
|
|
|
|
)
|
2024-09-21 02:25:23 +08:00
|
|
|
|
2025-03-17 11:48:30 +08:00
|
|
|
export const useSteamStore = () => {
|
|
|
|
|
void steamStore.start
|
|
|
|
|
const state = useSnapshot(steamStore.state)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
state,
|
2025-03-21 15:06:41 +08:00
|
|
|
store: steamStore,
|
2025-03-21 18:00:16 +08:00
|
|
|
cs2BaseDir,
|
2025-03-17 11:48:30 +08:00
|
|
|
setDir,
|
|
|
|
|
setCsDir,
|
|
|
|
|
setUsers,
|
2025-03-21 02:42:49 +08:00
|
|
|
setSteamDirValid,
|
|
|
|
|
setCs2DirValid,
|
|
|
|
|
checkSteamDirValid,
|
|
|
|
|
checkCs2DirValid,
|
2025-03-17 11:48:30 +08:00
|
|
|
currentUser,
|
|
|
|
|
resetSteamStore,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-21 18:00:16 +08:00
|
|
|
const cs2BaseDir = () => {
|
|
|
|
|
return path.normalize(steamStore.state.cs2Dir.replaceAll("\\", "/") + "/../../..")
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 11:48:30 +08:00
|
|
|
const setDir = (dir: string) => {
|
2025-03-21 02:42:49 +08:00
|
|
|
steamStore.state.steamDir = dir
|
2025-03-12 22:20:06 +08:00
|
|
|
}
|
2025-03-17 11:48:30 +08:00
|
|
|
const setCsDir = (dir: string) => {
|
2025-03-21 02:42:49 +08:00
|
|
|
steamStore.state.cs2Dir = dir
|
2025-03-12 22:20:06 +08:00
|
|
|
}
|
2025-03-17 11:48:30 +08:00
|
|
|
const setUsers = (users: SteamUser[]) => {
|
2025-03-12 22:20:06 +08:00
|
|
|
steamStore.state.users = users
|
|
|
|
|
}
|
2025-03-21 02:42:49 +08:00
|
|
|
const setSteamDirValid = (valid: boolean) => {
|
|
|
|
|
steamStore.state.steamDirValid = valid
|
2025-03-12 22:20:06 +08:00
|
|
|
}
|
2025-03-21 02:42:49 +08:00
|
|
|
const setCs2DirValid = (valid: boolean) => {
|
|
|
|
|
steamStore.state.cs2DirValid = valid
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-21 02:52:09 +08:00
|
|
|
const SetSteamDirChecking = (checking: boolean) => {
|
|
|
|
|
steamStore.state.steamDirChecking = checking
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SetCs2DirChecking = (checking: boolean) => {
|
|
|
|
|
steamStore.state.cs2DirChecking = checking
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-21 02:42:49 +08:00
|
|
|
const checkSteamDirValid = async () => {
|
2025-03-21 02:52:09 +08:00
|
|
|
SetSteamDirChecking(true)
|
2025-03-21 02:42:49 +08:00
|
|
|
const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.steamDir })
|
|
|
|
|
setSteamDirValid(pathExist)
|
2025-03-21 02:52:09 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
SetSteamDirChecking(false)
|
|
|
|
|
}, 500)
|
2025-03-21 02:42:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const checkCs2DirValid = async () => {
|
2025-03-21 02:52:09 +08:00
|
|
|
SetCs2DirChecking(true)
|
2025-03-21 02:42:49 +08:00
|
|
|
const pathExist = await invoke<boolean>("check_path", { path: steamStore.state.cs2Dir })
|
|
|
|
|
setCs2DirValid(pathExist)
|
2025-03-21 02:52:09 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
SetCs2DirChecking(false)
|
|
|
|
|
}, 500)
|
2025-03-12 22:20:06 +08:00
|
|
|
}
|
2024-09-21 02:25:23 +08:00
|
|
|
|
2025-03-17 11:48:30 +08:00
|
|
|
const currentUser = () => {
|
2025-03-12 11:22:32 +08:00
|
|
|
return steamStore.state.users[0] || defaultValue.users[0]
|
2024-09-27 10:38:40 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-17 11:48:30 +08:00
|
|
|
const resetSteamStore = () => {
|
2025-03-21 02:42:49 +08:00
|
|
|
setDir(defaultValue.steamDir)
|
|
|
|
|
setCsDir(defaultValue.cs2Dir)
|
2025-03-12 11:22:32 +08:00
|
|
|
setUsers(defaultValue.users)
|
2025-03-21 02:42:49 +08:00
|
|
|
setSteamDirValid(defaultValue.steamDirValid)
|
|
|
|
|
setCs2DirValid(defaultValue.cs2DirValid)
|
2025-03-21 02:52:09 +08:00
|
|
|
SetSteamDirChecking(defaultValue.steamDirChecking)
|
|
|
|
|
SetCs2DirChecking(defaultValue.cs2DirChecking)
|
2025-03-21 02:42:49 +08:00
|
|
|
}
|