diff options
| -rw-r--r-- | main.py | 84 | ||||
| -rwxr-xr-x | src/index.tsx | 154 |
2 files changed, 72 insertions, 166 deletions
@@ -1,57 +1,45 @@ +import json import os - -# The decky plugin module is located at decky-loader/plugin -# For easy intellisense checkout the decky-loader code repo -# and add the `decky-loader/plugin/imports` path to `python.analysis.extraPaths` in `.vscode/settings.json` import decky import asyncio class Plugin: - # A normal method. It can be called from the TypeScript side using @decky/api. - async def add(self, left: int, right: int) -> int: - return left + right - - async def long_running(self): - await asyncio.sleep(15) - # Passing through a bunch of random data, just as an example - await decky.emit("timer_event", "Hello from the backend!", True, 2) + async def get_installed_games(self) -> str: + library_file = "/home/deck/.steam/steam/steamapps/libraryfolders.vdf" + libraries = [] + + # Parse libraryfolders.vdf + if os.path.exists(library_file): + with open(library_file, "r") as f: + lines = f.readlines() + for line in lines: + if '"path"' in line: + path = line.split('"')[3] + libraries.append(os.path.join(path, "steamapps")) + + # Fetch installed games from libraries + games = [] + for library in libraries: + if os.path.exists(library): + manifest_files = [f for f in os.listdir(library) if f.startswith("appmanifest_")] + for manifest in manifest_files: + with open(os.path.join(library, manifest), "r") as f: + lines = f.readlines() + appid = "" + name = "" + for line in lines: + if '"appid"' in line: + appid = line.split('"')[3] + elif '"name"' in line: + name = line.split('"')[3] + if appid and name: + games.append({"appid": appid, "name": name}) + + # Return games as JSON string for compatibility with TSX + return json.dumps(games) - # Asyncio-compatible long-running code, executed in a task when the plugin is loaded async def _main(self): - self.loop = asyncio.get_event_loop() - decky.logger.info("Hello World!") + decky.logger.info("Plugin loaded.") - # Function called first during the unload process, utilize this to handle your plugin being stopped, but not - # completely removed async def _unload(self): - decky.logger.info("Goodnight World!") - pass - - # Function called after `_unload` during uninstall, utilize this to clean up processes and other remnants of your - # plugin that may remain on the system - async def _uninstall(self): - decky.logger.info("Goodbye World!") - pass - - async def start_timer(self): - self.loop.create_task(self.long_running()) - - # Migrations that should be performed before entering `_main()`. - async def _migration(self): - decky.logger.info("Migrating") - # Here's a migration example for logs: - # - `~/.config/decky-template/template.log` will be migrated to `decky.decky_LOG_DIR/template.log` - decky.migrate_logs(os.path.join(decky.DECKY_USER_HOME, - ".config", "decky-template", "template.log")) - # Here's a migration example for settings: - # - `~/homebrew/settings/template.json` is migrated to `decky.decky_SETTINGS_DIR/template.json` - # - `~/.config/decky-template/` all files and directories under this root are migrated to `decky.decky_SETTINGS_DIR/` - decky.migrate_settings( - os.path.join(decky.DECKY_HOME, "settings", "template.json"), - os.path.join(decky.DECKY_USER_HOME, ".config", "decky-template")) - # Here's a migration example for runtime data: - # - `~/homebrew/template/` all files and directories under this root are migrated to `decky.decky_RUNTIME_DIR/` - # - `~/.local/share/decky-template/` all files and directories under this root are migrated to `decky.decky_RUNTIME_DIR/` - decky.migrate_runtime( - os.path.join(decky.DECKY_HOME, "template"), - os.path.join(decky.DECKY_USER_HOME, ".local", "share", "decky-template")) + decky.logger.info("Plugin unloaded.")
\ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 15fe792..e8db057 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,131 +1,49 @@ -import { - ButtonItem, - PanelSection, - PanelSectionRow, - Navigation, - staticClasses, - Dropdown, - DropdownOption -} from "@decky/ui"; -import { - addEventListener, - removeEventListener, - callable, - definePlugin, - toaster, - // routerHook -} from "@decky/api" -import { useState } from "react"; +import { useState, useEffect } from "react"; +import { PanelSection, PanelSectionRow, Dropdown, DropdownOption } from "@decky/ui"; +import { callable, definePlugin } from "@decky/api"; import { FaShip } from "react-icons/fa"; -// import logo from "../assets/logo.png"; - -// This function calls the python function "add", which takes in two numbers and returns their sum (as a number) -// Note the type annotations: -// the first one: [first: number, second: number] is for the arguments -// the second one: number is for the return value -const add = callable<[first: number, second: number], number>("add"); - -// This function calls the python function "start_timer", which takes in no arguments and returns nothing. -// It starts a (python) timer which eventually emits the event 'timer_event' -const startTimer = callable<[], void>("start_timer"); - -const dropdownOptions: DropdownOption[] = [ - { label: "1", data: 1 }, - { label: "2", data: 2 }, - { label: "3", data: 3 }, -]; +const fetchInstalledGames = callable<[], string>("get_installed_games"); function Content() { - const [result, setResult] = useState<number | undefined>(); - const [selectedOption, setSelectedOption] = useState<number | undefined>(); + const [games, setGames] = useState<DropdownOption[]>([]); + const [selectedGame, setSelectedGame] = useState<DropdownOption | null>(null); + + useEffect(() => { + const loadGames = async () => { + const result = await fetchInstalledGames(); + const gameList = JSON.parse(result) as { appid: string; name: string }[]; + setGames(gameList.map(game => ({ data: game.appid, label: game.name }))); + }; - const onClick = async () => { - const result = await add(Math.random(), Math.random()); - setResult(result); - }; + loadGames(); + }, []); return ( - <PanelSection title="Panel Section"> - <PanelSectionRow> - <ButtonItem - layout="below" - onClick={onClick} - > - {result ?? "Add two numbers via Python"} - </ButtonItem> - </PanelSectionRow> - <PanelSectionRow> - <ButtonItem - layout="below" - onClick={() => startTimer()} - > - {"Start Python timer"} - </ButtonItem> - </PanelSectionRow> + <PanelSection title="Installed Games"> <PanelSectionRow> <Dropdown - rgOptions={dropdownOptions} - selectedOption={selectedOption} - onChange={(option) => setSelectedOption(option.data)} + rgOptions={games} + selectedOption={selectedGame?.data || null} + onChange={(option) => setSelectedGame(option)} + strDefaultLabel="Select a game" // Placeholder equivalent /> </PanelSectionRow> - - {/* <PanelSectionRow> - <div style={{ display: "flex", justifyContent: "center" }}> - <img src={logo} /> - </div> - </PanelSectionRow> */} - - {/*<PanelSectionRow> - <ButtonItem - layout="below" - onClick={() => { - Navigation.Navigate("/decky-plugin-test"); - Navigation.CloseSideMenus(); - }} - > - Router - </ButtonItem> - </PanelSectionRow>*/} + {selectedGame && ( + <PanelSectionRow> + <div>You selected: {selectedGame.label}</div> + </PanelSectionRow> + )} </PanelSection> ); -}; - -export default definePlugin(() => { - console.log("Template plugin initializing, this is called once on frontend startup") - - // serverApi.routerHook.addRoute("/decky-plugin-test", DeckyPluginRouterTest, { - // exact: true, - // }); - - // Add an event listener to the "timer_event" event from the backend - const listener = addEventListener<[ - test1: string, - test2: boolean, - test3: number - ]>("timer_event", (test1, test2, test3) => { - console.log("Template got timer_event with:", test1, test2, test3) - toaster.toast({ - title: "template got timer_event", - body: `${test1}, ${test2}, ${test3}` - }); - }); - - return { - // The name shown in various decky menus - name: "Test Plugin", - // The element displayed at the top of your plugin's menu - titleView: <div className={staticClasses.Title}>Decky Example Plugin</div>, - // The content of your plugin's menu - content: <Content />, - // The icon displayed in the plugin list - icon: <FaShip />, - // The function triggered when your plugin unloads - onDismount() { - console.log("Unloading") - removeEventListener("timer_event", listener); - // serverApi.routerHook.removeRoute("/decky-plugin-test"); - }, - }; -}); +} + +export default definePlugin(() => ({ + name: "Game Selector Plugin", + titleView: <div>Game Selector Plugin</div>, + content: <Content />, + icon: <FaShip />, + onDismount() { + console.log("Plugin unmounted"); + }, +}));
\ No newline at end of file |
