mirror of
https://github.com/SteamDeckHomebrew/decky-loader.git
synced 2026-06-17 08:47:49 +00:00
hot reload now refreshes iframe
also fixed fetch_nocors
This commit is contained in:
+15
-7
@@ -18,13 +18,13 @@ class FileChangeHandler(FileSystemEventHandler):
|
|||||||
src_path = event.src_path
|
src_path = event.src_path
|
||||||
if "__pycache__" in src_path:
|
if "__pycache__" in src_path:
|
||||||
return
|
return
|
||||||
self.loader.import_plugin(src_path)
|
self.loader.import_plugin(src_path, refresh=True)
|
||||||
|
|
||||||
def on_modified(self, event):
|
def on_modified(self, event):
|
||||||
src_path = event.src_path
|
src_path = event.src_path
|
||||||
if "__pycache__" in src_path:
|
if "__pycache__" in src_path:
|
||||||
return
|
return
|
||||||
self.loader.import_plugin(src_path)
|
self.loader.import_plugin(src_path, refresh=True)
|
||||||
|
|
||||||
class Loader:
|
class Loader:
|
||||||
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None:
|
def __init__(self, server_instance, plugin_path, loop, live_reload=False) -> None:
|
||||||
@@ -47,7 +47,7 @@ class Loader:
|
|||||||
web.get("/steam_resource/{path:.+}", self.get_steam_resource)
|
web.get("/steam_resource/{path:.+}", self.get_steam_resource)
|
||||||
])
|
])
|
||||||
|
|
||||||
def import_plugin(self, file):
|
def import_plugin(self, file, refresh=False):
|
||||||
try:
|
try:
|
||||||
spec = spec_from_file_location("_", file)
|
spec = spec_from_file_location("_", file)
|
||||||
module = module_from_spec(spec)
|
module = module_from_spec(spec)
|
||||||
@@ -69,15 +69,15 @@ class Loader:
|
|||||||
self.logger.info("Loaded {}".format(module.Plugin.name))
|
self.logger.info("Loaded {}".format(module.Plugin.name))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error("Could not load {}. {}".format(file, e))
|
self.logger.error("Could not load {}. {}".format(file, e))
|
||||||
|
finally:
|
||||||
|
if refresh:
|
||||||
|
self.loop.create_task(self.refresh_iframe())
|
||||||
|
|
||||||
def import_plugins(self):
|
def import_plugins(self):
|
||||||
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
|
files = [i for i in listdir(self.plugin_path) if i.endswith(".py")]
|
||||||
for file in files:
|
for file in files:
|
||||||
self.import_plugin(path.join(self.plugin_path, file))
|
self.import_plugin(path.join(self.plugin_path, file))
|
||||||
|
|
||||||
async def watch_for_file_change(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def reload_plugins(self, request=None):
|
async def reload_plugins(self, request=None):
|
||||||
self.logger.info("Re-importing plugins.")
|
self.logger.info("Re-importing plugins.")
|
||||||
self.import_plugins()
|
self.import_plugins()
|
||||||
@@ -89,7 +89,10 @@ class Loader:
|
|||||||
|
|
||||||
async def get_steam_resource(self, request):
|
async def get_steam_resource(self, request):
|
||||||
tab = (await get_tabs())[0]
|
tab = (await get_tabs())[0]
|
||||||
return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html")
|
try:
|
||||||
|
return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html")
|
||||||
|
except Exception as e:
|
||||||
|
return web.Response(text=str(e), status=400)
|
||||||
|
|
||||||
async def load_plugin(self, request):
|
async def load_plugin(self, request):
|
||||||
plugin = self.plugins[request.match_info["name"]]
|
plugin = self.plugins[request.match_info["name"]]
|
||||||
@@ -103,3 +106,8 @@ class Loader:
|
|||||||
@template('plugin_view.html')
|
@template('plugin_view.html')
|
||||||
async def plugin_iframe_route(self, request):
|
async def plugin_iframe_route(self, request):
|
||||||
return {"plugins": self.plugins.values()}
|
return {"plugins": self.plugins.values()}
|
||||||
|
|
||||||
|
async def refresh_iframe(self):
|
||||||
|
tab = next((i for i in await get_tabs() if i.title == "QuickAccess"), None)
|
||||||
|
await tab.open_websocket()
|
||||||
|
return await tab.evaluate_js("reloadIframe()")
|
||||||
@@ -2,7 +2,6 @@ class PluginEventTarget extends EventTarget { }
|
|||||||
method_call_ev_target = new PluginEventTarget();
|
method_call_ev_target = new PluginEventTarget();
|
||||||
|
|
||||||
window.addEventListener("message", function(evt) {
|
window.addEventListener("message", function(evt) {
|
||||||
console.log(evt);
|
|
||||||
let ev = new Event(evt.data.call_id);
|
let ev = new Event(evt.data.call_id);
|
||||||
ev.data = evt.data.result;
|
ev.data = evt.data.result;
|
||||||
method_call_ev_target.dispatchEvent(ev);
|
method_call_ev_target.dispatchEvent(ev);
|
||||||
@@ -27,6 +26,8 @@ async function fetch_nocors(url, request={}) {
|
|||||||
let args = { method: "POST", headers: {}, body: "" };
|
let args = { method: "POST", headers: {}, body: "" };
|
||||||
request = {...args, ...request};
|
request = {...args, ...request};
|
||||||
request.url = url;
|
request.url = url;
|
||||||
|
request.data = request.body;
|
||||||
|
delete request.body; //maintain api-compatibility with fetch
|
||||||
return await call_server_method("http_request", request);
|
return await call_server_method("http_request", request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,11 @@
|
|||||||
}, 100);
|
}, 100);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
function reloadIframe() {
|
||||||
|
console.log("reloading iframe");
|
||||||
|
document.getElementById("plugin_iframe").contentWindow.location.href = "http://127.0.0.1:1337/plugins/iframe";
|
||||||
|
}
|
||||||
|
|
||||||
function resolveMethodCall(call_id, result) {
|
function resolveMethodCall(call_id, result) {
|
||||||
let iframe = document.getElementById("plugin_iframe").contentWindow;
|
let iframe = document.getElementById("plugin_iframe").contentWindow;
|
||||||
iframe.postMessage({'call_id': call_id, 'result': result}, "http://127.0.0.1:1337");
|
iframe.postMessage({'call_id': call_id, 'result': result}, "http://127.0.0.1:1337");
|
||||||
|
|||||||
Reference in New Issue
Block a user