1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "com_lua_module.h"
19 #include "nel/misc/debug.h"
20 #include "nel/misc/path.h"
21 //#include "nel/misc/string.h"
23 #include "simulated_dmc.h"
24 //#include "palette.h"
25 //#include "property_accessor.h"
26 #include "simulated_client_edition_module.h"
27 #include "simulated_client_animation_module.h"
28 //#include "client_admin_module.h"
30 #include "r2_share/object.h"
31 #include "r2_share/r2_lua.h"
32 #include "r2_share/scenario.h"
41 //#include "lua_helper.h"
42 //#include "lua_ihm.h"
47 std::map
<lua_State
*, CComLuaModule
*> CComLuaModule::_Instance
;
49 #define CHECK_LUA_ARG_COUNT(count, funcName) if (args != count) { nlwarning("%d args required for lua function %s.", count, funcName); return 0; }
50 #define CHECK_LUA_ARG_COUNT_MAX(count, funcName) if (args > count) { nlwarning("Lua function %s accept at most %d arguments.", funcName, count); return 0; }
53 CComLuaModule::CComLuaModule(CDynamicMapClient
* client
, lua_State
*luaState
/*= NULL*/)
59 #ifdef LUA_NEVRAX_VERSION
60 _LuaState
= lua_open(NULL
, NULL
);
62 _LuaState
= lua_open();
64 _LuaOwnerShip
= false;
65 luaopen_base(_LuaState
);
66 luaopen_table(_LuaState
);
67 luaopen_io(_LuaState
);
68 luaopen_string(_LuaState
);
69 luaopen_math(_LuaState
);
70 luaopen_debug(_LuaState
);
76 _LuaOwnerShip
= false;
79 _Instance
[_LuaState
] = this;
86 void CComLuaModule::initLuaLib()
88 const luaL_reg methods
[] =
90 {"updateScenario", CComLuaModule::luaUpdateScenario
},
91 {"requestUpdateRtScenario", CComLuaModule::luaRequestUpdateRtScenario
},
92 {"requestCreateScenario", CComLuaModule::luaRequestCreateScenario
},
93 {"requestMapConnection", CComLuaModule::luaRequestMapConnection
},
94 {"requestReconnection", CComLuaModule::luaRequestReconnection
},
95 {"requestListAdventure", CComLuaModule::luaRequestListAdventure
},
97 {"requestInsertNode", CComLuaModule::luaRequestInsertNode
},
98 {"requestInsertGhostNode", CComLuaModule::luaRequestInsertGhostNode
},
99 {"requestSetNode", CComLuaModule::luaRequestSetNode
},
100 {"requestEraseNode", CComLuaModule::luaRequestEraseNode
},
101 {"requestMoveNode", CComLuaModule::luaRequestMoveNode
},
102 {"requestGoLive", CComLuaModule::luaRequestGoLive
},
103 {"requestStopLive", CComLuaModule::luaRequestStopLive
},
104 {"requestStartAct", CComLuaModule::luaRequestStartAct
},
105 {"requestStopAct", CComLuaModule::luaRequestStopAct
},
106 {"requestStopAct", CComLuaModule::luaRequestStopAct
},
107 {"requestCreatePrimitives", CComLuaModule::luaRequestCreatePrimitives
},
108 {"requestSetWeather", CComLuaModule::luaRequestSetWeather
},
110 {"requestTpPosition", CComLuaModule::luaRequestTpPosition
},
111 {"requestDespawnEntity", CComLuaModule::luaRequestDespawnEntity
},
112 {"requestDespawnGrp", CComLuaModule::luaRequestDespawnGrp
},
114 {"requestStopAct", CComLuaModule::luaRequestStopAct
},
115 {"newComponent", CComLuaModule::luaNewComponent
},
116 {"requestTranslateFeatures", CComLuaModule::luaRequestTranslateFeatures
},
117 {"registerGenerator", CComLuaModule::luaRegisterGenerator
},
118 {"show", CComLuaModule::luaShow
},
119 {"addPaletteElement", CComLuaModule::luaAddPaletteElement
},
120 {"getPaletteElement", CComLuaModule::luaGetPaletteElement
},
121 {"getPropertyValue", CComLuaModule::luaGetPropertyValue
},
122 {"getScenarioObj",CComLuaModule::luaGetScenarioObj
},
123 /*{"getPropertyList", CComLuaModule::luaGetPropertyList},*/
124 {"doFile2", CComLuaModule::luaDoFile2
},
125 {"print", CComLuaModule::luaPrint
},
126 {"save", CComLuaModule::luaSave
},
127 {"load",CComLuaModule::luaLoad
},
128 {"requestTalkAs",CComLuaModule::luaTalkAs
},
129 {"requestStopTalk",CComLuaModule::luaStopTalkAs
},
130 {"requestStringTable",CComLuaModule::luaRequestStringTable
},
131 {"requestSetStringValue", CComLuaModule::luaRequestSetStringValue
},
132 {"requestStringValue", CComLuaModule::luaRequestStringValue
},
133 {"requestIdList", CComLuaModule::luaRequestIdList
},
135 {"getNamespace", CComLuaModule::luaGetNamespace
},
136 {"getIslandsLocation", CComLuaModule::luaGetIslandsLocation
},
141 int initialStackSize
= lua_gettop(_LuaState
);
142 luaL_openlib(_LuaState
, R2_LUA_PATH
, methods
, 0);
143 lua_settop(_LuaState
, initialStackSize
);
144 // load r2 features & components
145 doFile( "r2_core.lua" );
149 void CComLuaModule::doFile(const std::string
& filename
)
151 std::string filePath
= NLMISC::CPath::lookup(filename
, false, true);
152 if (filePath
.empty())
154 nlwarning("Can't find %s", filename
.c_str());
157 std::string str
= NLMISC::toString("dofile(\"%s\")", filePath
.c_str() );
158 std::string errorMsg
;
159 runLuaScript(str
.c_str(), errorMsg
);
162 bool CComLuaModule::runLuaScript(const std::string
& script
, std::string
& erromsg
)
164 const char *buff
= script
.c_str();
165 size_t size
= script
.size();
166 const char *name
= script
.c_str();
168 int status
= luaL_loadbuffer(_LuaState
, buff
, size
, name
);
171 status
= lua_pcall(_LuaState
, 0, LUA_MULTRET
, 0); /* call main */
176 erromsg
= NLMISC::toString("%s\n", lua_tostring(_LuaState
, -1));
177 lua_pop(_LuaState
, 1);
183 sint
CComLuaModule::luaDoFile2(lua_State
* state
)
185 sint args
= lua_gettop(state
);
187 luaL_checktype(state
, 1, LUA_TSTRING
);
188 CComLuaModule
* this2
= getInstance(state
);
190 std::string
key( lua_tostring(state
, 1) );
196 void CComLuaModule::loadFeatures()
198 doFile("r2_core.lua");
201 CComLuaModule
* CComLuaModule::getInstance(lua_State
* state
)
203 std::map
<lua_State
*, CComLuaModule
*>::const_iterator
found(_Instance
.find(state
));
204 if (found
!= _Instance
.end())
206 return found
->second
;
211 void CComLuaModule::callTranslateFeatures(CObject
* scenario
)
215 nlwarning("<CComLuaModule::callTranslateFeatures> called on NULL scenario");
218 lua_getglobal(_LuaState
, "r2");
219 lua_pushstring(_LuaState
, "translateFeatures");
220 lua_gettable(_LuaState
, -2);
221 setObjectToLua(_LuaState
, scenario
);
222 lua_call(_LuaState
, 1, 0);
226 CObject
* CComLuaModule::translateFeatures(CObject
* hlScenario
, std::string
& errorMsg
) const
230 errorMsg
= "<CComLuaModule::translateFeatures> called on NULL scenario";
233 lua_getglobal(_LuaState
, "r2");
234 lua_pushstring(_LuaState
, "doTranslateFeatures");
235 lua_gettable(_LuaState
, -2);
236 setObjectToLua(_LuaState
, hlScenario
);
237 if ( lua_pcall(_LuaState
, 1, 1, 0) !=0 )
239 errorMsg
= NLMISC::toString( "error running function 'doTranslateFeatures': %s", lua_tostring(_LuaState
, -1));
242 CObject
* ret
= getObjectFromLua(_LuaState
, -1);
246 sint
CComLuaModule::luaAddPaletteElement(lua_State
* state
)
249 luaL_checktype(state
, 1, LUA_TSTRING
);
250 luaL_checktype(state
, 2, LUA_TTABLE
);
251 CComLuaModule
* this2
= getInstance(state
);
253 std::string
key( lua_tostring(state
, 1) );
254 CObject
* object
= this2
->getObjectFromLua(state
, 2);
255 this2
->_Client
->addPaletteElement(key
, object
);
259 sint
CComLuaModule::luaGetPropertyValue(lua_State
* state
)
261 sint args
= lua_gettop(state
);
263 luaL_checktype(state
, 1, LUA_TTABLE
);
264 luaL_checktype(state
, 2, LUA_TSTRING
);
265 CComLuaModule
* this2
= getInstance(state
);
267 CObject
* object
= this2
->getObjectFromLua(state
, 1);
268 std::string
attrName( lua_tostring(state
, 2) );
269 CObject
* toRet
= this2
->_Client
->getPropertyValue(object
, attrName
);
270 this2
->setObjectToLua(state
, toRet
);
275 /*sint CComLuaModule::luaGetPropertyList(lua_State* state)
277 sint args = lua_gettop(state);
279 luaL_checktype(state, 1, LUA_TTABLE);
280 CComLuaModule* this2 = getInstance(state);
282 CObject* object = this2->getObjectFromLua(state, 1);
283 CObject* toRet = this2->_Client->getPropertyList(object);
284 this2->setObjectToLua(state, toRet);
288 sint
CComLuaModule::luaGetPaletteElement(lua_State
* state
)
291 luaL_checktype(state
, 1, LUA_TSTRING
);
292 CComLuaModule
* this2
= getInstance(state
);
294 std::string
key( lua_tostring(state
, 1) );
295 CObject
* object
= this2
->_Client
->getPaletteElement(key
);
296 this2
->setObjectToLua(state
, object
);
300 sint
CComLuaModule::luaRequestTranslateFeatures(lua_State
* state
)
303 CComLuaModule
* this2
= getInstance(state
);
305 this2
->_Client
->requestTranslateFeatures();
309 sint
CComLuaModule::luaRequestGoLive(lua_State
* state
)
311 CComLuaModule
* this2
= getInstance(state
);
313 this2
->_Client
->requestGoTest();
318 sint
CComLuaModule::luaRequestCreatePrimitives(lua_State
* state
)
320 CComLuaModule
* this2
= getInstance(state
);
322 this2
->_Client
->getEditionModule().requestCreatePrimitives();
327 sint
CComLuaModule::luaRequestSetWeather(lua_State
* state
)
329 luaL_checktype(state
, 1, LUA_TNUMBER
);
330 uint16 weatherValue
= (uint16
) lua_tonumber(state
, 1);
331 CComLuaModule
* this2
= getInstance(state
);
333 this2
->_Client
->getAnimationModule().requestSetWeather(weatherValue
);
337 sint
CComLuaModule::luaRequestStopLive(lua_State
* state
)
340 CComLuaModule
* this2
= getInstance(state
);
342 this2
->_Client
->getEditionModule().requestStopTest();
346 sint
CComLuaModule::luaRequestStartAct(lua_State
* state
)
348 luaL_checktype(state
, 1, LUA_TNUMBER
);
349 uint32
actId(static_cast<uint32
>(lua_tonumber(state
, 1)));
350 CComLuaModule
* this2
= getInstance(state
);
352 this2
->_Client
->getAnimationModule().requestStartAct(actId
);
356 sint
CComLuaModule::luaRequestStopAct(lua_State
* state
)
358 CComLuaModule
* this2
= getInstance(state
);
360 this2
->_Client
->getAnimationModule().requestStopAct();
364 sint
CComLuaModule::luaNewComponent(lua_State
* state
)
367 CComLuaModule
* this2
= getInstance(state
);
370 luaL_checktype(state
, 1, LUA_TSTRING
);
371 std::string
key(lua_tostring(state
, 1));
372 CObject
* object
= this2
->_Client
->newComponent(key
);
378 setObjectToLua(state
, object
);
383 sint
CComLuaModule::luaRegisterGenerator(lua_State
* state
)
385 luaL_checktype(state
, 1, LUA_TTABLE
);
386 CComLuaModule
* this2
= getInstance(state
);
388 CObject
* object
= this2
->getObjectFromLua(state
, 1);
389 this2
->_Client
->registerGenerator(object
);
393 sint
CComLuaModule::luaShow(lua_State
* state
)
395 CComLuaModule
* this2
= getInstance(state
);
397 this2
->_Client
->show();
401 sint
CComLuaModule::luaRequestUpdateRtScenario(lua_State
* state
)
403 CComLuaModule
* this2
= getInstance(state
);
405 CObject
* object
= this2
->getObjectFromLua(state
, 1);
407 this2
->_Client
->requestUpdateRtScenario(object
);
411 sint
CComLuaModule::luaRequestCreateScenario(lua_State
* state
)
413 luaL_checktype(state
, 1, LUA_TTABLE
);
414 CComLuaModule
* this2
= getInstance(state
);
416 CObject
* object
= this2
->getObjectFromLua(state
, 1);
418 this2
->_Client
->requestCreateScenario(object
);
423 sint
CComLuaModule::luaPrint(lua_State
* state
)
426 sint args
= lua_gettop(state
);
427 CHECK_LUA_ARG_COUNT(1, "print");
429 // luaL_checktype(state, 1, LUA_TSTRING);
430 CComLuaModule
* this2
= getInstance(state
);
432 CObject
* object
= this2
->getObjectFromLua(state
, 1);
438 std::stringstream ss
;
440 object
->serialize(ss
);
441 while (std::getline(ss
, s
))
443 nlinfo("%s", s
.c_str());
447 //this2->_Client->requestCreateScenario(object);
454 sint
CComLuaModule::luaRequestMapConnection(lua_State
* state
)
456 sint args
= lua_gettop(state
);
457 CHECK_LUA_ARG_COUNT(1, "requestMapConnection");
458 luaL_checktype(state
, 1, LUA_TNUMBER
);
460 uint32 adventureId
= static_cast<uint32
>( lua_tonumber(state
, 1) );
462 CComLuaModule
* this2
= getInstance(state
);
464 this2
->_Client
->getEditionModule().requestMapConnection(adventureId
, true);
471 sint
CComLuaModule::luaRequestReconnection(lua_State
* state
)
473 sint args
= lua_gettop(state
);
474 CHECK_LUA_ARG_COUNT(0, "requestReconnection");
476 CComLuaModule
* this2
= getInstance(state
);
478 this2
->_Client
->getEditionModule().requestReconnection();
484 sint
CComLuaModule::luaRequestListAdventure(lua_State
* state
)
486 sint args
= lua_gettop(state
);
487 CHECK_LUA_ARG_COUNT(0, "requestListAdventure");
489 CComLuaModule
* this2
= getInstance(state
);
491 // AJM: this2->_Client->getAdminModule().requestListAdventure();
497 sint
CComLuaModule::requestInsertNode(lua_State
* state
, bool isGhost
)
499 sint args
= lua_gettop(state
);
500 CHECK_LUA_ARG_COUNT(5, "requestInsertNode");
501 luaL_checktype(state
, 1, LUA_TSTRING
);
502 luaL_checktype(state
, 2, LUA_TSTRING
);
503 luaL_checktype(state
, 3, LUA_TNUMBER
);
504 luaL_checktype(state
, 4, LUA_TSTRING
);
505 luaL_checkany(state
, 5); //TODO just string, number and table
507 std::string
instanceId(lua_tostring(state
, 1));
508 std::string
attrName(lua_tostring(state
, 2));
509 sint
position(static_cast<sint
>(lua_tonumber(state
, 3)));
510 std::string
key(lua_tostring(state
, 4));
511 CObject
* value
= getObjectFromLua(state
, 5);
512 // value->setGhost(isGhost);
513 CComLuaModule
* this2
= getInstance(state
);
515 this2
->_Client
->requestInsertNode(
516 instanceId
, attrName
, position
, key
, value
);
522 sint
CComLuaModule::luaRequestInsertNode(lua_State
* state
)
524 return requestInsertNode(state
, false);
527 sint
CComLuaModule::luaRequestInsertGhostNode(lua_State
* state
)
529 return requestInsertNode(state
, true);
532 sint
CComLuaModule::luaRequestSetNode(lua_State
* state
)
534 sint args
= lua_gettop(state
);
535 CHECK_LUA_ARG_COUNT(3, "requestSetNode");
536 luaL_checktype(state
, 1, LUA_TSTRING
);
537 luaL_checktype(state
, 2, LUA_TSTRING
);
538 luaL_checkany(state
, 3); //TODO just string, number and table
540 std::string
instanceId(lua_tostring(state
, 1));
541 std::string
attrName(lua_tostring(state
, 2));
542 CObject
* value
= getObjectFromLua(state
, 3);
545 nlwarning("requestSetNode : bad type for argument 3");
548 CComLuaModule
* this2
= getInstance(state
);
550 this2
->_Client
->requestSetNode( instanceId
, attrName
, value
);
555 sint
CComLuaModule::luaRequestEraseNode(lua_State
* state
)
557 sint args
= lua_gettop(state
);
558 CHECK_LUA_ARG_COUNT_MAX(3, "requestEraseNode")
559 luaL_checktype(state
, 1, LUA_TSTRING
);
560 if (args
>1) { luaL_checktype(state
, 2, LUA_TSTRING
); }
561 if (args
>2) { luaL_checknumber(state
, 3); }
563 std::string
instanceId(lua_tostring(state
, 1));
564 std::string attrName
= "";
566 if (args
>1){ attrName
= lua_tostring(state
, 2);}
567 if (args
>2){ position
= static_cast<sint
>(lua_tonumber(state
, 3));}
568 CComLuaModule
* this2
= getInstance(state
);
570 this2
->_Client
->requestEraseNode( instanceId
, attrName
, position
);
576 sint
CComLuaModule::luaRequestMoveNode(lua_State
* state
)
578 sint args
= lua_gettop(state
);
579 CHECK_LUA_ARG_COUNT(6, "requestMoveNode");
580 luaL_checktype(state
, 1, LUA_TSTRING
);
581 luaL_checktype(state
, 2, LUA_TSTRING
);
582 luaL_checktype(state
, 3, LUA_TNUMBER
);
583 luaL_checktype(state
, 4, LUA_TSTRING
);
584 luaL_checktype(state
, 5, LUA_TSTRING
);
585 luaL_checktype(state
, 6, LUA_TNUMBER
);
588 luaL_checkany(state
, 3); //TODO just string, number and table
590 std::string
instanceId(lua_tostring(state
, 1));
591 std::string
attrName(lua_tostring(state
, 2));
592 sint position
= static_cast<sint
>(lua_tonumber(state
, 3));
594 std::string
instanceId2(lua_tostring(state
, 4));
595 std::string
attrName2(lua_tostring(state
, 5));
596 sint position2
= static_cast<sint
>(lua_tonumber(state
, 6));
598 CComLuaModule
* this2
= getInstance(state
);
600 this2
->_Client
->requestMoveNode( instanceId
, attrName
, position
, instanceId2
, attrName2
, position2
);
606 sint
CComLuaModule::luaRequestTpPosition(lua_State
* state
)
608 sint args
= lua_gettop(state
);
609 CHECK_LUA_ARG_COUNT(2, "requestTpPosition");
610 luaL_checktype(state
, 1, LUA_TNUMBER
);
611 luaL_checktype(state
, 2, LUA_TNUMBER
);
613 float x
= static_cast<float>(lua_tonumber(state
, 1));
614 float y
= static_cast<float>(lua_tonumber(state
, 2));
615 CComLuaModule
* this2
= getInstance(state
);
617 this2
->_Client
->getAnimationModule().requestTpPosition(x
, y
);
622 sint
CComLuaModule::luaRequestDespawnEntity(lua_State
* state
)
624 sint args
= lua_gettop(state
);
625 CHECK_LUA_ARG_COUNT(2, "requestDespawnEntity");
626 luaL_checktype(state
, 1, LUA_TSTRING
);
628 std::string npcId
= lua_tostring(state
, 1);
629 CComLuaModule
* this2
= getInstance(state
);
631 this2
->_Client
->getAnimationModule().requestDespawnEntity(npcId
);
636 sint
CComLuaModule::luaRequestDespawnGrp(lua_State
* state
)
638 sint args
= lua_gettop(state
);
639 CHECK_LUA_ARG_COUNT(2, "requestDespawnGrp");
640 luaL_checktype(state
, 1, LUA_TSTRING
);
642 std::string grpId
= lua_tostring(state
, 1);
643 CComLuaModule
* this2
= getInstance(state
);
645 this2
->_Client
->getAnimationModule().requestDespawnEntity(grpId
);
649 void CComLuaModule::setObjectToLua(lua_State
* state
, CObject
* object
)
656 if ( object
->isNumber() )
658 lua_pushnumber(state
, object
->toNumber());
662 if (object
->isRefId())
664 int initialStackSize
= lua_gettop(state
);
666 lua_pushvalue(state
, LUA_GLOBALSINDEX
); // _G
668 lua_pushstring(state
, "r2"); // _G, "r2"
669 lua_gettable(state
, -2); // G, r2
670 lua_pushstring(state
, "RefIdMetatable"); // _G, r2, "RefIdMetatable"
671 lua_gettable(state
, -2); // _G, r2, RefIdMetatable
672 lua_insert(state
, -3); // RefIdMetatable, _G, r2
675 nlassert(lua_gettop(state
) == initialStackSize
+ 1);
676 lua_newtable(state
); // RefIdMetatable, {}
677 lua_pushstring(state
, "Value"); // RefIdMetatable, {}, "Value"
678 lua_pushstring(state
, object
->toString().c_str()); // RefIdMetatable, {}, "Value", Value
679 lua_settable(state
, -3); // RefIdMetatable, { "Value" = Value }
680 lua_insert(state
, -2); // { "Value" = Value }, RefIdMetatable
681 lua_setmetatable(state
, -2); // { "Value" = Value } + metatable
682 int finalStackSize
= lua_gettop(state
);
683 nlassert(finalStackSize
== initialStackSize
+ 1);
687 if ( object
->isString() )
689 lua_pushstring(state
, object
->toString().c_str());
693 if ( object
->isTable() )
697 uint32 last
= object
->getSize();
698 uint32 arraySize
= 0;
699 uint32 arrayIndex
= 0;
701 for ( ; first
!= last
; ++first
)
703 if (object
->getKey(first
).empty()) { ++arraySize
;}
706 std::vector
<std::string
> keys
;
707 for (first
=0 ; first
!= last
; ++first
)
709 std::string key
= object
->getKey(first
);
710 CObject
*value
= object
->getValue(first
);
713 lua_pushstring(state
, key
.c_str());
714 setObjectToLua(state
, value
);
715 lua_settable(state
, -3);
721 setObjectToLua(state
, value
);
722 lua_rawseti(state
, -2, arrayIndex
);
731 // if (!keys.empty())
733 lua_pushstring(state
, "Keys");
735 for (; first
!= last
; ++first
)
737 lua_pushstring(state
, keys
[first
].c_str());
738 lua_rawseti(state
, -2, first
+1);
740 luaL_setn(state
, -1, last
);
741 lua_settable(state
, -3);
744 luaL_setn(state
, -1, arraySize
);
751 nlwarning("error! object is not a string, not a number, not a table!");
757 CObject
* CComLuaModule::getObjectFromLua(lua_State
* state
, sint idx
)
759 lua_pushvalue(state
, idx
);
761 // special case for RefID
762 if (lua_type(state
, -1) == LUA_TTABLE
)
764 if (lua_getmetatable(state
, -1))
767 lua_pushvalue(state
, LUA_GLOBALSINDEX
); // obj, mt, _G
769 lua_pushstring(state
, "r2"); // obj, mt, _G, "r2"
771 lua_gettable(state
, -2); // obj, mt, _G, r2
773 lua_pushstring(state
, "RefIdMetatable"); // obj, mt, _G, r2, "RefIdMetatable"
775 lua_gettable(state
, -2); // obj, mt, _G, r2, RefIdMetatable
776 bool equal
= lua_rawequal(state
, -1, -4) != 0;
780 lua_pop(state
, 4); // obj
782 lua_pushstring(state
, "Value"); // obj, "Value"
784 lua_gettable(state
, -2); // obj, value
786 if ( lua_isstring (state
, -1) )
787 { const char* str
= lua_tostring(state
, -1);
790 // result = new CObjectRefIdClient(str);
794 nlwarning("RefId error invalid string");
801 nlwarning("RefId not a string");
813 lua_pop(state
, 4); // obj
818 switch (lua_type(state
, -1))
822 double value
= lua_tonumber(state
, -1);
824 return new CObjectNumber(value
);
830 std::string value
= lua_tostring(state
, -1);
832 return new CObjectString(value
);
838 CObjectTable
* table
= new CObjectTable(); //Client();
841 while (lua_next(state
, -2) != 0)
843 std::string key
= "";
844 if ( lua_type(state
, -2) == LUA_TSTRING
)
846 key
= lua_tostring(state
, -2);
848 CObject
* object
= getObjectFromLua(state
, -1);
851 table
->add(key
, object
);
862 // other types such as functions are ignored
870 CObject
* CComLuaModule::loadLocal(const std::string
& filename
)
872 CObject
* object
= NULL
;
873 if (lua_dofile(_LuaState
, filename
.c_str()) == 0)
875 lua_getglobal(_LuaState
, "scenario");
877 if (lua_type(_LuaState
, -1) == LUA_TTABLE
)
879 object
= getObjectFromLua(_LuaState
);
884 nlwarning("Error while loading %s", filename
.c_str());
890 bool CComLuaModule::load(const std::string
& filename
)
893 CObject
* object
= loadLocal(filename
);
896 nlwarning("Error while loading %s", filename
.c_str());
899 _Client
->requestUploadScenario(object
);
907 sint
CComLuaModule::luaUpdateScenario(lua_State
* state
)
909 luaL_checktype(state
, 1, LUA_TTABLE
);
911 // lua_pushvalue(state, 1);
912 CObject
* object
= getObjectFromLua(state
);
913 lua_pushliteral(state
, "tmp1");
914 lua_pushliteral(state
, "tmp1");
915 lua_pushliteral(state
, "tmp1");
917 lua_getglobal(state
, "write_table");
918 setObjectToLua(state
, object
);
919 lua_call(state
, 1, 0);
921 // lua_pop(state, 1);
924 std::cout
<< "updateScenario" << std::endl
;
925 CComLuaModule
* this2
= getInstance(state
);
926 nlassert(this2
->_LuaState
== state
);
931 CComLuaModule::~CComLuaModule()
935 lua_close(_LuaState
);
941 sint
CComLuaModule::luaLoad(lua_State
* state
)
943 luaL_checktype(state
, 1, LUA_TSTRING
);
944 CComLuaModule
* this2
= getInstance(state
);
946 std::string
filename( lua_tostring(state
, 1) );
947 bool ok
= this2
->_Client
->load(filename
);
954 lua_pushnumber(state
, 1);
959 sint
CComLuaModule::luaSave(lua_State
* state
)
961 luaL_checktype(state
, 1, LUA_TSTRING
);
962 CComLuaModule
* this2
= getInstance(state
);
964 std::string
filename( lua_tostring(state
, 1) );
965 this2
->_Client
->save(filename
);
969 sint
CComLuaModule::luaTalkAs(lua_State
* state
)
971 luaL_checktype(state
, 1, LUA_TSTRING
);
972 CComLuaModule
* this2
= getInstance(state
);
974 std::string
npcname( lua_tostring(state
, 1) );
975 nlinfo(("luaTalkAs:: "+npcname
).c_str());
976 this2
->_Client
->getAnimationModule().requestTalkAs(npcname
);
981 sint
CComLuaModule::luaStopTalkAs(lua_State
* state
)
983 CComLuaModule
* this2
= getInstance(state
);
985 this2
->_Client
->getAnimationModule().requestStopTalkAs();
989 sint
CComLuaModule::luaRequestStringTable(lua_State
* state
)
991 CComLuaModule
* this2
= getInstance(state
);
993 this2
->_Client
->getAnimationModule().requestStringTable();
997 sint
CComLuaModule::luaRequestSetStringValue(lua_State
* state
)
999 luaL_checktype(state
, 1, LUA_TSTRING
);
1000 luaL_checktype(state
, 2, LUA_TSTRING
);
1001 CComLuaModule
* this2
= getInstance(state
);
1003 std::string
localId( lua_tostring(state
, 1) );
1004 std::string
value(lua_tostring(state
,2));
1005 this2
->_Client
->getAnimationModule().requestSetStringValue(localId
,value
);
1009 sint
CComLuaModule::luaRequestStringValue(lua_State
* state
)
1011 luaL_checktype(state
, 1, LUA_TSTRING
);
1012 CComLuaModule
* this2
= getInstance(state
);
1014 std::string
localId( lua_tostring(state
, 1) );
1015 this2
->_Client
->getAnimationModule().requestStringValue(localId
);
1019 sint
CComLuaModule::luaRequestIdList(lua_State
* state
)
1021 CComLuaModule
* this2
= getInstance(state
);
1023 this2
->_Client
->getAnimationModule().requestIdList();
1029 sint
CComLuaModule::luaGetScenarioObj(lua_State
* state
)
1031 CComLuaModule
* this2
= getInstance(state
);
1033 CObject
* obj
= this2
->_Client
->getCurrentScenario()->getHighLevel();
1034 setObjectToLua(state
,obj
);
1038 sint
CComLuaModule::luaGetNamespace(lua_State
* state
)
1041 CComLuaModule
* this2
= getInstance(state
);
1043 TSessionId sessionId
= this2
->_Client
->getEditionModule().getCurrentAdventureId();
1044 std::string value
= NLMISC::toString("r2_%04d_",sessionId
);
1045 lua_pushstring(state
, value
.c_str());
1050 sint
CComLuaModule::luaGetIslandsLocation(lua_State
* state
)
1053 std::vector
<std::string
> locations
;
1054 CComLuaModule
* this2
= getInstance(state
);
1056 // AJM: this2->_Client->getAdminModule().getIslandsLocation(locations);
1060 uint
first(0), last( locations
.size() ) ;
1063 // lua_pushstring(state, "Keys");
1064 lua_newtable(state
);
1065 for (; first
!= last
; ++first
)
1067 lua_pushnumber(state
, first
+1);
1068 lua_pushstring(state
, locations
[first
].c_str());
1069 lua_settable(state
, -3);