Skip to content

Commit

Permalink
check of lua types
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Mar 21, 2024
1 parent 68d25b2 commit b5faa0f
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 52 deletions.
67 changes: 58 additions & 9 deletions src/luacombat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ int luaCombatCreate(lua_State* L)

int luaCombatDelete(lua_State* L)
{
if (!isType<Combat>(L, 1)) {
return 0;
}

Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (combat) {
combat.reset();
Expand All @@ -36,9 +40,14 @@ int luaCombatDelete(lua_State* L)
int luaCombatSetParameter(lua_State* L)
{
// combat:setParameter(key, value)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -58,9 +67,14 @@ int luaCombatSetParameter(lua_State* L)
int luaCombatGetParameter(lua_State* L)
{
// combat:getParameter(key)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -78,9 +92,14 @@ int luaCombatGetParameter(lua_State* L)
int luaCombatSetFormula(lua_State* L)
{
// combat:setFormula(type, mina, minb, maxa, maxb)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand Down Expand Up @@ -111,9 +130,14 @@ int luaCombatSetArea(lua_State* L)
return 1;
}

if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -126,9 +150,14 @@ int luaCombatSetArea(lua_State* L)
int luaCombatAddCondition(lua_State* L)
{
// combat:addCondition(condition)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -146,9 +175,14 @@ int luaCombatAddCondition(lua_State* L)
int luaCombatClearConditions(lua_State* L)
{
// combat:clearConditions()
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -161,9 +195,14 @@ int luaCombatClearConditions(lua_State* L)
int luaCombatSetCallback(lua_State* L)
{
// combat:setCallback(key, callback)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -182,9 +221,14 @@ int luaCombatSetCallback(lua_State* L)
int luaCombatSetOrigin(lua_State* L)
{
// combat:setOrigin(origin)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand All @@ -197,9 +241,14 @@ int luaCombatSetOrigin(lua_State* L)
int luaCombatExecute(lua_State* L)
{
// combat:execute(creature, variant)
if (!isType<Combat>(L, 1)) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}

const Combat_ptr& combat = getSharedPtr<Combat>(L, 1);
if (!combat) {
reportErrorFunc(L, LuaScriptInterface::getErrorDesc(LuaErrorCode::COMBAT_NOT_FOUND));
lua_pushnil(L);
return 1;
}
Expand Down
19 changes: 4 additions & 15 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,22 +2164,11 @@ void LuaScriptInterface::registerClass(const std::string& className, const std::
lua_rawseti(luaState, metatable, 'p');

// className.metatable['t'] = type
if (className == "Item") {
lua_pushinteger(luaState, LuaData_Item);
} else if (className == "Container") {
lua_pushinteger(luaState, LuaData_Container);
} else if (className == "Teleport") {
lua_pushinteger(luaState, LuaData_Teleport);
} else if (className == "Player") {
lua_pushinteger(luaState, LuaData_Player);
} else if (className == "Monster") {
lua_pushinteger(luaState, LuaData_Monster);
} else if (className == "Npc") {
lua_pushinteger(luaState, LuaData_Npc);
} else if (className == "Tile") {
lua_pushinteger(luaState, LuaData_Tile);
} else {
auto luaDataType = LuaDataTypeByClassName.find(className);
if (luaDataType == LuaDataTypeByClassName.end()) {
lua_pushinteger(luaState, LuaData_Unknown);
} else {
lua_pushinteger(luaState, luaDataType->second);
}
lua_rawseti(luaState, metatable, 't');

Expand Down
Loading

0 comments on commit b5faa0f

Please sign in to comment.