mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-25 22:42:04 +02:00
vcmi: modernize lua
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
#include "api/Registry.h"
|
#include "api/Registry.h"
|
||||||
#include "LuaStack.h"
|
#include "LuaStack.h"
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
|
|
||||||
@@ -187,6 +188,7 @@ public:
|
|||||||
template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)const>
|
template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)const>
|
||||||
class LuaMethodWrapper <U, R(T:: *)(P1)const, method>
|
class LuaMethodWrapper <U, R(T:: *)(P1)const, method>
|
||||||
{
|
{
|
||||||
|
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
|
||||||
public:
|
public:
|
||||||
static int invoke(lua_State * L)
|
static int invoke(lua_State * L)
|
||||||
{
|
{
|
||||||
@@ -196,7 +198,7 @@ public:
|
|||||||
if(!S.tryGet(1,obj))
|
if(!S.tryGet(1,obj))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P1 p1;
|
PM1 p1;
|
||||||
if(!S.tryGet(2, p1))
|
if(!S.tryGet(2, p1))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
@@ -210,6 +212,7 @@ public:
|
|||||||
template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)>
|
template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)>
|
||||||
class LuaMethodWrapper <U, R(T:: *)(P1), method>
|
class LuaMethodWrapper <U, R(T:: *)(P1), method>
|
||||||
{
|
{
|
||||||
|
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
|
||||||
public:
|
public:
|
||||||
static int invoke(lua_State * L)
|
static int invoke(lua_State * L)
|
||||||
{
|
{
|
||||||
@@ -219,7 +222,7 @@ public:
|
|||||||
if(!S.tryGet(1,obj))
|
if(!S.tryGet(1,obj))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P1 p1;
|
PM1 p1;
|
||||||
if(!S.tryGet(2, p1))
|
if(!S.tryGet(2, p1))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
@@ -233,6 +236,7 @@ public:
|
|||||||
template <typename U, typename T, typename P1, void(T:: * method)(P1)const>
|
template <typename U, typename T, typename P1, void(T:: * method)(P1)const>
|
||||||
class LuaMethodWrapper <U, void(T:: *)(P1)const, method>
|
class LuaMethodWrapper <U, void(T:: *)(P1)const, method>
|
||||||
{
|
{
|
||||||
|
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
|
||||||
public:
|
public:
|
||||||
static int invoke(lua_State * L)
|
static int invoke(lua_State * L)
|
||||||
{
|
{
|
||||||
@@ -242,7 +246,7 @@ public:
|
|||||||
if(!S.tryGet(1,obj))
|
if(!S.tryGet(1,obj))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P1 p1;
|
PM1 p1;
|
||||||
if(!S.tryGet(2, p1))
|
if(!S.tryGet(2, p1))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
@@ -256,6 +260,7 @@ public:
|
|||||||
template <typename U, typename T, typename P1, void(T:: * method)(P1)>
|
template <typename U, typename T, typename P1, void(T:: * method)(P1)>
|
||||||
class LuaMethodWrapper <U, void(T:: *)(P1), method>
|
class LuaMethodWrapper <U, void(T:: *)(P1), method>
|
||||||
{
|
{
|
||||||
|
using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
|
||||||
public:
|
public:
|
||||||
static int invoke(lua_State * L)
|
static int invoke(lua_State * L)
|
||||||
{
|
{
|
||||||
@@ -265,7 +270,7 @@ public:
|
|||||||
if(!S.tryGet(1,obj))
|
if(!S.tryGet(1,obj))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P1 p1;
|
PM1 p1;
|
||||||
if(!S.tryGet(2, p1))
|
if(!S.tryGet(2, p1))
|
||||||
return S.retVoid();
|
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>
|
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>
|
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:
|
public:
|
||||||
static int invoke(lua_State * L)
|
static int invoke(lua_State * L)
|
||||||
{
|
{
|
||||||
@@ -288,11 +295,11 @@ public:
|
|||||||
if(!S.tryGet(1, obj))
|
if(!S.tryGet(1, obj))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P1 p1;
|
PM1 p1;
|
||||||
if(!S.tryGet(2, p1))
|
if(!S.tryGet(2, p1))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P2 p2;
|
PM2 p2;
|
||||||
if(!S.tryGet(3, p2))
|
if(!S.tryGet(3, p2))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
@@ -306,6 +313,8 @@ public:
|
|||||||
template <typename U, typename T, typename P1, typename P2, void(T:: * method)(P1, P2)const>
|
template <typename U, typename T, typename P1, typename P2, void(T:: * method)(P1, P2)const>
|
||||||
class LuaMethodWrapper <U, void(T:: *)(P1, P2)const, method>
|
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:
|
public:
|
||||||
static int invoke(lua_State * L)
|
static int invoke(lua_State * L)
|
||||||
{
|
{
|
||||||
@@ -315,11 +324,11 @@ public:
|
|||||||
if(!S.tryGet(1, obj))
|
if(!S.tryGet(1, obj))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P1 p1;
|
PM1 p1;
|
||||||
if(!S.tryGet(2, p1))
|
if(!S.tryGet(2, p1))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
P2 p2;
|
PM2 p2;
|
||||||
if(!S.tryGet(3, p2))
|
if(!S.tryGet(3, p2))
|
||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ LuaReference::LuaReference(lua_State * L)
|
|||||||
key = luaL_ref(l, LUA_REGISTRYINDEX);
|
key = luaL_ref(l, LUA_REGISTRYINDEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaReference::LuaReference(LuaReference && other)
|
LuaReference::LuaReference(LuaReference && other) noexcept
|
||||||
: l(other.l),
|
: l(other.l),
|
||||||
key(other.key),
|
key(other.key),
|
||||||
doCleanup(false)
|
doCleanup(false)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public:
|
|||||||
//pop from the top of stack
|
//pop from the top of stack
|
||||||
LuaReference(lua_State * L);
|
LuaReference(lua_State * L);
|
||||||
|
|
||||||
LuaReference(LuaReference && other);
|
LuaReference(LuaReference && other) noexcept;
|
||||||
~LuaReference();
|
~LuaReference();
|
||||||
|
|
||||||
void push();
|
void push();
|
||||||
|
|||||||
@@ -34,13 +34,12 @@ namespace scripting
|
|||||||
|
|
||||||
const std::string LuaContext::STATE_FIELD = "DATA";
|
const std::string LuaContext::STATE_FIELD = "DATA";
|
||||||
|
|
||||||
LuaContext::LuaContext(const Script * source, const Environment * env_)
|
LuaContext::LuaContext(const Script * source, const Environment * env_):
|
||||||
: ContextBase(env_->logger()),
|
ContextBase(env_->logger()),
|
||||||
|
L(luaL_newstate()),
|
||||||
script(source),
|
script(source),
|
||||||
env(env_)
|
env(env_)
|
||||||
{
|
{
|
||||||
L = luaL_newstate();
|
|
||||||
|
|
||||||
static const std::vector<luaL_Reg> STD_LIBS =
|
static const std::vector<luaL_Reg> STD_LIBS =
|
||||||
{
|
{
|
||||||
{"", luaopen_base},
|
{"", luaopen_base},
|
||||||
@@ -417,7 +416,7 @@ void LuaContext::popAll()
|
|||||||
std::string LuaContext::toStringRaw(int index)
|
std::string LuaContext::toStringRaw(int index)
|
||||||
{
|
{
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
auto raw = lua_tolstring(L, index, &len);
|
const auto *raw = lua_tolstring(L, index, &len);
|
||||||
return std::string(raw, len);
|
return std::string(raw, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,7 +430,7 @@ void LuaContext::registerCore()
|
|||||||
|
|
||||||
popAll();//just in case
|
popAll();//just in case
|
||||||
|
|
||||||
for(auto & registar : api::Registry::get()->getCoreData())
|
for(const auto & registar : api::Registry::get()->getCoreData())
|
||||||
{
|
{
|
||||||
registar.second->pushMetatable(L); //table
|
registar.second->pushMetatable(L); //table
|
||||||
|
|
||||||
@@ -446,7 +445,7 @@ void LuaContext::registerCore()
|
|||||||
|
|
||||||
int LuaContext::require(lua_State * L)
|
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)
|
if(!self)
|
||||||
{
|
{
|
||||||
@@ -503,7 +502,7 @@ int LuaContext::loadModule()
|
|||||||
|
|
||||||
if(scope.empty())
|
if(scope.empty())
|
||||||
{
|
{
|
||||||
auto registar = api::Registry::get()->find(modulePath);
|
const auto *registar = api::Registry::get()->find(modulePath);
|
||||||
|
|
||||||
if(!registar)
|
if(!registar)
|
||||||
{
|
{
|
||||||
@@ -519,7 +518,7 @@ int LuaContext::loadModule()
|
|||||||
|
|
||||||
boost::algorithm::replace_all(modulePath, ".", "/");
|
boost::algorithm::replace_all(modulePath, ".", "/");
|
||||||
|
|
||||||
auto loader = CResourceHandler::get(CModHandler::scopeBuiltin());
|
auto *loader = CResourceHandler::get(CModHandler::scopeBuiltin());
|
||||||
|
|
||||||
modulePath = "scripts/lib/" + modulePath;
|
modulePath = "scripts/lib/" + modulePath;
|
||||||
|
|
||||||
@@ -582,7 +581,7 @@ int LuaContext::printImpl()
|
|||||||
|
|
||||||
int LuaContext::logError(lua_State * L)
|
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)
|
if(!self)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m, const Ef
|
|||||||
if(target.empty())
|
if(target.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for(auto & dest : target)
|
for(const auto & dest : target)
|
||||||
{
|
{
|
||||||
JsonNode targetData;
|
JsonNode targetData;
|
||||||
targetData.Vector().push_back(JsonUtils::intNode(dest.hexValue.hex));
|
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;
|
JsonNode requestP;
|
||||||
|
|
||||||
for(auto & dest : target)
|
for(const auto & dest : target)
|
||||||
{
|
{
|
||||||
JsonNode targetData;
|
JsonNode targetData;
|
||||||
targetData.Vector().push_back(JsonUtils::intNode(dest.hexValue.hex));
|
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);
|
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("effectLevel", m->getEffectLevel());
|
||||||
context->setGlobal("effectRangeLevel", m->getRangeLevel());
|
context->setGlobal("effectRangeLevel", m->getRangeLevel());
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ private:
|
|||||||
|
|
||||||
std::shared_ptr<Context> resolveScript(const Mechanics * m) const;
|
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) ;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ VCMI_LIB_NAMESPACE_BEGIN
|
|||||||
namespace scripting
|
namespace scripting
|
||||||
{
|
{
|
||||||
|
|
||||||
LuaStack::LuaStack(lua_State * L_)
|
LuaStack::LuaStack(lua_State * L_):
|
||||||
: L(L_)
|
L(L_),
|
||||||
|
initialTop(lua_gettop(L))
|
||||||
{
|
{
|
||||||
initialTop = lua_gettop(L);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaStack::balance()
|
void LuaStack::balance()
|
||||||
@@ -94,7 +94,7 @@ void LuaStack::push(const JsonNode & value)
|
|||||||
case JsonNode::JsonType::DATA_STRUCT:
|
case JsonNode::JsonType::DATA_STRUCT:
|
||||||
{
|
{
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
for(auto & keyValue : value.Struct())
|
for(const auto & keyValue : value.Struct())
|
||||||
{
|
{
|
||||||
push(keyValue.first);
|
push(keyValue.first);
|
||||||
push(keyValue.second);
|
push(keyValue.second);
|
||||||
@@ -154,7 +154,7 @@ bool LuaStack::tryGet(int position, std::string & value)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
auto raw = lua_tolstring(L, position, &len);
|
const auto *raw = lua_tolstring(L, position, &len);
|
||||||
value = std::string(raw, len);
|
value = std::string(raw, len);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ static void publishMap(lua_State * L, const T & map)
|
|||||||
for(auto & p : map)
|
for(auto & p : map)
|
||||||
{
|
{
|
||||||
const std::string & name = p.first;
|
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_pushstring(L, name.c_str());
|
||||||
lua_pushinteger(L, id);
|
lua_pushinteger(L, id);
|
||||||
|
|||||||
@@ -28,12 +28,12 @@ Registry * Registry::get()
|
|||||||
|
|
||||||
void Registry::add(const std::string & name, std::shared_ptr<Registar> item)
|
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)
|
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
|
const Registar * Registry::find(const std::string & name) const
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ int ServerCbProxy::commitPackage(lua_State * L)
|
|||||||
return S.retVoid();
|
return S.retVoid();
|
||||||
|
|
||||||
|
|
||||||
CPackForClient * pack = static_cast<CPackForClient *>(lua_touserdata(L, 1));
|
auto * pack = static_cast<CPackForClient *>(lua_touserdata(L, 1));
|
||||||
|
|
||||||
object->apply(pack);
|
object->apply(pack);
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ int InfoWindowProxy::addReplacement(lua_State * L)
|
|||||||
if(lua_isstring(L, 2))
|
if(lua_isstring(L, 2))
|
||||||
{
|
{
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
auto raw = lua_tolstring(L, 2, &len);
|
const auto *raw = lua_tolstring(L, 2, &len);
|
||||||
std::string text(raw, len);
|
std::string text(raw, len);
|
||||||
|
|
||||||
object->text.addReplacement(text);
|
object->text.addReplacement(text);
|
||||||
@@ -87,7 +87,7 @@ int InfoWindowProxy::addText(lua_State * L)
|
|||||||
if(lua_isstring(L, 2))
|
if(lua_isstring(L, 2))
|
||||||
{
|
{
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
auto raw = lua_tolstring(L, 2, &len);
|
const auto *raw = lua_tolstring(L, 2, &len);
|
||||||
std::string text(raw, len);
|
std::string text(raw, len);
|
||||||
|
|
||||||
object->text << text;
|
object->text << text;
|
||||||
|
|||||||
Reference in New Issue
Block a user