Skip to content

Commit

Permalink
重构:ObjectMapping移入ObjectRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
forsakenyang committed Apr 29, 2022
1 parent a930f73 commit 23ff1d2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
29 changes: 4 additions & 25 deletions Plugins/UnLua/Source/UnLua/Private/LuaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,39 +898,18 @@ bool GetFunctionList(lua_State *L, const char *InModuleName, TSet<FName> &Functi
return true;
}

/**
* Get Lua instance for a UObject
*/
bool GetObjectMapping(lua_State *L, UObjectBaseUtility *Object)
{
if (!Object)
{
UNLUA_LOGERROR(L, LogUnLua, Warning, TEXT("%s, Invalid object!"), ANSI_TO_TCHAR(__FUNCTION__));
return false;
}

lua_getfield(L, LUA_REGISTRYINDEX, "ObjectMap");
lua_pushlightuserdata(L, Object);
int32 Type = lua_rawget(L, -2);
if (Type != LUA_TNIL)
{
lua_remove(L, -2);
return true;
}
lua_pop(L, 2);
return false;
}

/**
* Push a Lua function (by a function name) and push a UObject instance as its first parameter
*/
int32 PushFunction(lua_State *L, UObjectBaseUtility *Object, const char *FunctionName)
{
int32 N = lua_gettop(L);
lua_pushcfunction(L, UnLua::ReportLuaCallError);
bool bSuccess = GetObjectMapping(L, Object);
if (bSuccess)
const auto& Env = UnLua::FLuaEnv::FindEnv(L);
const auto Ref = Env->GetObjectRegistry()->GetBoundRef((UObject*)Object);
if (Ref != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, Ref);
int32 Type = lua_type(L, -1);
if (Type == LUA_TTABLE /*|| Type == LUA_TUSERDATA*/)
{
Expand Down
4 changes: 0 additions & 4 deletions Plugins/UnLua/Source/UnLua/Private/LuaEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ namespace UnLua
AutoObjectReference.SetName("UnLua_AutoReference");
ManualObjectReference.SetName("UnLua_ManualReference");

lua_pushstring(L, "ObjectMap"); // create weak table 'ObjectMap'
CreateWeakValueTable(L);
lua_rawset(L, LUA_REGISTRYINDEX);

lua_pushstring(L, "StructMap"); // create weak table 'StructMap'
CreateWeakValueTable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
Expand Down
13 changes: 13 additions & 0 deletions Plugins/UnLua/Source/UnLua/Private/Registries/ObjectRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ namespace UnLua
: Env(Env)
{
const auto L = Env->GetMainState();

lua_pushstring(L, "ObjectMap"); // create weak table 'ObjectMap'
CreateWeakValueTable(L);
lua_rawset(L, LUA_REGISTRYINDEX);

luaL_newmetatable(L, "TSharedPtr");
lua_pushstring(L, "__gc");
lua_pushcfunction(L, ReleaseSharedPtr);
Expand Down Expand Up @@ -96,6 +101,14 @@ namespace UnLua
return ObjectRefs.Contains(Object);
}

int FObjectRegistry::GetBoundRef(const UObject* Object) const
{
const auto Ref = ObjectRefs.Find(Object);
if (Ref)
return *Ref;
return LUA_NOREF;
}

void FObjectRegistry::Unbind(UObject* Object)
{
int32 Ref;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ namespace UnLua
*/
bool IsBound(const UObject* Object) const;

/**
* 获取指定UObject在Lua里绑定的table的引用ID。
* @return 若没有绑定过则返回LUA_NOREF。
*/
int GetBoundRef(const UObject* Object) const;

/**
* 将指定的UObject从Lua环境解绑。
*/
Expand Down
1 change: 1 addition & 0 deletions TPSProject.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=collectgarbage/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Intelli/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=LOCTEXT/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=NOREF/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Parms/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Quat/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tickable/@EntryIndexedValue">True</s:Boolean>
Expand Down

0 comments on commit 23ff1d2

Please sign in to comment.