๐Ÿฑโ€๐Ÿ‘ค Cross-language static library for accessing the Lua state in Garry's Mod server plugins

Overview

gmserverplugin

This is a utility library for making Server Plugins that access the Lua state in Garry's Mod.

Currently, accessing the Lua state from a server plugin before it has been created yet requires hooking various internal functions to "steal" the Lua state pointer once it's been initialized. The problem with this is that if two server plugins do this, they'll end up changing the assembly of the function and the signature to find that function will no longer work.

There's a simple solution, but we can safely assume that the request will never be fulfilled. If it is, this repo will be archived for obvious reasons.

In the mean time, I have created this statically linked hacklioteque that will handle all the bullshit for you. You can use this library in any language you want, as long as it supports FFI with C.

Important Notes

  1. This library is NOT thread-safe. Do not call any of these functions outside of the main thread.
  2. Be careful with managing your state between map changes. You should test this thoroughly. Callbacks will still fire between map changes. You cannot unregister callbacks.

Signatures

If the signatures in this library are outdated, please let me know or submit a PR - the signatures can be found here.

Usage

If you're not using Rust, you can download a precompiled static library from the releases page.

Events

There are three events you can hook into.

Event Description
newstate Called when the Lua state is created and we have a pointer to it.
At this point, the Lua state is completely naked - no libraries have been loaded, no Lua scripts have been executed.
before_init Called before includes/init.lua is executed.
At this point, stuff like SERVER and NULL have been defined in the global state.
after_init Called after includes/init.lua is executed.
At this point, we've loaded all the Lua extension in Garry's Mod into the Lua state. The gamemode will be loaded shortly after this event.

Rust

Cargo.toml

[lib]
crate-type = ["cdylib"]

[dependencies]
gmserverplugin = "*"
# OR
gmod = { version = "*", features = ["server-plugin"] } # The gmod crate will re-export gmserverplugin for you with the `server-plugin` feature. https://github.com/WilliamVenner/gmod-rs

lib.rs

extern "C" fn newstate(lua_state: *mut std::ffi::c_void) {
    println!("newstate");
}

extern "C" fn before_init(lua_state: *mut std::ffi::c_void) {
    println!("before_init");
}

extern "C" fn after_init(lua_state: *mut std::ffi::c_void) {
    println!("after_init");
}

#[no_mangle]
unsafe extern "C" fn CreateInterface() -> *mut std::ffi::c_void {
    gmserverplugin::init(); // This will hook the relevant functions, if not already

    gmserverplugin::newstate(newstate); // Register a callback that will be called when the `newstate` event fires.
    gmserverplugin::before_init(before_init); // Register a callback that will be called when the `before_init` event fires.
    gmserverplugin::after_init(after_init); // Register a callback that will be called when the `after_init` event fires.

    // create an IServerPluginCallbacks instance and return a pointer to it
    // you'll probably want to use cxx for this!
}

C++

extern "C" {
    extern "C" typedef void (*Callback)(void* lua_State);

    void init();
    void newstate(Callback);
    void after_init(Callback);
    void before_init(Callback);
}
You might also like...
Pure Rust Lua implementation

purua Pure Rust Lua implementation Usage $ bat lua_examples/defun.lua โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ File: lua_examples/d

A memory safe Lua interpreter

Hematita Da Lua Hematita Da Lua is an interpreter for the scripting language Lua, written entirely in 100% safe Rust. Hematita is the portugese word f

A super-lightweight Lua microservice (toy) framework.

Hive A super-lightweight microservice (toy) framework written in Rust. It uses Lua as interface to provide simple, fun developing experience and fast

A parser, compiler, and virtual machine evaluator for a minimal subset of Lua; written from scratch in Rust.

lust: Lua in Rust This project implements a parser, compiler, and virtual machine evaluator for a minimal subset of Lua. It is written from scratch in

Rust scaffold system with Lua embedded applets.
Rust scaffold system with Lua embedded applets.

brickpack-2022 Demo Powered by Github Actions CI/CD (Heroku) https://demo-1642622230.herokuapp.com/#/users Frontent Runner Rendered sample code (Lua 5

