Skip to content

Commit

Permalink
ConVar proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
theY4Kman committed Aug 11, 2016
1 parent c12fa0f commit 9b6920a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
54 changes: 54 additions & 0 deletions smx/sourcemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ def native(f):
f.is_native = True
return f


class ConVar(object):
def __init__(self, name, default_value, description='', flags=0, min=None, max=None):
self.name = name
self.value = self.default_value = default_value
self.description = description
self.flags = flags
self.min = min
self.max = max

def __str__(self):
return self.value

def __repr__(self):
return '<ConVar %s %r>' % (self.name, self.value)


class SourceModNatives(object):
def __init__(self, sys):
"""
Expand Down Expand Up @@ -335,11 +352,45 @@ def CreateTimer(self, params):

return self.sys.timers.create_timer(interval, func, data, flags)

@native
def CreateConVar(self, params):
name = self.amx._local_to_string(params[1])
default_value = self.amx._local_to_string(params[2])
description = self.amx._local_to_string(params[3])
flags = params[4]
has_min = bool(params[5])
min = sp_ctof(params[6]) if has_min else None
has_max = bool(params[7])
max = sp_ctof(params[8]) if has_max else None

cvar = ConVar(name, default_value, description, flags, min, max)
self.sys.convars[name] = cvar
return self.sys.handles.new_handle(cvar)

@native
def GetConVarInt(self, params):
handle = params[1]
cvar = self.sys.handles[handle]
return int(cvar.value)


class SourceModHandles(object):
"""Emulates SourceMod's handles"""

def __init__(self, sys):
self.sys = sys
self._handle_counter = 0
self._handles = {}

def new_handle(self, obj):
self._handle_counter += 1
handle_id = self._handle_counter
self._handles[handle_id] = obj
return handle_id

def __getitem__(self, handle_id):
return self._handles[handle_id]


class SourceModTimers(object):
"""Handles SourceMod timers"""
Expand Down Expand Up @@ -397,10 +448,13 @@ def __init__(self, amx):

self.natives = SourceModNatives(self)
self.timers = SourceModTimers(self)
self.handles = SourceModHandles(self)

self.tickrate = 66
self.interval_per_tick = 1.0 / self.tickrate
self.last_tick = None

self.convars = {}

def tick(self):
self.last_tick = engine_time()
2 changes: 1 addition & 1 deletion smx/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _nativecall(self, index, paramoffs):
params_ptr = cast(pointer(params), POINTER(c_void_p))
params_ptr.contents.value += paramoffs

pyfunc(params)
return pyfunc(params)

def _pubcall(self, func_id):
if not func_id & 1:
Expand Down
16 changes: 16 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,19 @@ def test_interpreter(compile):

# But this doesn't
# assert plugin.runtime.call_function_by_name('ReturnTwentyThree') == 23


def test_convars(compile):
plugin = compile('''
new Handle:g_cvar = INVALID_HANDLE;
public TestCreateConVar() {
g_cvar = CreateConVar("pysmx_num", "350", "description");
}
public TestGetConVarInt() {
return GetConVarInt(g_cvar);
}
''')

plugin.runtime.call_function_by_name('TestCreateConVar')
value = plugin.runtime.call_function_by_name('TestGetConVarInt')
assert value == 350

0 comments on commit 9b6920a

Please # to comment.