Lua works; narrator works
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
#include <OgreFileSystemLayer.h>
|
||||
#include "GameData.h"
|
||||
#include "Components.h"
|
||||
#include "GUIModule.h"
|
||||
#include "LuaData.h"
|
||||
|
||||
extern "C" {
|
||||
int luaopen_lpeg(lua_State *L);
|
||||
}
|
||||
namespace ECS
|
||||
{
|
||||
|
||||
@@ -23,11 +28,100 @@ int LuaData::call_handler(const Ogre::String &event)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luaLibraryLoader(lua_State *L)
|
||||
{
|
||||
int i;
|
||||
if (!lua_isstring(L, 1)) {
|
||||
luaL_error(
|
||||
L,
|
||||
"luaLibraryLoader: Expected string for first parameter");
|
||||
}
|
||||
|
||||
std::string libraryFile = lua_tostring(L, 1);
|
||||
std::cout << libraryFile << std::endl;
|
||||
// In order to be compatible with the normal Lua file loader,
|
||||
// translate '.' to the file system seperator character.
|
||||
// In this case (An ogre resource) '/'
|
||||
while (libraryFile.find('.') != std::string::npos)
|
||||
libraryFile.replace(libraryFile.find('.'), 1, "/");
|
||||
|
||||
libraryFile += ".lua";
|
||||
std::cout << libraryFile << std::endl;
|
||||
Ogre::StringVectorPtr scripts =
|
||||
Ogre::ResourceGroupManager::getSingleton().listResourceNames(
|
||||
"LuaScripts", false);
|
||||
std::vector<Ogre::String> &strings = *scripts;
|
||||
for (i = 0; i < strings.size(); i++)
|
||||
std::cout << strings[i] << std::endl;
|
||||
|
||||
if (0 && !Ogre::ResourceGroupManager::getSingleton()
|
||||
.resourceExistsInAnyGroup(libraryFile)) {
|
||||
// Could not find the file.
|
||||
std::string errMessage = "\n no file '" + libraryFile +
|
||||
"' found in Ogre resource archives.";
|
||||
lua_pushstring(L, errMessage.c_str());
|
||||
} else {
|
||||
Ogre::DataStreamList streams =
|
||||
Ogre::ResourceGroupManager::getSingleton().openResources(
|
||||
"*.lua", "LuaScripts");
|
||||
Ogre::DataStreamPtr stream =
|
||||
Ogre::ResourceGroupManager::getSingleton().openResource(
|
||||
libraryFile, "LuaScripts");
|
||||
Ogre::String script = stream->getAsString();
|
||||
if (luaL_loadbuffer(L, script.c_str(), script.length(),
|
||||
libraryFile.c_str())) {
|
||||
luaL_error(
|
||||
L,
|
||||
"Error loading library '%s' from resource archive.\n%s",
|
||||
libraryFile.c_str(), lua_tostring(L, -1));
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void installLibraryLoader(lua_State *L)
|
||||
{
|
||||
// Insert the c++ func 'luaLibraryLoader' into package.loaders.
|
||||
// Inserted at the start of the table in order to take precedence.
|
||||
lua_getglobal(L, "table");
|
||||
lua_getfield(L, -1, "insert");
|
||||
lua_remove(L, -2); // table
|
||||
lua_getglobal(L, "package");
|
||||
lua_getfield(L, -1, "searchers");
|
||||
lua_remove(L, -2); // package
|
||||
lua_pushnumber(L, 1); // index where to insert into loaders table
|
||||
lua_pushcfunction(L, luaLibraryLoader);
|
||||
if (lua_pcall(L, 3, 0, 0))
|
||||
Ogre::LogManager::getSingleton().stream() << lua_tostring(L, 1);
|
||||
}
|
||||
|
||||
LuaData::LuaData()
|
||||
: L(luaL_newstate())
|
||||
{
|
||||
luaopen_base(L);
|
||||
luaopen_table(L);
|
||||
luaopen_package(L);
|
||||
luaL_requiref(L, "table", luaopen_table, 1);
|
||||
lua_pop(L, 1);
|
||||
luaL_requiref(L, "math", luaopen_math, 1);
|
||||
lua_pop(L, 1);
|
||||
luaL_requiref(L, "package", luaopen_package, 1);
|
||||
lua_pop(L, 1);
|
||||
luaL_requiref(L, "string", luaopen_string, 1);
|
||||
lua_pop(L, 1);
|
||||
luaL_requiref(L, "io", luaopen_io, 1);
|
||||
lua_pop(L, 1);
|
||||
luaL_requiref(L, "lpeg", luaopen_lpeg, 1);
|
||||
lua_pop(L, 1);
|
||||
#if 0
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||
lua_pushcfunction(L, luaopen_lpeg);
|
||||
lua_setfield(L, -2, "lpeg");
|
||||
lua_pop(L, 1); // remove PRELOAD table
|
||||
#endif
|
||||
installLibraryLoader(L);
|
||||
lua_pop(L, 1);
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
OgreAssert(false, "Crash function called");
|
||||
return 0;
|
||||
@@ -40,9 +134,24 @@ LuaData::LuaData()
|
||||
});
|
||||
lua_setglobal(L, "setup_handler");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
int args = lua_gettop(L);
|
||||
if (args < 1)
|
||||
return 0;
|
||||
ECS::get_mut<GUI>().choices.clear();
|
||||
luaL_checktype(L, 1, LUA_TSTRING);
|
||||
if (args > 1) {
|
||||
luaL_checktype(L, 2, LUA_TTABLE);
|
||||
lua_pushnil(L); /* first key */
|
||||
while (lua_next(L, 2) != 0) {
|
||||
Ogre::String s = lua_tostring(L, -1);
|
||||
ECS::get_mut<GUI>().choices.push_back(s);
|
||||
lua_pop(L, 1); /* remove value but keep key */
|
||||
}
|
||||
}
|
||||
size_t len;
|
||||
Ogre::String message(luaL_tolstring(L, 1, &len));
|
||||
std::cout << "narrator message: " << message
|
||||
<< " length: " << message.length() << std::endl;
|
||||
if (message.length() == 0 && ECS::get_mut<GUI>().narrationBox) {
|
||||
ECS::get_mut<GUI>().enabled = false;
|
||||
ECS::get_mut<GUI>().grab = true;
|
||||
@@ -50,12 +159,11 @@ LuaData::LuaData()
|
||||
ECS::get_mut<GUI>().narrationText = message;
|
||||
ECS::get_mut<GUI>().narrationBox = false;
|
||||
ECS::modified<GUI>();
|
||||
} else {
|
||||
std::cout << "narration ended\n";
|
||||
} else if (message.length() > 0) {
|
||||
std::replace(message.begin(), message.end(), '\n', ' ');
|
||||
std::replace(message.begin(), message.end(), '\r', ' ');
|
||||
|
||||
std::cout << "narrator message: " << message
|
||||
<< std::endl;
|
||||
ECS::get_mut<GUI>().enabled = true;
|
||||
ECS::get_mut<GUI>().grab = false;
|
||||
ECS::get_mut<GUI>().grabChanged = true;
|
||||
@@ -67,6 +175,12 @@ LuaData::LuaData()
|
||||
return 0;
|
||||
});
|
||||
lua_setglobal(L, "narrate");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
// ECS::get_mut<GUI>().mainMenu = true;
|
||||
lua_pushinteger(L, ECS::get<GUI>().narration_answer);
|
||||
return 1;
|
||||
});
|
||||
lua_setglobal(L, "narration_get_answer");
|
||||
lua_pushcfunction(L, [](lua_State *L) -> int {
|
||||
// ECS::get_mut<GUI>().mainMenu = true;
|
||||
ECS::get_mut<GUI>().enabled = true;
|
||||
|
||||
Reference in New Issue
Block a user