Shifted CodeBlock::smCurrentLineText to be a const char* so it's use is clearer

Ensured CodeBlock::smCurrentLineText is cleared at the end of execution
Tweaked and cleaned up the context lines passed along for further debugging data in the event of a script assert
This commit is contained in:
JeffR 2026-02-22 18:18:42 -06:00
parent 171211c4e2
commit 9084e81bc1
11 changed files with 107 additions and 23 deletions

View file

@ -225,7 +225,16 @@ bool SubScene::evaluateCondition()
Con::setBoolVariable(resVar.c_str(), false);
String command = resVar + "=" + mLoadIf + ";";
Con::evaluatef(command.c_str());
StringTableEntry objectName = getName();
if (objectName != NULL)
objectName = getIdString();
StringTableEntry groupName = getGroup()->getName();
if (groupName != NULL)
groupName = getGroup()->getIdString();
String context = String::ToString("%s\nGroup: %s, Object: %s", getFilename(), groupName, objectName);
Con::evaluate(command.c_str(), false, context);
return Con::getBoolVariable(resVar.c_str());
}
return true;

View file

@ -367,7 +367,17 @@ bool SpawnSphere::testCondition()
String resVar = getIdString() + String(".result");
Con::setBoolVariable(resVar.c_str(), false);
String command = resVar + "=" + mSpawnIf + ";";
Con::evaluatef(command.c_str());
StringTableEntry objectName = getName();
if (objectName != NULL)
objectName = getIdString();
StringTableEntry groupName = getGroup()->getName();
if (groupName != NULL)
groupName = getGroup()->getIdString();
String context = String::ToString("%s\nGroup: %s, Object: %s", getFilename(), groupName, objectName);
Con::evaluate(command.c_str(), false, context);
if (Con::getBoolVariable(resVar.c_str()) == 1)
{
return true;

View file

@ -704,7 +704,17 @@ bool Trigger::testCondition()
String resVar = getIdString() + String(".result");
Con::setBoolVariable(resVar.c_str(), false);
String command = resVar + "=" + mTripIf + ";";
Con::evaluatef(command.c_str());
StringTableEntry objectName = getName();
if (objectName != NULL)
objectName = getIdString();
StringTableEntry groupName = getGroup()->getName();
if (groupName != NULL)
groupName = getGroup()->getIdString();
String context = String::ToString("%s\nGroup: %s, Object: %s", getFilename(), groupName, objectName);
Con::evaluate(command.c_str(), false, context);
if (Con::getBoolVariable(resVar.c_str()) == 1)
{
return true;
@ -743,8 +753,15 @@ void Trigger::potentialEnterObject(GameBase* enter)
String command = String("%obj = ") + enter->getIdString() + ";";
command = command + String("%this = ") + getIdString() + ";" + mEnterCommand;
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getIdString();
String context = String::ToString("%s, %s", getGroup(), objectName);
StringTableEntry objectName = getName();
if (objectName != NULL)
objectName = getIdString();
StringTableEntry groupName = getGroup()->getName();
if (groupName != NULL)
groupName = getGroup()->getIdString();
String context = String::ToString("%s\nGroup: %s, Object: %s", getFilename(), groupName, objectName);
Con::evaluate(command.c_str(), false, context);
}
@ -795,8 +812,15 @@ void Trigger::processTick(const Move* move)
String command = String("%obj = ") + remove->getIdString() + ";";
command = command + String("%this = ") + getIdString() + ";" + mLeaveCommand;
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getIdString();
String context = String::ToString("%s, %s", getGroup(), objectName);
StringTableEntry objectName = getName();
if (objectName != NULL)
objectName = getIdString();
StringTableEntry groupName = getGroup()->getName();
if (groupName != NULL)
groupName = getGroup()->getIdString();
String context = String::ToString("%s\nGroup: %s, Object: %s", getFilename(), groupName, objectName);
Con::evaluate(command.c_str(), false, context);
}
if (testTrippable() && testCondition())
@ -807,8 +831,15 @@ void Trigger::processTick(const Move* move)
if (evalCmD(&mTickCommand))
{
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getIdString();
String context = String::ToString("%s, %s", getGroup(), objectName);
StringTableEntry objectName = getName();
if (objectName != NULL)
objectName = getIdString();
StringTableEntry groupName = getGroup()->getName();
if (groupName != NULL)
groupName = getGroup()->getIdString();
String context = String::ToString("%s\nGroup: %s, Object: %s", getFilename(), groupName, objectName);
Con::evaluate(mTickCommand.c_str(), false, context);
}

View file

@ -60,7 +60,7 @@ inline FuncVars* getFuncVars(S32 lineNumber)
if (gFuncVars == &gGlobalScopeFuncVars)
{
const char* lineTxt = CodeBlock::smCurrentLineText;
const char* str = avar("Attemping to use local variable in global scope. File: %s Line Num: %d \nLine: %s", CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineTxt);
const char* str = avar("Attemping to use local variable in global scope. File: %s\nLine Num: %d\nLine: \"%s\"", CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineTxt);
scriptErrorHandler(str);
}
return gFuncVars;

View file

