99
// TODO: this exact code is also in map loading
100
static bool m_filename_to_short(const std::string & s) {
103
static bool m_is_lua_file(const std::string & s) {
104
std::string ext = s.substr(s.size() - 4, s.size());
105
// std::transform fails on older system, therefore we use an explicit loop
106
for (uint32_t i = 0; i < ext.size(); i++)
107
ext[i] = std::tolower(ext[i]);
108
return (ext == ".lua");
110
void LuaInterface_Impl::m_register_core_scripts
111
(Widelands::Editor_Game_Base * egbase)
113
filenameset_t scripting_files;
115
// Theoretically, we should be able to use fs.FindFiles(*.lua) here,
116
// but since FindFiles doesn't support Globbing in Zips and most
117
// saved maps/games are zip, we have to work around this issue.
118
g_fs->FindFiles("scripting", "*", &scripting_files);
121
(filenameset_t::iterator i = scripting_files.begin();
122
i != scripting_files.end(); i++)
124
if (m_filename_to_short(*i) or not m_is_lua_file(*i))
128
std::string data(static_cast<char *>(g_fs->Load(*i, length)));
129
std::string name = i->substr(0, i->size() - 4); // strips '.lua'
130
size_t pos = name.rfind('/');
131
if (pos == std::string::npos)
132
pos = name.rfind("\\");
133
if (pos != std::string::npos)
134
name = name.substr(pos + 1, name.size());
136
log("Registering script: (core,%s)\n", name.c_str());
137
register_script("core", name, data);
139
// Immediately use() this script
140
// lua_getglobal(m_L, "use");
141
// lua_pushstring(m_L, "core");
142
// lua_pushstring(m_L, name.c_str());
143
// lua_call(m_L, 2, 0);
97
147
/*************************
98
148
* Public functions
99
149
*************************/
145
195
lua_pushstring(m_L, "game");
146
196
lua_pushlightuserdata(m_L, static_cast<void *>(egbase));
147
197
lua_settable(m_L, LUA_REGISTRYINDEX);
199
m_register_core_scripts(egbase);
150
202
LuaInterface_Impl::~LuaInterface_Impl() {