LuaTableData PropertyManager::getTableData(luabridge::LuaRef ref) const { LuaTableData properties(ref); lua_State* L = ref.state(); ref.push(L); luabridge::push(L, luabridge::Nil()); while (lua_next(L, -2)) { luabridge::LuaRef key = luabridge::LuaRef::fromStack(L, -2); luabridge::LuaRef val = luabridge::LuaRef::fromStack(L, -1); properties.data.push_back(std::make_pair(key, val)); lua_pop(L, 1); } return properties; }
void getClassMembers( lua_State* L, luabridge::LuaRef ret ) { // List keys where they don't start with underscore. lua_pushnil( L ); while( lua_next( L, -2 ) ) { lua_pushvalue( L, -2 ); // Push copy of key. const char *key = lua_tostring( L, -1 ); if( key ) { if( key[0] != '_' ) { ret.append( key ); } } lua_pop( L, 2 ); // Key copy and value } // List properties lua_pushstring( L, "__propget" ); lua_rawget( L, -2 ); if( lua_istable( L, -1 ) ) { lua_pushnil( L ); while( lua_next( L, -2 ) ) { lua_pushvalue( L, -2 ); // Push copy of key. const char *key = lua_tostring( L, -1 ); if( key ) { ret.append( key ); } lua_pop( L, 2 ); // Key copy and value } } lua_pop( L, 1 ); // __propget table return; }
EntityTemplate::EntityTemplate(World& world, luabridge::LuaRef luaE) : m_world(world) { IComponent* component; for (int j = 1; j <= luaE.length(); j++) { luabridge::LuaRef luaComponent = luaE[j]; assert(luaComponent.isTable()); luabridge::LuaRef luaType = luaComponent["type"]; assert(luaType.isString()); auto type = luaType.cast<std::string>(); if (type == "Physics") { component = m_world.getField().getSystem<PhysicsComponent>()->createTemplate(luaComponent); m_components.insert(std::make_pair(getType<PhysicsComponent>(), std::unique_ptr<IComponent>(component))); } } }
luabridge::LuaRef CLuaBinary::getStructAttr(luabridge::LuaRef &objAttr, std::string &strName, DataType &emType, int &iSize) { strName.clear(); emType = DTYPE_SINT8; iSize = Q_INIT_NUMBER; luabridge::LuaRef objTable = luabridge::newTable(m_pLua); for (int j = 1; j <= objAttr.length(); j++) { switch(j) { case STATTR_NAME: { strName = objAttr[j].cast<std::string>(); } break; case STATTR_TYPE: { emType = (DataType)objAttr[j].cast<int>(); } break; case STATTR_SIZE: { iSize = objAttr[j].cast<int>(); } break; case STATTR_STATTR: { objTable = objAttr[j]; } break; default: break; } } return objTable; }
luabridge::LuaRef CLuaBinary::getStruct(luabridge::LuaRef objAttr) { assert(objAttr.isTable()); DataType emType; int iSize = Q_INIT_NUMBER; std::string strName; luabridge::LuaRef objVal = luabridge::newTable(m_pLua); for (int i = 1; i <= objAttr.length(); i++) { luabridge::LuaRef objTmp = objAttr[i]; if (!objTmp.isTable() || STATTRTAB_MINLENS > objTmp.length()) { break; } luabridge::LuaRef objChildStAttr = getStructAttr(objTmp, strName, emType, iSize); switch(emType) { case DTYPE_SINT8: { objVal[strName] = getSint8(); } break; case DTYPE_UINT8: { objVal[strName] = getUint8(); } break; case DTYPE_BOOL: { objVal[strName] = getBool(); } break; case DTYPE_SINT16: { objVal[strName] = getSint16(); } break; case DTYPE_UINT16: { objVal[strName] = getUint16(); } break; case DTYPE_SINT32: { objVal[strName] = getSint32(); } break; case DTYPE_UINT32: { objVal[strName] = getUint32(); } break; case DTYPE_SINT64: { objVal[strName] = getSint64(); } break; case DTYPE_UINT64: { objVal[strName] = getUint64(); } break; case DTYPE_FLOAT: { objVal[strName] = getFloat(); } break; case DTYPE_DOUBLE: { objVal[strName] = getDouble(); } break; case DTYPE_STRING: { std::string strVal = getString(); objVal[strName] = strVal; skipRead((unsigned int)(iSize - (strVal.size() + 1))); } break; case DTYPE_BYTE: { objVal[strName] = getByte(iSize); } break; case DTYPE_STRUCT: { objVal[strName] = getStruct(objChildStAttr); } break; case DTYPE_SKIP: { if (iSize > Q_INIT_NUMBER) { skipRead(iSize); } } break; default: break; } } return objVal; }
bool CLuaBinary::setStruct(luabridge::LuaRef objVal, luabridge::LuaRef objAttr) { assert(objVal.isTable()); assert(objAttr.isTable()); bool bOk = false; DataType emType; int iSize = Q_INIT_NUMBER; std::string strName; for (int i = 1; i <= objAttr.length(); i++) { luabridge::LuaRef objTmp = objAttr[i]; if (!objTmp.isTable() || STATTRTAB_MINLENS > objTmp.length()) { return false; } luabridge::LuaRef objChildStAttr = getStructAttr(objTmp, strName, emType, iSize); luabridge::LuaRef objFieldVal = objVal[strName]; switch(emType) { case DTYPE_SINT8: { bOk = setSint8(getLuaNumber<short>(objFieldVal)); } break; case DTYPE_UINT8: { bOk = setUint8(getLuaNumber<unsigned short>(objFieldVal)); } break; case DTYPE_BOOL: { bOk = setBool(((Q_INIT_NUMBER != getLuaNumber<unsigned short>(objFieldVal)) ? true : false)); } break; case DTYPE_SINT16: { bOk = setSint16(getLuaNumber<short>(objFieldVal)); } break; case DTYPE_UINT16: { bOk = setUint16(getLuaNumber<unsigned short>(objFieldVal)); } break; case DTYPE_SINT32: { bOk = setSint32(getLuaNumber<int>(objFieldVal)); } break; case DTYPE_UINT32: { bOk = setUint32(getLuaNumber<unsigned int>(objFieldVal)); } break; case DTYPE_SINT64: { bOk = setSint64(Q_ToString(getLuaNumber<int64_t>(objFieldVal)).c_str()); } break; case DTYPE_UINT64: { bOk = setUint64(Q_ToString(getLuaNumber<uint64_t>(objFieldVal)).c_str()); } break; case DTYPE_FLOAT: { bOk = setFloat(getLuaNumber<float>(objFieldVal)); } break; case DTYPE_DOUBLE: { bOk = setDouble(getLuaNumber<double>(objFieldVal)); } break; case DTYPE_STRING: { if (!objFieldVal.isString()) { bOk = false; break; } const char *pszVal = objFieldVal.cast<const char*>(); if (objFieldVal.length() >= iSize) { bOk = false; Q_Printf("%s", "data len too large."); break; } bOk = setString(pszVal); if (bOk) { bOk = skipWrite(iSize - (objFieldVal.length() + 1)); } } break; case DTYPE_BYTE: { if (!objFieldVal.isString()) { bOk = false; break; } const char *pszVal = objFieldVal.cast<const char*>(); if (objFieldVal.length() > iSize) { bOk = false; Q_Printf("%s", "data len too large."); break; } bOk = setByte(pszVal, objFieldVal.length()); if (bOk) { bOk = skipWrite(iSize - objFieldVal.length()); } } break; case DTYPE_STRUCT: { if (!objFieldVal.isTable() || !objChildStAttr.isTable()) { bOk = false; break; } bOk = setStruct(objFieldVal, objChildStAttr); } break; case DTYPE_SKIP: { if (iSize > Q_INIT_NUMBER) { bOk = skipWrite(iSize); } } break; default: bOk = false; break; } if (!bOk) { return bOk; } } return true; }