Source Python for GMod?

All other Source.Python topics and issues.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Source Python for GMod?

Postby Ayuto » Sat Sep 03, 2016 8:15 pm

satoon101 wrote:I trie to add team data the other day, but there were no entities when I tried to iterate over the team manager entity (don't remember the name). So, I added bots (I don't own GMod, so I couldn't join the server), but still no entities showed. Obviously, not owning the game, I couldn't test out and add weapon data.

Yeah, I just ran into the same issue. The name of the team manager is "team_manager".
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Source Python for GMod?

Postby Ayuto » Sun Sep 04, 2016 4:38 pm

Doldol wrote:Urh, and I was thinking that at one point we'll probably have to access the Gmod Lua API if we want to do GMod specific stuff with SP (for example, create a server-side UI), since I'm pretty sure it wouldn't be possible, or hard to get access to all the C functions that Lua wraps for it's api functions, etc.

As far as I can see, it should be possible to wrap the functions for the C Lua API inside lua_shared_srv.so/dll, and call them like we would with any C functions wrapped from the engine. How to use the GLua API from C++: https://wiki.garrysmod.com/page/C_Lua:_Functions

Just played a little bit around with Lua. We can easily call Lua functions from Python and create new Lua function in Python.

Syntax: Select all

# =============================================================================
# >> IMPORTS
# =============================================================================
import memory

from memory import Convention
from memory import DataType
from memory import Callback

from memory.manager import CustomType
from memory.manager import manager


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
server = memory.find_binary('server')

SPECIAL_GLOB = 0 # Global table
SPECIAL_ENV = 1 # Environment table
SPECIAL_REG = 2 # Registry table


# =============================================================================
# >> CLASSES
# =============================================================================
class CLuaInterface(CustomType, metaclass=manager):
_Pop = manager.virtual_function(2, [DataType.INT])

def Pop(self, iAmt=1):
self._Pop(iAmt)

GetField = manager.virtual_function(4, [DataType.INT, DataType.STRING])
SetField = manager.virtual_function(5, [DataType.INT, DataType.STRING])
Call = manager.virtual_function(10, [DataType.INT, DataType.INT])

_GetString = manager.virtual_function(
23,
[DataType.INT, DataType.POINTER],
DataType.STRING
)

def GetString(self, iStackPos=-1, iOutLen=None):
return self._GetString(iStackPos, iOutLen)

_PushString = manager.virtual_function(29, [DataType.STRING, DataType.UINT])

def PushString(self, val, iLen=0):
self._PushString(val, iLen)

PushNumber = manager.virtual_function(30, [DataType.DOUBLE])
PushCFunction = manager.virtual_function(32, [DataType.POINTER])
PushSpecial = manager.virtual_function(38, [DataType.INT])
RunString = manager.virtual_function(
88,
[DataType.STRING, DataType.STRING, DataType.STRING, DataType.BOOL, DataType.BOOL],
DataType.BOOL
)

def run_simple_string(self, string):
"""Run a simple Lua string.

Example:

print('Hello!')
"""
return self.RunString('lua_run', '', string, True, True)

# TODO: Add the Windows identifier. The virtual function indexes are the same on both
# platforms.
g_Lua = memory.make_object(CLuaInterface, server.find_pointer('g_Lua', level=1))


# =============================================================================
# >> EXAMPLE 1
# =============================================================================
# Print Hello!
g_Lua.PushSpecial(SPECIAL_GLOB)
g_Lua.GetField(-1, 'print')
g_Lua.PushString('Hello!')
g_Lua.Call(1, 0)
g_Lua.Pop()


# =============================================================================
# >> EXAMPLE 2
# =============================================================================
# Print name of team 0
g_Lua.PushSpecial(SPECIAL_GLOB)
g_Lua.GetField(-1, 'team') # Get the 'team' module
g_Lua.GetField(-1, 'GetName') # Get the 'GetName' function
g_Lua.PushNumber(0) # Team 0/Unassigned
g_Lua.Call(1, 1) # 1 arg and 1 return value
print('Team name:', g_Lua.GetString(-1))
g_Lua.Pop(3)


# =============================================================================
# >> EXAMPLE 3
# =============================================================================
# Create a new Lua function defined in Python!
@Callback(Convention.CDECL, [DataType.POINTER], DataType.INT)
def do_something(args):
# Print something
print('Do something')

# Return 1337
g_Lua.PushNumber(1337)

# Tell Lua that we are returning 1 value
return 1

# Add the function to the global Lua table
g_Lua.PushSpecial(SPECIAL_GLOB)
g_Lua.PushCFunction(do_something)
g_Lua.SetField(-2, 'do_something')
g_Lua.Pop()

# -- ../garrysmod/lua/test.lua
# -- Run the script with "lua_openscript test.lua"
# print(do_something())
I love the third example: We define a function in Python that gets a C function wrapper using the memory module, so we have a pointer we can push to the global Lua function table and then call that function from Lua. :grin:
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: Source Python for GMod?

Postby Doldol » Mon Sep 05, 2016 4:28 am

Ayuto wrote:....
I love the third example: We define a function in Python that gets a C function wrapper using the memory module, so we have a pointer we can push to the global Lua function table and then call that function from Lua. :grin:


Nice Ayuto, that's what I was thinking. I wasn't sure how I'd call submodules (? methods in python), but you demonstrated that. :)

The 3rd example seems especially useful to define hooks Lua addons can use.

Edit: Actually, I don't understand the last 3 lines of code (the comments).
Edit: Nvm, I'm guessing that's a little LUA script that you renamed later on ^^
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Source Python for GMod?

Postby Ayuto » Mon Sep 05, 2016 5:02 am

Doldol wrote:Edit: Actually, I don't understand the last 3 lines of code (the comments).
Edit: Nvm, I'm guessing that's a little LUA script that you renamed later on ^^

Yes, just fixed the name in the comments.

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests