Add some utility functions to API

This commit is contained in:
NGnius (Graham) 2022-06-20 20:03:18 -04:00
parent d242cb9d70
commit 248c5837b5
16 changed files with 114 additions and 23 deletions

6
Cargo.lock generated
View file

@ -926,7 +926,7 @@ dependencies = [
[[package]] [[package]]
name = "usdpl-back" name = "usdpl-back"
version = "0.4.0" version = "0.5.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"tokio", "tokio",
@ -936,14 +936,14 @@ dependencies = [
[[package]] [[package]]
name = "usdpl-core" name = "usdpl-core"
version = "0.4.0" version = "0.5.0"
dependencies = [ dependencies = [
"base64", "base64",
] ]
[[package]] [[package]]
name = "usdpl-front" name = "usdpl-front"
version = "0.4.0" version = "0.5.0"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"js-sys", "js-sys",

View file

@ -12,6 +12,10 @@ readme = "README.md"
[profile.release] [profile.release]
# Tell `rustc` to optimize for small code size. # Tell `rustc` to optimize for small code size.
opt-level = "s" opt-level = "s"
debug = false
strip = true
lto = true
codegen-units = 4
[workspace] [workspace]
members = [ members = [

View file

@ -6,15 +6,16 @@
Universal Steam Deck Plugin Library Universal Steam Deck Plugin Library
A faster, lighter way to write plugins A faster, safer way to write plugin back-ends
### Goals ### Goals
- [x] Minimum viable plugin - [x] Minimum viable plugin
- [x] Call back-end API from front-end UI - [x] Call back-end API from front-end UI
- [x] External API documentation - [x] External API documentation
- [ ] Internal API documentation - [ ] Internal protocol documentation
- [x] Async support - [x] Async support
- [ ] Sync support - [ ] Encryption
- [ ] Plugin templates
- [ ] PluginLoader/Decky support - [ ] PluginLoader/Decky support
- [ ] Crankshaft support - [ ] Crankshaft support
- [ ] Unnamed plugin system support - [ ] Unnamed plugin system support

View file

@ -1,14 +1,15 @@
//! Universal Steam Deck Plugin Library //! Universal Steam Deck Plugin Library
//! //!
//! A faster, lighter way to write plugins //! A faster, safer way to write plugin back-ends
//! //!
//! ## Goals //! ## Goals
//! - [x] Minimum viable plugin //! - [x] Minimum viable plugin
//! - [x] Call back-end API from front-end UI //! - [x] Call back-end API from front-end UI
//! - [x] External API documentation //! - [x] External API documentation
//! - [ ] Internal API documentation //! - [ ] Internal protocol documentation
//! - [x] Async support //! - [x] Async support
//! - [ ] Sync support //! - [ ] Encryption
//! - [ ] Plugin templates
//! - [ ] PluginLoader/Decky support //! - [ ] PluginLoader/Decky support
//! - [ ] Crankshaft support //! - [ ] Crankshaft support
//! - [ ] Unnamed plugin system support //! - [ ] Unnamed plugin system support

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-back" name = "usdpl-back"
version = "0.4.0" version = "0.5.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs" repository = "https://github.com/NGnius/usdpl-rs"
@ -14,7 +14,7 @@ crankshaft = ["usdpl-core/crankshaft"]
blocking = ["tokio"] # synchronous API for async functionality, using tokio blocking = ["tokio"] # synchronous API for async functionality, using tokio
[dependencies] [dependencies]
usdpl-core = { version = "0.4.0", path = "../usdpl-core" } usdpl-core = { version = "0.5.0", path = "../usdpl-core" }
# HTTP web framework # HTTP web framework
warp = { version = "0.3" } warp = { version = "0.3" }

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,22 @@
//! Directories that may be hard to determine when running from the plugin framework's environment
use std::path::{Path, PathBuf};
use std::process::Command;
//use std::io;
/// The home directory of the user currently running the Steam Deck UI (specifically: running gamescope).
pub fn home() -> Option<PathBuf> {
let pid_out = Command::new("pidof")
.args(["gamescope"])
.output().ok()?;
let pid_out_str = String::from_utf8_lossy(pid_out.stdout.as_slice());
let pid_str = pid_out_str.split(" ").next()?;
//let pid: u32 = pid_str.parse().ok()?;
let user_info = Command::new("bash")
.args([format!("id `cat /proc/{}/loginuid`", pid_str)])
.output().ok()?;
let user_out_str = String::from_utf8_lossy(user_info.stdout.as_slice());
let user_str = user_out_str.split(")").next()?;
let user = &user_str[user_str.find("(")?+1..];
Some(Path::new("/home").join(user))
}

View file

@ -0,0 +1,22 @@
//! Common low-level file operations
use std::fmt::Display;
use std::path::Path;
use std::fs::File;
use std::io::{Read, Write, self};
use std::str::FromStr;
/// Write something to a file.
/// Useful for kernel configuration files.
pub fn write_single<P: AsRef<Path>, D: Display>(path: P, display: D) -> Result<(), io::Error> {
let mut file = File::create(path)?;
write!(file, "{}\n", display)
}
/// Read something from a file.
/// Useful for kernel configuration files.
pub fn read_single<P: AsRef<Path>, D: FromStr<Err=E>, E>(path: P) -> Result<D, (Option<io::Error>, Option<E>)> {
let mut file = File::create(path).map_err(|e| (Some(e), None))?;
let mut string = String::new();
file.read_to_string(&mut string).map_err(|e| (Some(e), None))?;
string.parse().map_err(|e| (None, Some(e)))
}

View file

@ -0,0 +1,2 @@
pub mod dirs;
pub mod files;

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@

View file

@ -5,6 +5,14 @@
//! //!
#![warn(missing_docs)] #![warn(missing_docs)]
#[cfg(not(any(feature = "decky", feature = "crankshaft")))]
mod api_any;
mod api_common;
#[cfg(all(feature = "crankshaft", not(any(feature = "decky"))))]
mod api_crankshaft;
#[cfg(all(feature = "decky", not(any(feature = "crankshaft"))))]
mod api_decky;
mod callable; mod callable;
//mod errors; //mod errors;
mod instance; mod instance;
@ -13,6 +21,18 @@ pub use callable::Callable;
pub use instance::Instance; pub use instance::Instance;
//pub use errors::{ServerError, ServerResult}; //pub use errors::{ServerError, ServerResult};
/// USDPL backend API.
/// This contains functionality used exclusively by the back-end.
pub mod api {
#[cfg(not(any(feature = "decky", feature = "crankshaft")))]
pub use super::api_any::*;
pub use super::api_common::*;
#[cfg(all(feature = "crankshaft", not(any(feature = "decky"))))]
pub use super::api_crankshaft::*;
#[cfg(all(feature = "decky", not(any(feature = "crankshaft"))))]
pub use super::api_decky::*;
}
/// usdpl-core re-export /// usdpl-core re-export
pub mod core { pub mod core {
pub use usdpl_core::*; pub use usdpl_core::*;

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-core" name = "usdpl-core"
version = "0.4.0" version = "0.5.0"
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs" repository = "https://github.com/NGnius/usdpl-rs"

View file

@ -90,12 +90,6 @@ impl Dumpable for Primitive {
} }
} }
impl std::convert::Into<Primitive> for String {
fn into(self) -> Primitive {
Primitive::String(self)
}
}
impl std::convert::Into<Primitive> for &str { impl std::convert::Into<Primitive> for &str {
fn into(self) -> Primitive { fn into(self) -> Primitive {
Primitive::String(self.to_string()) Primitive::String(self.to_string())
@ -108,6 +102,28 @@ impl std::convert::Into<Primitive> for () {
} }
} }
macro_rules! into_impl {
($type:ty, $variant:ident) => {
impl std::convert::Into<Primitive> for $type {
fn into(self) -> Primitive {
Primitive::$variant(self)
}
}
}
}
into_impl! {String, String}
into_impl! {bool, Bool}
into_impl! {u32, U32}
into_impl! {u64, U64}
into_impl! {i32, I32}
into_impl! {i64, I64}
into_impl! {f32, F32}
into_impl! {f64, F64}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-front" name = "usdpl-front"
version = "0.4.0" version = "0.5.0"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"] authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
@ -35,7 +35,7 @@ console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true } wee_alloc = { version = "0.4.5", optional = true }
web-sys = { version = "0.3", features = [ web-sys = { version = "0.3", features = [
'Headers', #'Headers',
'Request', 'Request',
'RequestInit', 'RequestInit',
'RequestMode', 'RequestMode',
@ -44,7 +44,7 @@ web-sys = { version = "0.3", features = [
]} ]}
js-sys = { version = "0.3" } js-sys = { version = "0.3" }
usdpl-core = { version = "0.4.0", path = "../usdpl-core" } usdpl-core = { version = "0.5.0", path = "../usdpl-core" }
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = { version = "0.3.13" } wasm-bindgen-test = { version = "0.3.13" }

View file

@ -18,7 +18,7 @@ pub async fn send_js(packet: socket::Packet, port: u16) -> Result<Vec<Primitive>
opts.method("POST"); opts.method("POST");
opts.mode(RequestMode::Cors); opts.mode(RequestMode::Cors);
let url = format!("http://localhost:{}/usdpl/call", port); let url = format!("http://{}:{}/usdpl/call", socket::HOST_STR, port);
let mut buffer = [0u8; socket::PACKET_BUFFER_SIZE]; let mut buffer = [0u8; socket::PACKET_BUFFER_SIZE];
let len = packet let len = packet
@ -29,7 +29,7 @@ pub async fn send_js(packet: socket::Packet, port: u16) -> Result<Vec<Primitive>
let request = Request::new_with_str_and_init(&url, &opts)?; let request = Request::new_with_str_and_init(&url, &opts)?;
request.headers().set("Accept", "application/bytes")?; //request.headers().set("Accept", "text/base64")?;
//.set("Authorization", "wasm TODO_KEY")?; //.set("Authorization", "wasm TODO_KEY")?;
let window = web_sys::window().unwrap(); let window = web_sys::window().unwrap();