bool LuaScriptUtilities::CheckArgumentCountOrDie(
    lua_State* const luaVM,
    const unsigned int argCount,
    const unsigned int optionalArgs)
{
    const unsigned int actualArgCount =
        static_cast<unsigned int>(lua_gettop(luaVM));

    if (actualArgCount < argCount || actualArgCount > (argCount + optionalArgs))
    {
        Ogre::LogManager* const logManager = Ogre::LogManager::getSingletonPtr();

        char buffer[2048];

        if (optionalArgs)
        {
            sprintf_s(
                buffer,
                sizeof(buffer),
                "LUA_ERROR: \"%s\" expected at least %d arguments but no more "
                "than %d arguments, encountered %d.",
                GetCallingFunctionName(luaVM).c_str(),
                argCount,
                argCount + optionalArgs,
                actualArgCount);
        }
        else
        {
            sprintf_s(
                buffer,
                sizeof(buffer),
                "LUA_ERROR: \"%s\" expected %d arguments, encountered %d.",
                GetCallingFunctionName(luaVM).c_str(),
                argCount,
                actualArgCount);
        }

        logManager->logMessage(
            buffer, Ogre::LML_CRITICAL);

        DumpStack(luaVM, Ogre::LML_CRITICAL);
        DumpStackTrace(luaVM, Ogre::LML_CRITICAL);

        PushErrorMessageAndDie(luaVM, buffer);

        return false;
    }

    return true;
}
void DumpStackTraceToString(string* stacktrace) {
  DumpStackTrace(1, DebugWriteToString, stacktrace);
}