From 33cbc2da8e3ffd83f8f9650043c260e7169caa9c Mon Sep 17 00:00:00 2001 From: xuyanghuang Date: Mon, 8 Nov 2021 20:39:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9AUUnLuaLatentAction?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=8C=85=E8=A3=85=E5=BC=82=E6=AD=A5=E8=A1=8C?= =?UTF-8?q?=E4=B8=BA=EF=BC=8C=E6=94=AF=E6=8C=81SetTickableWhenPaused?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Private/ReflectionUtils/FunctionDesc.cpp | 14 +++- .../UnLua/Private/UUnLuaLatentAction.cpp | 71 +++++++++++++++++++ .../Source/UnLua/Public/UUnLuaLatentAction.h | 64 +++++++++++++++++ TPSProject.sln.DotSettings | 6 ++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 Plugins/UnLua/Source/UnLua/Private/UUnLuaLatentAction.cpp create mode 100644 Plugins/UnLua/Source/UnLua/Public/UUnLuaLatentAction.h create mode 100644 TPSProject.sln.DotSettings diff --git a/Plugins/UnLua/Source/UnLua/Private/ReflectionUtils/FunctionDesc.cpp b/Plugins/UnLua/Source/UnLua/Private/ReflectionUtils/FunctionDesc.cpp index fb0484a9..4b69dfe7 100644 --- a/Plugins/UnLua/Source/UnLua/Private/ReflectionUtils/FunctionDesc.cpp +++ b/Plugins/UnLua/Source/UnLua/Private/ReflectionUtils/FunctionDesc.cpp @@ -19,6 +19,8 @@ #include "LuaContext.h" #include "LuaFunctionInjection.h" #include "DefaultParamCollection.h" +#include "UnLua.h" +#include "UUnLuaLatentAction.h" /** * Function descriptor constructor @@ -419,8 +421,18 @@ void* FFunctionDesc::PreCall(lua_State *L, int32 NumParams, int32 FirstParamInde Property->InitializeValue(Params); if (i == LatentPropertyIndex) { + const int32 ThreadRef = *((int32*)Userdata); + if(lua_type(L, FirstParamIndex + ParamIndex) == LUA_TUSERDATA) + { + // custom latent action info + FLatentActionInfo Info = UnLua::Get(L, FirstParamIndex + ParamIndex, UnLua::TType()); + if(Info.Linkage == UUnLuaLatentAction::MAGIC_LEGACY_LINKAGE) + Info.Linkage = ThreadRef; + Property->CopyValue(Params, &Info); + continue; + } + // bind a callback to the latent function - int32 ThreadRef = *((int32*)Userdata); FLatentActionInfo LatentActionInfo(ThreadRef, GetTypeHash(FGuid::NewGuid()), TEXT("OnLatentActionCompleted"), (UObject*)GLuaCxt->GetManager()); Property->CopyValue(Params, &LatentActionInfo); continue; diff --git a/Plugins/UnLua/Source/UnLua/Private/UUnLuaLatentAction.cpp b/Plugins/UnLua/Source/UnLua/Private/UUnLuaLatentAction.cpp new file mode 100644 index 00000000..d66c2d16 --- /dev/null +++ b/Plugins/UnLua/Source/UnLua/Private/UUnLuaLatentAction.cpp @@ -0,0 +1,71 @@ +// 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 "UUnLuaLatentAction.h" + +#include "LuaContext.h" + +void UUnLuaLatentAction::OnCompleted(int32 InLinkage) const +{ + if (Callback.IsBound()) + Callback.Execute(InLinkage); +} + +FLatentActionInfo UUnLuaLatentAction::CreateInfo(const int32 Linkage) +{ + return FLatentActionInfo(Linkage, GetTypeHash(FGuid::NewGuid()), TEXT("OnCompleted"), this); +} + +FLatentActionInfo UUnLuaLatentAction::CreateInfoForLegacy() +{ + Callback.BindDynamic(this, &UUnLuaLatentAction::OnLegacyCallback); + return CreateInfo(MAGIC_LEGACY_LINKAGE); +} + +bool UUnLuaLatentAction::GetTickableWhenPaused() +{ + return bTickEvenWhenPaused; +} + +void UUnLuaLatentAction::SetTickableWhenPaused(bool bTickableWhenPaused) +{ + bTickEvenWhenPaused = bTickableWhenPaused; +} + +void UUnLuaLatentAction::Tick(float DeltaTime) +{ + UWorld* World = GetWorld(); + if (World) + { + GetClass()->ClassFlags |= CLASS_CompiledFromBlueprint; + World->GetLatentActionManager().ProcessLatentActions(this, DeltaTime); + GetClass()->ClassFlags &= ~CLASS_CompiledFromBlueprint; + } +} + +bool UUnLuaLatentAction::IsTickable() const +{ + return bTickEvenWhenPaused; +} + +TStatId UUnLuaLatentAction::GetStatId() const +{ + return GetStatID(); +} + +void UUnLuaLatentAction::OnLegacyCallback(int32 InLinkage) +{ + Callback.Unbind(); + GLuaCxt->ResumeThread(InLinkage); +} diff --git a/Plugins/UnLua/Source/UnLua/Public/UUnLuaLatentAction.h b/Plugins/UnLua/Source/UnLua/Public/UUnLuaLatentAction.h new file mode 100644 index 00000000..33922c0e --- /dev/null +++ b/Plugins/UnLua/Source/UnLua/Public/UUnLuaLatentAction.h @@ -0,0 +1,64 @@ +// 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 "UUnLuaLatentAction.generated.h" + +DECLARE_DYNAMIC_DELEGATE_OneParam(FUnLuaLatentActionCallback, int32, InLinkage); + +UCLASS() +class UUnLuaLatentAction : public UObject, public FTickableGameObject +{ + GENERATED_BODY() + +public: + const static int32 MAGIC_LEGACY_LINKAGE = 0x01DC0DE; + + UPROPERTY() + FUnLuaLatentActionCallback Callback; + + UFUNCTION() + void OnCompleted(int32 InLinkage) const; + + UFUNCTION(BlueprintCallable) + FLatentActionInfo CreateInfo(int32 Linkage = 0); + + UFUNCTION(BlueprintCallable) + FLatentActionInfo CreateInfoForLegacy(); + + UFUNCTION(BlueprintCallable) + bool GetTickableWhenPaused(); + + UFUNCTION(BlueprintCallable) + void SetTickableWhenPaused(bool bTickableWhenPaused); + + // Begin Interface FTickableGameObject + + virtual void Tick(float DeltaTime) override; + + virtual bool IsTickable() const override; + + virtual TStatId GetStatId() const override; + + // End Interface FTickableGameObject + + UFUNCTION() + void OnLegacyCallback(int32 InLinkage); + +private: + UPROPERTY() + uint8 bTickEvenWhenPaused:1; +}; diff --git a/TPSProject.sln.DotSettings b/TPSProject.sln.DotSettings new file mode 100644 index 00000000..0318eec7 --- /dev/null +++ b/TPSProject.sln.DotSettings @@ -0,0 +1,6 @@ + + True + True + True + True + True \ No newline at end of file