Skip to content

Commit e177d80

Browse files
committed
Started implementation of converting GameEvent instances in events/pre-events to dictionaries and passing them as keyword arguments.
Thanks to @MarkusMeskanen for suggesting this: http://forums.sourcepython.com/showthread.php?960
1 parent a082015 commit e177d80

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

addons/source-python/packages/source-python/engines/precache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _precache(self):
104104
"""Precache the path."""
105105
self._precache_method(self._path, self._preload)
106106

107-
def _server_spawn(self, game_event):
107+
def _server_spawn(self, **kwargs):
108108
"""Precache the object on map change."""
109109
self._precache()
110110

addons/source-python/packages/source-python/events/hooks.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
from events.manager import game_event_manager
1818
# Hooks
1919
from hooks.exceptions import except_hooks
20+
# KeyValues
21+
from keyvalues import KeyValues
2022
# Memory
23+
from memory import get_object_pointer
2124
from memory import get_virtual_function
2225
from memory import make_object
2326
from memory.hooks import PreHook
@@ -176,14 +179,19 @@ def _pre_game_event(args):
176179
# Create a variable to know what to do after all pre-events are called
177180
event_action = EventAction.CONTINUE
178181

182+
# Convert the GameEvent object into a dictionary
183+
# TODO: use data or some other means to get the offset
184+
kwargs = make_object(
185+
KeyValues, get_object_pointer(game_event).get_pointer(8)).as_dict()
186+
179187
# Loop through all callbacks in the pre-event's list
180188
for callback in pre_event_manager[event_name]:
181189

182190
# Use try/except in case an error occurs during in the callback
183191
try:
184192

185193
# Call the callback and get its return value
186-
current_action = callback(game_event)
194+
current_action = callback(**kwargs)
187195

188196
# Is the return value invalid?
189197
if (current_action is not None and

addons/source-python/packages/source-python/events/listener.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
# Source.Python Imports
99
# Hooks
1010
from hooks.exceptions import except_hooks
11+
# KeyValues
12+
from keyvalues import KeyValues
1113
# Loggers
1214
from loggers import _sp_logger
15+
# Memory
16+
from memory import get_object_pointer
17+
from memory import make_object
1318

1419

1520
# =============================================================================
@@ -82,14 +87,19 @@ def remove(self, callback):
8287

8388
def fire_game_event(self, game_event):
8489
"""Loop through all callbacks for an event and calls them."""
90+
# Convert the GameEvent object into a dictionary
91+
# TODO: use data or some other means to get the offset
92+
kwargs = make_object(
93+
KeyValues, get_object_pointer(game_event).get_pointer(8)).as_dict()
94+
8595
# Loop through each callback in the event's list
8696
for callback in self:
8797

8898
# Try to call the callback
8999
try:
90100

91101
# Call the callback
92-
callback(game_event)
102+
callback(**kwargs)
93103

94104
# Was an error encountered?
95105
except:

addons/source-python/packages/source-python/settings/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def cursor(self):
150150
"""Return the cursor instance."""
151151
return self._cursor
152152

153-
def server_spawn(self, game_event):
153+
def server_spawn(self, **kwargs):
154154
"""Store the dictionary to the database on map change."""
155155
self.connection.commit()
156156

addons/source-python/packages/source-python/stringtables/downloads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _add_to_download_table(self, item):
8282
# Add the given file to the downloadables table.
8383
self.download_table.add_string(item, item)
8484

85-
def server_spawn(self, game_event):
85+
def server_spawn(self, **kwargs):
8686
"""Add all items stored as downloadables to the stringtable."""
8787
# Refresh the downloadables table instance
8888
self._refresh_table_instance()

0 commit comments

Comments
 (0)