~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to src/scripting/scripting.cc

  • Committer: Holger Rapp
  • Date: 2010-02-06 23:11:51 UTC
  • mto: (4917.6.11 win-conditions)
  • mto: This revision was merged to the branch mainline in revision 5208.
  • Revision ID: sirver@kallisto.local-20100206231151-vkg4t2vhh600pskx
First version of core scripting. Yet undecided about a few things

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <stdexcept>
22
22
 
23
23
#include "log.h"
 
24
#include "io/filesystem/layered_filesystem.h"
24
25
 
25
 
#include "persistence.h"
 
26
#include "c_utils.h"
 
27
#include "coroutine_impl.h"
26
28
#include "lua_debug.h"
27
29
#include "lua_game.h"
 
30
#include "lua_globals.h"
28
31
#include "lua_map.h"
29
 
#include "lua_globals.h"
30
 
#include "coroutine_impl.h"
31
 
#include "c_utils.h"
 
32
#include "persistence.h"
32
33
 
33
34
#include "scripting.h"
34
35
 
55
56
         */
56
57
        private:
57
58
                int m_check_for_errors(int);
 
59
                void m_register_core_scripts(Widelands::Editor_Game_Base *);
58
60
 
59
61
        public:
60
62
                LuaInterface_Impl(Widelands::Editor_Game_Base *);
94
96
        return rv;
95
97
}
96
98
 
 
99
// TODO: this exact code is also in map loading
 
100
static bool m_filename_to_short(const std::string & s) {
 
101
        return s.size() < 4;
 
102
}
 
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");
 
109
}
 
110
void LuaInterface_Impl::m_register_core_scripts
 
111
        (Widelands::Editor_Game_Base * egbase)
 
112
{
 
113
        filenameset_t scripting_files;
 
114
 
 
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);
 
119
 
 
120
        for
 
121
                (filenameset_t::iterator i = scripting_files.begin();
 
122
                 i != scripting_files.end(); i++)
 
123
        {
 
124
                if (m_filename_to_short(*i) or not m_is_lua_file(*i))
 
125
                        continue;
 
126
 
 
127
                size_t length;
 
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());
 
135
 
 
136
                log("Registering script: (core,%s)\n", name.c_str());
 
137
                register_script("core", name, data);
 
138
 
 
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);
 
144
        }
 
145
}
 
146
 
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);
 
198
 
 
199
        m_register_core_scripts(egbase);
148
200
}
149
201
 
150
202
LuaInterface_Impl::~LuaInterface_Impl() {