This tool converts Lua code to TS automatically, including the conversion of common standards to their TS equivalents.

lua-to-ts This tool converts Lua code to TS automatically, including the conversion of common standards to their TS equivalents. Code that fails to be

Node.js bindings to Lua

Node.js bindings to Lua

 โšก Fast Web Security Scanner written in Rust based on Lua Scripts ๐ŸŒ– ๐Ÿฆ€
โšก Fast Web Security Scanner written in Rust based on Lua Scripts ๐ŸŒ– ๐Ÿฆ€

โšก Fast Web Security Scanner written in Rust based on Lua Scripts ๐ŸŒ– ๐Ÿฆ€

The uncomplicated Yew State management library

Bounce The uncomplicated state management library for Yew. Bounce is inspired by Redux and Recoil. Rationale Yew state management solutions that are c

Owner
William
aka Billy
William
Another cursed Garry's Mod module. This time, it adds the C preprocessor to Lua scripts

gm_cpreprocessor Another cursed Garry's Mod module. This time, it adds the C preprocessor to Lua scripts. It works by detouring RunStringEx and overri

William 6 Aug 14, 2022
Lua bytecode parser written in Rust using nom, part of metaworm's lua decompiler

luac-parser (ไธญๆ–‡) luaๅญ—่Š‚็ ่งฃๆžๅ™จ, ็›ฎๅ‰ๆ”ฏๆŒ lua51, lua53, lua54 ่ฟ™ๆ˜ฏ็›ฎๅ‰ๆ•ˆๆžœๆœ€ๅฅฝ็š„luaๅ็ผ–่ฏ‘ๅ™จ metaworm's luadec ็š„ไธ€้ƒจๅˆ† ๅฏไปฅๅŸบไบŽๆญคไปฃ็ ๅฎšๅˆถไฝ ๆ‰€้œ€็š„luaๅญ—่Š‚็ ่งฃๆžๅ™จ๏ผŒ็ผ–่ฏ‘ๆˆWASM๏ผŒ่ฎฉmetaworm's luadecๅŠ ่ฝฝไฝฟ็”จ๏ผŒๆฅๅ็ผ–

metaworm 4 Mar 16, 2023
Rust bindings for accessing the Go containers/image stack

Rust bindings for accessing the Go containers/image stack This crate contains a Rust API that forks /usr/bin/skopeo and talks to it via a custom API.

Containers 16 Dec 26, 2022
Rust bindings for accessing the Go containers/image stack

Rust bindings for accessing the Go containers/image stack This crate contains a Rust API that forks /usr/bin/skopeo and talks to it via a custom API.

Colin Walters 1 Oct 15, 2021
A script language like Python or Lua written in Rust, with exactly the same syntax as Go's.

A script language like Python or Lua written in Rust, with exactly the same syntax as Go's.

null 1.4k Jan 1, 2023
Create WASM plugins easily in Rust.

Scotch Library for creating WASM plugins with Rust. Scotch allows you to pass complex types to/from functions in WASM plugins. It achieves that by enc

null 23 Feb 1, 2023
Rust library to interface with Lua

hlua This library is a high-level binding for Lua 5.2. You don't have access to the Lua stack, all you can do is read/write variables (including callb

Pierre Krieger 488 Dec 26, 2022
Lua 5.3 bindings for Rust

rust-lua53 Aims to be complete Rust bindings for Lua 5.3 and beyond. Currently, master is tracking Lua 5.3.3. Requires a Unix-like environment. On Win

J.C. Moyer 150 Dec 14, 2022
Safe Rust bindings to Lua 5.1

rust-lua Copyright 2014 Lily Ballard Description This is a set of Rust bindings to Lua 5.1. The goal is to provide a (relatively) safe interface to Lu

Lily Ballard 124 Jan 5, 2023
Zero-cost high-level lua 5.3 wrapper for Rust

td_rlua This library is a high-level binding for Lua 5.3. You don't have access to the Lua stack, all you can do is read/write variables (including ca

null 47 May 4, 2022