Skip to content

Commit

Permalink
Merge pull request #344 from chendo/master
Browse files Browse the repository at this point in the history
Allow calling functions that don't expect ScriptGameInstance
  • Loading branch information
Yamashi authored Jan 2, 2021
2 parents 7aa5afd + d6e171b commit 2eea72f
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/scripting/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ sol::object Scripting::Execute(const std::string& aFuncName, sol::variadic_args

static const auto hashcpPlayerSystem = RED4ext::FNV1a("cpPlayerSystem");
static const auto hashGameInstance = RED4ext::FNV1a("ScriptGameInstance");
auto* pGIType = pRtti->GetType(RED4ext::FNV1a("ScriptGameInstance"));

auto* pPlayerSystem = pRtti->GetClass(hashcpPlayerSystem);
auto* gameInstanceType = pRtti->GetClass(hashGameInstance);
Expand All @@ -496,24 +497,6 @@ sol::object Scripting::Execute(const std::string& aFuncName, sol::variadic_args
}
}

if (pFunc->params.size - 1 != aArgs.size())
{
aReturnMessage = "Function '" + aFuncName + "' expects " +
std::to_string(pFunc->params.size - 1) + " parameters, not " +
std::to_string(aArgs.size()) + ".";

return sol::nil;
}

const auto* engine = RED4ext::CGameEngine::Get();
auto* unk10 = engine->framework->gameInstance;

using CStackType = RED4ext::CStackType;
std::vector<CStackType> args(aArgs.size() + 1);

args[0].type = pFunc->params[0]->type;
args[0].value = &unk10;

// 8KB should cut it
static thread_local TiltedPhoques::ScratchAllocator s_scratchMemory(1 << 13);
struct ResetAllocator
Expand All @@ -525,15 +508,49 @@ sol::object Scripting::Execute(const std::string& aFuncName, sol::variadic_args
};
ResetAllocator ___allocatorReset;

using CStackType = RED4ext::CStackType;
const auto* engine = RED4ext::CGameEngine::Get();
auto* unk10 = engine->framework->gameInstance;

RED4ext::CName name;
uint8_t argOffset = 0;

if (pFunc->params.size > 0)
{
auto* pType = pFunc->params[0]->type;
// check if the first argument is expected to be ScriptGameInstance
if (pType == pGIType)
{
argOffset = 1;
}
}

std::vector<CStackType> args(aArgs.size() + argOffset);

if (pFunc->params.size - argOffset != aArgs.size())
{
aReturnMessage = "Function '" + aFuncName + "' expects " +
std::to_string(pFunc->params.size - argOffset) + " parameters, not " +
std::to_string(aArgs.size()) + ".";

return sol::nil;
}

if (argOffset > 0)
{
// Inject the ScriptGameInstance into first argument
args[0].type = pFunc->params[0]->type;
args[0].value = &unk10;
}

for (auto i = 0ull; i < aArgs.size(); ++i)
{
args[i + 1ull] = ToRED(aArgs[i].get<sol::object>(), pFunc->params[i + 1ull]->type, &s_scratchMemory);
args[i + argOffset] = ToRED(aArgs[i].get<sol::object>(), pFunc->params[i + argOffset]->type, &s_scratchMemory);

if(!args[i + 1ull].value)
if(!args[i + argOffset].value)
{
auto* pType = pFunc->params[i + 1]->type;
auto* pType = pFunc->params[i + argOffset]->type;

RED4ext::CName name;
pType->GetName(name);
if (name)
{
Expand Down

0 comments on commit 2eea72f

Please sign in to comment.