@@ -25,191 +25,250 @@ internal enum EConVarType : int
2525 EConVarType_MAX
2626} ;
2727
28- internal class ConVarService : IConVarService {
28+ internal class ConVarService : IConVarService
29+ {
2930
30- public IConVar < T > ? Find < T > ( string name ) {
31+ public IConVar < T > ? Find < T > ( string name )
32+ {
3133
32- if ( ! NativeConvars . ExistsConvar ( name ) ) {
34+ if ( ! NativeConvars . ExistsConvar ( name ) )
35+ {
3336 return null ;
3437 }
3538
3639 return new ConVar < T > ( name ) ;
3740
3841 }
3942
40- public IConVar < T > Create < T > ( string name , string helpMessage , T defaultValue , ConvarFlags flags = ConvarFlags . NONE ) {
41- if ( NativeConvars . ExistsConvar ( name ) ) {
43+ public IConVar < T > Create < T > ( string name , string helpMessage , T defaultValue , ConvarFlags flags = ConvarFlags . NONE )
44+ {
45+ if ( NativeConvars . ExistsConvar ( name ) )
46+ {
4247 throw new Exception ( $ "Convar { name } already exists.") ;
4348 }
4449
45- if ( defaultValue is bool boolValue ) {
50+ if ( defaultValue is bool boolValue )
51+ {
4652 NativeConvars . CreateConvarBool ( name , ( int ) EConVarType . EConVarType_Bool , ( ulong ) flags , helpMessage , boolValue , 0 , 0 ) ;
4753 }
48- else if ( defaultValue is short shortValue ) {
54+ else if ( defaultValue is short shortValue )
55+ {
4956 NativeConvars . CreateConvarInt16 ( name , ( int ) EConVarType . EConVarType_Int16 , ( ulong ) flags , helpMessage , shortValue , 0 , 0 ) ;
5057 }
51- else if ( defaultValue is ushort ushortValue ) {
58+ else if ( defaultValue is ushort ushortValue )
59+ {
5260 NativeConvars . CreateConvarUInt16 ( name , ( int ) EConVarType . EConVarType_UInt16 , ( ulong ) flags , helpMessage , ushortValue , 0 , 0 ) ;
5361 }
54- else if ( defaultValue is int intValue ) {
62+ else if ( defaultValue is int intValue )
63+ {
5564 NativeConvars . CreateConvarInt32 ( name , ( int ) EConVarType . EConVarType_Int32 , ( ulong ) flags , helpMessage , intValue , 0 , 0 ) ;
5665 }
57- else if ( defaultValue is uint uintValue ) {
66+ else if ( defaultValue is uint uintValue )
67+ {
5868 NativeConvars . CreateConvarUInt32 ( name , ( int ) EConVarType . EConVarType_UInt32 , ( ulong ) flags , helpMessage , uintValue , 0 , 0 ) ;
5969 }
60- else if ( defaultValue is long longValue ) {
70+ else if ( defaultValue is long longValue )
71+ {
6172 NativeConvars . CreateConvarInt64 ( name , ( int ) EConVarType . EConVarType_Int64 , ( ulong ) flags , helpMessage , longValue , 0 , 0 ) ;
6273 }
63- else if ( defaultValue is ulong ulongValue ) {
74+ else if ( defaultValue is ulong ulongValue )
75+ {
6476 NativeConvars . CreateConvarUInt64 ( name , ( int ) EConVarType . EConVarType_UInt64 , ( ulong ) flags , helpMessage , ulongValue , 0 , 0 ) ;
6577 }
66- else if ( defaultValue is float floatValue ) {
78+ else if ( defaultValue is float floatValue )
79+ {
6780 NativeConvars . CreateConvarFloat ( name , ( int ) EConVarType . EConVarType_Float32 , ( ulong ) flags , helpMessage , floatValue , 0 , 0 ) ;
6881 }
69- else if ( defaultValue is double doubleValue ) {
82+ else if ( defaultValue is double doubleValue )
83+ {
7084 NativeConvars . CreateConvarDouble ( name , ( int ) EConVarType . EConVarType_Float64 , ( ulong ) flags , helpMessage , doubleValue , 0 , 0 ) ;
7185 }
72- else if ( defaultValue is Vector2D vector2Value ) {
86+ else if ( defaultValue is Vector2D vector2Value )
87+ {
7388 NativeConvars . CreateConvarVector2D ( name , ( int ) EConVarType . EConVarType_Vector2 , ( ulong ) flags , helpMessage , vector2Value , 0 , 0 ) ;
7489 }
75- else if ( defaultValue is Vector vector3Value ) {
90+ else if ( defaultValue is Vector vector3Value )
91+ {
7692 NativeConvars . CreateConvarVector ( name , ( int ) EConVarType . EConVarType_Vector3 , ( ulong ) flags , helpMessage , vector3Value , 0 , 0 ) ;
7793 }
78- else if ( defaultValue is Vector4D vector4Value ) {
94+ else if ( defaultValue is Vector4D vector4Value )
95+ {
7996 NativeConvars . CreateConvarVector4D ( name , ( int ) EConVarType . EConVarType_Vector4 , ( ulong ) flags , helpMessage , vector4Value , 0 , 0 ) ;
8097 }
81- else if ( defaultValue is QAngle qAngleValue ) {
98+ else if ( defaultValue is QAngle qAngleValue )
99+ {
82100 NativeConvars . CreateConvarQAngle ( name , ( int ) EConVarType . EConVarType_Qangle , ( ulong ) flags , helpMessage , qAngleValue , 0 , 0 ) ;
83101 }
84- else if ( defaultValue is Color colorValue ) {
102+ else if ( defaultValue is Color colorValue )
103+ {
85104 NativeConvars . CreateConvarColor ( name , ( int ) EConVarType . EConVarType_Color , ( ulong ) flags , helpMessage , colorValue , 0 , 0 ) ;
86105 }
87- else if ( defaultValue is string stringValue ) {
106+ else if ( defaultValue is string stringValue )
107+ {
88108 NativeConvars . CreateConvarString ( name , ( int ) EConVarType . EConVarType_String , ( ulong ) flags , helpMessage , stringValue , 0 , 0 ) ;
89109 }
90- else {
110+ else
111+ {
91112 throw new Exception ( $ "Unsupported type { typeof ( T ) } .") ;
92113 }
93114
94115 return new ConVar < T > ( name ) ;
95116 }
96-
97- public IConVar < T > Create < T > ( string name , string helpMessage , T defaultValue , T ? minValue = null , T ? maxValue = null , ConvarFlags flags = ConvarFlags . NONE ) where T : unmanaged {
98117
99- if ( NativeConvars . ExistsConvar ( name ) ) {
118+ public IConVar < T > Create < T > ( string name , string helpMessage , T defaultValue , T ? minValue , T ? maxValue , ConvarFlags flags = ConvarFlags . NONE ) where T : unmanaged
119+ {
120+
121+ if ( NativeConvars . ExistsConvar ( name ) )
122+ {
100123 throw new Exception ( $ "Convar { name } already exists.") ;
101124 }
102- unsafe {
125+ unsafe
126+ {
103127
104- if ( defaultValue is short shortValue ) {
128+ if ( defaultValue is short shortValue )
129+ {
105130 short * pMin = stackalloc short [ 1 ] ;
106- if ( minValue . HasValue ) {
131+ if ( minValue . HasValue )
132+ {
107133 pMin [ 0 ] = ( short ) ( object ) minValue . Value ;
108134 }
109135
110136 short * pMax = stackalloc short [ 1 ] ;
111- if ( maxValue . HasValue ) {
137+ if ( maxValue . HasValue )
138+ {
112139 pMax [ 0 ] = ( short ) ( object ) maxValue . Value ;
113140 }
114141
115142 NativeConvars . CreateConvarInt16 ( name , ( int ) EConVarType . EConVarType_Int16 , ( ulong ) flags , helpMessage , shortValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
116143 }
117- else if ( defaultValue is ushort ushortValue ) {
144+ else if ( defaultValue is ushort ushortValue )
145+ {
118146 ushort * pMin = stackalloc ushort [ 1 ] ;
119- if ( minValue . HasValue ) {
147+ if ( minValue . HasValue )
148+ {
120149 pMin [ 0 ] = ( ushort ) ( object ) minValue . Value ;
121150 }
122151
123152 ushort * pMax = stackalloc ushort [ 1 ] ;
124- if ( maxValue . HasValue ) {
153+ if ( maxValue . HasValue )
154+ {
125155 pMax [ 0 ] = ( ushort ) ( object ) maxValue . Value ;
126156 }
127157
128158 NativeConvars . CreateConvarUInt16 ( name , ( int ) EConVarType . EConVarType_UInt16 , ( ulong ) flags , helpMessage , ushortValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
129159 }
130- else if ( defaultValue is int intValue ) {
160+ else if ( defaultValue is int intValue )
161+ {
131162 int * pMin = stackalloc int [ 1 ] ;
132- if ( minValue . HasValue ) {
163+ if ( minValue . HasValue )
164+ {
133165 pMin [ 0 ] = ( int ) ( object ) minValue . Value ;
134166 }
135167
136168 int * pMax = stackalloc int [ 1 ] ;
137- if ( maxValue . HasValue ) {
169+ if ( maxValue . HasValue )
170+ {
138171 pMax [ 0 ] = ( int ) ( object ) maxValue . Value ;
139172 }
140173
141174 NativeConvars . CreateConvarInt32 ( name , ( int ) EConVarType . EConVarType_Int32 , ( ulong ) flags , helpMessage , intValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
142175 }
143- else if ( defaultValue is uint uintValue ) {
176+ else if ( defaultValue is uint uintValue )
177+ {
144178 uint * pMin = stackalloc uint [ 1 ] ;
145- if ( minValue . HasValue ) {
179+ if ( minValue . HasValue )
180+ {
146181 pMin [ 0 ] = ( uint ) ( object ) minValue . Value ;
147182 }
148183
149184 uint * pMax = stackalloc uint [ 1 ] ;
150- if ( maxValue . HasValue ) {
185+ if ( maxValue . HasValue )
186+ {
151187 pMax [ 0 ] = ( uint ) ( object ) maxValue . Value ;
152188 }
153189
154190 NativeConvars . CreateConvarUInt32 ( name , ( int ) EConVarType . EConVarType_UInt32 , ( ulong ) flags , helpMessage , uintValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
155191 }
156- else if ( defaultValue is long longValue ) {
192+ else if ( defaultValue is long longValue )
193+ {
157194 long * pMin = stackalloc long [ 1 ] ;
158- if ( minValue . HasValue ) {
195+ if ( minValue . HasValue )
196+ {
159197 pMin [ 0 ] = ( long ) ( object ) minValue . Value ;
160198 }
161199
162200 long * pMax = stackalloc long [ 1 ] ;
163- if ( maxValue . HasValue ) {
201+ if ( maxValue . HasValue )
202+ {
164203 pMax [ 0 ] = ( long ) ( object ) maxValue . Value ;
165204 }
166205
167206 NativeConvars . CreateConvarInt64 ( name , ( int ) EConVarType . EConVarType_Int64 , ( ulong ) flags , helpMessage , longValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
168207 }
169- else if ( defaultValue is ulong ulongValue ) {
208+ else if ( defaultValue is ulong ulongValue )
209+ {
170210 ulong * pMin = stackalloc ulong [ 1 ] ;
171- if ( minValue . HasValue ) {
211+ if ( minValue . HasValue )
212+ {
172213 pMin [ 0 ] = ( ulong ) ( object ) minValue . Value ;
173214 }
174215
175216 ulong * pMax = stackalloc ulong [ 1 ] ;
176- if ( maxValue . HasValue ) {
217+ if ( maxValue . HasValue )
218+ {
177219 pMax [ 0 ] = ( ulong ) ( object ) maxValue . Value ;
178220 }
179221
180222 NativeConvars . CreateConvarUInt64 ( name , ( int ) EConVarType . EConVarType_UInt64 , ( ulong ) flags , helpMessage , ulongValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
181223 }
182- else if ( defaultValue is float floatValue ) {
224+ else if ( defaultValue is float floatValue )
225+ {
183226 float * pMin = stackalloc float [ 1 ] ;
184- if ( minValue . HasValue ) {
227+ if ( minValue . HasValue )
228+ {
185229 pMin [ 0 ] = ( float ) ( object ) minValue . Value ;
186230 }
187231
188232 float * pMax = stackalloc float [ 1 ] ;
189- if ( maxValue . HasValue ) {
233+ if ( maxValue . HasValue )
234+ {
190235 pMax [ 0 ] = ( float ) ( object ) maxValue . Value ;
191236 }
192237
193238 NativeConvars . CreateConvarFloat ( name , ( int ) EConVarType . EConVarType_Float32 , ( ulong ) flags , helpMessage , floatValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
194239 }
195- else if ( defaultValue is double doubleValue ) {
240+ else if ( defaultValue is double doubleValue )
241+ {
196242 double * pMin = stackalloc double [ 1 ] ;
197- if ( minValue . HasValue ) {
243+ if ( minValue . HasValue )
244+ {
198245 pMin [ 0 ] = ( double ) ( object ) minValue . Value ;
199246 }
200247
201248 double * pMax = stackalloc double [ 1 ] ;
202- if ( maxValue . HasValue ) {
249+ if ( maxValue . HasValue )
250+ {
203251 pMax [ 0 ] = ( double ) ( object ) maxValue . Value ;
204252 }
205253
206254 NativeConvars . CreateConvarDouble ( name , ( int ) EConVarType . EConVarType_Float64 , ( ulong ) flags , helpMessage , doubleValue , minValue . HasValue ? ( nint ) pMin : 0 , maxValue . HasValue ? ( nint ) pMax : 0 ) ;
207255 }
208- else {
256+ else
257+ {
209258 throw new Exception ( $ "You can't assign min and max values to { typeof ( T ) } .") ;
210259 }
211260 }
212261
213262 return new ConVar < T > ( name ) ;
214263 }
264+
265+ public IConVar < T > CreateOrFind < T > ( string name , string helpMessage , T defaultValue , ConvarFlags flags = ConvarFlags . NONE )
266+ {
267+ return NativeConvars . ExistsConvar ( name ) ? new ConVar < T > ( name ) : Create ( name , helpMessage , defaultValue , flags ) ;
268+ }
269+
270+ public IConVar < T > CreateOrFind < T > ( string name , string helpMessage , T defaultValue , T ? minValue , T ? maxValue , ConvarFlags flags = ConvarFlags . NONE ) where T : unmanaged
271+ {
272+ return NativeConvars . ExistsConvar ( name ) ? new ConVar < T > ( name ) : Create ( name , helpMessage , defaultValue , minValue , maxValue , flags ) ;
273+ }
215274}
0 commit comments