Skip to content

Commit 22a18a3

Browse files
Improve error reporting on class changes
1 parent 8387a94 commit 22a18a3

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

scripting/randomizer.sp

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ enum struct WeaponWhitelist //Whitelist of allowed weapon indexs
324324
}
325325
}
326326

327+
enum struct ClientClassDebug
328+
{
329+
FrameIterator hStack;
330+
float flTime;
331+
}
332+
327333
bool g_bEnabled;
328334
bool g_bTF2Items;
329335
bool g_bAllowGiveNamedItem;
@@ -347,7 +353,7 @@ ConVar g_cvRandomize[view_as<int>(RandomizedType_MAX)];
347353
bool g_bClientRefresh[MAXPLAYERS];
348354

349355
TFClassType g_iClientCurrentClass[MAXPLAYERS + 1][4];
350-
FrameIterator g_hClientCurrentClass[MAXPLAYERS + 1][4];
356+
ClientClassDebug g_ClientClassDebug[MAXPLAYERS + 1][10];
351357
bool g_bFeignDeath[MAXPLAYERS + 1];
352358
int g_iHypeMeterLoaded[MAXPLAYERS + 1] = {INVALID_ENT_REFERENCE, ...};
353359
bool g_bWeaponDecap[MAXPLAYERS + 1];
@@ -891,6 +897,9 @@ void SetClientClass(int iClient, TFClassType nClass)
891897
if (nClass == TFClass_Unknown)
892898
ThrowError("Got TFClass_Unknown in SetClientClass");
893899

900+
if (g_cvDebug.BoolValue)
901+
CreateDebugStack(iClient);
902+
894903
for (int i = 0; i < sizeof(g_iClientCurrentClass[]); i++)
895904
{
896905
if (g_iClientCurrentClass[iClient][i] != TFClass_Unknown)
@@ -902,46 +911,10 @@ void SetClientClass(int iClient, TFClassType nClass)
902911

903912
TF2_SetPlayerClass(iClient, nClass);
904913

905-
if (g_cvDebug.BoolValue)
906-
g_hClientCurrentClass[iClient][i] = new FrameIterator();
907-
908914
return;
909915
}
910916

911-
if (g_cvDebug.BoolValue)
912-
{
913-
LogError("Exceeded array limit on storing class");
914-
915-
for (int i = 0; i < sizeof(g_iClientCurrentClass[]); i++)
916-
{
917-
FrameIterator hIterator = g_hClientCurrentClass[iClient][i];
918-
if (!hIterator)
919-
continue;
920-
921-
hIterator.Reset();
922-
if (!hIterator.Next()) // skip SetClientClass
923-
continue;
924-
925-
LogError("Index %d stack trace: ", i);
926-
927-
int iCounter;
928-
929-
while (hIterator.Next() && hIterator.LineNumber)
930-
{
931-
char sFile[256], sFunction[256];
932-
hIterator.GetFilePath(sFile, sizeof(sFile));
933-
hIterator.GetFunctionName(sFunction, sizeof(sFunction));
934-
LogError(" [%d] Line %d, %s::%s", iCounter, hIterator.LineNumber, sFile, sFunction);
935-
936-
iCounter++;
937-
}
938-
}
939-
940-
}
941-
else
942-
{
943-
ThrowError("Exceeded array limit on storing class, enable randomizer_debug and try again for more infos");
944-
}
917+
ReportDebugStack(iClient, "Exceeded array limit on storing class");
945918
}
946919

947920
void SetClientClassOriginal(int iClient)
@@ -955,6 +928,9 @@ void SetClientClassOriginal(int iClient)
955928

956929
void RevertClientClass(int iClient)
957930
{
931+
if (g_cvDebug.BoolValue)
932+
CreateDebugStack(iClient);
933+
958934
for (int i = sizeof(g_iClientCurrentClass[]) - 1; i >= 0; i--)
959935
{
960936
if (g_iClientCurrentClass[iClient][i] == TFClass_Unknown)
@@ -963,11 +939,61 @@ void RevertClientClass(int iClient)
963939
TF2_SetPlayerClass(iClient, g_iClientCurrentClass[iClient][i]);
964940
g_iClientCurrentClass[iClient][i] = TFClass_Unknown;
965941

966-
delete g_hClientCurrentClass[iClient][i];
967942
return;
968943
}
969944

970-
ThrowError("Could not find class to revert back to");
945+
ReportDebugStack(iClient, "Could not find class to revert back to");
946+
}
947+
948+
void CreateDebugStack(int iClient)
949+
{
950+
if (!g_cvDebug.BoolValue)
951+
return;
952+
953+
// Shift array down to free up first index
954+
delete g_ClientClassDebug[iClient][sizeof(g_ClientClassDebug[]) - 1].hStack;
955+
for (int i = sizeof(g_ClientClassDebug[]) - 2; i >= 0; i--)
956+
g_ClientClassDebug[iClient][i + 1] = g_ClientClassDebug[iClient][i];
957+
958+
// Add new one here
959+
g_ClientClassDebug[iClient][0].hStack = new FrameIterator();
960+
g_ClientClassDebug[iClient][0].flTime = GetGameTime();
961+
}
962+
963+
void ReportDebugStack(int iClient, const char[] sError)
964+
{
965+
if (!g_cvDebug.BoolValue)
966+
{
967+
ThrowError("%s, enable randomizer_debug and try again for more infos", sError);
968+
return;
969+
}
970+
971+
LogError("%s, reporting last %d stack traces:", sError, sizeof(g_ClientClassDebug[]));
972+
973+
for (int i = 0; i < sizeof(g_ClientClassDebug[]); i++)
974+
{
975+
FrameIterator hIterator = g_ClientClassDebug[iClient][i].hStack;
976+
if (!hIterator)
977+
continue;
978+
979+
hIterator.Reset();
980+
if (!hIterator.Next() || !hIterator.Next()) // skip SetClientClass/RevertClientClass and CreateDebugStack
981+
continue;
982+
983+
LogError("Index %d stack trace at frame time %.6f: ", i, g_ClientClassDebug[iClient][i].flTime);
984+
985+
int iCounter;
986+
987+
while (hIterator.Next() && hIterator.LineNumber)
988+
{
989+
char sFile[256], sFunction[256];
990+
hIterator.GetFilePath(sFile, sizeof(sFile));
991+
hIterator.GetFunctionName(sFunction, sizeof(sFunction));
992+
LogError(" [%d] Line %d, %s::%s", iCounter, hIterator.LineNumber, sFile, sFunction);
993+
994+
iCounter++;
995+
}
996+
}
971997
}
972998

973999
public Action TF2Items_OnGiveNamedItem(int iClient, char[] sClassname, int iIndex, Handle &hItem)

0 commit comments

Comments
 (0)