Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 8feffd2

Browse files
committed
Refactor
* Reprecated rstring to rename to rstr to better reflect using &str type. * Bumped to 0.5.0 * C Types are in snake case * Fix clippy lints * Add a prelude that includes lua functions and basic types. * Make exposed symbols static instead of const as they should be.. * Added unit test for cstr! function * Put interface stuff under new ``interfaces`` feature (default enabled).
1 parent 7e8ff88 commit 8feffd2

File tree

11 files changed

+246
-211
lines changed

11 files changed

+246
-211
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rust:
77

88
branches:
99
only:
10-
- main
10+
- master
1111

1212
jobs:
1313
allow_failures:

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "rglua"
33
description = "Rust bindings to the lua api for gmod binary module creation"
4-
version = "0.4.0"
4+
version = "0.5.0"
55
authors = ["Vurv <[email protected]>"]
6-
keywords = ["glua","garrysmod","lua"]
6+
keywords = ["glua", "garrysmod", "lua", "gmod"]
77
readme = "README.md"
88
license = "MIT"
99
edition = "2018"
@@ -14,5 +14,9 @@ edition = "2018"
1414
libloading = "0.7.0"
1515
once_cell = "1.7.2"
1616

17-
vtables = "0.1.0"
18-
vtables_derive = "0.1.0"
17+
vtables = { version = "0.1.0", optional = true }
18+
vtables_derive = { version = "0.1.0", optional = true }
19+
20+
[features]
21+
default = ["interfaces"]
22+
interfaces = ["vtables", "vtables_derive"]

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rglua [![Release Shield](https://img.shields.io/github/v/release/Vurv78/rglua)](https://github.com/Vurv78/rglua/releases/latest) ![Linux Build Status](https://www.travis-ci.com/Vurv78/rglua.svg?branch=main) [![License](https://img.shields.io/github/license/Vurv78/rglua?color=red)](https://opensource.org/licenses/Apache-2.0) [![github/Vurv78](https://img.shields.io/discord/824727565948157963?color=7289DA&label=chat&logo=discord)](https://discord.gg/epJFC6cNsw)
1+
# rglua [![Release Shield](https://img.shields.io/github/v/release/Vurv78/rglua)](https://github.com/Vurv78/rglua/releases/latest) ![Build Status](https://www.travis-ci.com/Vurv78/rglua.svg?branch=main) [![License](https://img.shields.io/github/license/Vurv78/rglua?color=red)](https://opensource.org/licenses/Apache-2.0) [![github/Vurv78](https://img.shields.io/discord/824727565948157963?label=Discord&logo=discord&logoColor=ffffff&labelColor=7289DA&color=2c2f33)](https://discord.gg/epJFC6cNsw)
22

33
This is a crate that contains bindings for using the lua c api in garrysmod through bindings using the rust libloading library.
44
Can be used for either binary modules or just manual injections into gmod, like with [Autorun-rs](https://github.com/Vurv78/Autorun-rs)
@@ -15,7 +15,7 @@ Add this to your ``Cargo.toml`` file
1515
crate-type = ["cdylib"] # This tells rust we want to create a .dll file that links to C code.
1616

1717
[dependencies]
18-
rglua = { git = "https://github.com/Vurv78/rglua" }
18+
rglua = "0.5.0"
1919
```
2020

2121
## Building
@@ -38,16 +38,12 @@ Also do this if you have never compiled to 32 bit, to get rustup to install 32 b
3838

3939
## Example Module
4040
```rust
41-
use rglua::{
42-
types::{ LuaState, CharBuf },
43-
cstring,
44-
lua_shared::*
45-
};
41+
use rglua::prelude::*;
4642

4743
#[no_mangle]
4844
pub extern fn gmod13_open(state: LuaState) -> i32 {
49-
lua_getglobal( state, b"print\0".as_ptr() as CharBuf );
50-
lua_pushstring( state, cstring!("Hello from rust!") );
45+
lua_getglobal( state, cstr!("print") );
46+
lua_pushstring( state, cstr!("Hello from rust!") );
5147
lua_call( state, 1, 0 );
5248
0
5349
}

src/globals.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
use crate::types::*;
22

33
pub mod Lua {
4-
use super::CInt;
4+
use super::c_int;
55

6-
pub static VERSION: &'static str = "Lua 5.1";
7-
pub static RELEASE: &'static str = "Lua 5.1.4";
8-
pub static VERSION_NUM: CInt = 501;
9-
pub static COPYRIGHT: &'static str = "Copyright (C) 1994-2008 Lua.org, PUC-Rio";
10-
pub static AUTHORS: &'static str = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes";
6+
pub static REGISTRYINDEX: c_int = -10000;
7+
pub static ENVIRONINDEX: c_int = -10001;
8+
pub static GLOBALSINDEX: c_int = -10002;
119

12-
pub static REGISTRYINDEX: CInt = -10000;
13-
pub static ENVIRONINDEX: CInt = -10001;
14-
pub static GLOBALSINDEX: CInt = -10002;
10+
pub static MULTRET: c_int = -1;
1511

16-
pub static MULTRET: CInt = -1;
17-
pub static SIGNATURE: &'static str = "\x1bLua";
18-
pub static MINSTACK: CInt = 20;
19-
20-
pub static NUMTYPES: CInt = 9;
21-
pub static NUMTAGS: CInt = NUMTYPES;
12+
pub static NUMTYPES: c_int = 9;
13+
pub static NUMTAGS: c_int = NUMTYPES;
2214

2315

2416
// Proper enums to use. Cast these to integers when using them
@@ -89,11 +81,11 @@ pub mod Jit {
8981
MASK = 0x0ff // LUAJIT_MODE_MASK
9082
}
9183

92-
use super::CInt;
84+
use super::c_int;
9385
// Associated Constants, woah
9486
impl Mode {
95-
pub const OFF: CInt = 0x0000;
96-
pub const ON: CInt = 0x0100;
97-
pub const FLUSH: CInt = 0x0200;
87+
pub const OFF: c_int = 0x0000;
88+
pub const ON: c_int = 0x0100;
89+
pub const FLUSH: c_int = 0x0200;
9890
}
9991
}

src/helpers.rs renamed to src/helper/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#![allow(unused)]
22

3-
// Get a const char* from a &str
4-
53
#[deprecated(
64
since = "0.2.0",
7-
note = "Use b\"string\\0\".as_ptr() as *const i8 format or CStrings directly instead."
5+
note = "Use cstr instead."
86
)]
97
#[macro_export]
108
macro_rules! cstring {
@@ -16,9 +14,17 @@ macro_rules! cstring {
1614
}
1715
}
1816

17+
// Creates *const i8 from a rust string literal by concatting a \0 to the end of it.
18+
#[macro_export]
19+
macro_rules! cstr {
20+
($rstring:expr) => {
21+
concat!($rstring, "\0").as_ptr() as *const i8
22+
}
23+
}
24+
1925
// Get a rust &str from a const char*
2026
#[macro_export]
21-
macro_rules! rstring {
27+
macro_rules! rstr {
2228
($cstring:expr) => {
2329
{
2430
#[allow(unused_unsafe)]
@@ -28,6 +34,12 @@ macro_rules! rstring {
2834
}
2935
}
3036

37+
#[macro_export]
38+
#[deprecated(since = "0.5.0", note = "Use rstr instead.")]
39+
macro_rules! rstring {
40+
($a:tt) => { rstr!($a) }
41+
}
42+
3143
#[allow(unused_macros)]
3244
#[macro_export]
3345
/// Like println!, however it prints to the gmod server's console.
@@ -39,10 +51,10 @@ macro_rules! printgm {
3951
{
4052
let printargs = format!( $($x,)* );
4153
if let Ok(fmt) = std::ffi::CString::new(printargs) {
42-
rglua::lua_shared::lua_getglobal( $state, b"print\0".as_ptr() as *const i8 );
54+
rglua::lua_shared::lua_getglobal( $state, cstr!("print") );
4355
rglua::lua_shared::lua_pushstring( $state, fmt.as_ptr() );
4456
rglua::lua_shared::lua_call( $state, 1, 0 );
4557
}
4658
}
4759
};
48-
}
60+
}

src/interface.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11

22
use std::ffi::{ c_void, CString };
3-
43
pub use crate::interfaces::*;
54

65
pub type CreateInterfaceFn = extern "system" fn(
76
pName: *const i8,
87
pReturnCode: *mut i32
98
) -> *mut c_void;
109

10+
/// # Safety
11+
/// This function is unsafe to transmute the internal libloading symbol to a proper createinterface function pointer.
1112
pub unsafe fn get_interface_handle(file: &str) -> Result<CreateInterfaceFn, libloading::Error> {
1213
let lib = libloading::Library::new(file)?;
1314
let sym: libloading::Symbol<CreateInterfaceFn> = lib.get(b"CreateInterface\0")?;
@@ -25,7 +26,7 @@ pub fn get_from_interface(iface: &str, factory: CreateInterfaceFn) -> Result<*mu
2526
let mut status = 0;
2627

2728
let iface = CString::new(iface)
28-
.map_err( |e| InterfaceError::BadCString(e) )?;
29+
.map_err(InterfaceError::BadCString)?;
2930

3031
let result = factory( iface.as_ptr(), &mut status );
3132

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
pub mod types;
66
pub mod globals;
7-
pub mod helpers;
8-
7+
mod helper;
8+
#[cfg(feature = "interfaces")]
99
mod interfaces;
10+
#[cfg(feature = "interfaces")]
1011
pub mod interface;
1112

1213
#[macro_use]
13-
pub mod lua_shared;
14+
pub mod lua_shared;
15+
16+
pub mod prelude;

0 commit comments

Comments
 (0)