WIP
A side project I work on to learn more about Lua, and It's API for my gmod-lua-shared project
This project aims to provide all necessary functions to run Lua on different threads.
The Goal is to be able to create a ILuaInterface with all of Gmod's libraries, functions and classes.
You can download the last working version(which exist for 1 month) which was tested from here
If you want the newest version, just create a fork and run the Build Project (Artifact) workflow
If you have any Issue, Request or Question you can always open an Issue for it or ask me in Discord
NOTE:
This only works on Linux because on Windows creating a ILuaInterface on another thread currently crashes the game.
Currently my focus lies on HolyLib.
When I get to a version like 1.0 I'll get back to this and I'll probably redo most of it.
- Shared Table
- Better Error handling(It loved to crash on any Lua error.)
- bit
- coroutine
- os
- package
- jit
- timer
- hammer (useful for Windows later)
- engine
- gameevent
- file
- debug (Also
setlocal
,setupvalue
,upvalueid
andupvaluejoin
exists)
- physenv (Mostly implemented)
-
- physenv.AddSurfaceData
-
- physenv.GetLastSimulationTime
- resource (untested!)
- system (untested!)
- umsg (untested!)
- gmod (untested!)
- game (untested!)
-
- game.AddDecal
-
- game.CleanUpMap
-
- game.GetGlobalCounter
-
- game.GetGlobalState
-
- game.GetMapNext
-
- game.GetSkillLevel
-
- game.GetTimeScale
-
- game.GetWorld
-
- game.KickID
-
- game.LoadNextMap
-
- game.MountGMA
-
- game.RemoveRagdolls
-
- game.SetGlobalCounter
-
- game.SetGlobalState
-
- game.SetSkillLevel
-
- game.SetTimeScale
-
- game.StartSpot
- Angle (untested)
- Vector (untested)
- resource.AddFile & resource.AddSingleFile don't seem to work.
- Idk how gmod implemented (physenv.GetLastSimulationTime)
- Look into ILuaInterface:PushVector being broken.
- Created ILuaInterface should load all Gmod scripts/allow one to do so
- Change ILuaAction to use Enums instead of strings.
- Documentation
- Add all Gmod Enums
- Recreate all Gmod libraries
-
- Implement sql.Query. (Fixes sql and cookie library)
- Add a new way to send data across threads. Maybe something like net messages but for threads?
- Look again into Error handling. Maybe show which file had an error?
- Improve Performance of
LuaThreaded.SetValue
andLuaThreaded.GetValue
- Fix a memory leak related to LuaThreaded.SetValue not reusing the original key and value.
- File:Seek seems to have some issues?
- Requiring this module befor InitPostEntity was called causes a bunch of weird bugs. See LuaThreaded.ReadyThreads() for the implemented workaround.
I listed all Libraries below that are serverside. Regardless if they are implemented in lua or in c++.
-
- Global(functions)
-
- ai
-
- ai_schedule
-
- ai_task
-
- baseclass
-
- cleanup
-
- concommand
-
- constraint
-
- construct
-
- cookie
-
- cvars
-
- debugoverlay
-
- drive?
-
- duplicator
-
- effects
-
- ents
-
- gamemode
-
- gmsave
-
- hook
-
- http
-
- list
-
- math
-
- motionsensor
-
- navmesh
-
- net
-
- numpad
-
- player
-
- player_manager
-
- properties
-
- saverestore
-
- scripted_ents
-
- sound
-
- sql
-
- string
-
- table
-
- team
-
- undo
-
- usermessage
-
- utf8
-
- util
-
- weapons
-
- widgets
- Recreate all Gmod metatable functions
- Cleanup the code
- Make this compatible for x86-64
- Look again into Windows
Example usage:
require("lua_threaded")
iFace = LuaThreaded.CreateInterface() -- Creates an Interface.
iFace:InitGmod() -- Adds all Libraries and Classes
iFace:Autorun() -- Runs all Autorun scripts.
Returns a table containing all Interfaces created.
[ID] = ILuaInterface (Class)
Returns the Interface with the given ID.
Args:
- ID of the given Interface
Creates a new Interface.
Args:
- If the Created Interface should run on the main thread or not.
Rets:
- The newly created Interface.
Closes the given Interface.
Args:
- The Interface to close.
Prints a Message.
This function may be removed in the future!
Args:
- The string to print.
Returns the Shared table.
NOTE: You need to call this function to get an updated version!
Sets a Value inside the shared table.
Returns only the specified value from the shared table.
Args:
- The Key inside the Table
- The Value to set. Allowed Types: Number, Bool, String, Vector, Angle and Table
Enables all threads. Until then, all Threads will just wait and do nothing. You can still create Threads and give them tasks, but they won't do them until this function is called.
This is a workaround for a crash if you try to do stuff way too soon. This needs to be called if you require the module Inside or after InitPostEntity is called.
Runs the given code
Args:
- Code to run.
Initializes all Classes.
This function may be removed in the future!
Initializes all Libraries.
This function may be removed in the future!
Initializes all Enums.
This function may be removed in the future!
Initializes everything for Gmod.
Internally calls InitClasses, InitLibraries and InitEnums.
Loads a function into the Interface.
NOTE: This is unsafe and can cause random crashes.
This function will be removed in the future!
Args:
- Signature of the Function to load.
Loads everything like autorun does.
NOTE: It currently loves to crash. Just call RunFile with init.lua
Update: It seems like it became stable. Could be because I stopped breaking the stack.
Loads:
includes/init.lua
autorun/server/*.lua
autorun/server/sensorbones/*.lua
Runs the given File.
This function could be renamed in the future!
Runs the given Hook.
(Calls hook.Run with the name and args)
Locks the Thread / doesn't allow it to execute any task.
Also waits until the Thread is finished executing all tasks.
Unlocks the Thread / allows it to execute all tasks
Args:
- Path to the file.
NOTE: This will use theGAME
search path!
Creates the CLuaGameCallback class and implements all functions.
Files:
CLuaGameCallback.h
CLuaGameCallback.cpp
Contains all signatures and functions we need from Gmod.
Files:
detours.h
detours.cpp
Contains all ILuaInterface metatable functions.
Files:
lua_ILuaInterface.h
lua_ILuaInterface.cpp
Contains our LuaThread table and all its functions.
Files:
lua_LuaThread.h
lua_LuaThread.cpp
Contains our LuaThread table and all its functions.
Files:
lua_Enums.h
lua_Enums.cpp
Contains all implemented Global functions.
Status: Unfinished
Files:
library_Global.h
library_Global.cpp
Contains all implemented net functions.
Status: Unfinished
Files:
library_net.h
library_net.cpp
Contains all implemented Vector functions.
Status: Unfinished + Broken
Files:
class_vector.h
class_vector.cpp
Contains important functions PushValue, FillValue and so on.
Files:
ILuaConVars.h
(Needed for library_Global)lua_utils.h
lua_utils.cpp
Contains module base and manages everything for Init and Shutdown.
Files:
lua_threaded.h
lua_threaded.cpp
We should block entity creation while PackEntities_Normal
is running because else we might cause engine errors.
For the NW System I'm going to need CLuaNetworkedVars.
CLuaNetworkedVars::FindEntityVar -- Returns a LuaNetworkedVar_t. GetNW* and GetGlobal functions are gonna require this.
CLuaNetworkedVars::SetNetworkedVar -- Give it everything it needs and it'll do the rest. Probably going to need to add a mutex to it.
Als going to need LuaNetworkedVarEnts_t and going to recreate BuildNetworkVarTable & BuildEntityNetworkVarTable
This could become useful
This is going to be pain.
Going to need g_pServerWorldTable, SetDataTableVar, IGMODDataTable, CGMODVariant.
Already got: IGMODDataTable
ToDo: Find out what the CGMODVariant is.
NOTE: Found out.
Gmod umsg names:
- "LuaCmd"
- "SWEPCmd"
- "AmmoPickup"
- "WeaponPickup"
- "BreakModel"
- "CheapBreakModel"
This seems to be caused by something internal in the filesystem which also affects gmod.