Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/binding
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyanghuang-tencent committed Jun 30, 2022
2 parents 3adc180 + f948cd7 commit 5af39d4
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 39 deletions.
3 changes: 1 addition & 2 deletions Content/Script/AI/BP_AICharacter_C.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "UnLua"

local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")

local BP_AICharacter_C = Class("BP_CharacterBase_C")

function BP_AICharacter_C:Initialize(Initializer)
Expand All @@ -27,6 +25,7 @@ function BP_AICharacter_C:Died_Multicast_RPC(DamageType)
self.Mesh:SetAllBodiesBelowSimulatePhysics(self.BoneName, true, true)
local GameMode = UE.UGameplayStatics.GetGameMode(self)
if GameMode then
local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
BPI_Interfaces.NotifyEnemyDied(GameMode)
end
--self.Sphere.OnComponentBeginOverlap:Remove(self, BP_AICharacter_C.OnComponentBeginOverlap_Sphere)
Expand Down
7 changes: 2 additions & 5 deletions Content/Script/AI/BP_AIController_C.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ require "UnLua"

local BP_AIController_C = Class()

--BP_AIController_C.BehaviorTree = UObject.Load("/Game/Core/Blueprints/AI/BT_Enemy")
BP_AIController_C.BehaviorTree = LoadObject("/Game/Core/Blueprints/AI/BT_Enemy")

function BP_AIController_C:ReceiveBeginPlay()
--local BehaviorTree = UObject.Load("/Game/Core/Blueprints/AI/BT_Enemy")
self:RunBehaviorTree(self.BehaviorTree)
local BehaviorTree = UE.UObject.Load("/Game/Core/Blueprints/AI/BT_Enemy")
self:RunBehaviorTree(BehaviorTree)
end

return BP_AIController_C
5 changes: 2 additions & 3 deletions Content/Script/Animation/ABP_AICharacter_C.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require "UnLua"

local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
local BP_CharacterBase = UE.UClass.Load("/Game/Core/Blueprints/BP_CharacterBase.BP_CharacterBase_C")

local ABP_AICharacter_C = Class()

function ABP_AICharacter_C:AnimNotify_NotifyPhysics()
local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
BPI_Interfaces.ChangeToRagdoll(self.Pawn)
end

Expand All @@ -24,6 +22,7 @@ function ABP_AICharacter_C:BlueprintUpdateAnimation(DeltaTimeX)
return
end
self.Speed = Vel:Size()
local BP_CharacterBase = UE.UClass.Load("/Game/Core/Blueprints/BP_CharacterBase.BP_CharacterBase_C")
local Character = Pawn:Cast(BP_CharacterBase)
if Character then
if Character.IsDead and not self.IsDead then
Expand Down
5 changes: 2 additions & 3 deletions Content/Script/Animation/ABP_PlayerCharacter_C.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require "UnLua"

local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
local BP_CharacterBase = UE.UClass.Load("/Game/Core/Blueprints/BP_CharacterBase.BP_CharacterBase_C")

local ABP_PlayerCharacter_C = Class()

function ABP_PlayerCharacter_C:AnimNotify_NotifyPhysics()
local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
BPI_Interfaces.ChangeToRagdoll(self.Pawn)
end

Expand All @@ -26,6 +24,7 @@ function ABP_PlayerCharacter_C:BlueprintUpdateAnimation(DeltaTimeX)
if not Vel then
return
end
local BP_CharacterBase = UE.UClass.Load("/Game/Core/Blueprints/BP_CharacterBase.BP_CharacterBase_C")
local Character = Pawn:Cast(BP_CharacterBase)
if Character then
if Character.IsDead and not self.IsDead then
Expand Down
4 changes: 2 additions & 2 deletions Content/Script/Player/BP_PlayerController_C.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "UnLua"

local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")

local BP_PlayerController_C = Class()

function BP_PlayerController_C:UserConstructionScript()
Expand Down Expand Up @@ -76,12 +74,14 @@ end

