From 14811e59648ca1a8df4334f03f6d5d8b3690c895 Mon Sep 17 00:00:00 2001 From: mathusummut Date: Wed, 6 Mar 2019 20:20:16 +0100 Subject: [PATCH] Added debug mode to LUA --- source/game/game/script_manager.cpp | 109 ++++++++++++------ source/game/game/script_manager.h | 22 ++-- source/game/world/world.cpp | 39 ++++--- source/game/world/world.h | 10 +- source/shared_lib/sources/graphics/pixmap.cpp | 2 +- 5 files changed, 114 insertions(+), 68 deletions(-) diff --git a/source/game/game/script_manager.cpp b/source/game/game/script_manager.cpp index b1ba55099..9d4838968 100644 --- a/source/game/game/script_manager.cpp +++ b/source/game/game/script_manager.cpp @@ -521,6 +521,8 @@ namespace Game { "recallGroupSelection"); luaScript.registerFunction(removeUnitFromGroupSelection, "removeUnitFromGroupSelection"); + luaScript.registerFunction(setDebugMode, + "setDebugMode"); luaScript.registerFunction(setAttackWarningsEnabled, "setAttackWarningsEnabled"); luaScript.registerFunction(getAttackWarningsEnabled, @@ -645,27 +647,29 @@ namespace Game { luaScript.endCall(); } } catch (const game_runtime_error & ex) { - //string sErrBuf = ""; - //if(ex.wantStackTrace() == true) { - char - szErrBuf[8096] = ""; - //snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - string - sErrBuf = - string(szErrBuf) + - string("The game may no longer be stable!\n\n\t [") + - string(ex.what()) + string("]\n"); - //} - SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); - if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug(SystemFlags::debugSystem, - sErrBuf.c_str()); + if (debugMode) { + //string sErrBuf = ""; + //if(ex.wantStackTrace() == true) { + char + szErrBuf[8096] = ""; + //snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + string + sErrBuf = + string(szErrBuf) + + string("The game may no longer be stable!\n\n\t [") + + string(ex.what()) + string("]\n"); + //} + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + sErrBuf.c_str()); - thisScriptManager-> - addMessageToQueue(ScriptManagerMessage - (sErrBuf.c_str(), "error", -1, -1, true)); - thisScriptManager->onMessageBoxOk(false); + thisScriptManager-> + addMessageToQueue(ScriptManagerMessage + (sErrBuf.c_str(), "error", -1, -1, true)); + thisScriptManager->onMessageBoxOk(false); + } } if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA, @@ -1695,15 +1699,21 @@ namespace Game { } int - ScriptManager::createUnit(const string & unitName, int factionIndex, - Vec2i pos) { + ScriptManager::createUnit(const string & unitName, int factionIndex, Vec2i pos) { if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA, "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__, unitName.c_str(), factionIndex); - return world->createUnit(unitName, factionIndex, pos)->getId(); + Unit* unit = world->createUnit(unitName, factionIndex, pos); + if (unit == NULL) { + if (debugMode) + return 0; + else + throw game_runtime_error((string("Unit ") + unitName + " was not created successfully for faction ") + to_string(factionIndex) + (" at " + pos.getString())); + } else + return unit->getId(); } int @@ -1715,7 +1725,14 @@ namespace Game { extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__, unitName.c_str(), factionIndex); - return world->createUnit(unitName, factionIndex, pos, false)->getId(); + Unit* unit = world->createUnit(unitName, factionIndex, pos, false); + if (unit == NULL) { + if (debugMode) + return 0; + else + throw game_runtime_error((string("Unit ") + unitName + " was not created successfully for faction ") + to_string(factionIndex) + (" at " + pos.getString())); + } else + return unit->getId(); } void @@ -1753,12 +1770,7 @@ namespace Game { unit = world->findUnitById(unitId); if (unit != NULL) { // Make sure they die - bool - unit_dead = unit->decHp(unit->getHp() * unit->getHp()); - if (unit_dead == false) { - throw - game_runtime_error("unit_dead == false", true); - } + bool unit_dead = unit->decHp(unit->getHp() * unit->getHp()); unit->kill(); // If called from an existing die event we get a stack overflow //onUnitDied(unit); @@ -1922,7 +1934,7 @@ namespace Game { extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__); - world->givePositionCommand(unitId, commandName, pos); + world->givePositionCommand(unitId, commandName, pos, debugMode); } void @@ -1933,7 +1945,7 @@ namespace Game { extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__); - world->giveAttackCommand(unitId, unitToAttackId); + world->giveAttackCommand(unitId, unitToAttackId, debugMode); } void @@ -1945,7 +1957,7 @@ namespace Game { extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__); - world->giveProductionCommand(unitId, producedName); + world->giveProductionCommand(unitId, producedName, debugMode); } void @@ -1957,7 +1969,7 @@ namespace Game { extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__); - world->giveUpgradeCommand(unitId, producedName); + world->giveUpgradeCommand(unitId, producedName, debugMode); } void @@ -1970,8 +1982,7 @@ namespace Game { extractFileFromDirectoryPath(__FILE__). c_str(), __FUNCTION__, __LINE__); - world->giveAttackStoppedCommand(unitId, itemName, - (ignoreRequirements == 1)); + world->giveAttackStoppedCommand(unitId, itemName, (ignoreRequirements == 1), debugMode); } void @@ -3058,6 +3069,17 @@ namespace Game { world->removeUnitFromGroupSelection(unitId, groupIndex); } + void + ScriptManager::setDebugMode(bool enabled) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + debugMode = enabled; + } + void ScriptManager::setAttackWarningsEnabled(bool enabled) { if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) @@ -5815,6 +5837,21 @@ namespace Game { return luaArguments.getReturnCount(); } + int + ScriptManager::setDebugMode(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager-> + setDebugMode((luaArguments.getInt(-1) == + 0 ? false : true)); + } catch (const game_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int ScriptManager::setAttackWarningsEnabled(LuaHandle * luaHandle) { LuaArguments diff --git a/source/game/game/script_manager.h b/source/game/game/script_manager.h index c95070fa1..2b085d3ed 100644 --- a/source/game/game/script_manager.h +++ b/source/game/game/script_manager.h @@ -59,16 +59,11 @@ namespace Game { class ScriptManagerMessage { private: - string - text; - string - header; - int - factionIndex; - int - teamIndex; - bool - messageNotTranslated; + string text; + string header; + int factionIndex; + int teamIndex; + bool messageNotTranslated; public: ScriptManagerMessage(); @@ -312,8 +307,7 @@ namespace Game { lastAttackingUnitId; // end game state - bool - gameOver; + bool gameOver, debugMode; bool gameWon; PlayerModifiers @@ -731,6 +725,8 @@ namespace Game { recallGroupSelection(int groupIndex); void removeUnitFromGroupSelection(int unitId, int group); + void + setDebugMode(bool enabled); void setAttackWarningsEnabled(bool enabled); bool @@ -1051,6 +1047,8 @@ namespace Game { recallGroupSelection(LuaHandle * luaHandle); static int removeUnitFromGroupSelection(LuaHandle * luaHandle); + static int + setDebugMode(LuaHandle * luaHandle); static int setAttackWarningsEnabled(LuaHandle * luaHandle); static int diff --git a/source/game/world/world.cpp b/source/game/world/world.cpp index 5edcfa3a7..46968c994 100644 --- a/source/game/world/world.cpp +++ b/source/game/world/world.cpp @@ -1453,7 +1453,7 @@ namespace Game { return units; } - void World::givePositionCommand(int unitId, const string &commandName, const Vec2i &pos) { + void World::givePositionCommand(int unitId, const string &commandName, const Vec2i &pos, bool throwOnError) { Unit* unit = findUnitById(unitId); if (unit != NULL) { CommandClass cc; @@ -1463,17 +1463,22 @@ namespace Game { } else if (commandName == "attack") { cc = ccAttack; } else { - throw game_runtime_error("Invalid position command: " + commandName, true); + if (throwOnError) + throw game_runtime_error("Invalid position command: " + commandName, true); } if (unit->getType()->getFirstCtOfClass(cc) == NULL) { - throw game_runtime_error("Invalid command: [" + commandName + "] for unit: [" + unit->getType()->getName(false) + "] id [" + intToStr(unit->getId()) + "]", true); + if (throwOnError) + throw game_runtime_error("Invalid command: [" + commandName + "] for unit: [" + unit->getType()->getName(false) + "] id [" + intToStr(unit->getId()) + "]", true); + else + return; } if (SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "In [%s::%s Line: %d] cc = %d Unit [%s]\n", __FILE__, __FUNCTION__, __LINE__, cc, unit->getFullName(false).c_str()); unit->giveCommand(new Command(unit->getType()->getFirstCtOfClass(cc), pos)); if (SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); } else { - throw game_runtime_error("Invalid unitId index in givePositionCommand: " + intToStr(unitId) + " commandName = " + commandName, true); + if (throwOnError) + throw game_runtime_error("Invalid unitId index in givePositionCommand: " + intToStr(unitId) + " commandName = " + commandName, true); } } @@ -1543,7 +1548,7 @@ namespace Game { return (this->disableAttackEffects == false); } - void World::giveAttackCommand(int unitId, int unitToAttackId) { + void World::giveAttackCommand(int unitId, int unitToAttackId, bool throwOnError) { Unit* unit = findUnitById(unitId); if (unit != NULL) { Unit* targetUnit = findUnitById(unitToAttackId); @@ -1554,17 +1559,20 @@ namespace Game { unit->giveCommand(new Command(ct, targetUnit)); if (SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); } else { - throw game_runtime_error("Invalid ct in giveAttackCommand: " + intToStr(unitId) + " unitToAttackId = " + intToStr(unitToAttackId), true); + if (throwOnError) + throw game_runtime_error("Invalid ct in giveAttackCommand: " + intToStr(unitId) + " unitToAttackId = " + intToStr(unitToAttackId), true); } } else { - throw game_runtime_error("Invalid unitToAttackId index in giveAttackCommand: " + intToStr(unitId) + " unitToAttackId = " + intToStr(unitToAttackId), true); + if (throwOnError) + throw game_runtime_error("Invalid unitToAttackId index in giveAttackCommand: " + intToStr(unitId) + " unitToAttackId = " + intToStr(unitToAttackId), true); } } else { - throw game_runtime_error("Invalid unitId index in giveAttackCommand: " + intToStr(unitId) + " unitToAttackId = " + intToStr(unitToAttackId), true); + if (throwOnError) + throw game_runtime_error("Invalid unitId index in giveAttackCommand: " + intToStr(unitId) + " unitToAttackId = " + intToStr(unitToAttackId), true); } } - void World::giveProductionCommand(int unitId, const string &producedName) { + void World::giveProductionCommand(int unitId, const string &producedName, bool throwOnError) { //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); Unit *unit = findUnitById(unitId); //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); @@ -1595,12 +1603,13 @@ namespace Game { } } } else { - throw game_runtime_error("Invalid unitId index in giveProductionCommand: " + intToStr(unitId) + " producedName = " + producedName, true); + if (throwOnError) + throw game_runtime_error("Invalid unitId index in giveProductionCommand: " + intToStr(unitId) + " producedName = " + producedName, true); } //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); } - void World::giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements) { + void World::giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements, bool throwOnError) { Unit *unit = findUnitById(unitId); if (unit != NULL) { const UnitType *ut = unit->getType(); @@ -1637,7 +1646,8 @@ namespace Game { } } } else { - throw game_runtime_error("Invalid unitId index in giveAttackStoppedCommand: " + intToStr(unitId) + " itemName = " + itemName, true); + if (throwOnError) + throw game_runtime_error("Invalid unitId index in giveAttackStoppedCommand: " + intToStr(unitId) + " itemName = " + itemName, true); } } @@ -1763,7 +1773,7 @@ namespace Game { return game->getGameSettings()->getFactionTypeName(factionIndex); } - void World::giveUpgradeCommand(int unitId, const string &upgradeName) { + void World::giveUpgradeCommand(int unitId, const string &upgradeName, bool throwOnError) { Unit *unit = findUnitById(unitId); if (unit != NULL) { const UnitType *ut = unit->getType(); @@ -1784,7 +1794,8 @@ namespace Game { } } } else { - throw game_runtime_error("Invalid unitId index in giveUpgradeCommand: " + intToStr(unitId) + " upgradeName = " + upgradeName, true); + if (throwOnError) + throw game_runtime_error("Invalid unitId index in giveUpgradeCommand: " + intToStr(unitId) + " upgradeName = " + upgradeName, true); } } diff --git a/source/game/world/world.h b/source/game/world/world.h index 73755608a..784c1be6d 100644 --- a/source/game/world/world.h +++ b/source/game/world/world.h @@ -293,14 +293,14 @@ namespace Game { //scripting interface void morphToUnit(int unitId, const string &morphName, bool ignoreRequirements); Unit* createUnit(const string &unitName, int factionIndex, const Vec2i &pos, bool spaciated = true); - void givePositionCommand(int unitId, const string &commandName, const Vec2i &pos); + void givePositionCommand(int unitId, const string &commandName, const Vec2i &pos, bool throwError = true); vector getUnitsForFaction(int factionIndex, const string& commandTypeName, int field); int getUnitCurrentField(int unitId); bool getIsUnitAlive(int unitId); - void giveAttackCommand(int unitId, int unitToAttackId); - void giveProductionCommand(int unitId, const string &producedName); - void giveUpgradeCommand(int unitId, const string &upgradeName); - void giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements); + void giveAttackCommand(int unitId, int unitToAttackId, bool throwError = true); + void giveProductionCommand(int unitId, const string &producedName, bool throwError = true); + void giveUpgradeCommand(int unitId, const string &upgradeName, bool throwError = true); + void giveAttackStoppedCommand(int unitId, const string &itemName, bool ignoreRequirements, bool throwError = true); void playStaticSound(const string &playSound); void playStreamingSound(const string &playSound); void stopStreamingSound(const string &playSound); diff --git a/source/shared_lib/sources/graphics/pixmap.cpp b/source/shared_lib/sources/graphics/pixmap.cpp index d59f7a789..d33a5a7d5 100644 --- a/source/shared_lib/sources/graphics/pixmap.cpp +++ b/source/shared_lib/sources/graphics/pixmap.cpp @@ -941,7 +941,7 @@ namespace Shared { this->components = components; deletePixels(); - if (getPixelByteCount() < 0 || (h < 0 || w < 0 || components < 0)) { + if (h < 0 || w < 0 || components < 0) { char szBuf[8096]; snprintf(szBuf, 8096, "Invalid pixmap dimensions for [%s], h = %d, w = %d, components = %d\n", path.c_str(), h, w, components); throw game_runtime_error(szBuf);