- Notifications
You must be signed in to change notification settings - Fork1
Embedding the Lua Engine in SA:MP via Detours
License
LuxXx/samp-lua
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This DLL / ASI installs hooks to invoke lua events and registeres some exports several SA:MP functions to lua.
Similar projects can be found here:https://github.com/drakeee/samp-plugin-luahttps://github.com/THE-FYP/SAMP.Lua
Other libraries that I used:
- The LUA C API:https://www.lua.org/pil/24.html
- Sol2:http://sol2.readthedocs.io/en/latest/
A big thank you goes toagrippa1994 for his Utility functionality:https://github.com/SAMPProjects/Open-SAMP-API
What is Lua? Lua is a scripting language, that is designed for being embedded into other application.You can add aevents.lua
file to your SA:MP directory. The Lua engine emits events when SA:MP functions are calledFor example if you call the function SendChat by using your Keybinder or by yourself the engine emits the "onSendChat" event.This event is handled by triggering the "onSendChat" a lua function inside of theevents.lua
file.For Example:
functiononSendChat(msg)-- do something with your message-- return a true value to mark the command as handled, the command won't be sent.end
Not only your commands emit an event. Almost every SA:MP function calls an event.For Example (A simple Ad-Blocker):
functiononAddChatMessage(text)ifstring.match(text,'BUY PREMIUM NOW')thenreturntrueendreturnfalseend
You can also call the SA:MP functions inside lua:
functiononAddChatMessage(text,prefix)print(prefix..text)-- I repeat every messagereturnfalseend
This way to perform a response is much more faster than using a Keybinder:
functiononAddChatMessage(text)if (text=='Someone wants to refill your car for 1337$')thensendCommand('/accept refill')endreturnfalseend
You can register new commands or overwrite some:
functiononSendChat(msg)ifmsg=="/bk"thensendCommand('/f I need some backup!')returntrueendifmsg=="/help"thensendCommand('/admins')returntrueendend
Another example could be:
functiononAddChatMessage(text)ifstring.match(text,'SMS:')thenshowGameText(text,<iTime>,<iStyle>)-- SMS are important, I need them big!endreturnfalseend
Death event:
functiononDeath()sendChat('I just died in your arms tonight');end
Spam Filter:
locallastmsgfunctiononAddChatMessage(text)iflastmsg==textthenreturntrueendlastmsg=textreturnfalseend
Example for manipulating an AddChatMessage:
functiononAddChatMessage(text)if (string.find(text,'Noch')andstring.find(text,' Minuten bis zum Payday'))thenlocalminutes=string.match(text,'Noch (%d+) Minuten bis zum Payday')full_print('Geldregen in'..minutes..' Minuten',2,'Toller Prefix',16728128,5017777)returntrueendend
The shortest samp inline calculator ever: Just writecalc: <expr>
, e.g.calc: 1+3*192/9
functiononAddChatMessage(text)if (string.find(text,'calc:'))thenlocalresult=load('return'..string.sub(text,7))()print('Result:'..result)returntrueendend
A /repay method
lastPayer=''lastPayment=0functiononSendChat(msg)if (msg=="/repay")thenif (lastPayer==0orlastPayer=='')thenprint("Nothing to pay")elsesendCommand("/pay"..lastPayer..""..lastPayment)endreturntrueendendfunctiononAddChatMessage(text,prefix)if (string.match(text,"payed you"))thenlastPayer,lastPayment=string.match(text,"(.*) payed you $(.*)!")endend
You may also use the default lua libraries like io, math, os, etc.
I want to implement as much events as possible. If you have suggestions, feel free to use the issue tracker.
The lua script can be reloaded during the game with/reloadlua
.
Events | Comment | Written |
---|---|---|
onSendChat | handles your chat and your commands, has two parameters: text and prefix | Yes |
onAddChatMessage | handles the incoming chat messages | Yes |
onShowGameText | Not yet | |
onShowDialog | Not yet | |
onDeath | Not yet | |
onSpawn | Not yet | |
onObjectCreation | Not yet | |
onVehicleEnter | Not yet | |
onVehicleExit | Not yet | |
onVehicleCreation | Not yet | |
onFreeze | Not yet | |
onUnfreeze | Not yet | |
onFreeze | Not yet | |
onTeleport | Not yet | |
onEngineStart | Not yet | |
onEngineStop | Not yet | |
... |
Callbacks | Comment | Written |
---|---|---|
sendChat | sends a chat message | Yes |
sendCommand | sends a command | Yes |
addChatMessage | alias: print, shows a client message | Yes |
full_print | The full version of the print function. Call it like this: full_print(text, chattype, prefix, color, prefixColor). | Yes |
getHP | gets the current hp | No |
... | ||
SA:MP uses several ChatTypes that you can invoke usingfull_print : |
- 2 for normal chat
- 4 for info messages (blue font color)
- 8 for debug messages (green font color)
If you want to test the API do the following:
- Clone this repository
- Install Boost and Lua, via NuGet Manager (Make sure that you are using all package resources)
- Make sure that the sol2 library is in the root directory (if not, download it manually)
- Compile
To use the engine, inject the lua.dll, then the samp-lua.dll.Later I you will only need to copy the samp-lua.asi into your SA:MP directory
Keep in mind that you should have a corresponding lua file in your SA:MP directory. At the moment the lua file has to be namedevents.lua
.
Have fun implementing some neat things using this engine.
Greets and Have Fun
LuxXx
About
Embedding the Lua Engine in SA:MP via Detours