Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Fix natives, convert alt::IEntity to script id if its passed as an ar…
Browse files Browse the repository at this point in the history
…gument in native functions
  • Loading branch information
drakeee committed Jan 26, 2021
1 parent 0b050f6 commit 46e714c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/CLuaDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,10 @@ alt::IBaseObject* lua_tobaseobject(lua_State* L, int idx)

int lua_isinteger(lua_State* L, int index)
{
int32_t x = (int32_t)lua_tointeger(L, index);
//int32_t x = (int32_t)lua_tointeger(L, index);
//lua_Number n = lua_tonumber(L, index);
lua_Number n = lua_tonumber(L, index);
return ((lua_Number)x == n);
return (n == (int64_t)n);
}

alt::MValue lua_tomvalue(lua_State* L, int idx)
Expand All @@ -753,11 +754,13 @@ alt::MValue lua_tomvalue(lua_State* L, int idx)
switch (argType)
{
case LUA_TNUMBER:
{
if (lua_isinteger(L, idx))
mValue = Core->CreateMValueInt(lua_tointeger(L, idx));
mValue = Core->CreateMValueInt(lua_tonumber(L, idx));
else
mValue = Core->CreateMValueDouble(lua_tonumber(L, idx));
break;
}
case LUA_TBOOLEAN:
mValue = Core->CreateMValueBool(lua_toboolean(L, idx));
break;
Expand Down Expand Up @@ -971,10 +974,11 @@ const char* luaL_tolstring(lua_State* L, int idx, size_t* len)
else {
switch (lua_type(L, idx)) {
case LUA_TNUMBER: {
if (lua_isinteger(L, idx))
/*if (lua_isinteger(L, idx))
lua_pushfstring(L, "%d", lua_tointeger(L, idx));
else
lua_pushfstring(L, "%f", lua_tonumber(L, idx));
lua_pushfstring(L, "%f", lua_tonumber(L, idx));*/
lua_pushfstring(L, "%f", lua_tonumber(L, idx));
break;
}
case LUA_TSTRING:
Expand Down
2 changes: 1 addition & 1 deletion src/CLuaScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CLuaScriptRuntime : public alt::IScriptRuntime
~CLuaScriptRuntime() { };

private:
const semver::version version{ 1, 0, 10, alt::ICore::SDK_VERSION, semver::branch::dev };
const semver::version version{ 1, 0, 11, alt::ICore::SDK_VERSION, semver::branch::dev };
#ifdef ALT_SERVER_API
alt::config::Node::Dict serverConfigDict;
#endif
Expand Down
28 changes: 23 additions & 5 deletions src/Defs/CLuaNativeDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,30 @@ void PushArg(alt::INative::Context *ctx, alt::INative::Type argType, alt::MValue
ctx->Push(CreatePtr((int32_t)value.As<alt::IMValueBool>()->Value(), argType));
break;
case alt::INative::Type::ARG_INT32:
ctx->Push((int32_t)value.As<alt::IMValueInt>()->Value());
{
if (value->GetType() == alt::IMValue::Type::BASE_OBJECT)
{
auto baseObject = value.As<alt::IMValueBaseObject>()->Value().Get();
if (
baseObject->GetType() == alt::IBaseObject::Type::PLAYER ||
baseObject->GetType() == alt::IBaseObject::Type::VEHICLE
)
ctx->Push((dynamic_cast<alt::IEntity*>(baseObject)->GetScriptGuid()));
} else
ctx->Push((int32_t)value.As<alt::IMValueInt>()->Value());

break;
}
case alt::INative::Type::ARG_INT32_PTR:
ctx->Push(CreatePtr((int32_t)value.As<alt::IMValueInt>()->Value(), argType));
break;
case alt::INative::Type::ARG_UINT32:
ctx->Push((uint32_t)value.As<alt::IMValueUInt>()->Value());
{
ctx->Push((uint32_t)value.As<alt::IMValueInt>()->Value()); //numbers are either converted to IMValueInt or IMValueDouble
break;
}
case alt::INative::Type::ARG_UINT32_PTR:
ctx->Push(CreatePtr((uint32_t)value.As<alt::IMValueUInt>()->Value(), argType));
ctx->Push(CreatePtr((uint32_t)value.As<alt::IMValueInt>()->Value(), argType)); //numbers are either converted to IMValueInt or IMValueDouble
break;
case alt::INative::Type::ARG_FLOAT:
ctx->Push((float)value.As<alt::IMValueDouble>()->Value());
Expand All @@ -79,6 +93,7 @@ void PushArg(alt::INative::Context *ctx, alt::INative::Type argType, alt::MValue
break;
default:
Core->LogError("Unknown native arg type" + std::to_string((int)argType));
break;
}
}

Expand All @@ -93,8 +108,10 @@ void GetReturn(lua_State* L, alt::INative::Context* ctx, alt::INative::Type retu
lua_pushnumber(L, ctx->ResultInt());
break;
case alt::INative::Type::ARG_UINT32:
lua_pushnumber(L, ctx->ResultUint());
{
lua_pushnumber(L, (lua_Number)ctx->ResultUint());
break;
}
case alt::INative::Type::ARG_FLOAT:
lua_pushnumber(L, ctx->ResultFloat());
break;
Expand Down Expand Up @@ -191,8 +208,9 @@ int CLuaNativeDefs::InvokeNative(lua_State* L)
return 1;
}

uint16_t ptrCount = 0;
GetReturn(L, nativeCtx.Get(), native->GetRetnType());

uint16_t ptrCount = 0;
if (pointers.size() > 0)
{
for (auto&& pointer : pointers)
Expand Down

0 comments on commit 46e714c

Please sign in to comment.