|
8 | 8 | # Source.Python Imports
|
9 | 9 | # Core
|
10 | 10 | from core import AutoUnload
|
| 11 | +# Filters |
| 12 | +from filters.entities import EntityIter |
11 | 13 | # Entities
|
12 | 14 | from entities.entity import Entity
|
13 | 15 | from entities.helpers import index_from_inthandle
|
14 | 16 | # Listeners
|
| 17 | +from listeners import on_networked_entity_created_listener_manager |
15 | 18 | from listeners import on_networked_entity_deleted_listener_manager
|
16 | 19 |
|
17 | 20 |
|
18 | 21 | # ============================================================================
|
19 | 22 | # >> ALL DECLARATION
|
20 | 23 | # ============================================================================
|
21 | 24 | __all__ = ('EntityDictionary',
|
| 25 | + 'SyncedEntityDictionary', |
22 | 26 | )
|
23 | 27 |
|
24 | 28 |
|
@@ -140,3 +144,89 @@ def _unload_instance(self):
|
140 | 144 | """Unregister our networked entity deletion listener."""
|
141 | 145 | on_networked_entity_deleted_listener_manager.unregister_listener(
|
142 | 146 | 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() |
0 commit comments