summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/dlua.cc
blob: 563ad40d8516ad126400381db0d8d8faf6ac2832 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
 * @file
 * @brief Dungeon-builder Lua interface.
**/

#include "AppHdr.h"

#include <sstream>

#include "dlua.h"
#include "l_libs.h"
#include "strings.h"
#include "tags.h"

static int dlua_compiled_chunk_writer(lua_State *ls, const void *p,
                                      size_t sz, void *ud)
{
    ostringstream &out = *static_cast<ostringstream*>(ud);
    out.write(static_cast<const char *>(p), sz);
    return 0;
}

///////////////////////////////////////////////////////////////////////////
// dlua_chunk

dlua_chunk::dlua_chunk(const string &_context)
    : file(), chunk(), compiled(), context(_context), first(-1),
      last(-1), error()
{
    clear();
}

// Initialises a chunk from the function on the top of stack.
// This function must not be a closure, i.e. must not have any upvalues.
dlua_chunk::dlua_chunk(lua_State *ls)
    : file(), chunk(), compiled(), context(), first(-1), last(-1), error()
{
    clear();

    lua_stack_cleaner cln(ls);
    ostringstream out;
    const int err = lua_dump(ls, dlua_compiled_chunk_writer, &out);
    if (err)
    {
        const char *e = lua_tostring(ls, -1);
        error = e? e : "Unknown error compiling chunk";
    }
    compiled = out.str();
}

dlua_chunk dlua_chunk::precompiled(const string &_chunk)
{
    dlua_chunk dchunk;
    dchunk.compiled = _chunk;
    return dchunk;
}

string dlua_chunk::describe(const string &name) const
{
    if (chunk.empty())
        return "";
    return make_stringf("function %s()\n%s\nend\n",
                        name.c_str(), chunk.c_str());
}

void dlua_chunk::write(writer& outf) const
{
    if (empty())
    {
        marshallByte(outf, CT_EMPTY);
        return;
    }

    if (!compiled.empty())
    {
        marshallByte(outf, CT_COMPILED);
        marshallString4(outf, compiled);
    }
    else
    {
        marshallByte(outf, CT_SOURCE);
        marshallString4(outf, chunk);
    }

    marshallString4(outf, file);
    marshallInt(outf, first);
}

void dlua_chunk::read(reader& inf)
{
    clear();
    chunk_t type = static_cast<chunk_t>(unmarshallByte(inf));
    switch (type)
    {
    case CT_EMPTY:
        return;
    case CT_SOURCE:
        unmarshallString4(inf, chunk);
        break;
    case CT_COMPILED:
        unmarshallString4(inf, compiled);
        break;
    }
    unmarshallString4(inf, file);
    first = unmarshallInt(inf);
}

void dlua_chunk::clear()
{
    file.clear();
    chunk.clear();
    first = last = -1;
    error.clear();
    compiled.clear();
}

void dlua_chunk::set_file(const string &s)
{
    file = s;
}

void dlua_chunk::add(int line, const string &s)
{
    if (first == -1)
        first = line;

    if (line != last && last != -1)
    {
        while (last++ < line)
            chunk += '\n';
    }

    chunk += " ";
    chunk += s;
    last = line;
}

void dlua_chunk::set_chunk(const string &s)
{
    chunk = s;
}

int dlua_chunk::check_op(CLua &interp, int err)
{
    error = interp.error;
    return err;
}

int dlua_chunk::load(CLua &interp)
{
    if (!compiled.empty())
    {
        return check_op(interp,
                         interp.loadbuffer(compiled.c_str(), compiled.length(),
                                           context.c_str()));
    }

    if (empty())
    {
        chunk.clear();
        return E_CHUNK_LOAD_FAILURE;
    }

    int err = check_op(interp,
                        interp.loadstring(chunk.c_str(), context.c_str()));
    if (err)
        return err;
    ostringstream out;
    err = lua_dump(interp, dlua_compiled_chunk_writer, &out);
    if (err)
    {
        const char *e = lua_tostring(interp, -1);
        error = e? e : "Unknown error compiling chunk";
        lua_pop(interp, 2);
    }
    compiled = out.str();
    return err;
}

