Skip to content
This repository has been archived by the owner on Jan 11, 2025. It is now read-only.

Elytra Plugin API #24

Draft
wants to merge 18 commits into
base: mcs/devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion denji/examples/install-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::fs::create_dir;
use std::path::PathBuf;
use std::sync::mpsc::channel;
// use std::sync::Arc;
use std::env::consts;
use std::thread::spawn;
// use std::time::Duration;

Expand Down Expand Up @@ -89,7 +90,11 @@ impl ServerParameters for ForgeServer {
}

fn run_args(&self) -> String {
format!("java -jar libraries/net/minecraftforge/forge/{0}/forge-{0}-server.jar @usr_jvm_args.txt nogui \"$@\"", self.artifact_version())
format!(
"java @user_jvm_args.txt @libraries/net/minecraftforge/forge/{}/{}_args.txt nogui \"$@\"",
self.artifact_version(),
consts::FAMILY
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion denji/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub enum DenjiError {
#[error("http error:{0}")]
Http(#[from] reqwest::Error),

#[error("installer returned code {0}")]
#[error("Installer returned code {0}")]
Installer(i32),

#[error("while writing run script: {0}")]
Expand Down
54 changes: 27 additions & 27 deletions dms-backend/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,33 @@ async fn build_server(
"Building server {} for {} {}",
params.name, params.server, params.server_version
);
let (tx, rx) = std::sync::mpsc::channel::<String>();

info!("Spawning read thread");
std::thread::spawn(move || loop {
match rx.recv_timeout(std::time::Duration::from_secs(90)) {
Ok(line) => info!("{}", line),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
error!("Timeout reached while awaiting message");
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
}
});

let server_build = denji::MinecraftServer::new(
params.server.parse::<denji::ServerSoftware>().unwrap(),
&params.server_version,
&params.server_version,
"target/servers/dummy/",
);

tokio::task::spawn(async move {
if let Err(e) = server_build.build_server(tx).await {
error!("An error occurred while building the server: {:?}", e);
}
})
.await
.expect("expected this task to not return any errors");
// let (tx, rx) = std::sync::mpsc::channel::<String>();

// info!("Spawning read thread");
// std::thread::spawn(move || loop {
// match rx.recv_timeout(std::time::Duration::from_secs(90)) {
// Ok(line) => info!("{}", line),
// Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
// error!("Timeout reached while awaiting message");
// }
// Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
// }
// });

// let server_build = denji::MinecraftServer::new(
// params.server.parse::<denji::ServerSoftware>().unwrap(),
// &params.server_version,
// &params.server_version,
// "target/servers/dummy/",
// );

// tokio::task::spawn(async move {
// if let Err(e) = server_build.build_server(tx).await {
// error!("An error occurred while building the server: {:?}", e);
// }
// })
// .await
// .expect("expected this task to not return any errors");

// context.servers.push(MinecraftServer);

Expand Down
28 changes: 20 additions & 8 deletions elytra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
serde.workspace = true
thiserror.workspace = true
log.workspace = true
tokio.workspace = true
serde = { workspace = true, optional = true }
thiserror = { workspace = true, optional = true }
log = { workspace = true, optional = true }

reqwest = { workspace = true, optional = true }
zip = { workspace = true, optional = true }
Expand All @@ -16,18 +15,30 @@ chrono = { version = "0.4.38", default-features = false, features = [
"std",
"serde",
], optional = true }
libloading = { version = "0.8.6", optional = true }

mar = { path = "../mar", default-features = false, optional = true }

[features]
default = ["providers-modrinth", "utils-modpack", "utils-mod"]
default = ["providers-modrinth", "manifest"]

providers-base = ["dep:reqwest"]
providers-hangar = ["providers-base", "dep:bitflags", "dep:chrono"]
providers-modrinth = ["providers-base"]

utils-base = ["dep:zip"]
utils-modpack = ["utils-base"]
utils-mod = ["utils-base"]
utils-base = []
utils-modpack = ["utils-base", "dep:zip"]
utils-mod = ["utils-base", "dep:zip"]
utils-plugin = [
"utils-base",
"dep:libloading",
"dep:thiserror",
"dep:log",
"plugin",
]

manifest = ["dep:serde", "dep:log", "dep:thiserror"]
plugin = ["mar/type-maven-artifact"]

[lints]
workspace = true
Expand All @@ -37,3 +48,4 @@ env_logger = "0.11.5"
toml = "0.8.19"
serde_urlencoded = "0.7.1"
serde_json.workspace = true
tokio.workspace = true
10 changes: 10 additions & 0 deletions elytra/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
#[cfg(feature = "manifest")]
pub mod errors;
#[cfg(feature = "manifest")]
pub mod manifest;

#[cfg(feature = "plugin")]
pub mod plugin;
#[cfg(feature = "plugin")]
pub use plugin::*;

#[cfg(feature = "providers-base")]
pub mod providers;

#[cfg(feature = "utils-base")]
pub mod utils;
#[cfg(feature = "utils-base")]
pub use utils::*;
3 changes: 3 additions & 0 deletions elytra/src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod provider;

pub use provider::ServerProvider;
6 changes: 6 additions & 0 deletions elytra/src/plugin/provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub trait ServerProvider {
fn id(&self) -> &str;
fn name(&self) -> &str;
fn version(&self) -> &str;
fn execute(&self);
}
3 changes: 3 additions & 0 deletions elytra/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
pub mod modpack; // ModPackParse
#[cfg(feature = "utils-mod")]
pub mod parse;

#[cfg(feature = "utils-plugin")]
pub mod plugin;
44 changes: 44 additions & 0 deletions elytra/src/utils/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::ffi::OsStr;

use libloading::{Library, Symbol};
use log::{debug, info};
use thiserror::Error;

use crate::plugin::ServerProvider;

type PluginInit = fn() -> Box<dyn ServerProvider>;

#[derive(Debug, Error)]
pub enum PluginError {
#[error("while loading plugin: {0}")]
Load(#[from] libloading::Error),
}

#[repr(transparent)]
pub struct Plugin {
plugin: Box<dyn ServerProvider>,
}

impl Drop for Plugin {
fn drop(&mut self) {
info!(
"Unloading plugin {} v{}",
self.plugin.id(),
self.plugin.version()
);
}
}

impl Plugin {
pub fn load<P: AsRef<OsStr>>(path: P) -> Result<Self, PluginError> {
debug!("Loading library {}", path.as_ref().to_string_lossy());
let plugin = unsafe { Library::new(path.as_ref()) }?;
debug!("Loading init symbol");
let plugin_init: Symbol<PluginInit> = unsafe { plugin.get(b"init") }?;

info!("Initializing plugin: {}", path.as_ref().to_string_lossy());
let plugin = plugin_init();

Ok(Self { plugin })
}
}
5 changes: 4 additions & 1 deletion mar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ pub mod types;

#[cfg(feature = "all")]
pub use repository::*;
#[cfg(any(feature = "types", feature = "type-maven-artifact"))]
#[cfg(all(feature = "types", feature = "type-maven-artifact"))]
pub use types::MavenArtifactBuilder;

#[cfg(all(not(feature = "types"), feature = "type-maven-artifact"))]
pub use types::MavenArtifact;
6 changes: 6 additions & 0 deletions mar/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::str::FromStr;
#[cfg(feature = "all")]
use thiserror::Error;

mod deserialize;
pub use deserialize::*;

#[cfg(feature = "all")]
#[derive(Default)]
pub struct MavenArtifactBuilder<T> {
pub(crate) base_url: Option<T>,
Expand All @@ -20,6 +22,7 @@ pub struct MavenArtifact {
pub(crate) version: Option<String>,
}

#[cfg(feature = "all")]
#[derive(Debug, Error)]
pub enum MavenArtifactParseError {
#[error("input string is malformed: expected 3 semicolons, got {0}")]
Expand All @@ -30,6 +33,7 @@ pub enum MavenArtifactParseError {
Malformed,
}

#[cfg(feature = "all")]
impl FromStr for MavenArtifact {
type Err = MavenArtifactParseError;

Expand Down Expand Up @@ -75,6 +79,7 @@ impl MavenArtifact {
}
}

#[cfg(feature = "all")]
#[derive(Debug, Error)]
pub enum MavenArtifactBuildError {
#[error("missing field: base_url. run with_base_url to fix")]
Expand All @@ -85,6 +90,7 @@ pub enum MavenArtifactBuildError {
ArtifactID,
}

#[cfg(feature = "all")]
impl<T: ToString> MavenArtifactBuilder<T> {
pub fn with_base_url(mut self, base_url: T) -> Self {
self.base_url = Some(base_url);
Expand Down