1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

vcmi: modernize lua

This commit is contained in:
Konstantin 2023-02-05 19:24:34 +03:00
parent fb38050f9b
commit 976c5e7bd4
11 changed files with 43 additions and 35 deletions

View File

@ -12,6 +12,7 @@
#include "api/Registry.h"
#include "LuaStack.h"
#include <type_traits>
VCMI_LIB_NAMESPACE_BEGIN
@ -187,6 +188,7 @@ public:
template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)const>
class LuaMethodWrapper <U, R(T:: *)(P1)const, method>
{
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
public:
static int invoke(lua_State * L)
{
@ -196,7 +198,7 @@ public:
if(!S.tryGet(1,obj))
return S.retVoid();
P1 p1;
PM1 p1;
if(!S.tryGet(2, p1))
return S.retVoid();
@ -210,6 +212,7 @@ public:
template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)>
class LuaMethodWrapper <U, R(T:: *)(P1), method>
{
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
public:
static int invoke(lua_State * L)
{
@ -219,7 +222,7 @@ public:
if(!S.tryGet(1,obj))
return S.retVoid();
P1 p1;
PM1 p1;
if(!S.tryGet(2, p1))
return S.retVoid();
@ -233,6 +236,7 @@ public:
template <typename U, typename T, typename P1, void(T:: * method)(P1)const>
class LuaMethodWrapper <U, void(T:: *)(P1)const, method>
{
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
public:
static int invoke(lua_State * L)
{
@ -242,7 +246,7 @@ public:
if(!S.tryGet(1,obj))
return S.retVoid();
P1 p1;
PM1 p1;
if(!S.tryGet(2, p1))
return S.retVoid();
@ -256,6 +260,7 @@ public:
template <typename U, typename T, typename P1, void(T:: * method)(P1)>
class LuaMethodWrapper <U, void(T:: *)(P1), method>
{
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
public:
static int invoke(lua_State * L)
{
@ -265,7 +270,7 @@ public:
if(!S.tryGet(1,obj))
return S.retVoid();
P1 p1;
PM1 p1;
if(!S.tryGet(2, p1))
return S.retVoid();
@ -279,6 +284,8 @@ public:
template <typename U, typename T, typename R, typename P1, typename P2, R(T:: * method)(P1, P2)const>
class LuaMethodWrapper <U, R(T:: *)(P1, P2)const, method>
{
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
using PM2 = std::remove_cv_t<std::remove_reference_t<P2>>;
public:
static int invoke(lua_State * L)
{
@ -288,11 +295,11 @@ public:
if(!S.tryGet(1, obj))
return S.retVoid();
P1 p1;
PM1 p1;
if(!S.tryGet(2, p1))
return S.retVoid();
P2 p2;
PM2 p2;
if(!S.tryGet(3, p2))
return S.retVoid();
@ -306,6 +313,8 @@ public:
template <typename U, typename T, typename P1, typename P2, void(T:: * method)(P1, P2)const>
class LuaMethodWrapper <U, void(T:: *)(P1, P2)const, method>
{
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
using PM2 = std::remove_cv_t<std::remove_reference_t<P2>>;
public:
static int invoke(lua_State * L)
{
@ -315,11 +324,11 @@ public:
if(!S.tryGet(1, obj))
return S.retVoid();
P1 p1;
PM1 p1;
if(!S.tryGet(2, p1))
return S.retVoid();
P2 p2;
PM2 p2;
if(!S.tryGet(3, p2))
return S.retVoid();

View File

@ -23,7 +23,7 @@ LuaReference::LuaReference(lua_State * L)
key = luaL_ref(l, LUA_REGISTRYINDEX);
}
LuaReference::LuaReference(LuaReference && other)
LuaReference::LuaReference(LuaReference && other) noexcept
: l(other.l),
key(other.key),
doCleanup(false)

View File

@ -21,7 +21,7 @@ public:
//pop from the top of stack
LuaReference(lua_State * L);
LuaReference(LuaReference && other);
LuaReference(LuaReference && other) noexcept;
~LuaReference();
void push();

View File

@ -34,13 +34,12 @@ namespace scripting
const std::string LuaContext::STATE_FIELD = "DATA";
LuaContext::LuaContext(const Script * source, const Environment * env_)
: ContextBase(env_->logger()),
LuaContext::LuaContext(const Script * source, const Environment * env_):
ContextBase(env_->logger()),
L(luaL_newstate()),
script(source),
env(env_)
{
L = luaL_newstate();
static const std::vector<luaL_Reg> STD_LIBS =
{
{"", luaopen_base},
@ -417,7 +416,7 @@ void LuaContext::popAll()
std::string LuaContext::toStringRaw(int index)
{
size_t len = 0;
auto raw = lua_tolstring(L, index, &len);
const auto *raw = lua_tolstring(L, index, &len);
return std::string(raw, len);
}
@ -431,7 +430,7 @@ void LuaContext::registerCore()
popAll();//just in case
for(auto & registar : api::Registry::get()->getCoreData())
for(const auto & registar : api::Registry::get()->getCoreData())
{
registar.second->pushMetatable(L); //table
@ -446,7 +445,7 @@ void LuaContext::registerCore()
int LuaContext::require(lua_State * L)
{
LuaContext * self = static_cast<LuaContext *>(lua_touserdata(L, lua_upvalueindex(1)));
auto * self = static_cast<LuaContext *>(lua_touserdata(L, lua_upvalueindex(1)));
if(!self)
{
@ -503,7 +502,7 @@ int LuaContext::loadModule()
if(scope.empty())
{
auto registar = api::Registry::get()->find(modulePath);
const auto *registar = api::Registry::get()->find(modulePath);
if(!registar)
{
@ -519,7 +518,7 @@ int LuaContext::loadModule()
boost::algorithm::replace_all(modulePath, ".", "/");
auto loader = CResourceHandler::get(CModHandler::scopeBuiltin());
auto *loader = CResourceHandler::get(CModHandler::scopeBuiltin());
modulePath = "scripts/lib/" + modulePath;
@ -582,7 +581,7 @@ int LuaContext::printImpl()
int LuaContext::logError(lua_State * L)
{
LuaContext * self = static_cast<LuaContext *>(lua_touserdata(L, lua_upvalueindex(1)));
auto * self = static_cast<LuaContext *>(lua_touserdata(L, lua_upvalueindex(1)));
if(!self)
{

View File

@ -94,7 +94,7 @@ bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m, const Ef
if(target.empty())
return false;
for(auto & dest : target)
for(const auto & dest : target)
{
JsonNode targetData;
targetData.Vector().push_back(JsonUtils::intNode(dest.hexValue.hex));
@ -137,7 +137,7 @@ void LuaSpellEffect::apply(ServerCallback * server, const Mechanics * m, const E
JsonNode requestP;
for(auto & dest : target)
for(const auto & dest : target)
{
JsonNode targetData;
targetData.Vector().push_back(JsonUtils::intNode(dest.hexValue.hex));
@ -176,7 +176,7 @@ std::shared_ptr<Context> LuaSpellEffect::resolveScript(const Mechanics * m) cons
return m->battle()->getContextPool()->getContext(script);
}
void LuaSpellEffect::setContextVariables(const Mechanics * m, std::shared_ptr<Context> context) const
void LuaSpellEffect::setContextVariables(const Mechanics * m, const std::shared_ptr<Context>& context)
{
context->setGlobal("effectLevel", m->getEffectLevel());
context->setGlobal("effectRangeLevel", m->getRangeLevel());

View File

@ -68,7 +68,7 @@ private:
std::shared_ptr<Context> resolveScript(const Mechanics * m) const;
void setContextVariables(const Mechanics * m, std::shared_ptr<Context> context) const;
static void setContextVariables(const Mechanics * m, const std::shared_ptr<Context>& context) ;
};
}

View File

@ -19,10 +19,10 @@ VCMI_LIB_NAMESPACE_BEGIN
namespace scripting
{
LuaStack::LuaStack(lua_State * L_)
: L(L_)
LuaStack::LuaStack(lua_State * L_):
L(L_),
initialTop(lua_gettop(L))
{
initialTop = lua_gettop(L);
}
void LuaStack::balance()
@ -94,7 +94,7 @@ void LuaStack::push(const JsonNode & value)
case JsonNode::JsonType::DATA_STRUCT:
{
lua_newtable(L);
for(auto & keyValue : value.Struct())
for(const auto & keyValue : value.Struct())
{
push(keyValue.first);
push(keyValue.second);
@ -154,7 +154,7 @@ bool LuaStack::tryGet(int position, std::string & value)
return false;
size_t len = 0;
auto raw = lua_tolstring(L, position, &len);
const auto *raw = lua_tolstring(L, position, &len);
value = std::string(raw, len);
return true;

View File

@ -159,7 +159,7 @@ static void publishMap(lua_State * L, const T & map)
for(auto & p : map)
{
const std::string & name = p.first;
int32_t id = static_cast<int32_t>(p.second);
auto id = static_cast<int32_t>(p.second);
lua_pushstring(L, name.c_str());
lua_pushinteger(L, id);

View File

@ -28,12 +28,12 @@ Registry * Registry::get()
void Registry::add(const std::string & name, std::shared_ptr<Registar> item)
{
data[name] = item;
data[name] = std::move(item);
}
void Registry::addCore(const std::string & name, std::shared_ptr<Registar> item)
{
coreData[name] = item;
coreData[name] = std::move(item);
}
const Registar * Registry::find(const std::string & name) const

View File

@ -72,7 +72,7 @@ int ServerCbProxy::commitPackage(lua_State * L)
return S.retVoid();
CPackForClient * pack = static_cast<CPackForClient *>(lua_touserdata(L, 1));
auto * pack = static_cast<CPackForClient *>(lua_touserdata(L, 1));
object->apply(pack);

View File

@ -53,7 +53,7 @@ int InfoWindowProxy::addReplacement(lua_State * L)
if(lua_isstring(L, 2))
{
size_t len = 0;
auto raw = lua_tolstring(L, 2, &len);
const auto *raw = lua_tolstring(L, 2, &len);
std::string text(raw, len);
object->text.addReplacement(text);
@ -87,7 +87,7 @@ int InfoWindowProxy::addText(lua_State * L)
if(lua_isstring(L, 2))
{
size_t len = 0;
auto raw = lua_tolstring(L, 2, &len);
const auto *raw = lua_tolstring(L, 2, &len);
std::string text(raw, len);
object->text << text;