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

Commit 42dbf4d

Browse files
committed
Large Changes
* Changed file structure again sorry :( * Added bindings for luaL_register * Fixed luaL_openlib binding * Added luaL_openlibs binding * Renamed helper to util and make public * Added try_cstr, try_rstr and reg macros * Added util::dump_stack * Renamed LuaL_Buffer to LuaBuffer * Added LuaReg type * Added is_even binary module example. * ``Type`` is no longer a useless enum and is just a namespace with constants in it. * Removed deprecated rstring and cstring * Reorganized interfaces * Add ``thiserror`` as a dependency * Status and Mode are repr(i32) now for cast safety, will probably follow suit with Type in being namespaces with constants later. * Shortened var signatures of bindings like 50% (Still ugly!) Largest addition here is lua regs, to make life a lot easier. Especially with the reg! macro which lets you create them easily (And no need for a null reg at the end to tell C you're done adding functions, it does that for you.) Most of these are breaking changes but I think this will be the last time for a while, having too many single files was getting bad.
1 parent 58db989 commit 42dbf4d

File tree

22 files changed

+718
-524
lines changed

22 files changed

+718
-524
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target
22
Cargo.lock
3+
.vscode

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rglua"
33
description = "Rust tooling for garrysmod development with the LuaJIT api"
4-
version = "0.6.1"
4+
version = "0.7.0"
55
authors = ["Vurv <[email protected]>"]
66
keywords = ["glua", "garrysmod", "lua", "gmod"]
77
readme = "README.md"
@@ -13,6 +13,7 @@ edition = "2021"
1313
[dependencies]
1414
libloading = "0.7.2"
1515
once_cell = "1.8.0"
16+
thiserror = "1.0.30"
1617

1718
vtables = { version = "0.1.0", optional = true }
1819
vtables_derive = { version = "0.1.0", optional = true }

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = "0.6.1"
18+
rglua = "0.7.0"
1919
```
2020

2121
## Building
@@ -32,13 +32,14 @@ cargo build --release --target=i686-pc-windows-msvc
3232
* The nature of this crate is super unsafe and sort of defeats the purpose of rust's safety because of the interfacing you require to unsafe C code and the nature of linking to them.
3333

3434
## Example Module
35+
Find another larger example at examples/is_even
3536
```rust
3637
use rglua::prelude::*;
3738

3839
#[no_mangle]
3940
pub extern fn gmod13_open(state: LuaState) -> i32 {
4041
lua_getglobal(state, cstr!("print"));
41-
lua_pushstring(state, cstr!("Hello from rust!"));
42+
lua_pushstring(state, cstr!("Hello from rust!")); // Print to the in-game console
4243
lua_call(state, 1, 0);
4344

4445
// or

examples/is_even/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "gmod_is_even"
3+
description = "Binary module that exposes a function called 'is_even' that does what it says."
4+
version = "0.1.0"
5+
edition = "2021"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
rglua = { path = "../.." }

examples/is_even/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use rglua::prelude::*;
2+
3+
// The functions we want to provide to lua
4+
extern "C" fn is_even(l: LuaState) -> i32 {
5+
let num = luaL_checkinteger(l, 1);
6+
// Ask for the first argument of the function.
7+
// If this is the wrong type or missing, an error will be thrown to lua (if you don't want this, use the lua_to* functions)
8+
9+
lua_pushboolean(l, (num % 2 == 0) as i32);
10+
11+
// This returns one value
12+
1
13+
}
14+
15+
extern "C" fn is_odd(l: LuaState) -> i32 {
16+
let num = luaL_checkinteger(l, 1);
17+
18+
lua_pushboolean(l, (num % 2 != 0) as i32);
19+
1
20+
}
21+
22+
#[no_mangle]
23+
extern "C" fn gmod13_open(l: LuaState) -> i32 {
24+
// Print to the gmod console
25+
printgm!(l, "Loaded is_even module!");
26+
27+
// Create a library to organize all of our functions to export to gmod.
28+
let lib = reg! [
29+
"is_even" => is_even,
30+
"is_odd" => is_odd
31+
];
32+
33+
// Register our functions in ``_G.math``
34+
// This WILL NOT overwrite _G.math if it already exists ()
35+
luaL_register(l, cstr!("math"), lib.as_ptr());
36+
1
37+
}
38+
39+
#[no_mangle]
40+
extern "C" fn gmod13_close(l: LuaState) -> i32 {
41+
printgm!(l, "Goodbye garrysmod!");
42+
0
43+
}

src/globals.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ pub mod Lua {
1212
pub static NUMTYPES: c_int = 9;
1313
pub static NUMTAGS: c_int = NUMTYPES;
1414

15-
// Proper enums to use. Cast these to integers when using them
16-
pub enum Type {
17-
None = -1,
18-
Nil,
19-
Bool,
20-
LUserData,
21-
Number,
22-
String,
23-
Table,
24-
Function,
25-
UserData,
26-
Thread,
15+
pub mod Type {
16+
pub const None: i32 = -1;
17+
pub const Nil: i32 = 0;
18+
pub const Bool: i32 = 1;
19+
pub const LUserData: i32 = 2;
20+
pub const Number: i32 = 3;
21+
pub const String: i32 = 4;
22+
pub const Table: i32 = 5;
23+
pub const Function: i32 = 6;
24+
pub const UserData: i32 = 7;
25+
pub const Thread: i32 = 8;
2726
}
2827

28+
#[repr(i32)]
2929
pub enum Status {
3030
Ok = 0,
3131
Yield,
@@ -68,6 +68,7 @@ pub mod Lua {
6868
}
6969

7070
pub mod Jit {
71+
#[repr(i32)]
7172
pub enum Mode {
7273
ENGINE,
7374
DEBUG,

src/helper/mod.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/interface.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/interface/common.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[repr(C)]
2+
pub struct PlayerInfo {
3+
unknown: u64,
4+
xuid: u64,
5+
name: [i8; 32],
6+
unknown01: [char; 96],
7+
m_szPlayerName: [char; 128],
8+
m_nUserID: i32,
9+
m_szSteamID: [char; 33],
10+
m_nSteam3ID: u32,
11+
userID: i32,
12+
guid: [char; 33],
13+
friendsID: i32,
14+
fakeplayer: bool,
15+
ishltv: bool,
16+
customFiles: [u64; 4],
17+
filesDownloaded: u8,
18+
pad: [i8; 304],
19+
}

src/interfaces/engine.rs renamed to src/interface/engine.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use vtables::VTable;
2-
use vtables_derive::*;
3-
4-
use super::structs::*;
1+
use super::prelude::*;
52

63
use std::os::raw::c_char;
74

0 commit comments

Comments
 (0)