function BP_PlayerController_C:Aim_Pressed()
if self.Pawn then
local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
BPI_Interfaces.UpdateAiming(self.Pawn, true)
end
end

function BP_PlayerController_C:Aim_Released()
if self.Pawn then
local BPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
BPI_Interfaces.UpdateAiming(self.Pawn, false)
end
end
Expand Down
5 changes: 5 additions & 0 deletions Content/Script/Tests/Regression/Issue476/TestStub.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "UnLua"

local M = Class()

return M
3 changes: 1 addition & 2 deletions Content/Script/Weapon/BP_ProjectileBase_C.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "UnLua"

local BP_CharacterBase = UE.UClass.Load("/Game/Core/Blueprints/BP_CharacterBase.BP_CharacterBase_C")

local BP_ProjectileBase_C = Class()

function BP_ProjectileBase_C:UserConstructionScript()
Expand All @@ -15,6 +13,7 @@ function BP_ProjectileBase_C:ReceiveBeginPlay()
end

function BP_ProjectileBase_C:OnComponentHit_Sphere(HitComponent, OtherActor, OtherComp, NormalImpulse, Hit)
local BP_CharacterBase = UE.UClass.Load("/Game/Core/Blueprints/BP_CharacterBase.BP_CharacterBase_C")
local Character = OtherActor:Cast(BP_CharacterBase)
if Character then
Character.BoneName = Hit.BoneName;
Expand Down
3 changes: 1 addition & 2 deletions Content/Script/Weapon/BP_WeaponBase_C.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "UnLua"

local UBPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")

local BP_WeaponBase_C = Class()

local EFireType = { FT_Projectile = 0, FT_InstantHit = 1 }
Expand Down Expand Up @@ -86,6 +84,7 @@ function BP_WeaponBase_C:InstantFire()
end

function BP_WeaponBase_C:GetFireInfo()
local UBPI_Interfaces = UE.UClass.Load("/Game/Core/Blueprints/BPI_Interfaces.BPI_Interfaces_C")
local TraceStart, TraceDirection = UBPI_Interfaces.GetWeaponTraceInfo(self.Instigator)
local Delta = TraceDirection * self.WeaponTraceDistance
local TraceEnd = TraceStart + Delta
Expand Down
3 changes: 3 additions & 0 deletions Plugins/UnLua/Source/UnLua/Private/BaseLib/LuaLib_Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ int32 UObject_Delete(lua_State *L)
return 0;

UObject* Object = (UObject*)*(void**)Userdata;
if (UnLua::LowLevel::IsReleasedPtr(Object))
return 0;

UnLua::FLuaEnv::FindEnvChecked(L).GetObjectRegistry()->NotifyUObjectLuaGC(Object);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ namespace UnLua
lua_pushlightuserdata(L, Object);
lua_pushvalue(L, -2);
lua_rawset(L, -4);

if (!Object->IsNative())
Env->AutoObjectReference.Add(Object);

ObjectRefs.Add(Object, LUA_NOREF);
}
lua_remove(L, -2);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ struct FUnLuaTest_Issue288 : FUnLuaTestBase
{
FUnLuaTestBase::SetUp();

const char* Chunk1 = "\
local UMGClass = UE.UClass.Load('/UnLuaTestSuite/Tests/Regression/Issue288/UnLuaTestUMG_Issue288.UnLuaTestUMG_Issue288_C')\
G_UMG = NewObject(UMGClass)\
";
const auto Chunk1 = R"(
local UMGClass = UE.UClass.Load('/UnLuaTestSuite/Tests/Regression/Issue288/UnLuaTestUMG_Issue288.UnLuaTestUMG_Issue288_C')
G_UMG = NewObject(UMGClass)
)";
UnLua::RunChunk(L, Chunk1);

auto Texture2D = FindObject<UTexture2D>(nullptr, TEXT("/Game/FPWeapon/Textures/UE4_LOGO_CARD.UE4_LOGO_CARD"));
RUNNER_TEST_NOT_NULL(Texture2D);

