Skip to content

Commit

Permalink
Introduce a separate server-config crate for server configs (#194)
Browse files Browse the repository at this point in the history
* Introduce new server-config crate with new config prototypes
* Mark the validator examples as requiring the script feature
  • Loading branch information
steamroller-airmash committed Jun 17, 2022
1 parent b9b587f commit 85df9d9
Show file tree
Hide file tree
Showing 17 changed files with 1,380 additions and 23 deletions.
121 changes: 98 additions & 23 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"ctf",
"ffa",
"server",
"server-config",
"server-macros",
"utils/kdtree",
"utils/serde-rlua"
Expand Down
34 changes: 34 additions & 0 deletions server-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "server-config"
version = "0.1.0"
edition = "2021"

[features]
script = [ "rlua", "serde-rlua" ]

[dependencies]
serde = { version = "1.0", features = [ "derive" ] }
serde-value = "0.7"
serde_path_to_error = "0.1.7"
nalgebra = { version = "0.31", features = ["serde-serialize"] }
doc-cfg = "0.1"

rlua = { version = "0.19", optional = true }
serde-rlua = { path = "../utils/serde-rlua", optional = true }

[dependencies.protocol]
version = "0.5"
package = "airmash-protocol"
features = [ "serde" ]

[dev-dependencies]
serde_json = "1.0"
anyhow = "1.0"

[[example]]
name = "export"
required-features = ["script"]

[[example]]
name = "validate"
required-features = ["script"]
1 change: 1 addition & 0 deletions server-config/configs/default.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Empty LUA file means no edits to the config
6 changes: 6 additions & 0 deletions server-config/configs/infinite-fire.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

-- Set the firing delay and energy for all planes to 0.
for key, plane in pairs(data.planes) do
plane.fire_delay = 0
plane.fire_energy = 0
end
40 changes: 40 additions & 0 deletions server-config/examples/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::io::Write;

use serde::Deserialize;
use server_config::GamePrototype;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = std::env::args().skip(1).collect::<Vec<_>>();
// let args = vec!["server-prototypes/configs/infinite-fire.lua"];

let mut prototype = GamePrototype::default();

let lua = rlua::Lua::new();
lua.context(|lua| -> Result<(), Box<dyn std::error::Error>> {
for file in args {
let contents = std::fs::read_to_string(file)?;
let value = prototype.patch_direct(lua, &contents)?;

let mut track = serde_path_to_error::Track::new();
let de = serde_rlua::Deserializer::new(value);
let de = serde_path_to_error::Deserializer::new(de, &mut track);

match GamePrototype::deserialize(de) {
Ok(proto) => prototype = proto,
Err(e) => Err(format!(
"Error while deserializing field {}: {}",
track.path(),
e
))?,
}
}

Ok(())
})?;

let mut stdout = std::io::stdout().lock();
serde_json::to_writer_pretty(&mut stdout, &prototype)?;
stdout.write_all(b"\n")?;

Ok(())
}
Loading

0 comments on commit 85df9d9

Please sign in to comment.