Skip to content

Commit 4d63cb0

Browse files
committed
Changed engines.precache classes to precache on instantiation and server_spawn instead of relying on calling their index property.
1 parent cabf965 commit 4d63cb0

File tree

1 file changed

+20
-11
lines changed
  • addons/source-python/packages/source-python/engines

1 file changed

+20
-11
lines changed

Diff for: addons/source-python/packages/source-python/engines/precache.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
from path import Path
1515

1616
# Source.Python Imports
17+
# Core
18+
from core import AutoUnload
1719
# Engines
1820
from engines.server import engine_server
21+
# Events
22+
from events.manager import event_registry
1923
# Stringtables
2024
from stringtables import INVALID_STRING_INDEX
2125
from stringtables import string_tables
@@ -39,7 +43,7 @@ class PrecacheError(Exception):
3943
"""Object was not able to be precached due to limit being reached."""
4044

4145

42-
class _PrecacheBase(Path):
46+
class _PrecacheBase(Path, AutoUnload):
4347

4448
"""Base precache class used to interact with a specific object."""
4549

@@ -52,6 +56,12 @@ def __init__(self, path, download=False):
5256
# Call Path's __init__ with the given path
5357
super(_PrecacheBase, self).__init__(path)
5458

59+
# Precache the instance
60+
self._precache_method(self)
61+
62+
# Register the server_spawn event to precache every map change
63+
event_registry.register_for_event('server_spawn', self._server_spawn)
64+
5565
# Should the path be added to the downloadables?
5666
if download:
5767

@@ -71,25 +81,24 @@ def index(self):
7181
# Return the precache index
7282
return index
7383

74-
# Attempt to precache the object
75-
index = self._precache_method(self)
76-
77-
# Was the object able to be precached?
78-
if index != INVALID_STRING_INDEX:
79-
80-
# Return the precache index
81-
return index
82-
8384
# If the object was not precached, raise an error
8485
raise PrecacheError(
8586
'{0} was not able to be precached due to the '.format(self) +
8687
'{0} table reaching its limit.'.format(self._precache_table))
8788

89+
def _server_spawn(self, game_event):
90+
"""Precache the object on map change."""
91+
self._precache_method(self)
92+
8893
def _unload_instance(self):
89-
"""Remove the path from the downloads list."""
94+
"""Remove from the downloads list and unregister server_spawn."""
95+
# Remove the path from the downloads list
9096
with suppress(AttributeError):
9197
self._downloads._unload_instance()
9298

99+
# Unregister the server_spawn event
100+
event_registry.unregister_for_event('server_spawn', self._server_spawn)
101+
93102

94103
class Decal(_PrecacheBase):
95104

0 commit comments

Comments
 (0)