| 181 | } |
| 182 | |
| 183 | void Script::FillCollection(lua_State* pState, UInt32 size) { |
| 184 | int index = -1-(size<<1); // index collection (-1-2*size) |
| 185 | |
| 186 | if (!lua_getmetatable(pState, index)) { |
| 187 | SCRIPT_BEGIN(pState) |
| 188 | SCRIPT_ERROR("Invalid collection to fill, no metatable") |
| 189 | SCRIPT_END |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | lua_getfield(pState, -1, "|items"); |
| 194 | if (!lua_istable(pState, -1)) { |
| 195 | lua_pop(pState, 2); |
| 196 | SCRIPT_BEGIN(pState) |
| 197 | SCRIPT_ERROR("Invalid collection to fill, no |items field in metatable") |
| 198 | SCRIPT_END |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | lua_insert(pState, index-1); // insert |item to the collection index, just before keys/values |
| 203 | lua_insert(pState, index-1); // insert metatable just before |items |
| 204 | |
| 205 | int count(0); |
| 206 | while (size-- > 0) { |
| 207 | |
| 208 | if (lua_isstring(pState, -2)) { // key |
| 209 | const char* key(lua_tostring(pState, -2)); |
| 210 | const char* sub(strchr(key, '.')); |
| 211 | if (sub) { |
| 212 | *(char*)sub = '\0'; |
| 213 | Collection(pState, index-2, key); |
| 214 | *(char*)sub = '.'; |
| 215 | lua_pushstring(pState, sub + 1); |
| 216 | lua_pushvalue(pState, -3); // value |
| 217 | FillCollection(pState, 1); |
| 218 | lua_pop(pState, 1); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // check if key exists already to update count |
| 223 | lua_pushvalue(pState, -2); // key |
| 224 | lua_rawget(pState, index-1); |
| 225 | if (!lua_isnil(pState, -1)) { // if old value exists |
| 226 | if (lua_isnil(pState, -2)) // if new value is nil (erasing) |
| 227 | --count; |
| 228 | } else if (lua_isnil(pState, -1)) // if old value doesn't exists |
| 229 | ++count; |
| 230 | lua_pop(pState, 1); |
| 231 | |
| 232 | lua_rawset(pState,index); |
| 233 | index += 2; |
| 234 | } |
| 235 | |
| 236 | lua_pop(pState, 1); // remove |items |
| 237 | |
| 238 | lua_getfield(pState,-1,"|count"); |
| 239 | lua_pushnumber(pState,lua_tonumber(pState,-1)+count); |
| 240 | lua_replace(pState, -2); |
nothing calls this directly
no test coverage detected