From 1ef5eb7a0f31f9e3324d17f65f1e0c062f90d033 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 10 Jun 2014 00:52:19 +1000 Subject: [PATCH] Pager's heavyweight API methods call the Region's counterparts --- common/map/pager_api.cpp | 71 +++++++++++++++++++++++++++++++++------ common/map/region_api.cpp | 10 ++---- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/common/map/pager_api.cpp b/common/map/pager_api.cpp index 16c10dd..332cc5e 100644 --- a/common/map/pager_api.cpp +++ b/common/map/pager_api.cpp @@ -24,12 +24,12 @@ #include "region_pager.hpp" #include "region.hpp" +#include #include static int setTile(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5)); - lua_pop(L, 5); lua_pushinteger(L, ret); return 1; } @@ -37,7 +37,6 @@ static int setTile(lua_State* L) { static int getTile(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4)); - lua_pop(L, 4); lua_pushinteger(L, ret); return 1; } @@ -45,7 +44,6 @@ static int getTile(lua_State* L) { static int getRegion(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); - lua_pop(L, 3); lua_pushlightuserdata(L, region); return 1; } @@ -53,7 +51,6 @@ static int getRegion(lua_State* L) { static int setDirectory(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); std::string s = pager->SetDirectory(lua_tostring(L, 2)); - lua_pop(L, 2); lua_pushstring(L, s.c_str()); return 1; } @@ -61,28 +58,82 @@ static int setDirectory(lua_State* L) { static int getDirectory(lua_State* L) { RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); std::string s = pager->GetDirectory(); - lua_pop(L, 1); lua_pushstring(L, s.c_str()); return 1; } static int loadRegion(lua_State* L) { - //TODO: fill this - return 0; + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + std::string s = pager->GetDirectory(); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "load"); + lua_pushlightuserdata(L, region); + lua_pushstring(L, s.c_str()); + + //call the method + if (lua_pcall(L, 2, 1, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } + return 1; } static int saveRegion(lua_State* L) { - //TODO: fill this + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + std::string s = pager->GetDirectory(); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "save"); + lua_pushlightuserdata(L, region); + lua_pushstring(L, s.c_str()); + + //call the method + if (lua_pcall(L, 2, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } return 0; } static int createRegion(lua_State* L) { - //TODO: fill this + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "create"); + lua_pushlightuserdata(L, region); + //TODO: parameters + + //call the method + if (lua_pcall(L, 1, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } return 0; } static int unloadRegion(lua_State* L) { - //TODO: fill this + //get the parameters + RegionPager* pager = reinterpret_cast(lua_touserdata(L, 1)); + Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3)); + std::string s = pager->GetDirectory(); + + //push the parameters + lua_getglobal(L, "region"); + lua_getfield(L, -1, "unload"); + lua_pushlightuserdata(L, region); + lua_pushstring(L, s.c_str()); + + //call the method + if (lua_pcall(L, 2, 0, 0) != LUA_OK) { + throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(L, -1) )); + } return 0; } diff --git a/common/map/region_api.cpp b/common/map/region_api.cpp index 9e16d45..df37532 100644 --- a/common/map/region_api.cpp +++ b/common/map/region_api.cpp @@ -26,7 +26,6 @@ static int setTile(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5)); - lua_pop(L, 5); lua_pushinteger(L, ret); return 1; } @@ -34,24 +33,19 @@ static int setTile(lua_State* L) { static int getTile(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1); - lua_pop(L, 4); lua_pushinteger(L, ret); return 1; } static int getX(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); - int ret = region->GetX(); - lua_pop(L, 1); - lua_pushinteger(L, ret); + lua_pushinteger(L, region->GetX()); return 1; } static int getY(lua_State* L) { Region* region = reinterpret_cast(lua_touserdata(L, 1)); - int ret = region->GetY(); - lua_pop(L, 1); - lua_pushinteger(L, ret); + lua_pushinteger(L, region->GetY()); return 1; }