Skip to content

Commit 6495f0a

Browse files
committed
Port raw command text, incrementvar and multvar
1 parent 4ca4679 commit 6495f0a

4 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎Source.Engine/Cbuf.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public bool AddText(ReadOnlySpan<char> text, int tickDelay = 0) {
105105
return true;
106106
}
107107

108+
#if GMOD_DLL
109+
public bool AddRawText(ReadOnlySpan<char> text, int tickDelay = 0) {
110+
return InsertCommand(text.SliceNullTerminatedString(), currentTick + tickDelay);
111+
}
112+
#endif
113+
108114
void Compact() {
109115
argBufferSize = 0;
110116
Span<char> tempBuffer = stackalloc char[ARGS_BUFFER_LENGTH];
@@ -312,6 +318,15 @@ public void AddText(ReadOnlySpan<char> text) {
312318
}
313319
}
314320

321+
#if GMOD_DLL
322+
public void AddRawText(ReadOnlySpan<char> text) {
323+
lock (Buffer) {
324+
if (!Buffer.AddRawText(text))
325+
Dbg.ConWarning("Cbuf_AddRawText: buffer overflow\n");
326+
}
327+
}
328+
#endif
329+
315330
public void InsertText(ReadOnlySpan<char> text) {
316331
lock (Buffer) {
317332
Dbg.Assert(Buffer.IsProcessingCommands());

‎Source.Engine/CdllEngineInterface.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,9 @@ public void GMOD_R_RedownloadAllLightmaps(bool unk) {
654654
}
655655

656656
public void GMOD_RawClientCmd_Unrestricted(ReadOnlySpan<char> command) {
657-
throw new NotImplementedException();
657+
#if GMOD_DLL
658+
Cbuf.AddRawText(command);
659+
#endif
658660
}
659661

660662
public IGMODDataTable GMOD_CreateDataTable(GMODDataTableCallbackFn fnCallback) => new GMODDataTable(fnCallback);

‎Source.Engine/EngineServerImpl.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,9 @@ public void GMOD_SendToClient(int client, ReadOnlySpan<byte> data, int dataBits)
10871087
}
10881088

10891089
public void GMOD_RawServerCommand(ReadOnlySpan<char> command) {
1090-
throw new NotImplementedException();
1090+
#if GMOD_DLL
1091+
Cbuf.AddRawText(command);
1092+
#endif
10911093
}
10921094

10931095
public IGMODDataTable GMOD_CreateDataTable() => new GMODDataTable(null);

‎Source.Engine/Host.cs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Steamworks;
1919

2020
using System.Diagnostics.CodeAnalysis;
21+
using System.Globalization;
2122
using System.Runtime.CompilerServices;
2223
using System.Text;
2324

@@ -1610,6 +1611,78 @@ public void ShutdownServer() {
16101611
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive);
16111612
}
16121613

1614+
[ConCommand(helpText: "Increment specified convar value.", flags: FCvar.DontRecord)]
1615+
void incrementvar(in TokenizedCommand args) {
1616+
if (args.ArgC() != 5) {
1617+
Warning("Usage: incrementvar varName minValue maxValue delta\n");
1618+
return;
1619+
}
1620+
1621+
ReadOnlySpan<char> varName = args[1];
1622+
1623+
ConVar? var = cvar.FindVar(varName);
1624+
if (var == null) {
1625+
ConDMsg("cvar \"%s\" not found\n", varName.ToString());
1626+
return;
1627+
}
1628+
1629+
float currentValue = var.GetFloat();
1630+
float startValue = (float)atof(args[2]);
1631+
float endValue = (float)atof(args[3]);
1632+
float delta = (float)atof(args[4]);
1633+
float newValue = currentValue + delta;
1634+
if (newValue > endValue)
1635+
newValue = startValue;
1636+
else if (newValue < startValue)
1637+
newValue = endValue;
1638+
1639+
string value = ((double)newValue).ToString("F6", CultureInfo.InvariantCulture);
1640+
string command = $"{varName} {value}";
1641+
#if GMOD_DLL
1642+
Cbuf.AddRawText(command);
1643+
#else
1644+
Cbuf.AddText(command);
1645+
#endif
1646+
1647+
ConDMsg("%s = %s\n", var.GetName(), value);
1648+
}
1649+
1650+
[ConCommand(helpText: "Multiply specified convar value.", flags: FCvar.DontRecord)]
1651+
void multvar(in TokenizedCommand args) {
1652+
if (args.ArgC() != 5) {
1653+
Warning("Usage: multvar varName minValue maxValue factor\n");
1654+
return;
1655+
}
1656+
1657+
ReadOnlySpan<char> varName = args[1];
1658+
1659+
ConVar? var = cvar.FindVar(varName);
1660+
if (var == null) {
1661+
ConDMsg("cvar \"%s\" not found\n", varName.ToString());
1662+
return;
1663+
}
1664+
1665+
float currentValue = var.GetFloat();
1666+
float startValue = (float)atof(args[2]);
1667+
float endValue = (float)atof(args[3]);
1668+
float factor = (float)atof(args[4]);
1669+
float newValue = currentValue * factor;
1670+
if (newValue > endValue)
1671+
newValue = endValue;
1672+
else if (newValue < startValue)
1673+
newValue = startValue;
1674+
1675+
string value = ((double)newValue).ToString("F6", CultureInfo.InvariantCulture);
1676+
string command = $"{varName} {value}";
1677+
#if GMOD_DLL
1678+
Cbuf.AddRawText(command);
1679+
#else
1680+
Cbuf.AddText(command);
1681+
#endif
1682+
1683+
ConDMsg("%s = %s\n", var.GetName(), value);
1684+
}
1685+
16131686
[ConCommand]
16141687
void dumpstringtables() {
16151688
SV.DumpStringTables();

0 commit comments

Comments
 (0)