int dlua_chunk::run(CLua &interp)
{
    int err = load(interp);
    if (err)
        return err;
    // callfn returns true on success, but we want to return 0 on success.
    return check_op(interp, !interp.callfn(NULL, 0, 0));
}

int dlua_chunk::load_call(CLua &interp, const char *fn)
{
    int err = load(interp);
    if (err == E_CHUNK_LOAD_FAILURE)
        return 0;
    if (err)
        return err;

    return check_op(interp, !interp.callfn(fn, fn? 1 : 0, 0));
}

string dlua_chunk::orig_error() const
{
    rewrite_chunk_errors(error);
    return error;
}

bool dlua_chunk::empty() const
{
    return compiled.empty() && trimmed_string(chunk).empty();
}

bool dlua_chunk::rewrite_chunk_errors(string &s) const
{
    const string contextm = "[string \"" + context + "\"]:";
    string::size_type dlwhere = s.find(contextm);

    if (dlwhere == string::npos)
        return false;

    if (!dlwhere)
    {
        s = rewrite_chunk_prefix(s);
        return true;
    }

    // Our chunk is mentioned, go back through and rewrite lines.
    vector<string> lines = split_string("\n", s);
    string newmsg = lines[0];
    bool wrote_prefix = false;
    for (int i = 2, size = lines.size() - 1; i < size; ++i)
    {
        const string &st = lines[i];
        if (st.find(context) != string::npos)
        {
            if (!wrote_prefix)
            {
                newmsg = get_chunk_prefix(st) + ": " + newmsg;
                wrote_prefix = true;
            }
            else
                newmsg += "\n" + rewrite_chunk_prefix(st);
        }
    }
    s = newmsg;
    return true;
}

string dlua_chunk::rewrite_chunk_prefix(const string &line, bool skip_body) const
{
    string s = line;
    const string contextm = "[string \"" + context + "\"]:";
    const string::size_type ps = s.find(contextm);
    if (ps == string::npos)
        return s;

    const string::size_type lns = ps + contextm.length();
    string::size_type pe = s.find(':', ps + contextm.length());
    if (pe != string::npos)
    {
        const string line_num = s.substr(lns, pe - lns);
        const int lnum = atoi(line_num.c_str());
        const string newlnum = make_stringf("%d", lnum + first - 1);
        s = s.substr(0, lns) + newlnum + s.substr(pe);
        pe = lns + newlnum.length();
    }

    return s.substr(0, ps) + (file.empty()? context : file) + ":"
        + (skip_body? s.substr(lns, pe - lns)
                    : s.substr(lns));
}

string dlua_chunk::get_chunk_prefix(const string &sorig) const
{
    return rewrite_chunk_prefix(sorig, true);
}

static void _dlua_register_constants(CLua &lua)
{
    lua_pushstring(lua, CORPSE_NEVER_DECAYS);
    lua.setglobal("CORPSE_NEVER_DECAYS");
}

void init_dungeon_lua()
{
    lua_stack_cleaner clean(dlua);

    dluaopen_colour(dlua);
    dluaopen_crawl(dlua);
    dluaopen_file(dlua);
    dluaopen_mapgrd(dlua);
    dluaopen_monsters(dlua);
    dluaopen_you(dlua);
    dluaopen_dgn(dlua);

    luaL_openlib(dlua, "feat", feat_dlib, 0);
    luaL_openlib(dlua, "spells", spells_dlib, 0);
    luaL_openlib(dlua, "debug", debug_dlib, 0);
    luaL_openlib(dlua, "los", los_dlib, 0);

    dlua.execfile("dlua/dungeon.lua", true, true);
    dlua.execfile("dlua/luamark.lua", true, true);
    dlua.execfile("dlua/mapinit.lua", true, true);

    lua_getglobal(dlua, "dgn_run_map");
    luaopen_debug(dlua);
    luaL_newmetatable(dlua, MAP_METATABLE);

    luaopen_dgnevent(dlua);
    luaopen_mapmarker(dlua);
    luaopen_ray(dlua);

    register_itemlist(dlua);
    register_monslist(dlua);

    _dlua_register_constants(dlua);
}