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

Commit d3387c5

Browse files
committed
Add basic interface stuff
Added basic engine, panel & lua interface bindings
1 parent dd5f363 commit d3387c5

File tree

8 files changed

+167
-1
lines changed

8 files changed

+167
-1
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rglua"
33
description = "Rust bindings to the lua api for gmod binary module creation"
4-
version = "0.3.0"
4+
version = "0.4.0"
55
authors = ["Vurv <[email protected]>"]
66
keywords = ["glua","garrysmod","lua"]
77
readme = "README.md"
@@ -13,3 +13,6 @@ edition = "2018"
1313
[dependencies]
1414
libloading = "0.7.0"
1515
once_cell = "1.7.2"
16+
17+
vtables = "0.1.0"
18+
vtables_derive = "0.1.0"

src/interface.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
use std::ffi::{ c_void, CString };
3+
4+
pub use crate::interfaces::*;
5+
6+
pub type CreateInterfaceFn = extern "system" fn(
7+
pName: *const i8,
8+
pReturnCode: *mut i32
9+
) -> *mut c_void;
10+
11+
pub unsafe fn get_interface_handle(file: &str) -> Result<CreateInterfaceFn, libloading::Error> {
12+
let lib = libloading::Library::new(file)?;
13+
let sym: libloading::Symbol<CreateInterfaceFn> = lib.get(b"CreateInterface\0")?;
14+
15+
Ok( std::mem::transmute(sym) )
16+
}
17+
18+
#[derive(Debug)]
19+
pub enum InterfaceError {
20+
BadCString( std::ffi::NulError ),
21+
FactoryNotFound,
22+
}
23+
24+
pub fn get_from_interface(iface: &str, factory: CreateInterfaceFn) -> Result<*mut (), InterfaceError> {
25+
let mut status = 0;
26+
27+
let iface = CString::new(iface)
28+
.map_err( |e| InterfaceError::BadCString(e) )?;
29+
30+
let result = factory( iface.as_ptr(), &mut status );
31+
32+
if status == 0 && !result.is_null() {
33+
Ok(result as *mut ())
34+
} else {
35+
Err( InterfaceError::FactoryNotFound )
36+
}
37+
}

src/interfaces/engine.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use vtables::VTable;
2+
use vtables_derive::*;
3+
4+
use super::structs::*;
5+
6+
use std::os::raw::c_char;
7+
8+
#[has_vtable]
9+
#[derive(VTable, Debug)]
10+
pub struct EngineClient {
11+
pub vtable: usize
12+
}
13+
14+
impl EngineClient {
15+
#[virtual_index(5)]
16+
pub fn GetScreenSize(&self, width: *mut i32, height: *mut i32) {}
17+
18+
#[virtual_index(8)]
19+
pub fn GetPlayerInfo(&self, p_info: *mut PlayerInfo) -> bool {}
20+
21+
#[virtual_index(9)]
22+
pub fn GetPlayerForUserID(&self, userid: i32) -> i32 {}
23+
24+
#[virtual_index(12)]
25+
pub fn GetLocalPlayer(&self) -> i32 {}
26+
27+
#[virtual_index(14)]
28+
pub fn Time(&self) -> f32 {}
29+
30+
#[virtual_index(26)]
31+
pub fn IsInGame(&self) -> bool {}
32+
33+
#[virtual_index(27)]
34+
pub fn IsConnected(&self) -> bool {}
35+
36+
#[virtual_index(108)]
37+
pub fn ExecuteClientCmd(&self, command: *const c_char) {}
38+
39+
#[virtual_index(113)]
40+
pub fn ClientCmd_Unrestricted(&self, command: *const c_char) {}
41+
}

src/interfaces/lua.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use vtables::VTable;
2+
use vtables_derive::*;
3+
4+
use super::structs::ILuaInterface;
5+
6+
#[has_vtable]
7+
#[derive(VTable)]
8+
pub struct CLuaShared {}
9+
10+
impl CLuaShared {
11+
#[virtual_index(6)]
12+
pub fn GetLuaInterface(&self, realm: u8) -> *mut ILuaInterface {}
13+
}

src/interfaces/mod.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
pub mod structs {
3+
use vtables::VTable;
4+
use vtables_derive::*;
5+
6+
#[repr(C)]
7+
pub struct PlayerInfo {
8+
unknown: u64,
9+
xuid: u64,
10+
name: [i8; 32],
11+
unknown01: [char; 96],
12+
m_szPlayerName: [char; 128],
13+
m_nUserID: i32,
14+
m_szSteamID: [char; 33],
15+
m_nSteam3ID: u32,
16+
userID: i32,
17+
guid: [char; 33],
18+
friendsID: i32,
19+
fakeplayer: bool,
20+
ishltv: bool,
21+
customFiles: [u64; 4],
22+
filesDownloaded: u8,
23+
pad: [i8; 304]
24+
}
25+
26+
#[has_vtable]
27+
#[derive(VTable)]
28+
pub struct ILuaInterface {
29+
pub vtable: usize
30+
}
31+
32+
impl ILuaInterface {
33+
#[virtual_index(20)]
34+
pub fn IsServer(&self) -> bool {}
35+
36+
#[virtual_index(21)]
37+
pub fn IsClient(&self) -> bool {}
38+
39+
#[virtual_index(22)]
40+
pub fn IsMenu(&self) -> bool {}
41+
}
42+
}
43+
44+
mod engine;
45+
pub use engine::EngineClient;
46+
47+
mod lua;
48+
pub use lua::CLuaShared;
49+
50+
mod panel;
51+
pub use panel::IPanel;

src/interfaces/panel.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use vtables::VTable;
2+
use vtables_derive::*;
3+
4+
#[has_vtable]
5+
#[derive(VTable)]
6+
pub struct IPanel {
7+
pub vtable: usize
8+
}
9+
10+
impl IPanel {
11+
#[virtual_index(36)]
12+
pub fn GetName(&self, vguiPanel: u32) -> *const i8 {}
13+
14+
#[virtual_index(41)]
15+
pub fn PaintTraverse(&self, vguiPanel: u32, forceRepaint: bool, allowForce: bool) {}
16+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ pub mod types;
66
pub mod globals;
77
pub mod helpers;
88

9+
mod interfaces;
10+
pub mod interface;
11+
912
#[macro_use]
1013
pub mod lua_shared;

src/lua_shared.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ use once_cell::sync::Lazy;
6262
use crate::types::*;
6363
use crate::globals::Lua::{self, GLOBALSINDEX};
6464

65+
expose_symbol!( CreateInterface, *mut CVoid, (pName: CharBuf, pReturnCode: *mut CInt) );
66+
6567
// Load lua Code
6668
expose_symbol!( luaL_loadbufferx, CInt, (state: LuaState, code: CharBuf, size: SizeT, id: CharBuf, mode: CharBuf) );
6769
expose_symbol!( luaL_loadbuffer, CInt, (state: LuaState, code: CharBuf, size: SizeT, id: CharBuf) );

0 commit comments

Comments
 (0)