finished rewriting the room API
This commit is contained in:
@@ -29,21 +29,36 @@ static int getPager(lua_State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int create(lua_State* L) {
|
||||
//EMPTY
|
||||
//NOTE: This can be used to set defaults for the pager
|
||||
static int setRoomName(lua_State* L) {
|
||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||
room->SetRoomName(lua_tostring(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unload(lua_State* L) {
|
||||
//EMPTY
|
||||
static int getRoomName(lua_State* L) {
|
||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||
lua_pushstring(L, room->GetRoomName().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int setTilesetName(lua_State* L) {
|
||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||
room->SetTilesetName(lua_tostring(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getTilesetName(lua_State* L) {
|
||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||
lua_pushstring(L, room->GetTilesetName().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const luaL_Reg roomLib[] = {
|
||||
{"GetPager",getPager},
|
||||
{"Create", create},
|
||||
{"Unload", unload},
|
||||
{"SetRoomName", setRoomName},
|
||||
{"GetRoomName", getRoomName},
|
||||
{"SetTileset", setTilesetName},
|
||||
{"GetTileset", getTilesetName},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
@@ -35,6 +35,9 @@ public:
|
||||
//accessors and mutators
|
||||
RegionPagerLua* GetPager() { return &pager; }
|
||||
|
||||
std::string SetRoomName(std::string s) { return roomName = s; }
|
||||
std::string GetRoomName() { return roomName; }
|
||||
|
||||
std::string SetTilesetName(std::string s) { return tilesetName = s; }
|
||||
std::string GetTilesetName() { return tilesetName; }
|
||||
|
||||
@@ -43,7 +46,10 @@ private:
|
||||
|
||||
//members
|
||||
RegionPagerLua pager;
|
||||
std::string roomName;
|
||||
std::string tilesetName;
|
||||
//TODO: pass the room name & tileset name to the clients
|
||||
//TODO: lua references i.e. create, unload, etc.
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -30,74 +30,44 @@
|
||||
//-------------------------
|
||||
|
||||
int RoomManager::Create() {
|
||||
/* //create the room
|
||||
RoomData* newRoom =
|
||||
newRoom->pager.SetLuaState(luaState);
|
||||
|
||||
//register the room
|
||||
roomMap[counter] = newRoom;
|
||||
|
||||
//API hook
|
||||
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||
lua_getfield(luaState, -1, "Create");
|
||||
lua_pushlightuserdata(luaState, newRoom);
|
||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
||||
}
|
||||
lua_pop(luaState, 1);
|
||||
//create the room
|
||||
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
|
||||
newRoom->pager.SetLuaState(lua);
|
||||
|
||||
//finish the routine
|
||||
*/ return counter++;
|
||||
return counter++;
|
||||
}
|
||||
|
||||
int RoomManager::Load() {
|
||||
//TODO: RoomManageR::Load()
|
||||
//TODO: RoomManager::Load()
|
||||
return -1;
|
||||
}
|
||||
|
||||
int RoomManager::Save(int uid) {
|
||||
//TODO: RoomManageR::Save(uid)
|
||||
//TODO: RoomManager::Save(uid)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void RoomManager::Unload(int uid) {
|
||||
/* //find the room
|
||||
RoomData* room = elementMap.find(uid);
|
||||
if (room == elementMap.end()) {
|
||||
//find the room
|
||||
std::map<int, RoomData>::iterator it = elementMap.find(uid);
|
||||
if (it == elementMap.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//API hook
|
||||
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||
lua_getfield(luaState, -1, "Unload");
|
||||
lua_pushlightuserdata(luaState, room);
|
||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
||||
}
|
||||
lua_pop(luaState, 1);
|
||||
|
||||
//free the memory
|
||||
delete room;
|
||||
roomMap.erase(uid);
|
||||
*/}
|
||||
elementMap.erase(uid);
|
||||
}
|
||||
|
||||
void RoomManager::Delete(int uid) {
|
||||
//
|
||||
//TODO: RoomManager::Delete(int uid)
|
||||
//NOTE: aliased to RoomManager::Unload(int uid)
|
||||
Unload(uid);
|
||||
}
|
||||
|
||||
void RoomManager::UnloadAll() {
|
||||
/* lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||
|
||||
for (auto& it : roomMap) {
|
||||
//API hook
|
||||
lua_getfield(luaState, -1, "Unload");
|
||||
lua_pushlightuserdata(luaState, it.second);
|
||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
||||
}
|
||||
}
|
||||
|
||||
lua_pop(luaState, 1);
|
||||
roomMap.clear();
|
||||
*/}
|
||||
elementMap.clear();
|
||||
}
|
||||
|
||||
void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) {
|
||||
std::map<int, RoomData>::iterator it = elementMap.begin();
|
||||
|
||||
@@ -23,34 +23,34 @@
|
||||
|
||||
#include "room_manager.hpp"
|
||||
|
||||
#include <string>
|
||||
/*
|
||||
static int getRoom(lua_State* L) {
|
||||
//find, push and return the room
|
||||
RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2));
|
||||
lua_pushlightuserdata(L, reinterpret_cast<void*>(room));
|
||||
return 1;
|
||||
int createRoom(lua_State* L) {
|
||||
//create & get the room
|
||||
RoomManager& roomMgr = RoomManager::GetSingleton();
|
||||
int uid = roomMgr.Create();
|
||||
RoomData* room = roomMgr.Get(uid);
|
||||
|
||||
//setup the room
|
||||
//TODO: room parameters only set via lua, fix this
|
||||
room->SetRoomName(lua_tostring(L, 1));
|
||||
room->SetTilesetName(lua_tostring(L, 2));
|
||||
|
||||
//return room, uid
|
||||
lua_pushlightuserdata(L, static_cast<void*>(room));
|
||||
lua_pushinteger(L, uid);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int createRoom(lua_State* L) {
|
||||
//TODO: check parameter count for the glue functions
|
||||
|
||||
//create, find and return the room
|
||||
int uid = RoomManager::GetSingleton().CreateRoom();
|
||||
lua_pushlightuserdata(L, RoomManager::GetSingleton().FindRoom(uid));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int unloadRoom(lua_State* L) {
|
||||
//unload the specified room
|
||||
RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
|
||||
int unloadRoom(lua_State* L) {
|
||||
//TODO: check authorization for room deletion
|
||||
RoomManager& roomMgr = RoomManager::GetSingleton();
|
||||
roomMgr.Unload(lua_tointeger(L, 1));
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
static const luaL_Reg roomManagerLib[] = {
|
||||
// {"GetRoom",getRoom},
|
||||
// {"CreateRoom",createRoom},
|
||||
// {"UnloadRoom",unloadRoom},
|
||||
{"CreateRoom", createRoom},
|
||||
{"UnloadRoom", unloadRoom},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user