const char* Chunk2 = "\
G_UMG:Release()\
G_UMG = nil\
";
const auto Chunk2 = R"(
G_UMG:Release()
G_UMG = nil
)";
UnLua::RunChunk(L, Chunk2);

lua_gc(L, LUA_GCCOLLECT, 0);
Expand All @@ -49,13 +49,7 @@ struct FUnLuaTest_Issue288 : FUnLuaTestBase

Texture2D = FindObject<UTexture2D>(nullptr, TEXT("/Game/FPWeapon/Textures/UE4_LOGO_CARD.UE4_LOGO_CARD"));
if (Texture2D)
{
#if ENGINE_MAJOR_VERSION > 4 || (ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 26)
FReferenceChainSearch Search(Texture2D, EReferenceChainSearchMode::PrintAllResults | EReferenceChainSearchMode::FullChain);
#else
FReferenceChainSearch Search(Texture2D, EReferenceChainSearchMode::PrintAllResults);
#endif
}
UnLuaTestSuite::PrintReferenceChain(Texture2D);
RUNNER_TEST_NULL(Texture2D);

return true;
Expand All @@ -64,4 +58,4 @@ struct FUnLuaTest_Issue288 : FUnLuaTestBase

IMPLEMENT_UNLUA_INSTANT_TEST(FUnLuaTest_Issue288, TEXT("UnLua.Regression.Issue288 UMG里Image用到的Texture内存泄漏"))

#endif //WITH_DEV_AUTOMATION_TESTS
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 "Blueprint/UserWidget.h"
#include "UnLuaTestCommon.h"
#include "UnLuaTestHelpers.h"
#include "Misc/AutomationTest.h"

#if WITH_DEV_AUTOMATION_TESTS

struct FUnLuaTest_Issue476 : FUnLuaTestBase
{
virtual bool InstantTest() override
{
return true;
}

virtual bool SetUp() override
{
FUnLuaTestBase::SetUp();

const auto Chunk = R"(
local Outer = NewObject(UE.UUnLuaTestStub)
local StubClass = UE.UClass.Load("/UnLuaTestSuite/Tests/Regression/Issue476/BP_UnLuaTestStub_Issue476.BP_UnLuaTestStub_Issue476_C")
G_Stub = NewObject(StubClass, Outer, "Tests.Regression.Issue476.TestStub")
return G_Stub
)";
UnLua::RunChunk(L, Chunk);

const FWeakObjectPtr Stub = UnLua::GetUObject(L, -1);
const FWeakObjectPtr Outer = Stub.Get()->GetOuter();
CollectGarbage(RF_NoFlags, true);

if (Stub.IsValid())
UnLuaTestSuite::PrintReferenceChain(Stub.Get());
if (Outer.IsValid())
UnLuaTestSuite::PrintReferenceChain(Outer.Get());

RUNNER_TEST_FALSE(Stub.IsValid());
RUNNER_TEST_FALSE(Outer.IsValid());
return true;
}
};

IMPLEMENT_UNLUA_INSTANT_TEST(FUnLuaTest_Issue476, TEXT("UnLua.Regression.Issue476 通过NewObject接口添加的UObject引用无法释放"))

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@ void FUnLuaTestBase::LoadMap(FString MapName) const
GWorld = OldWorld;
}

void UnLuaTestSuite::PrintReferenceChain(UObject* Target)
{
#if ENGINE_MAJOR_VERSION > 4 || (ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 26)
FReferenceChainSearch Search(Target, EReferenceChainSearchMode::PrintAllResults | EReferenceChainSearchMode::FullChain);
#else
FReferenceChainSearch Search(Texture2D, EReferenceChainSearchMode::PrintAllResults);
#endif
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,9 @@ IMPLEMENT_SIMPLE_AUTOMATION_TEST(TestClass, PrettyName, (EAutomationTestFlags::C
return true;\
}

namespace UnLuaTestSuite
{
void PrintReferenceChain(UObject* Target);
}

#endif //WITH_DEV_AUTOMATION_TESTS

0 comments on commit 5af39d4

Please sign in to comment.