@ -35,7 +35,7 @@ using namespace Compiler;
bool CodeBlock::smInFunction = false;
CodeBlock * CodeBlock::smCodeBlockList = NULL;
StringTableEntry CodeBlock::smCurrentLineText = StringTable->EmptyString();
const char* CodeBlock::smCurrentLineText = "\0";
TorqueScriptParser *CodeBlock::smCurrentParser = NULL;
extern FuncVars gEvalFuncVars;
@ -625,6 +625,7 @@ Con::EvalResult CodeBlock::compileExec(StringTableEntry fileName, const char *in
if (!Script::gStatementList)
{
smCurrentLineText = "\0";
delete this;
return Con::EvalResult(Con::getVariable("$ScriptError"));
}
@ -670,7 +671,10 @@ Con::EvalResult CodeBlock::compileExec(StringTableEntry fileName, const char *in
Con::warnf(ConsoleLogEntry::General, "precompile size mismatch, precompile: %d compile: %d", codeSize, lastIp);
// repurpose argc as local register counter for global state
return (exec(0, fileName, NULL, localRegisterCount, 0, noCalls, NULL, setFrame));
Con::EvalResult execResult = (exec(0, fileName, NULL, localRegisterCount, 0, noCalls, NULL, setFrame));
smCurrentLineText = "\0";
return execResult;
}
//-------------------------------------------------------------------------

View file

@ -61,7 +61,7 @@ private:
public:
static bool smInFunction;
static TorqueScriptParser * smCurrentParser;
static StringTableEntry smCurrentLineText;
static const char* smCurrentLineText;
static CodeBlock *getCodeBlockList()
{

View file

@ -160,8 +160,16 @@ S32 FuncVars::assign(StringTableEntry var, TypeReq currentType, S32 lineNumber,
if (found->second.isConstant)
{
const char* lineTxt = CodeBlock::smCurrentLineText;
const char* str = avar("Script Warning: Reassigning variable %s when it is a constant. File: %s Line Num: %d \nLine: %s", var, CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineTxt);
const char* lineText = CodeBlock::smCurrentLineText;
String codeString = CodeBlock::smCurrentLineText;
Vector<String> splitLines;
codeString.split("\n", splitLines);
if (lineNumber > 0 && splitLines.size() > lineNumber)
lineText = splitLines[lineNumber - 1].c_str();
const char* str = avar("Script Warning: Reassigning variable %s when it is a constant. File: %s\nLine Num: %d\nLine: \"%s\"", var, CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineText);
scriptErrorHandler(str);
}
return found->second.reg;
@ -180,8 +188,16 @@ S32 FuncVars::lookup(StringTableEntry var, S32 lineNumber)
if (found == vars.end())
{
const char* lineTxt = CodeBlock::smCurrentLineText;
const char* str = avar("Script Warning: Variable %s referenced before used when compiling script. File: %s Line Num: %d \nLine: %s", var, CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineTxt);
const char* lineText = CodeBlock::smCurrentLineText;
String codeString = CodeBlock::smCurrentLineText;
Vector<String> splitLines;
codeString.split("\n", splitLines);
if (lineNumber > 0 && splitLines.size() > lineNumber)
lineText = splitLines[lineNumber - 1].c_str();
const char* str = avar("Script Warning: Variable %s referenced before used when compiling script. File: %s\nLine Num: %d\nLine: \"%s\"", var, CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineText);
scriptErrorHandler(str);
return assign(var, TypeReqString, lineNumber, false);
@ -197,7 +213,15 @@ TypeReq FuncVars::lookupType(StringTableEntry var, S32 lineNumber)
if (found == vars.end())
{
const char* lineText = CodeBlock::smCurrentLineText;
const char* str = avar("Script Warning: Variable %s referenced before used when compiling script. File: %s Line Num: %d \nLine: %s", var, CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineText);
String codeString = CodeBlock::smCurrentLineText;
Vector<String> splitLines;
codeString.split("\n", splitLines);
if (lineNumber > 0 && splitLines.size() > lineNumber)
lineText = splitLines[lineNumber-1].c_str();
const char* str = avar("Script Warning: Variable %s referenced before used when compiling script. File: %s\nLine Num: %d\nLine: \"%s\"", var, CodeBlock::smCurrentParser->getCurrentFile(), lineNumber, lineText);
scriptErrorHandler(str);
assign(var, TypeReqString, lineNumber, false);

View file

@ -975,7 +975,7 @@ void GuiGameListMenuCtrl::doScriptCommand(StringTableEntry command)
{
setThisControl();
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getInternalName();
String context = String::ToString("%s, Object: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
String context = String::ToString("%s\nObject: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
Con::evaluate(command, false, context);
}
}

View file

@ -495,7 +495,7 @@ void GuiGameSettingsCtrl::doScriptCommand(StringTableEntry command)
{
setThisControl();
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getInternalName();
String context = String::ToString("%s, Object: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
String context = String::ToString("%s\nObject: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
Con::evaluate(command, false, context);
}
}

View file

@ -2495,8 +2495,14 @@ void GuiControl::getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent
const char* GuiControl::evaluate( const char* str )
{
smThisControl = this;
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getInternalName();
String context = String::ToString("%s, Object: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
StringTableEntry objectName = getName();
if (getName() == NULL)
objectName = getInternalName();
StringTableEntry fileName = getFilename();
if (fileName != NULL)
fileName = Platform::makeRelativePathName(fileName, NULL);
String context = String::ToString("%s\nObject: %s", fileName, objectName);
const char* result = Con::evaluate(str, false, context).value;
smThisControl = NULL;

View file

@ -1485,7 +1485,7 @@ bool ActionMap::processAction(const InputEventInfo* pEvent)
if (pNode->makeConsoleCommand)
{
StringTableEntry objectName = getName() != StringTable->EmptyString() ? getName() : getInternalName();
String context = String::ToString("%s, Object: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
String context = String::ToString("%s\nObject: %s", Platform::makeRelativePathName(getFilename(), NULL), objectName);
Con::evaluate(pNode->makeConsoleCommand, false, context);
}
}