Skip to content

Commit ebbd158

Browse files
authoredMar 3, 2018
c_converter: Function template for numeric fields, add v3s16 default (#7090)
1 parent a1cf8a1 commit ebbd158

File tree

2 files changed

+39
-77
lines changed

2 files changed

+39
-77
lines changed
 

‎src/script/common/c_converter.cpp

+7-65
Original file line numberDiff line numberDiff line change
@@ -467,71 +467,6 @@ bool getstringfield(lua_State *L, int table,
467467
return got;
468468
}
469469

470-
bool getintfield(lua_State *L, int table,
471-
const char *fieldname, int &result)
472-
{
473-
lua_getfield(L, table, fieldname);
474-
bool got = false;
475-
if(lua_isnumber(L, -1)){
476-
result = lua_tointeger(L, -1);
477-
got = true;
478-
}
479-
lua_pop(L, 1);
480-
return got;
481-
}
482-
483-
bool getintfield(lua_State *L, int table,
484-
const char *fieldname, u8 &result)
485-
{
486-
lua_getfield(L, table, fieldname);
487-
bool got = false;
488-
if(lua_isnumber(L, -1)){
489-
result = lua_tointeger(L, -1);
490-
got = true;
491-
}
492-
lua_pop(L, 1);
493-
return got;
494-
}
495-
496-
bool getintfield(lua_State *L, int table,
497-
const char *fieldname, s8 &result)
498-
{
499-
lua_getfield(L, table, fieldname);
500-
bool got = false;
501-
if (lua_isnumber(L, -1)) {
502-
result = lua_tointeger(L, -1);
503-
got = true;
504-
}
505-
lua_pop(L, 1);
506-
return got;
507-
}
508-
509-
bool getintfield(lua_State *L, int table,
510-
const char *fieldname, u16 &result)
511-
{
512-
lua_getfield(L, table, fieldname);
513-
bool got = false;
514-
if(lua_isnumber(L, -1)){
515-
result = lua_tointeger(L, -1);
516-
got = true;
517-
}
518-
lua_pop(L, 1);
519-
return got;
520-
}
521-
522-
bool getintfield(lua_State *L, int table,
523-
const char *fieldname, u32 &result)
524-
{
525-
lua_getfield(L, table, fieldname);
526-
bool got = false;
527-
if(lua_isnumber(L, -1)){
528-
result = lua_tointeger(L, -1);
529-
got = true;
530-
}
531-
lua_pop(L, 1);
532-
return got;
533-
}
534-
535470
bool getfloatfield(lua_State *L, int table,
536471
const char *fieldname, float &result)
537472
{
@@ -612,6 +547,13 @@ bool getboolfield_default(lua_State *L, int table,
612547
return result;
613548
}
614549

550+
v3s16 getv3s16field_default(lua_State *L, int table,
551+
const char *fieldname, v3s16 default_)
552+
{
553+
getv3intfield(L, table, fieldname, default_);
554+
return default_;
555+
}
556+
615557
void setstringfield(lua_State *L, int table,
616558
const char *fieldname, const char *value)
617559
{

‎src/script/common/c_converter.h

+32-12
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,51 @@ bool getboolfield_default(lua_State *L, int table,
4242
const char *fieldname, bool default_);
4343
float getfloatfield_default(lua_State *L, int table,
4444
const char *fieldname, float default_);
45-
int getintfield_default (lua_State *L, int table,
45+
int getintfield_default(lua_State *L, int table,
4646
const char *fieldname, int default_);
4747

48+
template<typename T>
49+
bool getintfield(lua_State *L, int table,
50+
const char *fieldname, T &result)
51+
{
52+
lua_getfield(L, table, fieldname);
53+
bool got = false;
54+
if (lua_isnumber(L, -1)){
55+
result = lua_tointeger(L, -1);
56+
got = true;
57+
}
58+
lua_pop(L, 1);
59+
return got;
60+
}
61+
62+
template<class T>
63+
bool getv3intfield(lua_State *L, int index,
64+
const char *fieldname, T &result)
65+
{
66+
lua_getfield(L, index, fieldname);
67+
bool got = false;
68+
if (lua_istable(L, -1)) {
69+
got = getintfield(L, index, "x", result.X) ||
70+
getintfield(L, index, "y", result.Y) ||
71+
getintfield(L, index, "z", result.Z);
72+
}
73+
lua_pop(L, 1);
74+
return got;
75+
}
76+
77+
v3s16 getv3s16field_default(lua_State *L, int table,
78+
const char *fieldname, v3s16 default_);
4879
bool getstringfield(lua_State *L, int table,
4980
const char *fieldname, std::string &result);
5081
size_t getstringlistfield(lua_State *L, int table,
5182
const char *fieldname,
5283
std::vector<std::string> *result);
53-
bool getintfield(lua_State *L, int table,
54-
const char *fieldname, int &result);
55-
bool getintfield(lua_State *L, int table,
56-
const char *fieldname, u8 &result);
57-
bool getintfield(lua_State *L, int table,
58-
const char *fieldname, s8 &result);
59-
bool getintfield(lua_State *L, int table,
60-
const char *fieldname, u16 &result);
61-
bool getintfield(lua_State *L, int table,
62-
const char *fieldname, u32 &result);
6384
void read_groups(lua_State *L, int index,
6485
std::unordered_map<std::string, int> &result);
6586
bool getboolfield(lua_State *L, int table,
6687
const char *fieldname, bool &result);
6788
bool getfloatfield(lua_State *L, int table,
6889
const char *fieldname, float &result);
69-
7090
std::string checkstringfield(lua_State *L, int table,
7191
const char *fieldname);
7292

0 commit comments

Comments
 (0)
Please sign in to comment.