Skip to content

Commit efd119f

Browse files
committed
Added SyncedEntityDictionary.
1 parent 543ebcf commit efd119f

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

Diff for: addons/source-python/packages/source-python/entities/dictionary.py

+90
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
# Source.Python Imports
99
# Core
1010
from core import AutoUnload
11+
# Filters
12+
from filters.entities import EntityIter
1113
# Entities
1214
from entities.entity import Entity
1315
from entities.helpers import index_from_inthandle
1416
# Listeners
17+
from listeners import on_networked_entity_created_listener_manager
1518
from listeners import on_networked_entity_deleted_listener_manager
1619

1720

1821
# ============================================================================
1922
# >> ALL DECLARATION
2023
# ============================================================================
2124
__all__ = ('EntityDictionary',
25+
'SyncedEntityDictionary',
2226
)
2327

2428

@@ -140,3 +144,89 @@ def _unload_instance(self):
140144
"""Unregister our networked entity deletion listener."""
141145
on_networked_entity_deleted_listener_manager.unregister_listener(
142146
self._on_networked_entity_deleted)
147+
148+
149+
class SyncedEntityDictionary(EntityDictionary):
150+
"""Helper class used to keep entity instances synced with the game."""
151+
152+
def __init__(
153+
self, factory=Entity, iterator=EntityIter(), *args, **kwargs):
154+
"""Initializes the dictionary.
155+
156+
:param callable factory:
157+
Factory class or function.
158+
159+
Factory signature: index, *args, **kwargs
160+
:param iterable iterator:
161+
Iterator used to generates instances on initialization and resync.
162+
:param tuple args:
163+
Arguments passed to the factory class or function.
164+
:param dict kwargs:
165+
Keyword arguments passed to the factory class or function.
166+
167+
:raise ValueError:
168+
If the factory is set to None.
169+
"""
170+
if factory is None:
171+
raise ValueError(
172+
'Factory cannot be None for synced dictionaries.')
173+
174+
# Initialize the dictionary
175+
super().__init__(factory, *args, **kwargs)
176+
177+
# Store the given iterator and resync the dictionary
178+
self._iterator = iterator
179+
self.resync()
180+
181+
# Register our networked entity creation listener
182+
on_networked_entity_created_listener_manager.register_listener(
183+
self._on_networked_entity_created)
184+
185+
def __missing__(self, index):
186+
"""Raises a KeyError, because creation of missing instances is not
187+
allowed for synced dictionaries.
188+
"""
189+
raise KeyError(index)
190+
191+
def resync(self):
192+
"""Resync the dictionary with the game."""
193+
# Clear the dictionary
194+
self.clear()
195+
196+
# Loop through all entities and add them to the dictionary
197+
for entity in self._iterator:
198+
self._on_networked_entity_created(entity)
199+
200+
def on_automatically_created(self, index):
201+
"""Called when an index is automatically added.
202+
203+
:param int index:
204+
The index of the entity instance being added.
205+
"""
206+
207+
def _on_networked_entity_created(self, entity):
208+
"""Internal networked entity creation callback.
209+
210+
:param Entity entity:
211+
The networked entity being created.
212+
"""
213+
# Validate the entity
214+
if not self._iterator._is_valid(entity):
215+
return
216+
217+
# Get the index of the entity
218+
index = entity.index
219+
220+
# Add the index to the dictionary
221+
super().__missing__(index)
222+
223+
# Call the creation callback for the index
224+
self.on_automatically_created(index)
225+
226+
def _unload_instance(self):
227+
"""Unregister our networked entity creation listener."""
228+
on_networked_entity_created_listener_manager.unregister_listener(
229+
self._on_networked_entity_created)
230+
231+
# Unload the dictionary
232+
super()._unload_instance()

Diff for: addons/source-python/packages/source-python/loggers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def _log(self, level, msg, *args, **kwargs):
253253

254254
# Prepend prefix
255255
if prepend_prefix:
256-
message = prefix + message
256+
message = self.prefix + message
257257

258258
# Print to the main log
259259
engine_server.log_print(message + '\n')

0 commit comments

Comments
 (0)