Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Allow limited transfer of data across lua states
Browse files Browse the repository at this point in the history
  • Loading branch information
Causeless committed Nov 14, 2023
1 parent 1385c41 commit adcdef7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
12 changes: 7 additions & 5 deletions Lua/LuaAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,8 @@ namespace RTE {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LuaAdaptersMovableObject::SendMessage2(MovableObject *luaSelfObject, const std::string &message, luabind::object context) {
// We're not transferring context between lua states, so send context to singlethreaded scripts
LuabindObjectWrapper wrapper(&context, "", false);
luaSelfObject->RunScriptedFunctionInAppropriateScripts("OnMessage", false, false, {}, { message }, { &wrapper }, ThreadScriptsToRun::SingleThreaded);
luaSelfObject->RunScriptedFunctionInAppropriateScripts("OnMessage", false, false, {}, { message }, { }, ThreadScriptsToRun::MultiThreaded);
luaSelfObject->RunScriptedFunctionInAppropriateScripts("OnMessage", false, false, {}, { message }, { &wrapper });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -562,8 +560,12 @@ namespace RTE {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LuaAdaptersMovableMan::SendGlobalMessage1(MovableMan &movableMan, const std::string &message) {
luabind::object context;
SendGlobalMessage2(movableMan, message, context);
GAScripted* scriptedActivity = dynamic_cast<GAScripted*>(g_ActivityMan.GetActivity());
if (scriptedActivity) {
scriptedActivity->RunLuaFunction("OnGlobalMessage", {}, { message });
}

movableMan.RunLuaFunctionOnAllMOs("OnGlobalMessage", true, {}, { message });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
34 changes: 34 additions & 0 deletions Lua/LuabindObjectWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,38 @@ LuabindObjectWrapper::~LuabindObjectWrapper() {
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

luabind::adl::object GetCopyForStateInternal(const luabind::adl::object& obj, lua_State& targetState) {
if (obj.is_valid()) {
int type = luabind::type(obj);
if (type == LUA_TNUMBER) {
return luabind::adl::object(&targetState, luabind::object_cast<double>(obj));
}
else if (type == LUA_TBOOLEAN) {
return luabind::adl::object(&targetState, luabind::object_cast<bool>(obj));
}
else if (type == LUA_TSTRING) {
return luabind::adl::object(&targetState, luabind::object_cast<std::string>(obj));
}
else if (type == LUA_TTABLE) {
luabind::object table = luabind::newtable(&targetState);
for (luabind::iterator itr(obj), itrEnd; itr != itrEnd; ++itr) {
table[GetCopyForStateInternal(itr.key(), targetState)] = GetCopyForStateInternal(*itr, targetState);
}
return table;
}
}

// Dear god, I hope this is safe and equivalent to nil, because I can't find another way of doing it.
return luabind::adl::object();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

LuabindObjectWrapper LuabindObjectWrapper::GetCopyForState(lua_State& targetState) const {
luabind::adl::object* copy = new luabind::adl::object(GetCopyForStateInternal(*m_LuabindObject, targetState));
return LuabindObjectWrapper(copy, m_FilePath, true);
}

}
7 changes: 7 additions & 0 deletions Lua/LuabindObjectWrapper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _RTELUABINDOBJECTWRAPPER_
#define _RTELUABINDOBJECTWRAPPER_

struct lua_State;

namespace luabind::adl {
class object;
}
Expand Down Expand Up @@ -47,6 +49,11 @@ namespace RTE {
~LuabindObjectWrapper();
#pragma endregion

/// <summary>
/// Attempts to copy a luabind object into another state.
/// </summary>
LuabindObjectWrapper GetCopyForState(lua_State& newState) const;

#pragma region Getters
/// <summary>
/// Gets the LuabindObjectWrapper's luabind object. Ownership is NOT transferred!
Expand Down
7 changes: 6 additions & 1 deletion Managers/LuaMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,12 @@ namespace RTE {
}

for (const LuabindObjectWrapper *functionObjectArgument : functionObjectArguments) {
functionObjectArgument->GetLuabindObject()->push(m_State);
if (functionObjectArgument->GetLuabindObject()->interpreter() != m_State) {
LuabindObjectWrapper copy = functionObjectArgument->GetCopyForState(*m_State);
copy.GetLuabindObject()->push(m_State);
} else {
functionObjectArgument->GetLuabindObject()->push(m_State);
}
}

const std::string& path = functionObject->GetFilePath();
Expand Down

0 comments on commit adcdef7

Please sign in to comment.