-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
lua_kernel_base.cpp
1063 lines (937 loc) · 33.3 KB
/
lua_kernel_base.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
Part of the Battle for Wesnoth Project https://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 "scripting/lua_kernel_base.hpp"
#include "game_config.hpp"
#include "game_errors.hpp"
#include "gui/core/gui_definition.hpp" // for remove_single_widget_definition
#include "log.hpp"
#include "lua_jailbreak_exception.hpp" // for lua_jailbreak_exception
#include "seed_rng.hpp"
#include "deprecation.hpp"
#include "language.hpp" // for get_language
#ifdef DEBUG_LUA
#include "scripting/debug_lua.hpp"
#endif
#include "scripting/lua_color.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/lua_cpp_function.hpp"
#include "scripting/lua_fileops.hpp"
#include "scripting/lua_formula_bridge.hpp"
#include "scripting/lua_gui2.hpp"
#include "scripting/lua_wml.hpp"
#include "scripting/lua_stringx.hpp"
#include "scripting/lua_map_location_ops.hpp"
#include "scripting/lua_mathx.hpp"
#include "scripting/lua_rng.hpp"
#include "scripting/lua_widget.hpp"
#include "scripting/push_check.hpp"
#include "game_version.hpp" // for do_version_check, etc
#include "picture.hpp"
#include <functional>
#include "utils/name_generator.hpp"
#include "utils/markov_generator.hpp"
#include "utils/context_free_grammar_generator.hpp"
#include <cstring>
#include <exception>
#include <new>
#include <string>
#include <sstream>
#include <vector>
#include "lua/lauxlib.h"
#include "lua/lua.h"
#include "lua/lualib.h"
static lg::log_domain log_scripting_lua("scripting/lua");
static lg::log_domain log_user("scripting/lua/user");
#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)
// Registry key for metatable
static const char * Gen = "name generator";
// Registry key for lua interpreter environment
static const char * Interp = "lua interpreter";
// Callback implementations
/**
* Compares 2 version strings - which is newer.
* - Args 1,3: version strings
* - Arg 2: comparison operator (string)
* - Ret 1: comparison result
*/
static int intf_compare_versions(lua_State* L)
{
char const *v1 = luaL_checkstring(L, 1);
const VERSION_COMP_OP vop = parse_version_op(luaL_checkstring(L, 2));
if(vop == OP_INVALID) return luaL_argerror(L, 2, "unknown version comparison operator - allowed are ==, !=, <, <=, > and >=");
char const *v2 = luaL_checkstring(L, 3);
const bool result = do_version_check(version_info(v1), vop, version_info(v2));
lua_pushboolean(L, result);
return 1;
}
/**
* Decomposes a version into its component parts
*/
static int intf_decompose_version(lua_State* L) {
version_info vers(luaL_checkstring(L, 1));
lua_createtable(L, vers.components().size(), vers.special_version_separator() ? 5 : 3);
lua_pushinteger(L, vers.major_version());
lua_setfield(L, -2, "major");
lua_pushinteger(L, vers.minor_version());
lua_setfield(L, -2, "minor");
lua_pushinteger(L, vers.revision_level());
lua_setfield(L, -2, "revision");
if(char sep = vers.special_version_separator()) {
lua_pushlstring(L, &sep, 1);
lua_setfield(L, -2, "sep");
std::string special = vers.special_version();
lua_pushlstring(L, special.c_str(), special.size());
lua_setfield(L, -2, "special");
}
int i = 0;
for(int n : vers.components()) {
lua_pushinteger(L, n);
lua_seti(L, -2, ++i);
}
return 1;
}
/**
* Builds a version string from its component parts
*/
static int intf_make_version(lua_State* L) {
int major = luaL_checkinteger(L, 1), minor = luaL_checkinteger(L, 2), rev = luaL_checkinteger(L, 3);
std::string sep, special;
if(!lua_isnoneornil(L, 4)) {
special = luaL_checkstring(L, 4);
if(!special.empty() && std::isalpha(special[0])) {
sep.push_back('+');
} else {
sep.push_back(special[0]);
special = special.substr(1);
}
} else {
sep.push_back(0);
}
version_info vers(major, minor, rev, sep[0], special);
lua_push(L, vers.str());
return 1;
}
/**
* Returns the current Wesnoth version
*/
static int intf_current_version(lua_State* L) {
lua_push(L, game_config::wesnoth_version.str());
return 1;
}
/**
* Replacement print function -- instead of printing to std::cout, print to the command log.
* Intended to be bound to this' command_log at registration time.
*/
int lua_kernel_base::intf_print(lua_State* L)
{
DBG_LUA << "intf_print called:\n";
std::size_t nargs = lua_gettop(L);
lua_getglobal(L, "tostring");
for (std::size_t i = 1; i <= nargs; ++i) {
lua_pushvalue(L, -1); // function to call: "tostring"
lua_pushvalue(L, i); // value to pass through tostring() before printing
lua_call(L, 1, 1);
const char * str = lua_tostring(L, -1);
if (!str) {
LOG_LUA << "'tostring' must return a value to 'print'\n";
str = "";
}
if (i > 1) {
cmd_log_ << "\t"; //separate multiple args with tab character
}
cmd_log_ << str;
DBG_LUA << "'" << str << "'\n";
lua_pop(L, 1); // Pop the output of tostrring()
}
lua_pop(L, 1); // Pop 'tostring' global
cmd_log_ << "\n";
DBG_LUA << "\n";
return 0;
}
static void impl_warn(void* p, const char* msg, int tocont) {
static const char*const prefix = "Warning:\n ";
static std::ostringstream warning(prefix);
warning.seekp(0, std::ios::end);
warning << msg << ' ';
if(!tocont) {
auto& lk = lua_kernel_base::get_lua_kernel<lua_kernel_base>(reinterpret_cast<lua_State*>(p));
lk.add_log_to_console(warning.str());
warning.str(prefix);
}
}
void lua_kernel_base::add_log_to_console(const std::string& msg) {
cmd_log_ << msg << "\n";
DBG_LUA << "'" << msg << "'\n";
}
/**
* Replacement load function. Mostly the same as regular load, but disallows loading binary chunks
* due to CVE-2018-1999023.
*/
static int intf_load(lua_State* L)
{
std::string chunk = luaL_checkstring(L, 1);
const char* name = luaL_optstring(L, 2, chunk.c_str());
std::string mode = luaL_optstring(L, 3, "t");
bool override_env = !lua_isnone(L, 4);
if(mode != "t") {
return luaL_argerror(L, 3, "binary chunks are not allowed for security reasons");
}
int result = luaL_loadbufferx(L, chunk.data(), chunk.length(), name, "t");
if(result != LUA_OK) {
lua_pushnil(L);
// Move the nil as the first return value, like Lua's own load() does.
lua_insert(L, -2);
return 2;
}
if(override_env) {
// Copy "env" to the top of the stack.
lua_pushvalue(L, 4);
// Set "env" as the first upvalue.
const char* upvalue_name = lua_setupvalue(L, -2, 1);
if(upvalue_name == nullptr) {
// lua_setupvalue() didn't remove the copy of "env" from the stack, so we need to do it ourselves.
lua_pop(L, 1);
}
}
return 1;
}
// The show lua console callback is similarly a method of lua kernel
int lua_kernel_base::intf_show_lua_console(lua_State *L)
{
if (cmd_log_.external_log_) {
std::string message = "There is already an external logger attached to this lua kernel, you cannot open the lua console right now.";
log_error(message.c_str());
cmd_log_ << message << "\n";
return 0;
}
return lua_gui2::show_lua_console(L, this);
}
static int impl_name_generator_call(lua_State *L)
{
name_generator* gen = static_cast<name_generator*>(lua_touserdata(L, 1));
lua_pushstring(L, gen->generate().c_str());
return 1;
}
static int impl_name_generator_collect(lua_State *L)
{
name_generator* gen = static_cast<name_generator*>(lua_touserdata(L, 1));
gen->~name_generator();
return 0;
}
static int intf_name_generator(lua_State *L)
{
std::string type = luaL_checkstring(L, 1);
name_generator* gen = nullptr;
try {
if(type == "markov" || type == "markov_chain") {
std::vector<std::string> input;
if(lua_istable(L, 2)) {
input = lua_check<std::vector<std::string>>(L, 2);
} else {
input = utils::parenthetical_split(luaL_checkstring(L, 2), ',');
}
int chain_sz = luaL_optinteger(L, 3, 2);
int max_len = luaL_optinteger(L, 4, 12);
gen = new(L) markov_generator(input, chain_sz, max_len);
// Ensure the pointer didn't change when cast
assert(static_cast<void*>(gen) == dynamic_cast<markov_generator*>(gen));
} else if(type == "context_free" || type == "cfg" || type == "CFG") {
if(lua_istable(L, 2)) {
std::map<std::string, std::vector<std::string>> data;
for(lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) {
if(lua_type(L, -2) != LUA_TSTRING) {
lua_pushstring(L, "CFG generator: invalid nonterminal name (must be a string)");
return lua_error(L);
}
if(lua_isstring(L, -1)) {
data[lua_tostring(L,-2)] = utils::split(lua_tostring(L,-1),'|');
} else if(lua_istable(L, -1)) {
data[lua_tostring(L,-2)] = lua_check<std::vector<std::string>>(L, -1);
} else {
lua_pushstring(L, "CFG generator: invalid noterminal value (must be a string or list of strings)");
return lua_error(L);
}
}
if(!data.empty()) {
gen = new(L) context_free_grammar_generator(data);
}
} else {
gen = new(L) context_free_grammar_generator(luaL_checkstring(L, 2));
}
if(gen) {
assert(static_cast<void*>(gen) == dynamic_cast<context_free_grammar_generator*>(gen));
}
} else {
return luaL_argerror(L, 1, "should be either 'markov_chain' or 'context_free'");
}
}
catch (const name_generator_invalid_exception& ex) {
lua_pushstring(L, ex.what());
return lua_error(L);
}
// We set the metatable now, even if the generator is invalid, so that it
// will be properly collected if it was invalid.
luaL_getmetatable(L, Gen);
lua_setmetatable(L, -2);
return 1;
}
/**
* Logs a message
* Arg 1: (optional) Logger
* Arg 2: Message
*/
static int intf_log(lua_State *L) {
const std::string& logger = lua_isstring(L, 2) ? luaL_checkstring(L, 1) : "";
std::string msg = lua_isstring(L, 2) ? luaL_checkstring(L, 2) : luaL_checkstring(L, 1);
if(msg.empty() || msg.back() != '\n') {
msg += '\n';
}
if(logger == "err" || logger == "error") {
LOG_STREAM(err, log_user) << msg;
} else if(logger == "warn" || logger == "wrn" || logger == "warning") {
LOG_STREAM(warn, log_user) << msg;
} else if((logger == "debug" || logger == "dbg")) {
LOG_STREAM(debug, log_user) << msg;
} else {
LOG_STREAM(info, log_user) << msg;
}
return 0;
}
/**
* Logs a deprecation message. See deprecation.cpp for details
* Arg 1: Element to be deprecated.
* Arg 2: Deprecation level.
* Arg 3: Version when element may be removed.
* Arg 4: Additional detail message.
*/
static int intf_deprecated_message(lua_State* L) {
const std::string elem = luaL_checkstring(L, 1);
// This could produce an invalid deprecation level, but that possibility is handled in deprecated_message()
const DEP_LEVEL level = DEP_LEVEL(luaL_checkinteger(L, 2));
const std::string ver_str = lua_isnoneornil(L, 3) ? "" : luaL_checkstring(L, 3);
const std::string detail = luaW_checktstring(L, 4);
const version_info ver = ver_str.empty() ? game_config::wesnoth_version.str() : ver_str;
const std::string msg = deprecated_message(elem, level, ver, detail);
if(level < DEP_LEVEL::INDEFINITE || level >= DEP_LEVEL::REMOVED) {
// Invalid deprecation level or level 4 deprecation should raise an interpreter error
lua_push(L, msg);
return lua_error(L);
}
lua_warning(L, msg.c_str(), false);
return 0;
}
/**
* Gets the dimension of an image.
* - Arg 1: string.
* - Ret 1: width.
* - Ret 2: height.
*/
static int intf_get_image_size(lua_State *L) {
char const *m = luaL_checkstring(L, 1);
image::locator img(m);
if(!img.file_exists()) return 0;
surface s = get_image(img);
lua_pushinteger(L, s->w);
lua_pushinteger(L, s->h);
return 2;
}
static int intf_get_language(lua_State* L)
{
lua_push(L, get_language().localename);
return 1;
}
// End Callback implementations
// Template which allows to push member functions to the lua kernel base into lua as C functions, using a shim
typedef int (lua_kernel_base::*member_callback)(lua_State *L);
template <member_callback method>
int dispatch(lua_State *L) {
return ((lua_kernel_base::get_lua_kernel<lua_kernel_base>(L)).*method)(L);
}
// Ctor, initialization
lua_kernel_base::lua_kernel_base()
: mState(luaL_newstate())
, cmd_log_()
{
get_lua_kernel_base_ptr(mState) = this;
lua_State *L = mState;
cmd_log_ << "Initializing " << my_name() << "...\n";
// Define the CPP_function metatable ( so we can override print to point to a C++ member function, add certain functions for this kernel, etc. )
// Do it first of all in case C++ functions are ever used in the core Wesnoth libs loaded in the next step
cmd_log_ << "Adding boost function proxy...\n";
lua_cpp::register_metatable(L);
// Open safe libraries.
// Debug and OS are not, but most of their functions will be disabled below.
cmd_log_ << "Adding standard libs...\n";
static const luaL_Reg safe_libs[] {
{ "", luaopen_base },
{ "table", luaopen_table },
{ "string", luaopen_string },
{ "math", luaopen_math },
{ "coroutine", luaopen_coroutine },
{ "debug", luaopen_debug },
{ "os", luaopen_os },
{ "utf8", luaopen_utf8 }, // added in Lua 5.3
// Wesnoth libraries
{ "stringx",lua_stringx::luaW_open },
{ "mathx", lua_mathx::luaW_open },
{ "wml", lua_wml::luaW_open },
{ "gui", lua_gui2::luaW_open },
{ nullptr, nullptr }
};
for (luaL_Reg const *lib = safe_libs; lib->func; ++lib)
{
luaL_requiref(L, lib->name, lib->func, true);
lua_pop(L, 1); /* remove lib */
}
// Disable functions from os which we don't want.
lua_getglobal(L, "os");
lua_pushnil(L);
while(lua_next(L, -2) != 0) {
lua_pop(L, 1);
char const* function = lua_tostring(L, -1);
if(strcmp(function, "clock") == 0 || strcmp(function, "date") == 0
|| strcmp(function, "time") == 0 || strcmp(function, "difftime") == 0) continue;
lua_pushnil(L);
lua_setfield(L, -3, function);
}
lua_pop(L, 1);
// Delete dofile and loadfile.
lua_pushnil(L);
lua_setglobal(L, "dofile");
lua_pushnil(L);
lua_setglobal(L, "loadfile");
// Store the error handler.
cmd_log_ << "Adding error handler...\n";
push_error_handler(L);
lua_settop(L, 0);
// Add some callback from the wesnoth lib
cmd_log_ << "Registering basic wesnoth API...\n";
static luaL_Reg const callbacks[] {
{ "deprecated_message", &intf_deprecated_message },
{ "have_file", &lua_fileops::intf_have_file },
{ "read_file", &lua_fileops::intf_read_file },
{ "canonical_path", &lua_fileops::intf_canonical_path },
{ "textdomain", &lua_common::intf_textdomain },
{ "dofile", &dispatch<&lua_kernel_base::intf_dofile> },
{ "require", &dispatch<&lua_kernel_base::intf_require> },
{ "kernel_type", &dispatch<&lua_kernel_base::intf_kernel_type> },
{ "compile_formula", &lua_formula_bridge::intf_compile_formula},
{ "eval_formula", &lua_formula_bridge::intf_eval_formula},
{ "name_generator", &intf_name_generator },
{ "log", &intf_log },
{ "get_image_size", &intf_get_image_size },
{ "get_language", &intf_get_language },
{ nullptr, nullptr }
};
lua_getglobal(L, "wesnoth");
if (!lua_istable(L,-1)) {
lua_newtable(L);
}
luaL_setfuncs(L, callbacks, 0);
//lua_cpp::set_functions(L, cpp_callbacks, 0);
lua_setglobal(L, "wesnoth");
// Create the gettext metatable.
cmd_log_ << lua_common::register_gettext_metatable(L);
// Create the tstring metatable.
cmd_log_ << lua_common::register_tstring_metatable(L);
lua_widget::register_metatable(L);
// Override the print function
cmd_log_ << "Redirecting print function...\n";
lua_getglobal(L, "print");
lua_setglobal(L, "std_print"); //storing original impl as 'std_print'
lua_settop(L, 0); //clear stack, just to be sure
lua_setwarnf(L, &::impl_warn, L);
lua_pushcfunction(L, &dispatch<&lua_kernel_base::intf_print>);
lua_setglobal(L, "print");
lua_pushcfunction(L, intf_load);
lua_setglobal(L, "load");
lua_pushnil(L);
lua_setglobal(L, "loadstring");
cmd_log_ << "Initializing package repository...\n";
// Create the package table.
lua_getglobal(L, "wesnoth");
lua_newtable(L);
lua_setfield(L, -2, "package");
lua_pop(L, 1);
lua_settop(L, 0);
lua_pushstring(L, "lua/package.lua");
int res = intf_require(L);
if(res != 1) {
cmd_log_ << "Error: Failed to initialize package repository. Falling back to less flexible C++ implementation.\n";
}
cmd_log_ << "Adding version table...\n";
static luaL_Reg const version_callbacks[] {
{ "compare", &intf_compare_versions },
{ "make", &intf_make_version },
{ "decompose", &intf_decompose_version },
{ "current", &intf_current_version },
{ nullptr, nullptr }
};
lua_newtable(L);
luaL_setfuncs(L, version_callbacks, 0);
lua_setglobal(L, "versions");
// Get some callbacks for map locations
cmd_log_ << "Adding map table...\n";
static luaL_Reg const map_callbacks[] {
{ "get_direction", &lua_map_location::intf_get_direction },
{ "vector_sum", &lua_map_location::intf_vector_sum },
{ "vector_diff", &lua_map_location::intf_vector_diff },
{ "vector_negation", &lua_map_location::intf_vector_negation },
{ "rotate_right_around_center", &lua_map_location::intf_rotate_right_around_center },
{ "tiles_adjacent", &lua_map_location::intf_tiles_adjacent },
{ "get_adjacent_tiles", &lua_map_location::intf_get_adjacent_tiles },
{ "distance_between", &lua_map_location::intf_distance_between },
{ "get_in_basis_N_NE", &lua_map_location::intf_get_in_basis_N_NE },
{ "get_relative_dir", &lua_map_location::intf_get_relative_dir },
{ nullptr, nullptr }
};
// Create the map_location table.
lua_getglobal(L, "wesnoth");
lua_newtable(L);
luaL_setfuncs(L, map_callbacks, 0);
lua_setfield(L, -2, "map");
lua_pop(L, 1);
// Create the game_config variable with its metatable.
cmd_log_ << "Adding game_config table...\n";
lua_getglobal(L, "wesnoth");
lua_newuserdatauv(L, 0, 0);
lua_createtable(L, 0, 3);
lua_pushcfunction(L, &dispatch<&lua_kernel_base::impl_game_config_get>);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, &dispatch<&lua_kernel_base::impl_game_config_set>);
lua_setfield(L, -2, "__newindex");
lua_pushstring(L, "game config");
lua_setfield(L, -2, "__metatable");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "game_config");
lua_pop(L, 1);
// Add mersenne twister rng wrapper
cmd_log_ << "Adding rng tables...\n";
lua_rng::load_tables(L);
cmd_log_ << "Adding name generator metatable...\n";
luaL_newmetatable(L, Gen);
static luaL_Reg const generator[] {
{ "__call", &impl_name_generator_call},
{ "__gc", &impl_name_generator_collect},
{ nullptr, nullptr}
};
luaL_setfuncs(L, generator, 0);
// Create formula bridge metatables
cmd_log_ << lua_formula_bridge::register_metatables(L);
cmd_log_ << lua_colors::register_metatables(L);
// Create the Lua interpreter table
cmd_log_ << "Sandboxing Lua interpreter...\nTo make variables visible outside the interpreter, assign to _G.variable.\n";
cmd_log_ << "The special variable _ holds the result of the last expression (if any).\n";
lua_newtable(L);
lua_createtable(L, 0, 1);
lua_getglobal(L, "_G");
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setfield(L, LUA_REGISTRYINDEX, Interp);
// Loading ilua:
cmd_log_ << "Loading ilua...\n";
lua_settop(L, 0);
luaW_getglobal(L, "wesnoth", "require");
lua_pushstring(L, "lua/ilua.lua");
if(protected_call(1, 1)) {
//run "ilua.set_strict()"
lua_pushstring(L, "set_strict");
lua_gettable(L, -2);
if (!this->protected_call(0,0, std::bind(&lua_kernel_base::log_error, this, std::placeholders::_1, std::placeholders::_2))) {
cmd_log_ << "Failed to activate strict mode.\n";
} else {
cmd_log_ << "Activated strict mode.\n";
}
lua_setglobal(L, "ilua"); //save ilua table as a global
} else {
cmd_log_ << "Error: failed to load ilua.\n";
}
lua_settop(L, 0);
// Disable functions from debug which we don't want.
// We do this last because ilua needs to be able to use debug.getmetatable
lua_getglobal(L, "debug");
lua_pushnil(L);
while(lua_next(L, -2) != 0) {
lua_pop(L, 1);
char const* function = lua_tostring(L, -1);
if(strcmp(function, "traceback") == 0 || strcmp(function, "getinfo") == 0) continue; //traceback is needed for our error handler
lua_pushnil(L); //getinfo is needed for ilua strict mode
lua_setfield(L, -3, function);
}
lua_pop(L, 1);
}
lua_kernel_base::~lua_kernel_base()
{
for (const auto& pair : this->registered_widget_definitions_) {
gui2::remove_single_widget_definition(std::get<0>(pair), std::get<1>(pair));
}
lua_close(mState);
}
void lua_kernel_base::log_error(char const * msg, char const * context)
{
ERR_LUA << context << ": " << msg << '\n';
}
void lua_kernel_base::throw_exception(char const * msg, char const * context)
{
throw game::lua_error(msg, context);
}
bool lua_kernel_base::protected_call(int nArgs, int nRets)
{
error_handler eh = std::bind(&lua_kernel_base::log_error, this, std::placeholders::_1, std::placeholders::_2 );
return this->protected_call(nArgs, nRets, eh);
}
bool lua_kernel_base::load_string(char const * prog, const std::string& name)
{
error_handler eh = std::bind(&lua_kernel_base::log_error, this, std::placeholders::_1, std::placeholders::_2 );
return this->load_string(prog, name, eh);
}
bool lua_kernel_base::protected_call(int nArgs, int nRets, error_handler e_h)
{
return this->protected_call(mState, nArgs, nRets, e_h);
}
bool lua_kernel_base::protected_call(lua_State * L, int nArgs, int nRets, error_handler e_h)
{
int errcode = luaW_pcall_internal(L, nArgs, nRets);
if (errcode != LUA_OK) {
char const * msg = lua_tostring(L, -1);
std::string context = "When executing, ";
if (errcode == LUA_ERRRUN) {
context += "Lua runtime error: ";
} else if (errcode == LUA_ERRERR) {
context += "Lua error in attached debugger: ";
} else if (errcode == LUA_ERRMEM) {
context += "Lua out of memory error: ";
} else {
context += "unknown lua error: ";
}
if(lua_isstring(L, -1)) {
context += msg ? msg : "null string";
} else {
context += lua_typename(L, lua_type(L, -1));
}
lua_pop(L, 1);
e_h(context.c_str(), "Lua Error");
return false;
}
return true;
}
bool lua_kernel_base::load_string(char const * prog, const std::string& name, error_handler e_h)
{
// pass 't' to prevent loading bytecode which is unsafe and can be used to escape the sandbox.
int errcode = luaL_loadbufferx(mState, prog, strlen(prog), name.empty() ? name.c_str() : prog, "t");
if (errcode != LUA_OK) {
char const * msg = lua_tostring(mState, -1);
std::string message = msg ? msg : "null string";
std::string context = "When parsing a string to lua, ";
if (errcode == LUA_ERRSYNTAX) {
context += " a syntax error";
} else if(errcode == LUA_ERRMEM){
context += " a memory error";
} else {
context += " an unknown error";
}
lua_pop(mState, 1);
e_h(message.c_str(), context.c_str());
return false;
}
return true;
}
void lua_kernel_base::run_lua_tag(const config& cfg)
{
int nArgs = 0;
if (const config& args = cfg.child("args")) {
luaW_pushconfig(this->mState, args);
++nArgs;
}
this->run(cfg["code"].str().c_str(), cfg["name"].str(), nArgs);
}
// Call load_string and protected call. Make them throw exceptions.
//
void lua_kernel_base::throwing_run(const char * prog, const std::string& name, int nArgs, bool in_interpreter)
{
cmd_log_ << "$ " << prog << "\n";
error_handler eh = std::bind(&lua_kernel_base::throw_exception, this, std::placeholders::_1, std::placeholders::_2 );
this->load_string(prog, name, eh);
if(in_interpreter) {
lua_getfield(mState, LUA_REGISTRYINDEX, Interp);
if(lua_setupvalue(mState, -2, 1) == nullptr)
lua_pop(mState, 1);
}
lua_insert(mState, -nArgs - 1);
this->protected_call(nArgs, in_interpreter ? LUA_MULTRET : 0, eh);
}
// Do a throwing run, but if we catch a lua_error, reformat it with signature for this function and log it.
void lua_kernel_base::run(const char * prog, const std::string& name, int nArgs)
{
try {
this->throwing_run(prog, name, nArgs);
} catch (const game::lua_error & e) {
cmd_log_ << e.what() << "\n";
lua_kernel_base::log_error(e.what(), "In function lua_kernel::run()");
}
}
// Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally. Throws exceptions.
void lua_kernel_base::interactive_run(char const * prog) {
std::string experiment = "return ";
experiment += prog;
int top = lua_gettop(mState);
error_handler eh = std::bind(&lua_kernel_base::throw_exception, this, std::placeholders::_1, std::placeholders::_2 );
luaW_getglobal(mState, "ilua", "_pretty_print");
try {
// Try to load the experiment without syntax errors
this->load_string(experiment.c_str(), "interactive", eh);
lua_getfield(mState, LUA_REGISTRYINDEX, Interp);
if(lua_setupvalue(mState, -2, 1) == nullptr)
lua_pop(mState, 1);
} catch (const game::lua_error &) {
this->throwing_run(prog, "interactive", 0, true); // Since it failed, fall back to the usual throwing_run, on the original input.
if(lua_gettop(mState) == top + 1) {
// Didn't return anything
lua_settop(mState, top);
return;
} else goto PRINT;
}
// experiment succeeded, now run but log normally.
cmd_log_ << "$ " << prog << "\n";
this->protected_call(0, LUA_MULTRET, eh);
PRINT:
int nRets = lua_gettop(mState) - top - 1;
{
// Assign first result to _
lua_getfield(mState, LUA_REGISTRYINDEX, Interp);
int env_idx = lua_gettop(mState);
lua_pushvalue(mState, top + 2);
lua_setfield(mState, -2, "_");
// Now duplicate EVERY result and pass it to table.pack, assigning to _all
luaW_getglobal(mState, "table", "pack");
for(int i = top + 2; i < env_idx; i++)
lua_pushvalue(mState, i);
this->protected_call(nRets, 1, eh);
lua_setfield(mState, -2, "_all");
lua_pop(mState, 1);
}
// stack is now ilua._pretty_print followed by any results of prog
this->protected_call(lua_gettop(mState) - top - 1, 0, eh);
}
/**
* Loads and executes a Lua file.
* - Arg 1: string containing the file name.
* - Ret *: values returned by executing the file body.
*/
int lua_kernel_base::intf_dofile(lua_State* L)
{
luaL_checkstring(L, 1);
lua_rotate(L, 1, -1);
if (lua_fileops::load_file(L) != 1) return 0;
//^ should end with the file contents loaded on the stack. actually it will call lua_error otherwise, the return 0 is redundant.
lua_rotate(L, 1, 1);
// Using a non-protected call here appears to fix an issue in plugins.
// The protected call isn't technically necessary anyway, because this function is called from Lua code,
// which should already be in a protected environment.
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
/**
* Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package.
* Stores the result of the script in wesnoth.package and returns it.
* - Arg 1: string containing the file name.
* - Ret 1: value returned by the script.
*/
int lua_kernel_base::intf_require(lua_State* L)
{
const char * m = luaL_checkstring(L, 1);
if(!m) {
return luaL_argerror(L, 1, "found a null string argument to wesnoth require");
}
// Check if there is already an entry.
lua_getglobal(L, "wesnoth");
lua_pushstring(L, "package");
lua_rawget(L, -2);
lua_pushvalue(L, 1);
lua_rawget(L, -2);
if(!lua_isnil(L, -1) && !game_config::debug_lua) {
return 1;
}
lua_pop(L, 1);
lua_pushvalue(L, 1);
// stack is now [packagename] [wesnoth] [package] [packagename]
if(lua_fileops::load_file(L) != 1) {
// should end with the file contents loaded on the stack. actually it will call lua_error otherwise, the return 0 is redundant.
// stack is now [packagename] [wesnoth] [package] [chunk]
return 0;
}
DBG_LUA << "require: loaded a file, now calling it\n";
if (!this->protected_call(L, 0, 1, std::bind(&lua_kernel_base::log_error, this, std::placeholders::_1, std::placeholders::_2))) {
// historically if wesnoth.require fails it just yields nil and some logging messages, not a lua error
return 0;
}
// stack is now [packagename] [wesnoth] [package] [results]
lua_pushvalue(L, 1);
lua_pushvalue(L, -2);
// stack is now [packagename] [wesnoth] [package] [results] [packagename] [results]
// Add the return value to the table.
lua_settable(L, -4);
// stack is now [packagename] [wesnoth] [package] [results]
return 1;
}
int lua_kernel_base::intf_kernel_type(lua_State* L)
{
lua_push(L, my_name());
return 1;
}
int lua_kernel_base::impl_game_config_get(lua_State* L)
{
char const *m = luaL_checkstring(L, 2);
return_int_attrib("base_income", game_config::base_income);
return_int_attrib("village_income", game_config::village_income);
return_int_attrib("village_support", game_config::village_support);
return_int_attrib("poison_amount", game_config::poison_amount);
return_int_attrib("rest_heal_amount", game_config::rest_heal_amount);
return_int_attrib("recall_cost", game_config::recall_cost);
return_int_attrib("kill_experience", game_config::kill_experience);
return_int_attrib("combat_experience", game_config::combat_experience);
return_string_attrib_deprecated("version", "wesnoth.game_config", INDEFINITE, "1.17", "Use version.current() instead", game_config::wesnoth_version.str());
return_bool_attrib("debug", game_config::debug);
return_bool_attrib("debug_lua", game_config::debug_lua);
return_bool_attrib("mp_debug", game_config::mp_debug);
return 0;
}
int lua_kernel_base::impl_game_config_set(lua_State* L)
{
std::string err_msg = "unknown modifiable property of game_config: ";
err_msg += luaL_checkstring(L, 2);
return luaL_argerror(L, 2, err_msg.c_str());
}
/**
* Loads the "package" package into the Lua environment.
* This action is inherently unsafe, as Lua scripts will now be able to
* load C libraries on their own, hence granting them the same privileges
* as the Wesnoth binary itself.
*/
void lua_kernel_base::load_package()
{
lua_State *L = mState;
lua_pushcfunction(L, luaopen_package);
lua_pushstring(L, "package");
lua_call(L, 1, 0);
}
void lua_kernel_base::load_core()
{
lua_State* L = mState;
lua_settop(L, 0);
cmd_log_ << "Loading core...\n";
luaW_getglobal(L, "wesnoth", "require");
lua_pushstring(L, "lua/core.lua");
if(!protected_call(1, 1)) {
cmd_log_ << "Error: Failed to load core.\n";
}
lua_settop(L, 0);
}
/**
* Gets all the global variable names in the Lua environment. This is useful for tab completion.
*/
std::vector<std::string> lua_kernel_base::get_global_var_names()
{
std::vector<std::string> ret;
lua_State *L = mState;
int idx = lua_gettop(L);
lua_getglobal(L, "_G");
lua_pushnil(L);
while (lua_next(L, idx+1) != 0) {
if (lua_isstring(L, -2)) {
ret.push_back(lua_tostring(L,-2));
}
lua_pop(L,1);
}
lua_settop(L, idx);
return ret;
}
/**
* Gets all attribute names of an extended variable name. This is useful for tab completion.
*/
std::vector<std::string> lua_kernel_base::get_attribute_names(const std::string & input)
{
std::vector<std::string> ret;
std::string base_path = input;
std::size_t last_dot = base_path.find_last_of('.');
std::string partial_name = base_path.substr(last_dot + 1);
base_path.erase(last_dot);
std::string load = "return " + base_path;
lua_State* L = mState;
int save_stack = lua_gettop(L);
int result = luaL_loadstring(L, load.c_str());
if(result != LUA_OK) {
// This isn't at error level because it's a really low priority error; it just means the user tried to tab-complete something that doesn't exist.
LOG_LUA << "Error when attempting tab completion:\n";