44
55namespace SwiftlyS2 . Core . Convars ;
66
7- internal enum EConVarType : int
8- {
9- EConVarType_Invalid = - 1 ,
10- EConVarType_Bool ,
11- EConVarType_Int16 ,
12- EConVarType_UInt16 ,
13- EConVarType_Int32 ,
14- EConVarType_UInt32 ,
15- EConVarType_Int64 ,
16- EConVarType_UInt64 ,
17- EConVarType_Float32 ,
18- EConVarType_Float64 ,
19- EConVarType_String ,
20- EConVarType_Color ,
21- EConVarType_Vector2 ,
22- EConVarType_Vector3 ,
23- EConVarType_Vector4 ,
24- EConVarType_Qangle ,
25- EConVarType_MAX
26- } ;
27-
287internal class ConVarService : IConVarService
298{
309
@@ -272,31 +251,36 @@ public IConVar<T> CreateOrFind<T>( string name, string helpMessage, T defaultVal
272251 return NativeConvars . ExistsConvar ( name ) ? new ConVar < T > ( name ) : Create ( name , helpMessage , defaultValue , minValue , maxValue , flags ) ;
273252 }
274253
275- public unsafe string ? ReadValueAsString ( string name )
254+ public string ? ReadValueAsString ( string name )
276255 {
277256 if ( ! NativeConvars . ExistsConvar ( name ) ) return null ;
278257
279258 var type = ( EConVarType ) NativeConvars . GetConvarType ( name ) ;
280259
281260 var valuePtr = NativeConvars . GetValuePtr ( name ) ;
282261
283- return type switch {
284- EConVarType . EConVarType_Bool => * ( byte * ) valuePtr == 1 ? "1" : "0" ,
262+ return GetValueFromPtr ( type , valuePtr ) ;
263+ }
264+
265+ private unsafe string GetValueFromPtr ( EConVarType kind , nint ptr )
266+ {
267+ return kind switch {
268+ EConVarType . EConVarType_Bool => * ( byte * ) ptr == 1 ? "1" : "0" ,
285269 EConVarType. EConVarType_Invalid => string . Empty ,
286- EConVarType . EConVarType_Int16 => ( * ( short * ) valuePtr ) . ToString ( ) ,
287- EConVarType. EConVarType_UInt16 => ( * ( ushort * ) valuePtr ) . ToString ( ) ,
288- EConVarType . EConVarType_Int32 => ( * ( int * ) valuePtr ) . ToString ( ) ,
289- EConVarType . EConVarType_UInt32 => ( * ( uint * ) valuePtr ) . ToString ( ) ,
290- EConVarType . EConVarType_Int64 => ( * ( long * ) valuePtr ) . ToString ( ) ,
291- EConVarType . EConVarType_UInt64 => ( * ( ulong * ) valuePtr ) . ToString ( ) ,
292- EConVarType . EConVarType_Float32 => ( * ( float * ) valuePtr ) . ToString ( ) ,
293- EConVarType . EConVarType_Float64 => ( * ( double * ) valuePtr ) . ToString ( ) ,
294- EConVarType . EConVarType_String => ( ( CUtlString * ) valuePtr ) ->Value ,
295- EConVarType . EConVarType_Color => $"{(*(Color*)valuePtr ).R},{(*(Color*)valuePtr ).G},{(*(Color*)valuePtr ).B}" ,
296- EConVarType . EConVarType_Vector2 => $"{(*(Vector2D*)valuePtr ).X},{(*(Vector2D*)valuePtr ).Y}" ,
297- EConVarType . EConVarType_Vector3 => $"{(*(Vector*)valuePtr ).X},{(*(Vector*)valuePtr ).Y},{(*(Vector*)valuePtr ).Z}" ,
298- EConVarType . EConVarType_Vector4 => $"{(*(Vector4D*)valuePtr ).X},{(*(Vector4D*)valuePtr ).Y},{(*(Vector4D*)valuePtr ).Z},{(*(Vector4D*)valuePtr ).W}" ,
299- EConVarType . EConVarType_Qangle => $"{(*(QAngle*)valuePtr ).Pitch},{(*(QAngle*)valuePtr ).Yaw},{(*(QAngle*)valuePtr ).Roll}" ,
270+ EConVarType . EConVarType_Int16 => ( * ( short * ) ptr ) . ToString ( ) ,
271+ EConVarType. EConVarType_UInt16 => ( * ( ushort * ) ptr ) . ToString ( ) ,
272+ EConVarType . EConVarType_Int32 => ( * ( int * ) ptr ) . ToString ( ) ,
273+ EConVarType . EConVarType_UInt32 => ( * ( uint * ) ptr ) . ToString ( ) ,
274+ EConVarType . EConVarType_Int64 => ( * ( long * ) ptr ) . ToString ( ) ,
275+ EConVarType . EConVarType_UInt64 => ( * ( ulong * ) ptr ) . ToString ( ) ,
276+ EConVarType . EConVarType_Float32 => ( * ( float * ) ptr ) . ToString ( ) ,
277+ EConVarType . EConVarType_Float64 => ( * ( double * ) ptr ) . ToString ( ) ,
278+ EConVarType . EConVarType_String => ( ( CUtlString * ) ptr ) ->Value ,
279+ EConVarType . EConVarType_Color => $"{(*(Color*)ptr ).R},{(*(Color*)ptr ).G},{(*(Color*)ptr ).B}" ,
280+ EConVarType . EConVarType_Vector2 => $"{(*(Vector2D*)ptr ).X},{(*(Vector2D*)ptr ).Y}" ,
281+ EConVarType . EConVarType_Vector3 => $"{(*(Vector*)ptr ).X},{(*(Vector*)ptr ).Y},{(*(Vector*)ptr ).Z}" ,
282+ EConVarType . EConVarType_Vector4 => $"{(*(Vector4D*)ptr ).X},{(*(Vector4D*)ptr ).Y},{(*(Vector4D*)ptr ).Z},{(*(Vector4D*)ptr ).W}" ,
283+ EConVarType . EConVarType_Qangle => $"{(*(QAngle*)ptr ).Pitch},{(*(QAngle*)ptr ).Yaw},{(*(QAngle*)ptr ).Roll}" ,
300284 EConVarType . EConVarType_MAX => string . Empty ,
301285 _ => string . Empty ,
302286 } ;
@@ -466,4 +450,42 @@ public EConVarType GetConVarType( string name )
466450 {
467451 return ! NativeConvars . ExistsConvar ( name ) ? EConVarType . EConVarType_Invalid : ( EConVarType ) NativeConvars . GetConvarType ( name ) ;
468452 }
453+
454+ public string ? GetDefaultValue ( string name )
455+ {
456+ if ( ! NativeConvars . ExistsConvar ( name ) ) return null ;
457+
458+ var type = ( EConVarType ) NativeConvars . GetConvarType ( name ) ;
459+
460+ var valuePtr = NativeConvars . GetDefaultValuePtr ( name ) ;
461+ return valuePtr == 0 ? null : GetValueFromPtr ( type , valuePtr ) ;
462+ }
463+
464+ public unsafe string ? GetMinValue ( string name )
465+ {
466+ if ( ! NativeConvars . ExistsConvar ( name ) ) return null ;
467+
468+ var type = ( EConVarType ) NativeConvars . GetConvarType ( name ) ;
469+
470+ if ( type > EConVarType . EConVarType_Invalid && type < EConVarType . EConVarType_MAX && type != EConVarType . EConVarType_String && type != EConVarType . EConVarType_Color )
471+ {
472+ var valuePtr = * ( void * * ) NativeConvars . GetMinValuePtrPtr ( name ) ;
473+ return valuePtr == null ? null : GetValueFromPtr ( type , ( nint ) valuePtr ) ;
474+ }
475+ else return null ;
476+ }
477+
478+ public unsafe string ? GetMaxValue ( string name )
479+ {
480+ if ( ! NativeConvars . ExistsConvar ( name ) ) return null ;
481+
482+ var type = ( EConVarType ) NativeConvars . GetConvarType ( name ) ;
483+
484+ if ( type > EConVarType . EConVarType_Invalid && type < EConVarType . EConVarType_MAX && type != EConVarType . EConVarType_String && type != EConVarType . EConVarType_Color )
485+ {
486+ var valuePtr = * ( void * * ) NativeConvars . GetMaxValuePtrPtr ( name ) ;
487+ return valuePtr == null ? null : GetValueFromPtr ( type , ( nint ) valuePtr ) ;
488+ }
489+ else return null ;
490+ }
469491}
0 commit comments