55# =============================================================================
66# >> IMPORTS
77# =============================================================================
8- # Python Imports
9- # Collections
8+ # Python
109from collections import OrderedDict
11- # Contextlib
1210from contextlib import suppress
1311
14- # Source.Python Imports
15- # Engines
12+ # Source.Python
13+ from cvars import ConVar
1614from engines .server import engine_server
17- # Menus
1815from menus import PagedMenu
1916from menus import PagedOption
20- # Messages
2117from messages import SayText
22- # Players
2318from players .helpers import uniqueid_from_index
24- # Settings
2519from settings import _settings_strings
2620from settings .storage import _player_settings_storage
2721
@@ -103,6 +97,16 @@ def menu(self):
10397 """Return the setting's menu."""
10498 return self ._menu
10599
100+ @property
101+ def _type (self ):
102+ """Inheriting class must define attribute."""
103+ raise NotImplementedError ('No _type defined for class.' )
104+
105+ @property
106+ def _is_valid_setting (self ):
107+ """Inheriting class must define method."""
108+ raise NotImplementedError ('No _is_valid_setting defined for class.' )
109+
106110 def get_setting (self , index ):
107111 """Return the setting value for the given player index."""
108112 # Get the client's convar value
@@ -112,7 +116,7 @@ def get_setting(self, index):
112116 with suppress (ValueError ):
113117
114118 # Typecast the given value
115- value = self ._type (value )
119+ value = self ._typecase_value (value )
116120
117121 # Is the given value a proper one for the convar?
118122 if self ._is_valid_setting (value ):
@@ -136,7 +140,7 @@ def get_setting(self, index):
136140 with suppress (ValueError ):
137141
138142 # Typecast the given value
139- value = self ._type (value )
143+ value = self ._typecast_value (value )
140144
141145 # Is the given value a proper one for the convar?
142146 if self ._is_valid_setting (value ):
@@ -145,8 +149,23 @@ def get_setting(self, index):
145149 return value
146150
147151 # Return the default value
152+ if isinstance (self .default , ConVar ):
153+ return self ._typecast_default_convar ()
148154 return self .default
149155
156+ def _typecast_value (self , value ):
157+ """Cast the value to the proper type."""
158+ return self ._type (value )
159+
160+ def _typecast_default_convar (self ):
161+ """Return the typecasted value of the default ConVar."""
162+ return getattr (
163+ self .default ,
164+ 'get_{value_type}' .format (
165+ value_type = str (self ._type )
166+ )
167+ )()
168+
150169 def _menu_build (self , menu , index ):
151170 """Set the default value in the menu description."""
152171 self .menu .description .tokens = {'value' : self .get_setting (index )}
@@ -212,6 +231,24 @@ class _IntegerSetting(_NumericalSetting):
212231 _type = int
213232
214233
234+ class _BoolSetting (_SettingsType ):
235+ """Class used to store boolean value settings."""
236+
237+ _type = bool
238+
239+ def _typecast_value (self , value ):
240+ """Cast the given value to a boolean."""
241+ try :
242+ value = int (value )
243+ return bool (value )
244+ except ValueError :
245+ return ''
246+
247+ def _is_valid_setting (self , value ):
248+ """Return whether the given value is a valid boolean value."""
249+ return isinstance (value , self ._type )
250+
251+
215252class _StringSetting (_SettingsType ):
216253 """Class used to store string value settings with available options."""
217254
0 commit comments