From 007d5e813d1ce379a0b57c1488898b37ef1f98d6 Mon Sep 17 00:00:00 2001 From: William Venner Date: Tue, 23 Nov 2021 18:45:02 +0000 Subject: [PATCH] Add an example --- Cargo.lock | 2 +- Cargo.toml | 3 ++ README.md | 40 +------------------ example/my-first-binary-module/Cargo.toml | 11 +++++ example/my-first-binary-module/README.md | 40 +++++++++++++++++++ .../rust-toolchain.toml | 2 + example/my-first-binary-module/src/lib.rs | 15 +++++++ 7 files changed, 73 insertions(+), 40 deletions(-) create mode 100644 example/my-first-binary-module/Cargo.toml create mode 100644 example/my-first-binary-module/README.md create mode 100644 example/my-first-binary-module/rust-toolchain.toml create mode 100644 example/my-first-binary-module/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9edb8a6..f15f105 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,7 +129,7 @@ dependencies = [ [[package]] name = "gmod" -version = "9.0.0" +version = "9.1.0" dependencies = [ "cfg_table", "cstr", diff --git a/Cargo.toml b/Cargo.toml index 7206d44..c665c2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,7 @@ members = [ "gmod", "gmod-macros" +] +exclude = [ + "example/my-first-binary-module" ] \ No newline at end of file diff --git a/README.md b/README.md index 5f4c0be..715f986 100644 --- a/README.md +++ b/README.md @@ -8,42 +8,4 @@ A swiss army knife for creating binary modules for Garry's Mod in Rust. # Example -### rust-toolchain.toml - -Because we're using the [`C-unwind`](https://rust-lang.github.io/rfcs/2797-project-ffi-unwind.html) ABI, this crate must be used on a [Nightly Rust](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) compiler. - -```toml -[toolchain] -channel = "nightly" -``` - -### Cargo.toml - -```toml -[lib] -crate-type = ["cdylib"] - -[dependencies] -gmod = "*" -``` - -### lib.rs - -```rust -#![feature(c_unwind)] - -#[macro_use] -extern crate gmod; - -#[gmod13_open] -fn gmod13_open(lua: gmod::lua::State) -> i32 { - println!("Hello from binary module!"); - 0 -} - -#[gmod13_close] -fn gmod13_close(lua: gmod::lua::State) -> i32 { - println!("Goodbye from binary module!"); - 0 -} -``` \ No newline at end of file +[Click here](https://github.com/WilliamVenner/gmod-rs/tree/master/example/my-first-binary-module) to see an example. \ No newline at end of file diff --git a/example/my-first-binary-module/Cargo.toml b/example/my-first-binary-module/Cargo.toml new file mode 100644 index 0000000..0c790f4 --- /dev/null +++ b/example/my-first-binary-module/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "my-first-binary-module" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] + +[dependencies] +gmod = "*" \ No newline at end of file diff --git a/example/my-first-binary-module/README.md b/example/my-first-binary-module/README.md new file mode 100644 index 0000000..668de11 --- /dev/null +++ b/example/my-first-binary-module/README.md @@ -0,0 +1,40 @@ +# Installing Rust + +Installing Rust is as easy as downloading [rustup](https://rustup.rs/) and running it! + +# Building the example + +To build the example in debug mode, simply type in a terminal: + +`cargo build` + +You can find the compiled binary in `target/debug/my_first_binary_module.dll` (or `.so` if you're building on Linux) + +# Using the example in Garry's Mod + +First, rename the compiled binary to `gmsv_my_first_binary_module_PLATFORM.dll` where `PLATFORM` is one of the following: + +| Platform | Description | +|:---:|:---:| +| `win32` | Windows 32-bit
Use this if your server is running Windows and is on the `main` branch of Garry's Mod (this is the default branch.) | +| `win64` | Windows 64-bit
Use this if your server is running Windows and is on the `x86-64` branch of Garry's Mod. | +| `linux` | Linux 32-bit
Use this if your server is running Linux and is on the `main` branch of Garry's Mod (this is the default branch.) | +| `linux64` | Linux 64-bit
Use this if your server is running Linux and is on the `x86-64` branch of Garry's Mod. | + +Then, move the compiled binary to `garrysmod/lua/bin/` on your server. If the `bin` folder doesn't exist, create it. + +Finally, you can load the module from Lua! + +```lua +require("my_first_binary_module") +``` + +# Preparing your module for release + +If you've written a useful module and want to release it to the world, or just on your server, build with the `--release` flag: + +`cargo build --release` + +This enables performance optimization of the compiled binary and removes debug symbols which make the binary huge, whilst taking longer to compile. + +On Linux, you'll want to run the `strip` command on the compiled binary to remove debug symbols. diff --git a/example/my-first-binary-module/rust-toolchain.toml b/example/my-first-binary-module/rust-toolchain.toml new file mode 100644 index 0000000..271800c --- /dev/null +++ b/example/my-first-binary-module/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" \ No newline at end of file diff --git a/example/my-first-binary-module/src/lib.rs b/example/my-first-binary-module/src/lib.rs new file mode 100644 index 0000000..4a7d5ab --- /dev/null +++ b/example/my-first-binary-module/src/lib.rs @@ -0,0 +1,15 @@ +#![feature(c_unwind)] + +#[macro_use] extern crate gmod; + +#[gmod13_open] +fn gmod13_open(lua: gmod::lua::State) -> i32 { + println!("Hello from binary module!"); + 0 +} + +#[gmod13_close] +fn gmod13_close(lua: gmod::lua::State) -> i32 { + println!("Goodbye from binary module!"); + 0 +} \ No newline at end of file