Skip to content

Commit 3842149

Browse files
committed
Added cached properties implementation.
1 parent e65bf54 commit 3842149

10 files changed

Lines changed: 672 additions & 23 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
core.cache module
2+
==================
3+
4+
.. automodule:: core.cache
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
.. autoclass:: _core._cache.CachedProperty
10+
:members:
11+
:undoc-members:
12+
:show-inheritance:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ../core/cache.py
2+
3+
"""Provides caching functionality.
4+
5+
.. data:: cached_property
6+
An alias of :class:`core.cache.CachedProperty`.
7+
"""
8+
9+
10+
# =============================================================================
11+
# >> FORWARD IMPORTS
12+
# =============================================================================
13+
# Source.Python Imports
14+
# Core
15+
from _core._cache import CachedProperty
16+
from _core._cache import cached_property
17+
18+
19+
# =============================================================================
20+
# >> ALL DECLARATION
21+
# =============================================================================
22+
__all__ = [
23+
'CachedProperty',
24+
'cached_property'
25+
]

addons/source-python/packages/source-python/entities/_base.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# Core
1818
from core import GAME_NAME
1919
from core import BoostPythonClass
20+
from core.cache import cached_property
2021
# Entities
2122
from entities.constants import INVALID_ENTITY_INDEX
2223
# Engines
@@ -295,7 +296,7 @@ def is_networked(self):
295296
"""
296297
return True
297298

298-
@property
299+
@cached_property
299300
def index(self):
300301
"""Return the entity's index.
301302
@@ -316,30 +317,30 @@ def owner(self):
316317
except (ValueError, OverflowError):
317318
return None
318319

319-
@property
320+
@cached_property
320321
def server_classes(self):
321322
"""Yield all server classes for the entity."""
322323
yield from server_classes.get_entity_server_classes(self)
323324

324-
@property
325+
@cached_property
325326
def properties(self):
326327
"""Iterate over all descriptors available for the entity."""
327328
for server_class in self.server_classes:
328329
yield from server_class.properties
329330

330-
@property
331+
@cached_property
331332
def inputs(self):
332333
"""Iterate over all inputs available for the entity."""
333334
for server_class in self.server_classes:
334335
yield from server_class.inputs
335336

336-
@property
337+
@cached_property
337338
def outputs(self):
338339
"""Iterate over all outputs available for the entity."""
339340
for server_class in self.server_classes:
340341
yield from server_class.outputs
341342

342-
@property
343+
@cached_property
343344
def keyvalues(self):
344345
"""Iterate over all entity keyvalues available for the entity.
345346

addons/source-python/packages/source-python/players/_base.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from bitbuffers import BitBufferWrite
1515
# Core
1616
from core import GAME_NAME
17+
from core.cache import cached_property
1718
# Engines
1819
from engines.server import server
1920
from engines.server import engine_server
@@ -89,7 +90,6 @@ def __init__(self, index, caching=True):
8990
"""
9091
PlayerMixin.__init__(self, index)
9192
Entity.__init__(self, index)
92-
object.__setattr__(self, '_playerinfo', None)
9393

9494
@classmethod
9595
def from_userid(cls, userid):
@@ -101,7 +101,7 @@ def from_userid(cls, userid):
101101
"""
102102
return cls(index_from_userid(userid))
103103

104-
@property
104+
@cached_property
105105
def raw_steamid(self):
106106
"""Return the player's unformatted SteamID.
107107
@@ -117,26 +117,23 @@ def permissions(self):
117117
"""
118118
return auth_manager.get_player_permissions_from_steamid(self.steamid)
119119

120-
@property
120+
@cached_property
121121
def playerinfo(self):
122122
"""Return player information.
123123
124124
:rtype: PlayerInfo
125125
"""
126-
if self._playerinfo is None:
127-
playerinfo = playerinfo_from_index(self.index)
128-
object.__setattr__(self, '_playerinfo', playerinfo)
129-
return self._playerinfo
126+
return playerinfo_from_index(self.index)
130127

131-
@property
128+
@cached_property
132129
def userid(self):
133130
"""Return the player's userid.
134131
135132
:rtype: int
136133
"""
137134
return self.playerinfo.userid
138135

139-
@property
136+
@cached_property
140137
def steamid(self):
141138
"""Return the player's SteamID.
142139
@@ -157,15 +154,15 @@ def set_name(self, name):
157154

158155
name = property(get_name, set_name)
159156

160-
@property
157+
@cached_property
161158
def client(self):
162159
"""Return the player's client instance.
163160
164161
:rtype: Client
165162
"""
166163
return server.get_client(self.index - 1)
167164

168-
@property
165+
@cached_property
169166
def base_client(self):
170167
"""Return the player's base client instance.
171168
@@ -174,15 +171,15 @@ def base_client(self):
174171
from players import BaseClient
175172
return make_object(BaseClient, get_object_pointer(self.client) - 4)
176173

177-
@property
174+
@cached_property
178175
def uniqueid(self):
179176
"""Return the player's unique ID.
180177
181178
:rtype: str
182179
"""
183180
return uniqueid_from_playerinfo(self.playerinfo)
184181

185-
@property
182+
@cached_property
186183
def address(self):
187184
"""Return the player's IP address and port.
188185
@@ -208,13 +205,15 @@ def is_fake_client(self):
208205
"""
209206
return self.playerinfo.is_fake_client()
210207

208+
@cached_property
211209
def is_hltv(self):
212210
"""Return whether the player is HLTV.
213211
214212
:rtype: bool
215213
"""
216214
return self.playerinfo.is_hltv()
217215

216+
@cached_property
218217
def is_bot(self):
219218
"""Return whether the player is a bot.
220219
@@ -249,7 +248,7 @@ def set_team(self, value):
249248

250249
team = property(get_team, set_team)
251250

252-
@property
251+
@cached_property
253252
def language(self):
254253
"""Return the player's language.
255254

src/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ Set(SOURCEPYTHON_CORE_MODULE_SOURCES
153153
core/modules/core/core_wrap.cpp
154154
)
155155

156+
Set(SOURCEPYTHON_CORE_CACHE_MODULE_HEADERS
157+
core/modules/core/core_cache.h
158+
)
159+
160+
Set(SOURCEPYTHON_CORE_CACHE_MODULE_SOURCES
161+
core/modules/core/core_cache.cpp
162+
core/modules/core/core_cache_wrap.cpp
163+
)
164+
156165
# ------------------------------------------------------------------
157166
# Cvars module.
158167
# ------------------------------------------------------------------
@@ -466,6 +475,9 @@ Set(SOURCEPYTHON_WEAPONS_MODULE_SOURCES
466475
# All module source files
467476
# ------------------------------------------------------------------
468477
Set(SOURCEPYTHON_MODULE_FILES
478+
${SOURCEPYTHON_CORE_CACHE_MODULE_HEADERS}
479+
${SOURCEPYTHON_CORE_CACHE_MODULE_SOURCES}
480+
469481
# CFunctionInfo must be exposed at first
470482
${SOURCEPYTHON_MEMORY_MODULE_HEADERS}
471483
${SOURCEPYTHON_MEMORY_MODULE_SOURCES}

0 commit comments

Comments
 (0)