From 5998256125edc2eef58f566b33a9027f4ef5d6de Mon Sep 17 00:00:00 2001 From: terminx Date: Sun, 18 Nov 2018 18:14:15 +0000 Subject: [PATCH] This should improve the reliability of CON_SWITCH, and maybe allow nesting them to work. git-svn-id: https://svn.eduke32.com/eduke32@7231 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/duke3d/src/gamedef.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index 8fe52a94e..8b1c391a8 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -1500,9 +1500,6 @@ static void C_SetScriptSize(int32_t newsize) g_scriptSize = newsize; g_scriptPtr = apScript + (intptr_t)g_scriptPtr - oscript; - if (g_caseScriptPtr) - g_caseScriptPtr = apScript + (intptr_t)g_caseScriptPtr - oscript; - int32_t const size = (newsize >= osize) ? osize : newsize; for (int i = 0; i < size - 1; ++i) @@ -1604,8 +1601,6 @@ static void C_SkipComments(void) } fallthrough__; case 0: // EOF - if ((g_scriptPtr-apScript) > (g_scriptSize-32)) - C_SetScriptSize(g_scriptSize<<1); return; } } @@ -2935,6 +2930,9 @@ static bool C_ParseCommand(bool loop) if (EDUKE32_PREDICT_FALSE(g_errorCnt > 63 || (*textptr == '\0') || (*(textptr+1) == '\0'))) return 1; + if ((g_scriptPtr - apScript) > (g_scriptSize - 4096) && g_caseScriptPtr == NULL) + C_SetScriptSize(g_scriptSize << 1); + if (EDUKE32_PREDICT_FALSE(g_scriptDebug)) C_ReportError(-1); @@ -5041,17 +5039,22 @@ DO_DEFSTATE: case CON_SWITCH: { - intptr_t tempoffset; - g_checkingSwitch++; // allow nesting (if other things work) C_GetNextVar(); - tempoffset = (unsigned)(g_scriptPtr-apScript); + intptr_t const tempoffset = (unsigned)(g_scriptPtr-apScript); + BITPTR_CLEAR(g_scriptPtr-apScript); *g_scriptPtr++=0; // leave spot for end location (for after processing) + BITPTR_CLEAR(g_scriptPtr-apScript); *g_scriptPtr++=0; // count of case statements + + auto const backupCaseScriptPtr = g_caseScriptPtr; g_caseScriptPtr=g_scriptPtr; // the first case's pointer. + + int const backupNumCases = g_numCases; + BITPTR_CLEAR(g_scriptPtr-apScript); *g_scriptPtr++=0; // leave spot for 'default' location (null if none) @@ -5063,7 +5066,7 @@ DO_DEFSTATE: g_scriptPtr+=j*2; C_SkipComments(); g_scriptPtr-=j*2; // allocate buffer for the table - intptr_t *tempscrptr = (intptr_t *)(apScript+tempoffset); + auto tempscrptr = (intptr_t *)(apScript+tempoffset); //AddLog(g_szBuf); @@ -5138,15 +5141,8 @@ DO_DEFSTATE: //Bsprintf(g_szBuf,"ERROR::%s %d",__FILE__,__LINE__); //AddLog(g_szBuf); } - g_caseScriptPtr=NULL; - // decremented in endswitch. Don't decrement here... - // g_checkingSwitch--; // allow nesting (maybe if other things work) - tempscrptr=NULL; - if (g_checkingSwitch) - { - //Bsprintf(g_szBuf,"ERROR::%s %d: g_checkingSwitch=%d",__FILE__,__LINE__, g_checkingSwitch); - //AddLog(g_szBuf); - } + g_caseScriptPtr=backupCaseScriptPtr; + g_numCases=backupNumCases; //AddLog("End of Switch statement"); } continue;