mirror of
https://github.com/TombEngine/TombEngine.git
synced 2025-05-01 01:08:01 +03:00

SetErrorMode is called via Lua in settings.lua to allow us or the LD to choose between the three ERROR_MODEs for script asserts. ScriptWarn is for when we want to follow up on a failed assert in ERROR_MODE::WARN. e.g. if the player gives in an invalid HP value, we would ScriptWarn to tell them that the HP is being set to zero instead. If we added this information in ScriptAssert, we would end up displaying this message in TERMINATE mode, too - which is incorrect, since we won't have set the HP to zero, as we will have terminated the game.
56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#include "framework.h"
|
|
#include "ScriptAssert.h"
|
|
|
|
static ERROR_MODE ScriptErrorMode = ERROR_MODE::WARN;
|
|
|
|
void ScriptWarn(std::string const& msg)
|
|
{
|
|
switch (ScriptErrorMode)
|
|
{
|
|
case ERROR_MODE::TERMINATE:
|
|
case ERROR_MODE::WARN:
|
|
TENLog(msg, LogLevel::Warning, LogConfig::All);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
bool ScriptAssert(bool cond, std::string const& msg, std::optional<ERROR_MODE> forceMode)
|
|
{
|
|
if (!cond)
|
|
{
|
|
ERROR_MODE mode = forceMode ? *forceMode : ScriptErrorMode;
|
|
switch (mode)
|
|
{
|
|
case ERROR_MODE::WARN:
|
|
TENLog(msg, LogLevel::Warning, LogConfig::All);
|
|
break;
|
|
case ERROR_MODE::TERMINATE:
|
|
throw TENScriptException(msg);
|
|
break;
|
|
}
|
|
}
|
|
return cond;
|
|
}
|
|
|
|
void SetErrorMode(std::string const& mode)
|
|
{
|
|
std::string noCase{ mode };
|
|
std::transform(std::cbegin(noCase), std::cend(noCase), std::begin(noCase), [](unsigned char c) {return std::tolower(c); });
|
|
if (noCase == "silent")
|
|
{
|
|
ScriptErrorMode = ERROR_MODE::SILENT;
|
|
}
|
|
else if (noCase == "warn")
|
|
{
|
|
ScriptErrorMode = ERROR_MODE::WARN;
|
|
}
|
|
else if (noCase == "terminate")
|
|
{
|
|
ScriptErrorMode = ERROR_MODE::TERMINATE;
|
|
}
|
|
else
|
|
{
|
|
TENLog("Wrong error mode set - valid settings are \"silent\", \"warn\" and \"terminate\"; defaulting to \"warn\".", LogLevel::Warning);
|
|
}
|
|
}
|