Skip to content

Commit c9aa6e3

Browse files
committed
feat(ConVars): GetDefaultValue, GetMin/MaxValue
1 parent e34e26c commit c9aa6e3

File tree

3 files changed

+104
-38
lines changed

3 files changed

+104
-38
lines changed

managed/src/SwiftlyS2.Core/Modules/Convars/ConVarService.cs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,6 @@
44

55
namespace 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-
287
internal 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
}

managed/src/SwiftlyS2.Shared/Modules/Convars/IConVarService.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using SwiftlyS2.Core.Convars;
2+
using SwiftlyS2.Shared.Natives;
23

34
namespace SwiftlyS2.Shared.Convars;
45

@@ -82,4 +83,25 @@ public interface IConVarService
8283
/// <param name="name">The name of the convar.</param>
8384
/// <returns>The type of the convar.</returns>
8485
public EConVarType GetConVarType( string name );
86+
87+
/// <summary>
88+
/// Gets the default value of a convar by name.
89+
/// </summary>
90+
/// <param name="name">The name of the convar.</param>
91+
/// <returns>The default value of the convar as a string.</returns>
92+
public string? GetDefaultValue( string name );
93+
94+
/// <summary>
95+
/// Gets the min value of a convar by name.
96+
/// </summary>
97+
/// <param name="name">The name of the convar.</param>
98+
/// <returns>The min value of the convar as a string.</returns>
99+
public string? GetMinValue( string name );
100+
101+
/// <summary>
102+
/// Gets the max value of a convar by name.
103+
/// </summary>
104+
/// <param name="name">The name of the convar.</param>
105+
/// <returns>The max value of the convar as a string.</returns>
106+
public string? GetMaxValue( string name );
85107
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace SwiftlyS2.Shared.Natives;
2+
3+
public enum EConVarType : int
4+
{
5+
EConVarType_Invalid = -1,
6+
EConVarType_Bool,
7+
EConVarType_Int16,
8+
EConVarType_UInt16,
9+
EConVarType_Int32,
10+
EConVarType_UInt32,
11+
EConVarType_Int64,
12+
EConVarType_UInt64,
13+
EConVarType_Float32,
14+
EConVarType_Float64,
15+
EConVarType_String,
16+
EConVarType_Color,
17+
EConVarType_Vector2,
18+
EConVarType_Vector3,
19+
EConVarType_Vector4,
20+
EConVarType_Qangle,
21+
EConVarType_MAX
22+
};

0 commit comments

Comments
 (0)