Remove HTTP server due to plugin store requirements
This commit is contained in:
parent
c1d80a32fa
commit
137d8454a8
4 changed files with 48 additions and 115 deletions
5
main.py
5
main.py
|
@ -5,7 +5,7 @@ import asyncio
|
|||
import pathlib
|
||||
import subprocess
|
||||
|
||||
VERSION = "0.7.0-indev"
|
||||
VERSION = "0.7.0-indev2"
|
||||
HOME_DIR = "/home/deck"
|
||||
DEFAULT_SETTINGS_LOCATION = HOME_DIR + "/.config/powertools/default_settings.json"
|
||||
LOG_LOCATION = "/tmp/powertools.log"
|
||||
|
@ -466,7 +466,8 @@ class Plugin:
|
|||
self.current_gameid = None
|
||||
|
||||
async def get_per_game_profile(self) -> bool:
|
||||
return self.current_gameid is not None and current_game.has_settings()
|
||||
current_game = pt_server.http_server.game()
|
||||
return current_game is not None and current_game.has_settings()
|
||||
|
||||
async def on_game_start(self, game_id: int, data) -> bool:
|
||||
pt_server.http_server.set_game(game_id, data)
|
||||
|
|
81
server.py
81
server.py
|
@ -42,19 +42,12 @@ class GameInfo:
|
|||
return os.path.exists(self.settings_path())
|
||||
|
||||
|
||||
class Server(web.Application):
|
||||
class Server:
|
||||
|
||||
def __init__(self, version):
|
||||
super().__init__()
|
||||
self.version = version
|
||||
self.current_game = None
|
||||
self.add_routes([
|
||||
web.get("/", lambda req: self.index(req)),
|
||||
web.post("/on_game_start/{game_id}", lambda req: self.on_game_start(req)),
|
||||
web.post("/on_game_exit/{game_id}", lambda req: self.on_game_exit(req)),
|
||||
web.post("/on_game_exit_null", lambda req: self.on_game_exit_null(req)),
|
||||
web.get("/self_destruct", lambda req: self.self_destruct(req))
|
||||
])
|
||||
logging.debug("Server init complete")
|
||||
|
||||
def game(self) -> GameInfo:
|
||||
|
@ -69,78 +62,10 @@ class Server(web.Application):
|
|||
if game_id is None or self.current_game.gameid == game_id:
|
||||
self.current_game = None
|
||||
|
||||
async def index(self, request):
|
||||
logging.debug("Debug index page accessed")
|
||||
current_game = None if self.current_game is None else self.current_game.gameid
|
||||
game_info = None if self.current_game is None else self.current_game.game_info
|
||||
settings_info = None if self.current_game is None else self.current_game.load_settings()
|
||||
return web.json_response({
|
||||
"name": "PowerTools",
|
||||
"version": self.version,
|
||||
"latest_game_id": current_game,
|
||||
"game_info": game_info,
|
||||
"settings": settings_info
|
||||
}, headers={"Access-Control-Allow-Origin": "*"})
|
||||
|
||||
async def on_game_start(self, request):
|
||||
game_id = request.match_info["game_id"]
|
||||
data = await request.text()
|
||||
logging.debug(f"on_game_start {game_id} body:\n{data}")
|
||||
try:
|
||||
game_id = int(game_id)
|
||||
data = json.loads(data)
|
||||
except:
|
||||
return web.Response(text="WTF", status=400)
|
||||
self.current_game = GameInfo(game_id, data)
|
||||
if self.current_game.has_settings():
|
||||
self.last_recognised_game = self.current_game
|
||||
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||
|
||||
async def on_game_exit(self, request):
|
||||
# ignored for now
|
||||
game_id = request.match_info["game_id"]
|
||||
data = await request.text()
|
||||
logging.debug(f"on_game_exit {game_id}")
|
||||
try:
|
||||
game_id = int(game_id)
|
||||
except ValueError:
|
||||
return web.Response(text="WTF", status=400)
|
||||
if self.current_game.gameid == game_id:
|
||||
self.current_game = None
|
||||
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||
|
||||
async def on_game_exit_null(self, request):
|
||||
# ignored for now
|
||||
logging.info(f"on_game_exit_null")
|
||||
self.current_game = None
|
||||
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||
|
||||
async def self_destruct(self, request):
|
||||
logging.warning("Geodude self-destructed")
|
||||
await shutdown()
|
||||
# unreachable \/ \/ \/
|
||||
return web.Response(status=204, headers={"Access-Control-Allow-Origin": "*"})
|
||||
|
||||
async def start(version):
|
||||
global http_runner, http_server, http_site
|
||||
# make sure old server has shutdown
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get('http://127.0.0.1:5030/self_destruct') as response:
|
||||
await response.text()
|
||||
except:
|
||||
pass
|
||||
global http_server
|
||||
http_server = Server(version)
|
||||
http_runner = web.AppRunner(http_server)
|
||||
await http_runner.setup()
|
||||
http_site = web.TCPSite(http_runner, '127.0.0.1', 5030)
|
||||
await http_site.start()
|
||||
|
||||
async def shutdown(): # never really called
|
||||
global http_runner, http_server, http_site
|
||||
if http_runner is not None:
|
||||
await http_runner.cleanup()
|
||||
http_runner = None
|
||||
http_site.stop()
|
||||
http_site = None
|
||||
global http_server
|
||||
http_server = None
|
||||
|
|
|
@ -70,7 +70,7 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => {
|
|||
|
||||
const [persistGlobal, setPersist] = useState<boolean>(false);
|
||||
const [perGameProfileGlobal, setPerGameProfile] = useState<boolean>(false);
|
||||
const [gameGlobal, setGame] = useState<string>("with your mom");
|
||||
const [gameGlobal, setGame] = useState<string>(lastGame);
|
||||
|
||||
reload = function () {
|
||||
python.execute(python.onViewReady());
|
||||
|
@ -85,7 +85,21 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => {
|
|||
|
||||
python.resolve(python.getPersistent(), setPersist);
|
||||
python.resolve(python.getPerGameProfile(), setPerGameProfile);
|
||||
};
|
||||
};
|
||||
|
||||
if (periodicHook == null) {
|
||||
periodicHook = setInterval(function() {
|
||||
python.resolve(python.getCurrentGame(), (game: string) => {
|
||||
python.resolve(python.getChargeNow(), setChargeNow);
|
||||
if (lastGame != game) {
|
||||
setGame(game);
|
||||
lastGame = game;
|
||||
reload();
|
||||
}
|
||||
python.resolve(python.getChargeFull(), setChargeFull);
|
||||
});
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
|
||||
if (firstTime) {
|
||||
|
@ -99,35 +113,7 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({serverAPI}) => {
|
|||
|
||||
python.resolve(python.getCurrentGame(), setGame);
|
||||
|
||||
periodicHook = setInterval(function() {
|
||||
python.resolve(python.getChargeNow(), setChargeNow);
|
||||
python.resolve(python.getChargeFull(), setChargeFull);
|
||||
python.resolve(python.getCurrentGame(), (game: string) => {
|
||||
if (lastGame != game) {
|
||||
setGame(game);
|
||||
lastGame = game;
|
||||
reload();
|
||||
}
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
python.resolve(python.getVersion(), (v: string) => {versionGlobal = v;});
|
||||
|
||||
//@ts-ignore
|
||||
lifetimeHook = SteamClient.GameSessions.RegisterForAppLifetimeNotifications((update) => {
|
||||
if (update.bRunning) {
|
||||
console.log("AppID " + update.unAppID.toString() + " is now running");
|
||||
} else {
|
||||
console.log("AppID " + update.unAppID.toString() + " is no longer running");
|
||||
python.execute(python.onGameStop(null));
|
||||
}
|
||||
});
|
||||
//@ts-ignore
|
||||
SteamClient.Apps.RegisterForGameActionStart((actionType, id) => {
|
||||
//@ts-ignore
|
||||
let gameInfo: any = appStore.GetAppOverviewByGameID(id);
|
||||
python.execute(python.onGameStart(id, gameInfo));
|
||||
});
|
||||
}
|
||||
|
||||
const FieldWithSeparator = joinClassNames(gamepadDialogClasses.Field, gamepadDialogClasses.WithBottomSeparatorStandard);
|
||||
|
@ -353,6 +339,24 @@ export default definePlugin((serverApi: ServerAPI) => {
|
|||
exact: true,
|
||||
});
|
||||
|
||||
//@ts-ignore
|
||||
lifetimeHook = SteamClient.GameSessions.RegisterForAppLifetimeNotifications((update) => {
|
||||
if (update.bRunning) {
|
||||
console.log("AppID " + update.unAppID.toString() + " is now running");
|
||||
} else {
|
||||
console.log("AppID " + update.unAppID.toString() + " is no longer running");
|
||||
python.execute(python.onGameStop(null));
|
||||
}
|
||||
});
|
||||
//@ts-ignore
|
||||
startHook = SteamClient.Apps.RegisterForGameActionStart((actionType, id) => {
|
||||
//@ts-ignore
|
||||
let gameInfo: any = appStore.GetAppOverviewByGameID(id);
|
||||
python.execute(python.onGameStart(id, gameInfo));
|
||||
});
|
||||
|
||||
console.log("Registered PowerTools callbacks, hello!");
|
||||
|
||||
return {
|
||||
title: <div className={staticClasses.Title}>PowerTools</div>,
|
||||
content: <Content serverAPI={serverApi} />,
|
||||
|
@ -360,9 +364,12 @@ export default definePlugin((serverApi: ServerAPI) => {
|
|||
onDismount() {
|
||||
console.log("PowerTools shutting down");
|
||||
clearInterval(periodicHook!);
|
||||
lifetimeHook.unregister();
|
||||
startHook.unregister();
|
||||
lifetimeHook!.unregister();
|
||||
startHook!.unregister();
|
||||
serverApi.routerHook.removeRoute("/decky-plugin-test");
|
||||
firstTime = true;
|
||||
lastGame = "";
|
||||
console.log("Unregistered PowerTools callbacks, goodbye.");
|
||||
},
|
||||
};
|
||||
});
|
||||
|
|
|
@ -7,11 +7,11 @@ var server: ServerAPI | undefined = undefined;
|
|||
export function resolve(promise: Promise<any>, setter: any) {
|
||||
(async function () {
|
||||
let data = await promise;
|
||||
console.log("Got resolved", data);
|
||||
console.debug("Got resolved", data);
|
||||
if (data.success) {
|
||||
setter(data.result);
|
||||
} else {
|
||||
console.log("Resolve failed:", data);
|
||||
console.warn("Resolve failed:", data);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ export function resolve(promise: Promise<any>, setter: any) {
|
|||
export function execute(promise: Promise<any>) {
|
||||
(async function () {
|
||||
let data = await promise;
|
||||
console.log("Got executed", data);
|
||||
console.debug("Got executed", data);
|
||||
})();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue