Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace recursion flag from boolean to int #95

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 41 additions & 50 deletions rts/Lua/LuaSyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ using std::min;
/******************************************************************************/
/******************************************************************************/

bool LuaSyncedCtrl::inCreateUnit = false;
bool LuaSyncedCtrl::inDestroyUnit = false;
bool LuaSyncedCtrl::inTransferUnit = false;
bool LuaSyncedCtrl::inCreateFeature = false;
bool LuaSyncedCtrl::inDestroyFeature = false;
bool LuaSyncedCtrl::inGiveOrder = false;
bool LuaSyncedCtrl::inHeightMap = false;
bool LuaSyncedCtrl::inSmoothMesh = false;

static int heightMapx1 = 0;
static int heightMapx2 = 0;
static int heightMapz1 = 0;
Expand All @@ -119,12 +110,12 @@ bool LuaSyncedCtrl::PushEntries(lua_State* L)
{
{
// these need to be re-initialized here since we might have reloaded
inCreateUnit = false;
inDestroyUnit = false;
inCreateUnit = 0;
inDestroyUnit = 0;
inCreateFeature = 0;
inDestroyFeature = 0;
inGiveOrder = 0;
inTransferUnit = false;
inCreateFeature = false;
inDestroyFeature = false;
inGiveOrder = false;
inHeightMap = false;
inSmoothMesh = false;

Expand Down Expand Up @@ -1243,8 +1234,8 @@ int LuaSyncedCtrl::CreateUnit(lua_State* L)
{
CheckAllowGameChanges(L);

if (inCreateUnit) {
luaL_error(L, "[%s()]: recursion is not permitted", __func__);
if (inCreateUnit >= MAX_CMD_RECURSION_DEPTH) {
luaL_error(L, "[%s()]: recursion is not permitted, max depth: %d", __func__, MAX_CMD_RECURSION_DEPTH);
return 0;
}

Expand Down Expand Up @@ -1295,7 +1286,7 @@ int LuaSyncedCtrl::CreateUnit(lua_State* L)
ASSERT_SYNCED(pos);
ASSERT_SYNCED(facing);

inCreateUnit = true;
inCreateUnit++;

CUnit* builder = unitHandler.GetUnit(luaL_optint(L, 10, -1));

Expand All @@ -1311,7 +1302,7 @@ int LuaSyncedCtrl::CreateUnit(lua_State* L)
params.flattenGround = flattenGround;

CUnit* unit = unitLoader->LoadUnit(params);
inCreateUnit = false;
inCreateUnit--;

if (unit == nullptr)
return 0;
Expand Down Expand Up @@ -1341,18 +1332,18 @@ int LuaSyncedCtrl::DestroyUnit(lua_State* L)
if (args >= 4)
attacker = ParseUnit(L, __func__, 4);

if (inDestroyUnit)
luaL_error(L, "DestroyUnit() recursion is not permitted");
if (inDestroyUnit >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "DestroyUnit() recursion is not permitted, max depth: %d", MAX_CMD_RECURSION_DEPTH);

inDestroyUnit = true;
inDestroyUnit++;

ASSERT_SYNCED(unit->id);
unit->ForcedKillUnit(attacker, selfDestr, reclaimed);

if (recycleID)
unitHandler.GarbageCollectUnit(unit->id);

inDestroyUnit = false;
inDestroyUnit--;

return 0;
}
Expand Down Expand Up @@ -2907,11 +2898,11 @@ int LuaSyncedCtrl::CreateFeature(lua_State* L)
if (!CanControlFeatureAllyTeam(L, allyTeam))
luaL_error(L, "CreateFeature() bad team permission %d", team);

if (inCreateFeature)
luaL_error(L, "CreateFeature() recursion is not permitted");
if (inCreateFeature >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "CreateFeature() recursion is not permitted, max depth: %d", MAX_CMD_RECURSION_DEPTH);

// use SetFeatureResurrect() to fill in the missing bits
inCreateFeature = true;
inCreateFeature++;

FeatureLoadParams params;
params.parentObj = nullptr;
Expand All @@ -2928,7 +2919,7 @@ int LuaSyncedCtrl::CreateFeature(lua_State* L)
params.smokeTime = 0;

CFeature* feature = featureHandler.LoadFeature(params);
inCreateFeature = false;
inCreateFeature--;

if (feature != nullptr) {
lua_pushnumber(L, feature->id);
Expand All @@ -2946,12 +2937,12 @@ int LuaSyncedCtrl::DestroyFeature(lua_State* L)
if (feature == nullptr)
return 0;

if (inDestroyFeature)
luaL_error(L, "DestroyFeature() recursion is not permitted");
if (inDestroyFeature >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "DestroyFeature() recursion is not permitted, max depth: %d", MAX_CMD_RECURSION_DEPTH);

inDestroyFeature = true;
inDestroyFeature++;
featureHandler.DeleteFeature(feature);
inDestroyFeature = false;
inDestroyFeature--;

return 0;
}
Expand Down Expand Up @@ -3566,12 +3557,12 @@ int LuaSyncedCtrl::GiveOrderToUnit(lua_State* L)
return 1;
}

if (inGiveOrder)
luaL_error(L, "[%s] recursion not permitted", __func__);
if (inGiveOrder >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "[%s] recursion not permitted, max depth: %d", __func__, MAX_CMD_RECURSION_DEPTH);

inGiveOrder = true;
inGiveOrder++;
unit->commandAI->GiveCommand(cmd, -1, true, true);
inGiveOrder = false;
inGiveOrder--;

lua_pushboolean(L, true);
return 1;
Expand All @@ -3594,18 +3585,18 @@ int LuaSyncedCtrl::GiveOrderToUnitMap(lua_State* L)

Command cmd = LuaUtils::ParseCommand(L, __func__, 2);

if (inGiveOrder)
luaL_error(L, "[%s] recursion not permitted", __func__);
if (inGiveOrder >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "[%s] recursion not permitted, max depth: %d", __func__, MAX_CMD_RECURSION_DEPTH);

inGiveOrder = true;
inGiveOrder++;
int count = 0;
for (CUnit* unit: units) {
if (CanControlUnit(L, unit)) {
unit->commandAI->GiveCommand(cmd, -1, true, true);
count++;
}
}
inGiveOrder = false;
inGiveOrder--;

lua_pushnumber(L, count);
return 1;
Expand All @@ -3628,10 +3619,10 @@ int LuaSyncedCtrl::GiveOrderToUnitArray(lua_State* L)

Command cmd = LuaUtils::ParseCommand(L, __func__, 2);

if (inGiveOrder)
luaL_error(L, "[%s] recursion not permitted", __func__);
if (inGiveOrder >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "[%s] recursion not permitted, max depth: %d", __func__, MAX_CMD_RECURSION_DEPTH);

inGiveOrder = true;
inGiveOrder++;

int count = 0;
for (CUnit* unit: units) {
Expand All @@ -3641,7 +3632,7 @@ int LuaSyncedCtrl::GiveOrderToUnitArray(lua_State* L)
}
}

inGiveOrder = false;
inGiveOrder--;

lua_pushnumber(L, count);
return 1;
Expand All @@ -3663,10 +3654,10 @@ int LuaSyncedCtrl::GiveOrderArrayToUnitMap(lua_State* L)
return 1;
}

if (inGiveOrder)
luaL_error(L, "[%s] recursion not permitted", __func__);
if (inGiveOrder >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "[%s] recursion not permitted, max depth: %d", __func__, MAX_CMD_RECURSION_DEPTH);

inGiveOrder = true;
inGiveOrder++;

int count = 0;
for (CUnit* unit: units) {
Expand All @@ -3677,7 +3668,7 @@ int LuaSyncedCtrl::GiveOrderArrayToUnitMap(lua_State* L)
count++;
}
}
inGiveOrder = false;
inGiveOrder--;

lua_pushnumber(L, count);
return 1;
Expand All @@ -3700,10 +3691,10 @@ int LuaSyncedCtrl::GiveOrderArrayToUnitArray(lua_State* L)
return 1;
}

if (inGiveOrder)
luaL_error(L, "[%s] recursion not permitted", __func__);
if (inGiveOrder >= MAX_CMD_RECURSION_DEPTH)
luaL_error(L, "[%s] recursion not permitted, max depth: %d", __func__, MAX_CMD_RECURSION_DEPTH);

inGiveOrder = true;
inGiveOrder++;

int count = 0;

Expand All @@ -3726,7 +3717,7 @@ int LuaSyncedCtrl::GiveOrderArrayToUnitArray(lua_State* L)
}
}
}
inGiveOrder = false;
inGiveOrder--;

lua_pushnumber(L, count);
return 1;
Expand Down
18 changes: 10 additions & 8 deletions rts/Lua/LuaSyncedCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@

struct lua_State;

constexpr int MAX_CMD_RECURSION_DEPTH = 16;

class LuaSyncedCtrl
{
public:
static bool PushEntries(lua_State* L);
static void CheckAllowGameChanges(lua_State* L);

private:
static bool inCreateUnit;
static bool inDestroyUnit;
static bool inTransferUnit;
static bool inCreateFeature;
static bool inDestroyFeature;
static bool inGiveOrder;
static bool inHeightMap;
static bool inSmoothMesh;
inline static int inCreateUnit;
inline static int inDestroyUnit;
inline static int inCreateFeature;
badosu marked this conversation as resolved.
Show resolved Hide resolved
inline static int inDestroyFeature;
inline static int inGiveOrder;
inline static bool inTransferUnit;
badosu marked this conversation as resolved.
Show resolved Hide resolved
inline static bool inHeightMap;
inline static bool inSmoothMesh;

private:
// all LuaHandleSynced
Expand Down