-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17910d1
commit b059468
Showing
11 changed files
with
276 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Plugins/UnLua/Source/UnLua/Private/DelegateRegistry.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Tencent is pleased to support the open source community by making UnLua available. | ||
// | ||
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/MIT | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
|
||
#include "DelegateRegistry.h" | ||
|
||
#include "LuaDelegateHandler.h" | ||
#include "lauxlib.h" | ||
#include "UEObjectReferencer.h" | ||
#include "UnLuaBase.h" | ||
#include "LuaEnv.h" | ||
|
||
|
||
UnLua::FDelegateRegistry::FDelegateRegistry(lua_State* GL) | ||
: GL(GL) | ||
{ | ||
} | ||
|
||
void UnLua::FDelegateRegistry::Bind(lua_State* L, int32 Index, FScriptDelegate* Delegate, UObject* Lifecycle) | ||
{ | ||
const auto Type = lua_type(L, Index); | ||
if (Type == LUA_TFUNCTION) | ||
{ | ||
auto& Env = FLuaEnv::FindEnvChecked(GL); | ||
lua_pushvalue(L, Index); | ||
const auto Ref = luaL_ref(L, LUA_REGISTRYINDEX); | ||
const auto Handler = NewObject<ULuaDelegateHandler>(); | ||
GObjectReferencer.AddObjectRef(Handler); | ||
Handler->BindTo(Delegate, &Env, Ref, Lifecycle); | ||
return; | ||
} | ||
|
||
if (Type == LUA_TUSERDATA) | ||
{ | ||
const auto Func = (UFunction*)UnLua::GetUObject(L, -1); | ||
Delegate->BindUFunction(Func->GetOuter(), Func->GetFName()); | ||
return; | ||
} | ||
} | ||
|
||
void UnLua::FDelegateRegistry::Register(FScriptDelegate* Delegate, FDelegateProperty* Property) | ||
{ | ||
FDelegateInfo Info; | ||
Info.Property = Property; | ||
Info.Desc = nullptr; | ||
Delegates.Add(Delegate, Info); | ||
} | ||
|
||
void UnLua::FDelegateRegistry::Execute(const ULuaDelegateHandler* Handler, void* Params, int32 LuaRef) | ||
{ | ||
if (!Handler->IsAlive()) | ||
return; | ||
|
||
const auto SignatureDesc = GetSignatureDesc(Handler->Delegate); | ||
if (!SignatureDesc) | ||
{ | ||
// TODO: should not be here | ||
check(false); | ||
return; | ||
} | ||
|
||
const auto L = Handler->Env->GetMainState(); | ||
SignatureDesc->CallLua(L, LuaRef, Params, Handler->Lifecycle.Get()); | ||
} | ||
|
||
int32 UnLua::FDelegateRegistry::Execute(lua_State* L, FScriptDelegate* Delegate, int32 NumParams, int32 FirstParamIndex) | ||
{ | ||
auto SignatureDesc = GetSignatureDesc(Delegate); | ||
if (!SignatureDesc) | ||
{ | ||
// TODO: should not be here | ||
check(false); | ||
return 0; | ||
} | ||
|
||
const auto Ret = SignatureDesc->ExecuteDelegate(L, NumParams, FirstParamIndex, Delegate); | ||
return Ret; | ||
} | ||
|
||
FFunctionDesc* UnLua::FDelegateRegistry::GetSignatureDesc(const FScriptDelegate* Delegate) | ||
{ | ||
const auto Info = Delegates.Find(Delegate); | ||
if (!Info) | ||
return nullptr; | ||
if (!Info->Desc) | ||
Info->Desc = new FFunctionDesc(Info->Property->SignatureFunction, nullptr); | ||
return Info->Desc; | ||
} | ||
|
||
void UnLua::FDelegateRegistry::Unbind(FScriptDelegate* Delegate) | ||
{ | ||
FDelegateInfo Info = Delegates.FindAndRemoveChecked(Delegate); | ||
Delegate->Unbind(); | ||
delete Info.Desc; // TODO: optimize this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Tencent is pleased to support the open source community by making UnLua available. | ||
// | ||
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/MIT | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
|
||
|
||
#include "LuaDelegateHandler.h" | ||
#include "LuaEnv.h" | ||
|
||
void ULuaDelegateHandler::Dummy() | ||
{ | ||
} | ||
|
||
bool ULuaDelegateHandler::IsAlive() const | ||
{ | ||
return Lifecycle.IsValid(); | ||
} | ||
|
||
void ULuaDelegateHandler::BindTo(FScriptDelegate* InDelegate, UnLua::FLuaEnv* InEnv, int32 InLuaRef, UObject* InLifeCycle) | ||
{ | ||
static const FName NAME_Invoke = TEXT("Dummy"); | ||
|
||
Env = InEnv; | ||
LuaRef = InLuaRef; | ||
Lifecycle = InLifeCycle; | ||
Delegate = InDelegate; | ||
Delegate->BindUFunction(this, NAME_Invoke); | ||
} | ||
|
||
void ULuaDelegateHandler::ProcessEvent(UFunction* Function, void* Parms) | ||
{ | ||
Env->GetDelegateRegistry()->Execute(this, Parms, LuaRef); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Tencent is pleased to support the open source community by making UnLua available. | ||
// | ||
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/MIT | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include "lua.h" | ||
#include "LuaDelegateHandler.h" | ||
#include "ReflectionUtils/FunctionDesc.h" | ||
|
||
namespace UnLua | ||
{ | ||
class FDelegateRegistry | ||
{ | ||
public: | ||
explicit FDelegateRegistry(lua_State* GL); | ||
|
||
lua_State* GL; | ||
|
||
void Bind(lua_State* L, int32 Index, FScriptDelegate* Delegate, UObject* Lifecycle); | ||
|
||
void Register(FScriptDelegate* ScriptDelegate, FDelegateProperty* DelegateProperty); | ||
|
||
void Execute(const ULuaDelegateHandler* Handler, void* Params, int32 LuaRef); | ||
|
||
int32 Execute(lua_State* L, FScriptDelegate* Delegate, int32 NumParams, int32 FirstParamIndex); | ||
|
||
void Unbind(FScriptDelegate* Delegate); | ||
|
||
private: | ||
FFunctionDesc* GetSignatureDesc(const FScriptDelegate* Delegate); | ||
|
||
struct FDelegateInfo | ||
{ | ||
FDelegateProperty* Property; | ||
FFunctionDesc* Desc; | ||
}; | ||
|
||
TMap<FScriptDelegate*, FDelegateInfo> Delegates; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Tencent is pleased to support the open source community by making UnLua available. | ||
// | ||
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// http://opensource.org/licenses/MIT | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
#include "UObject/Object.h" | ||
#include "LuaDelegateHandler.generated.h" | ||
|
||
namespace UnLua | ||
{ | ||
class FLuaEnv; | ||
} | ||
|
||
UCLASS() | ||
class UNLUA_API ULuaDelegateHandler : public UObject | ||
{ | ||
GENERATED_BODY() | ||
|
||
public: | ||
UFUNCTION() | ||
void Dummy(); | ||
|
||
bool IsAlive() const; | ||
|
||
void BindTo(FScriptDelegate* Delegate, UnLua::FLuaEnv* InEnv, int32 InLuaRef, UObject* InLifecycle); | ||
|
||
virtual void ProcessEvent(UFunction* Function, void* Parms) override; | ||
|
||
TWeakObjectPtr<UObject> Lifecycle; | ||
FScriptDelegate* Delegate; | ||
UnLua::FLuaEnv* Env; | ||
int32 LuaRef; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters