14
14
from path import Path
15
15
16
16
# Source.Python Imports
17
+ # Core
18
+ from core import AutoUnload
17
19
# Engines
18
20
from engines .server import engine_server
21
+ # Events
22
+ from events .manager import event_registry
19
23
# Stringtables
20
24
from stringtables import INVALID_STRING_INDEX
21
25
from stringtables import string_tables
@@ -39,7 +43,7 @@ class PrecacheError(Exception):
39
43
"""Object was not able to be precached due to limit being reached."""
40
44
41
45
42
- class _PrecacheBase (Path ):
46
+ class _PrecacheBase (Path , AutoUnload ):
43
47
44
48
"""Base precache class used to interact with a specific object."""
45
49
@@ -52,6 +56,12 @@ def __init__(self, path, download=False):
52
56
# Call Path's __init__ with the given path
53
57
super (_PrecacheBase , self ).__init__ (path )
54
58
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
+
55
65
# Should the path be added to the downloadables?
56
66
if download :
57
67
@@ -71,25 +81,24 @@ def index(self):
71
81
# Return the precache index
72
82
return index
73
83
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
-
83
84
# If the object was not precached, raise an error
84
85
raise PrecacheError (
85
86
'{0} was not able to be precached due to the ' .format (self ) +
86
87
'{0} table reaching its limit.' .format (self ._precache_table ))
87
88
89
+ def _server_spawn (self , game_event ):
90
+ """Precache the object on map change."""
91
+ self ._precache_method (self )
92
+
88
93
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
90
96
with suppress (AttributeError ):
91
97
self ._downloads ._unload_instance ()
92
98
99
+ # Unregister the server_spawn event
100
+ event_registry .unregister_for_event ('server_spawn' , self ._server_spawn )
101
+
93
102
94
103
class Decal (_PrecacheBase ):
95
104
0 commit comments