-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
lua_fileops.cpp
237 lines (217 loc) · 6.34 KB
/
lua_fileops.cpp
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
/*
Copyright (C) 2014 - 2016 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "lua_fileops.hpp"
#include "exceptions.hpp"
#include "filesystem.hpp"
#include "game_config.hpp" //for game_config::debug_lua
#include "game_errors.hpp"
#include "log.hpp"
#include "scripting/lua_api.hpp" // for chat_message, luaW_pcall
#include "scripting/lua_common.hpp" // for chat_message, luaW_pcall
#include <exception>
#include <string>
#include <boost/scoped_ptr.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "lua/lauxlib.h"
#include "lua/lua.h"
#include "lua/luaconf.h" // for LUAL_BUFFERSIZE
static lg::log_domain log_scripting_lua("scripting/lua");
#define DBG_LUA LOG_STREAM(debug, log_scripting_lua)
#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
#define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
#define ERR_LUA LOG_STREAM(err, log_scripting_lua)
namespace lua_fileops {
/// resolves @a filename where @a currentdir is the current directory, note that @a currentdir
/// is no absolute directory
/// @returns true iff the filename was sucesful resolved.
static bool resolve_filename(std::string& filename, const std::string& currentdir)
{
if(filename.size() < 2) {
return false;
}
if(filename[0] == '.' && filename[1] == '/') {
filename = currentdir + filename.substr(1);
}
if(std::find(filename.begin(), filename.end(), '\\') != filename.end()) {
return false;
}
//resolve /./
while(true) {
size_t pos = filename.find("/./");
if(pos == std::string::npos) {
break;
}
filename = filename.replace(pos, 2, "");
}
//resolve //
while(true) {
size_t pos = filename.find("//");
if(pos == std::string::npos) {
break;
}
filename = filename.replace(pos, 1, "");
}
//resolve /../
while(true) {
size_t pos = filename.find("/..");
if(pos == std::string::npos) {
break;
}
size_t pos2 = filename.find_last_of('/', pos - 1);
if(pos == std::string::npos || pos2 >= pos) {
return false;
}
filename = filename.replace(pos2, pos- pos2 + 3, "");
}
if(filename.find("..") != std::string::npos) {
return false;
}
return true;
}
/**
* Checks if a file exists (not necessarily a Lua script).
* - Arg 1: string containing the file name.
* - Ret 1: boolean
*/
int intf_have_file(lua_State *L)
{
char const *m = luaL_checkstring(L, 1);
std::string p = filesystem::get_wml_location(m);
if (p.empty()) { lua_pushboolean(L, false); }
else { lua_pushboolean(L, true); }
return 1;
}
/**
* Checks if a file exists (not necessarily a Lua script).
* - Arg 1: string containing the file name.
* - Ret 1: string
*/
int intf_read_file(lua_State *L)
{
std::string m = luaL_checkstring(L, 1);
std::string current_dir = "";
lua_Debug ar;
if(lua_getstack(L, 1, &ar)) {
lua_getinfo(L, "S", &ar);
if(ar.source[0] == '@') {
current_dir = filesystem::directory_name(std::string(ar.source + 1));
}
}
if(!resolve_filename(m, current_dir)) {
return luaL_argerror(L, -1, "file not found");
}
std::string p = filesystem::get_wml_location(m);
if(p.empty()) {
return luaL_argerror(L, -1, "file not found");
}
boost::scoped_ptr<std::istream> fs(filesystem::istream_file(p));
fs->exceptions(std::ios_base::goodbit);
size_t size = 0;
fs->seekg(0, std::ios::end);
if(!fs->good()) {
return luaL_error(L, "Error when reading file");
}
size = fs->tellg();
fs->seekg(0, std::ios::beg);
if(!fs->good()) {
return luaL_error(L, "Error when reading file");
}
luaL_Buffer b;
luaL_buffinit(L, &b);
//throws an exception if malloc failed.
char* out = luaL_prepbuffsize(&b, size);
fs->read(out, size);
if(!fs->good()) {
luaL_addsize(&b, size);
}
luaL_pushresult(&b);
return 1;
}
class lua_filestream
{
public:
lua_filestream(const std::string& fname)
: pistream_(filesystem::istream_file(fname))
{
}
static const char * lua_read_data(lua_State * /*L*/, void *data, size_t *size)
{
lua_filestream* lfs = static_cast<lua_filestream*>(data);
//int startpos = lfs->pistream_->tellg();
lfs->pistream_->read(lfs->buff_, LUAL_BUFFERSIZE);
//int newpos = lfs->pistream_->tellg();
*size = lfs->pistream_->gcount();
#if 0
ERR_LUA << "read bytes from " << startpos << " to " << newpos << " in total " *size << " from steam\n";
ERR_LUA << "streamstate beeing "
<< " goodbit:" << lfs->pistream_->good()
<< " endoffile:" << lfs->pistream_->eof()
<< " badbit:" << lfs->pistream_->bad()
<< " failbit:" << lfs->pistream_->fail() << "\n";
#endif
return lfs->buff_;
}
static int lua_loadfile(lua_State *L, const std::string& fname, const std::string& relativename)
{
lua_filestream lfs(fname);
//lua uses '@' to know that this is a file (as opposed to something loaded via loadstring )
std::string chunkname = '@' + relativename;
LOG_LUA << "starting to read from " << fname << "\n";
return lua_load(L, &lua_filestream::lua_read_data, &lfs, chunkname.c_str(), NULL);
}
private:
char buff_[LUAL_BUFFERSIZE];
boost::scoped_ptr<std::istream> pistream_;
};
/**
* Loads a Lua file and pushes the contents on the stack.
* - Arg 1: string containing the file name.
* - Ret 1: the loaded contents of the file
*/
int load_file(lua_State *L)
{
std::string m = luaL_checkstring(L, -1);
std::string current_dir = "";
lua_Debug ar;
if(lua_getstack(L, 1, &ar)) {
lua_getinfo(L, "S", &ar);
if(ar.source[0] == '@') {
current_dir = filesystem::directory_name(std::string(ar.source + 1));
}
}
if(!resolve_filename(m, current_dir)) {
return luaL_argerror(L, -1, "file not found");
}
std::string p = filesystem::get_wml_location(m);
if (p.empty()) {
return luaL_argerror(L, -1, "file not found");
}
#if 1
try
{
if(lua_filestream::lua_loadfile(L, p, m)) {
return lua_error(L);
}
}
catch(const std::exception & ex)
{
luaL_argerror(L, -1, ex.what());
}
#else
//oldcode to be deleted if newcode works
if (luaL_loadfile(L, p.c_str()))
return lua_error(L);
#endif
lua_remove(L, -2); //remove the filename from the stack
return 1;
}
} // end namespace lua_fileops