diff --git a/Content/Lua/TestMap.lua b/Content/Lua/TestMap.lua index c8f3e60d..3f996707 100644 --- a/Content/Lua/TestMap.lua +++ b/Content/Lua/TestMap.lua @@ -63,4 +63,8 @@ function TestMap:ReceiveBeginPlay() test() end +function TestMap:update() + +end + return Class(ni, nil, TestMap) \ No newline at end of file diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActor.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActor.cpp index d6aeb4a8..6c32632d 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActor.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActor.cpp @@ -7,14 +7,6 @@ ALuaActor::ALuaActor(const FObjectInitializer& ObjectInitializer) { } -void ALuaActor::PostInitializeComponents() -{ - Super::PostInitializeComponents(); - - CallReceivePreRep(LuaFilePath); - TryHookActorComponents(); -} - void ALuaActor::EndPlay(const EEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); @@ -30,6 +22,12 @@ FString ALuaActor::GetLuaFilePath_Implementation() const return LuaFilePath; } +void ALuaActor::PostInitializeComponents() +{ + Super::PostInitializeComponents(); + ILuaOverriderInterface::PostLuaHook(); +} + void ALuaActor::RegistLuaTick(float TickInterval) { EnableLuaTick = true; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActorComponent.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActorComponent.cpp index b10a0b63..10fe3718 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActorComponent.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaActorComponent.cpp @@ -8,13 +8,6 @@ ULuaActorComponent::ULuaActorComponent(const FObjectInitializer& ObjectInitializ bWantsInitializeComponent = true; } -void ULuaActorComponent::InitializeComponent() -{ - Super::InitializeComponent(); - - CallReceivePreRep(LuaFilePath); -} - void ULuaActorComponent::EndPlay(const EEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); @@ -42,6 +35,13 @@ void ULuaActorComponent::UnRegistLuaTick() state->unRegistLuaTick(this); } +void ULuaActorComponent::InitializeComponent() +{ + Super::InitializeComponent(); + + TryHook(); +} + void ULuaActorComponent::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaFunctionAccelerator.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaFunctionAccelerator.cpp index 0906b2ea..150b12c4 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaFunctionAccelerator.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaFunctionAccelerator.cpp @@ -35,19 +35,19 @@ namespace NS_SLUA FProperty* prop = *it; uint64 propflag = prop->GetPropertyFlags(); - FOutParmRec* out = nullptr; if (prop->HasAnyPropertyFlags(CPF_OutParm)) { outParmRecProps.Add(prop); } - FCheckerInfo checkerInfo = {false, false, false, propIndex, prop->GetOffset_ForInternal(), prop}; + FCheckerInfo checkerInfo = {false, false, false, false, propIndex, prop->GetOffset_ForInternal(), prop}; FCheckerInfo* checkerRef = &checkerInfo; - if (!prop->HasAnyPropertyFlags(CPF_NoDestructor) && - !(IsReferenceParam(prop->PropertyFlags, func) && LuaObject::getReferencer(prop))) + if (!prop->HasAnyPropertyFlags(CPF_NoDestructor)) { checkerInfo.bInit = true; + checkerInfo.bReference = IsReferenceParam(prop->PropertyFlags, func) && LuaObject::getReferencer(prop); paramsChecker.Add(checkerInfo); + checkerRef = ¶msChecker.Top(); } @@ -152,8 +152,8 @@ namespace NS_SLUA auto funcPtr = cache.Find(inFunc); if (funcPtr) { - cache.Remove(inFunc); delete *funcPtr; + cache.Remove(inFunc); return true; } @@ -230,7 +230,7 @@ namespace NS_SLUA for (auto& checkerInfo : paramsChecker) { auto prop = checkerInfo.prop; - if (checkerInfo.bInit && (lua_type(L, i) == LUA_TUSERDATA)) + if (checkerInfo.bInit && !(checkerInfo.bReference && (lua_type(L, i) == LUA_TUSERDATA))) { if (!prop->HasAnyPropertyFlags(CPF_ZeroConstructor)) { @@ -353,7 +353,7 @@ namespace NS_SLUA for (auto& checkerInfo : paramsChecker) { auto prop = checkerInfo.prop; - if (checkerInfo.bInit && (lua_type(L, i) == LUA_TUSERDATA)) + if (checkerInfo.bInit && !(checkerInfo.bReference && (lua_type(L, i) == LUA_TUSERDATA))) { if (!prop->HasAnyPropertyFlags(CPF_ZeroConstructor)) { diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameMode.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameMode.cpp index 8acb78f7..828b2f25 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameMode.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameMode.cpp @@ -9,11 +9,3 @@ FString ALuaGameMode::GetLuaFilePath_Implementation() const { return LuaFilePath; } - -void ALuaGameMode::PostInitializeComponents() -{ - Super::PostInitializeComponents(); - - CallReceivePreRep(LuaFilePath); - TryHookActorComponents(); -} diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameState.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameState.cpp index 3518e47e..f47e8e3e 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameState.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaGameState.cpp @@ -11,14 +11,6 @@ FString ALuaGameState::GetLuaFilePath_Implementation() const return LuaFilePath; } -void ALuaGameState::PostInitializeComponents() -{ - Super::PostInitializeComponents(); - - CallReceivePreRep(LuaFilePath); - TryHookActorComponents(); -} - void ALuaGameState::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaInstancedActorComponent.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaInstancedActorComponent.cpp deleted file mode 100644 index 0ef88689..00000000 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaInstancedActorComponent.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "LuaInstancedActorComponent.h" -#include "LuaState.h" - -ULuaInstancedActorComponent::ULuaInstancedActorComponent(const FObjectInitializer& ObjectInitializer) - : Super(ObjectInitializer) -{ -} - -void ULuaInstancedActorComponent::EndPlay(const EEndPlayReason::Type EndPlayReason) -{ - Super::EndPlay(EndPlayReason); - if (EnableLuaTick) - { - UnRegistLuaTick(); - } -} - -FString ULuaInstancedActorComponent::GetLuaFilePath_Implementation() const -{ - return LuaFilePath; -} - -void ULuaInstancedActorComponent::RegistLuaTick(float TickInterval) -{ - EnableLuaTick = true; - auto state = NS_SLUA::LuaState::get(); - state->registLuaTick(this, TickInterval); -} - -void ULuaInstancedActorComponent::UnRegistLuaTick() -{ - auto state = NS_SLUA::LuaState::get(); - state->unRegistLuaTick(this); -} - -void ULuaInstancedActorComponent::PostInitProperties() -{ - Super::PostInitProperties(); - TryHook(); -} - -void ULuaInstancedActorComponent::PostDuplicate(bool bDuplicateForPIE) -{ - Super::PostDuplicate(bDuplicateForPIE); - - // if place Actor in Level, PostDuplicate will be called when open Level in editor. - // if no LuaState exist, maybe not playing. - NS_SLUA::LuaState* State = NS_SLUA::LuaState::get(); - if (!State) - { - return; - } - if (HasAnyFlags(RF_ArchetypeObject)) - { - return; - } - if (LuaFilePath.IsEmpty()) - { - return; - } - - UE_LOG(Slua, Log, TEXT("ULuaInstancedActorComponent::hookObject %s"), *GetFName().ToString()); - NS_SLUA::LuaState::hookObject(nullptr, this, false, false, true); -} \ No newline at end of file diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaLevelScriptActor.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaLevelScriptActor.cpp index 37f25fec..3cf41243 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaLevelScriptActor.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaLevelScriptActor.cpp @@ -24,7 +24,7 @@ void ALuaLevelScriptActor::onLuaStateInit(NS_SLUA::lua_State* L) { if (!HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) { - NS_SLUA::LuaState::hookObject(nullptr, this); + NS_SLUA::LuaState::hookObject(nullptr, this, true); } } diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverrider.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverrider.cpp index a31a04b9..6cd76495 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverrider.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverrider.cpp @@ -12,7 +12,6 @@ #include "LuaOverriderInterface.h" #include "LuaOverriderSuper.h" #include "Kismet/BlueprintFunctionLibrary.h" -#include "LuaInstancedActorComponent.h" #include "Engine/GameEngine.h" #include "Engine/NetDriver.h" #include "GameFramework/InputSettings.h" @@ -532,7 +531,7 @@ namespace NS_SLUA if (!obj->HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) { - tryHook(obj, false, false); + tryHook(obj, false); } // Process UInputComponent @@ -582,9 +581,7 @@ namespace NS_SLUA { if (overridedClasses.Contains(cls)) { -#if WITH_EDITOR - removeOneOverride(cls); -#endif + removeOneOverride(cls, true); overridedClasses.Remove(cls); } @@ -592,13 +589,13 @@ namespace NS_SLUA } } - bool LuaOverrider::tryHook(const UObjectBaseUtility* obj, bool bIsPostLoad/* = false*/, bool bCDOLua/* = true*/, bool bHookInstancedObj/* = false*/) + bool LuaOverrider::tryHook(const UObjectBaseUtility* obj, bool bHookImmediate/* = false*/, bool bPostLoad/* = false*/) { if (isHookable(obj)) { - if (IsInGameThread() && !bIsPostLoad) + if (IsInGameThread() && !bPostLoad) { - if (!obj->HasAnyFlags(RF_NeedPostLoad) || bHookInstancedObj) + if (!obj->HasAnyFlags(RF_NeedPostLoad) || bHookImmediate) { //NS_SLUA::Log::Log("LuaOverrider::NotifyUObjectCreated %s", TCHAR_TO_UTF8(*obj->GetFName().ToString())); UGameInstance* gameInstance = LuaState::getObjectGameInstance((UObject*)obj); @@ -608,9 +605,9 @@ namespace NS_SLUA } UClass* cls = obj->GetClass(); - if (bHookInstancedObj) + if (bHookImmediate) { - bindOverrideFuncs(obj, cls, bHookInstancedObj); + bindOverrideFuncs(obj, cls); return true; } @@ -626,7 +623,7 @@ namespace NS_SLUA } FScopeLock lock(&asyncLoadedObjectCS); - asyncLoadedObjects.Add(AsyncLoadedObject{ (UObject*)obj, bCDOLua }); + asyncLoadedObjects.Add(AsyncLoadedObject{ (UObject*)obj }); } return false; @@ -661,7 +658,7 @@ namespace NS_SLUA FUObjectThreadContext& ThreadContext = FUObjectThreadContext::Get(); auto lastConstructedObject = ThreadContext.ConstructedObject; - // set InstanceGraph(in UE4)/SubobjectOverrides(in UE5)ˇ˘ComponentInits to initialize to avoid destructor twice + // set InstanceGraph(in UE4)/SubobjectOverrides(in UE5) and ComponentInits to initialize to avoid destructor twice new (&ObjectInitializerProxy) FObjectInitializer(); ThreadContext.PopInitializer(); @@ -698,13 +695,18 @@ namespace NS_SLUA #endif } + bool bActorComponent = Cast(obj) != nullptr; + auto tempClassHookLinker = currentHook; while (tempClassHookLinker->obj == obj || tempClassHookLinker->cls == cls) { ensure(tempClassHookLinker->cls == cls); auto overrider = tempClassHookLinker->overrider; auto currentLinker = tempClassHookLinker; - overrider->bindOverrideFuncs(obj, cls, false); + if (!bActorComponent) + { + overrider->bindOverrideFuncs(obj, cls); + } tempClassHookLinker = tempClassHookLinker->pre; if (currentLinker == currentHook) @@ -721,24 +723,13 @@ namespace NS_SLUA cls->ClassConstructor = clsConstructor; } -#if WITH_EDITOR - void clearSuperFuncCache(UClass* cls) - { - if (!IsValid(cls)) - { - return; - } - cls->ClearFunctionMapsCaches(); - clearSuperFuncCache(cls->GetSuperClass()); - } - - void LuaOverrider::removeOneOverride(UClass* cls) + void LuaOverrider::removeOneOverride(UClass* cls, bool bObjectDeleted) { auto &duplicatedFuncs = overridedClasses.FindChecked(cls); auto ProcessFunc = [cls, this, &duplicatedFuncs](UFunction* func) { if (!func || !(func->FunctionFlags & OverrideFuncFlags)) return; - +#if WITH_EDITOR if (luaNet->luaRPCFuncs.Contains(func)) { for (auto FieldAddress = &cls->Children; (FieldAddress && *FieldAddress); FieldAddress = &((*FieldAddress)->Next)) @@ -758,6 +749,7 @@ namespace NS_SLUA func->RemoveFromRoot(); } else +#endif { if (duplicatedFuncs.Contains(func)) { @@ -773,6 +765,8 @@ namespace NS_SLUA { func->Script.RemoveAt(0, CodeSize, false); } + +#if WITH_EDITOR // func hooked by SetNativeFunc if (func->GetNativeFunc() == (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc) { @@ -782,37 +776,53 @@ namespace NS_SLUA if (!nativeFunc) return; func->SetNativeFunc(*nativeFunc); } +#endif } }; - if (auto funcNames = classHookedFuncNames.Find(cls)) + if (!bObjectDeleted) { - for (const FName& funcName : *funcNames) + if (auto funcNames = classHookedFuncNames.Find(cls)) { - UFunction* supercallFunc = cls->FindFunctionByName(FName(*(SUPER_CALL_FUNC_NAME_PREFIX + funcName.ToString()))); - ProcessFunc(supercallFunc); - UFunction* func = cls->FindFunctionByName(funcName); - ProcessFunc(func); + for (const FName& funcName : *funcNames) + { + UFunction* supercallFunc = cls->FindFunctionByName(FName(*(SUPER_CALL_FUNC_NAME_PREFIX + funcName.ToString()))); + ProcessFunc(supercallFunc); + UFunction* func = cls->FindFunctionByName(funcName); + ProcessFunc(func); + } } } duplicatedFuncs.Empty(); classHookedFuncNames.Remove(cls); +#if WITH_EDITOR cacheNativeFuncs.Remove(cls); +#endif if (NS_SLUA::LuaNet::classLuaReplicatedMap.Contains(cls)) - { + { + auto &classLuaReplicated = NS_SLUA::LuaNet::classLuaReplicatedMap.FindChecked(cls); + if (classLuaReplicated.ustruct.IsValid()) { - auto &classLuaReplicated = NS_SLUA::LuaNet::classLuaReplicatedMap.FindChecked(cls); - if (classLuaReplicated.ustruct.IsValid()) - { - classLuaReplicated.ustruct->RemoveFromRoot(); - } + classLuaReplicated.ustruct->RemoveFromRoot(); } + NS_SLUA::LuaNet::classLuaReplicatedMap.Remove(cls); } } +#if WITH_EDITOR + void clearSuperFuncCache(UClass* cls) + { + if (!IsValid(cls)) + { + return; + } + cls->ClearFunctionMapsCaches(); + clearSuperFuncCache(cls->GetSuperClass()); + } + void LuaOverrider::removeOverrides() { for (auto iter = overridedClasses.CreateIterator(); iter; ++iter) @@ -820,29 +830,10 @@ namespace NS_SLUA UClass* OverrideClass = iter.Key(); if (!OverrideClass) continue; - removeOneOverride(OverrideClass); + removeOneOverride(OverrideClass, false); clearSuperFuncCache(OverrideClass); } - for (auto luaRPC : luaNet->luaRPCFuncs) - { - if (UClass* cls = Cast(luaRPC->GetOuter())) - { - for (auto fieldAddress = &cls->Children; (fieldAddress && *fieldAddress); fieldAddress = &((*fieldAddress)->Next)) - { - if (*fieldAddress == luaRPC) - { - *fieldAddress = luaRPC->Next; - luaRPC->Next = nullptr; - break; - } - } - cls->RemoveFunctionFromFunctionMap(luaRPC); - cls->NetFields.Remove(luaRPC); - luaRPC->RemoveFromRoot(); - } - } - luaNet->luaRPCFuncs.Empty(); NS_SLUA::LuaNet::addedRPCClasses.Empty(); @@ -873,8 +864,8 @@ namespace NS_SLUA UGameInstance* gameInstance = LuaState::getObjectGameInstance(obj); if (!gameInstance || gameInstance == sluaState->getGameInstance()) { - UClass* cls = objInfo.obj->GetClass(); - bindOverrideFuncs(objInfo.obj, cls, true); + UClass* cls = obj->GetClass(); + bindOverrideFuncs(obj, cls); } } else @@ -1044,7 +1035,7 @@ namespace NS_SLUA return newFunc; } - bool LuaOverrider::bindOverrideFuncs(const UObjectBase* objBase, UClass* cls, bool bHookInstancedObj) { + bool LuaOverrider::bindOverrideFuncs(const UObjectBase* objBase, UClass* cls) { SCOPE_CYCLE_COUNTER(STAT_LuaOverrider_bindOverrideFuncs); //UE_LOG(Slua, Log, TEXT("LuaOverrider::BindOverrideFuncs %s"), *objBase->GetFName().ToString()); @@ -1058,23 +1049,7 @@ namespace NS_SLUA return true; } - if (!bHookInstancedObj && cls->ImplementsInterface(UInstancedLuaInterface::StaticClass())) - { - //NS_SLUA::Log::Log("LuaOverrider::BindOverrideFuncs Delay Hook for Instanced Obj %s", TCHAR_TO_UTF8(*obj->GetFName().ToString())); - auto objPtr = sluaState->delayHookStateMap.Find(obj); - if (objPtr) - { - (*objPtr).Add(sluaState); - } - else - { - TArray arr; - arr.Add(sluaState); - sluaState->delayHookStateMap.Add(obj, arr); - } - return false; - } - + bool bHookInstancedObj; FString luaFilePath = getLuaFilePath(obj, cls, false, bHookInstancedObj); if (luaFilePath.IsEmpty()) { //NS_SLUA::Log::Log("LuaOverrider::BindOverrideFuncs LuaFilePath empty of Object[%s]", TCHAR_TO_UTF8(*(obj->GetFName().ToString()))); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverriderInterface.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverriderInterface.cpp index e7122aee..910018bc 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverriderInterface.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverriderInterface.cpp @@ -1,63 +1,6 @@ #include "LuaOverriderInterface.h" #include "LuaOverrider.h" -UInstancedLuaInterface::UInstancedLuaInterface(const class FObjectInitializer& ObjectInitializer) - : Super(ObjectInitializer) -{ -} - -void ILuaOverriderInterface::TryHook() -{ - if (bTryHookSuccess) - { - return; - } - // if no LuaState exist, maybe not playing. - NS_SLUA::LuaState* state = NS_SLUA::LuaState::get(); - if (!state) - { - return; - } - UObject* obj = Cast(this); - if (!obj) - { - return; - } - if (obj->HasAnyFlags(RF_ArchetypeObject)) - { - return; - } - bool bHookInstancedObj; - const FString luaFilePath = NS_SLUA::LuaOverrider::getLuaFilePath(obj, obj->GetClass(), false, bHookInstancedObj); - if (luaFilePath.IsEmpty()) - { - return; - } - UE_LOG(Slua, Log, TEXT("ILuaOverriderInterface::TryHook %s"), *obj->GetFName().ToString()); - NS_SLUA::LuaState::hookObject(nullptr, obj, false, false, true); - bTryHookSuccess = true; -} - -void ILuaOverriderInterface::TryHookActorComponents() -{ - AActor* self = Cast(this); - if (nullptr == self) - { - return; - } - - for (UActorComponent* component : self->GetComponents()) - { - if (component && component->GetClass() && component->GetClass()->ImplementsInterface(UInstancedLuaInterface::StaticClass())) - { - if (ILuaOverriderInterface* luaInterface = Cast(component)) - { - luaInterface->TryHook(); - } - } - } -} - ULuaOverriderInterface::ULuaOverriderInterface(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { @@ -76,24 +19,23 @@ NS_SLUA::LuaVar ILuaOverriderInterface::GetSelfTable() const } } -bool ILuaOverriderInterface::CallReceivePreRep(const FString& LuaFilePath) +void ILuaOverriderInterface::PostLuaHook() { - if (!LuaFilePath.IsEmpty()) - { - NS_SLUA::LuaVar selfTable = GetSelfTable(); - if (!selfTable.isValid() || !selfTable.isTable()) - { - return false; - } - static FString ReceivePreRepFunction = TEXT("ReceivePreRep"); - NS_SLUA::LuaVar selfLuaFunc = selfTable.getFromTable(ReceivePreRepFunction, false); - if (!selfLuaFunc.isValid() || !selfLuaFunc.isFunction()) - { - return false; - } + static FString PostConstructFunction = TEXT("_PostConstruct"); + CallLuaFunctionIfExist(PostConstructFunction); +} - CallLuaFunctionIfExist(ReceivePreRepFunction); - return true; +void ILuaOverriderInterface::TryHook() +{ + UObject* obj = Cast(this); + if (!obj) + { + return; + } + if (obj->HasAnyFlags(RF_ArchetypeObject)) + { + return; } - return false; + + NS_SLUA::LuaState::hookObject(nullptr, obj, true); } diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerController.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerController.cpp index f3766e1f..1e4d98fd 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerController.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerController.cpp @@ -11,14 +11,6 @@ FString ALuaPlayerController::GetLuaFilePath_Implementation() const return LuaFilePath; } -void ALuaPlayerController::PostInitializeComponents() -{ - Super::PostInitializeComponents(); - - CallReceivePreRep(LuaFilePath); - TryHookActorComponents(); -} - void ALuaPlayerController::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerState.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerState.cpp index 7aa2d914..2863f44e 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerState.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaPlayerState.cpp @@ -11,14 +11,6 @@ FString ALuaPlayerState::GetLuaFilePath_Implementation() const return LuaFilePath; } -void ALuaPlayerState::PostInitializeComponents() -{ - Super::PostInitializeComponents(); - - CallReceivePreRep(LuaFilePath); - TryHookActorComponents(); -} - void ALuaPlayerState::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaSimulate.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaSimulate.cpp index 56004f31..f6ba0d83 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaSimulate.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaSimulate.cpp @@ -70,7 +70,7 @@ namespace NS_SLUA { if (!Obj->HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) { StartSimulateLua(); - NS_SLUA::LuaState::hookObject(SluaState, Obj); + NS_SLUA::LuaState::hookObject(SluaState, Obj, true); SimulatingObj = Obj; } } diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaState.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaState.cpp index efaaa99e..7f869ba3 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaState.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaState.cpp @@ -42,9 +42,6 @@ #include "luasocket/luasocket.h" namespace NS_SLUA { - - LuaState::DelayHookMap LuaState::delayHookStateMap; - FLuaStateInitEvent LuaState::onInitEvent; const int MaxLuaExecTime = 60; // in second @@ -457,15 +454,15 @@ namespace NS_SLUA { } } - void LuaState::callLuaTick(UObject* obj, NS_SLUA::LuaVar& tickFunc, float dtime) { + void LuaState::callLuaTick(UObject* obj, LuaVar& tickFunc, float dtime) { ILuaOverriderInterface* overrideInterface = Cast(obj); if (!overrideInterface) { Log::Error("callLuaTick cast fail: %s. if obj implement ILuaOverriderInterface in BP, change to c++ instead.", TCHAR_TO_UTF8(*obj->GetName())); return; } - NS_SLUA::LuaVar self = overrideInterface->GetSelfTable(); + LuaVar self = overrideInterface->GetSelfTable(); if (!tickFunc.isFunction()) { - NS_SLUA::LuaVar tick = self.getFromTable("LuaTick"); + LuaVar tick = self.getFromTable("LuaTick"); if (tick.isFunction()) { tickFunc = tick; } @@ -963,47 +960,27 @@ namespace NS_SLUA { return latentDelegate; } - bool LuaState::hookObject(LuaState* inState, const UObjectBaseUtility* obj, bool bIsPostLoad/* = true*/, bool bCDOLua/* = true*/, bool bHookInstancedObj/* = false*/) + bool LuaState::hookObject(LuaState* inState, const UObjectBaseUtility* obj, bool bHookImmediate/* = false*/, bool bPostLoad/* = false*/) { - auto hook = [&](NS_SLUA::LuaState* state) + auto hook = [&](LuaState* state) { if (!state || !state->overrider) { return false; } - return state->overrider->tryHook(obj, bIsPostLoad, bCDOLua, bHookInstancedObj); + return state->overrider->tryHook(obj, bHookImmediate, bPostLoad); }; -#if WITH_EDITOR - auto luaStateArrPtr = delayHookStateMap.Find((UObject*)obj); - if (luaStateArrPtr) - { - for (int i = 0; i < (*luaStateArrPtr).Num(); ++i) + LuaState* state = inState; + if (!state) { + auto* gameInstance = getObjectGameInstance((UObject*)obj); + state = gameInstance ? get(gameInstance) : nullptr; + if (!state) { - NS_SLUA::LuaState* state = (*luaStateArrPtr)[i]; - hook(state); + state = get(); } - delayHookStateMap.Remove((UObject*)obj); - return true; } - else - { - NS_SLUA::LuaState* state = inState; - if (!state) { - auto* gameInstance = NS_SLUA::LuaState::getObjectGameInstance((UObject*)obj); - state = gameInstance ? NS_SLUA::LuaState::get(gameInstance) : nullptr; - if (!state) - { - state = NS_SLUA::LuaState::get(); - } - } - - return hook(state); - } -#else - NS_SLUA::LuaState* state = NS_SLUA::LuaState::get(); - hook(state); - return true; -#endif + + return hook(state); } #if UE_BUILD_DEVELOPMENT diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaUEObject.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaUEObject.cpp index 61e1e977..3c5527be 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/LuaUEObject.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/LuaUEObject.cpp @@ -9,26 +9,4 @@ ULuaObject::ULuaObject(const FObjectInitializer& ObjectInitializer) FString ULuaObject::GetLuaFilePath_Implementation() const { return LuaFilePath; -} - -void ULuaObject::PostInitProperties() -{ - Super::PostInitProperties(); - TryHook(); -} - -void ULuaObject::PostDuplicate(bool bDuplicateForPIE) -{ - Super::PostDuplicate(bDuplicateForPIE); - - if (HasAnyFlags(RF_ArchetypeObject)) - { - return; - } - if (LuaFilePath.IsEmpty()) - { - return; - } - - NS_SLUA::LuaState::hookObject(nullptr, this, false, false, true); } \ No newline at end of file diff --git a/Plugins/slua_unreal/Source/slua_unreal/Private/SluaLib.cpp b/Plugins/slua_unreal/Source/slua_unreal/Private/SluaLib.cpp index f932b9e0..7d859bf9 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Private/SluaLib.cpp +++ b/Plugins/slua_unreal/Source/slua_unreal/Private/SluaLib.cpp @@ -264,6 +264,13 @@ namespace NS_SLUA { int index = 1; UClass* WidgetClass = UUserWidget::StaticClass(); for (auto& it : map) { + GenericUserData* userData = it.Value; + // Only stastics object with UD_REFERENCE flag + if (userData && !(userData->flag & UD_REFERENCE)) + { + continue; + } + UObject* obj = it.Key; UClass* cls = obj->GetClass(); if (cls && cls->IsValidLowLevel() && (cls == WidgetClass || cls->IsChildOf(WidgetClass))) { diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActor.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActor.h index eac3b7a9..6286fd79 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActor.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActor.h @@ -13,9 +13,11 @@ class SLUA_UNREAL_API ALuaActor : public AActor, public ILuaOverriderInterface GENERATED_UCLASS_BODY() public: - virtual void PostInitializeComponents() override; - virtual FString GetLuaFilePath_Implementation() const override; + void PostInitializeComponents() override; + void PostLuaHook() override + { + } UFUNCTION(Blueprintcallable) void RegistLuaTick(float TickInterval); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActorComponent.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActorComponent.h index c3f5cdf5..bf0beb1d 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActorComponent.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaActorComponent.h @@ -21,7 +21,7 @@ class SLUA_UNREAL_API ULuaActorComponent : public UActorComponent, public ILuaOv UFUNCTION(Blueprintcallable) void UnRegistLuaTick(); - + virtual void InitializeComponent() override; void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaFunctionAccelerator.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaFunctionAccelerator.h index 1c71416e..162f6c23 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaFunctionAccelerator.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaFunctionAccelerator.h @@ -71,9 +71,10 @@ namespace NS_SLUA { struct FCheckerInfo { - bool bLatent; - bool bInit; - bool bCheck; + bool bLatent : 1; + bool bInit : 1; + bool bReference : 1; + bool bCheck : 1; int32 index; int32 offset; FProperty* prop; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameMode.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameMode.h index d57ed1b3..3c01f287 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameMode.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameMode.h @@ -12,8 +12,6 @@ class SLUA_UNREAL_API ALuaGameMode : public AGameMode, public ILuaOverriderInter GENERATED_UCLASS_BODY() public: - virtual void PostInitializeComponents() override; - virtual FString GetLuaFilePath_Implementation() const override; protected: diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameState.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameState.h index 4ce156b0..ba2211bf 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameState.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaGameState.h @@ -12,8 +12,6 @@ class SLUA_UNREAL_API ALuaGameState : public AGameState, public ILuaOverriderInt GENERATED_UCLASS_BODY() public: - virtual void PostInitializeComponents() override; - virtual FString GetLuaFilePath_Implementation() const override; virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaInstancedActorComponent.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaInstancedActorComponent.h deleted file mode 100644 index bc29f258..00000000 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaInstancedActorComponent.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "CoreMinimal.h" -#include "LuaOverriderInterface.h" -#include "Components/ActorComponent.h" -#include "LuaInstancedActorComponent.generated.h" - -UCLASS(BlueprintType, Blueprintable) -class SLUA_UNREAL_API ULuaInstancedActorComponent : public UActorComponent, public ILuaOverriderInterface, public IInstancedLuaInterface -{ - GENERATED_UCLASS_BODY() - -public: - FString GetLuaFilePath_Implementation() const override; - - virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; - - UFUNCTION(Blueprintcallable) - void RegistLuaTick(float TickInterval); - - UFUNCTION(Blueprintcallable) - void UnRegistLuaTick(); - - virtual void PostInitProperties() override; - virtual void PostDuplicate(bool bDuplicateForPIE) override; - -protected: - UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "slua") - FString LuaFilePath; - -private: - bool EnableLuaTick = false; -}; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverrider.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverrider.h index 3e97f3a2..82d652a8 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverrider.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverrider.h @@ -72,7 +72,6 @@ namespace NS_SLUA struct AsyncLoadedObject { UObject* obj; - bool bCDOLua; }; class SLUA_UNREAL_API LuaOverrider @@ -94,14 +93,14 @@ namespace NS_SLUA void NotifyUObjectDeleted(const UObjectBase* Object, int32 Index) override; static bool isHookable(const UObjectBaseUtility* obj); - bool tryHook(const UObjectBaseUtility* obj, bool bIsPostLoad = false, bool bCDOLua = true, bool bHookInstancedObj = false); + bool tryHook(const UObjectBaseUtility* obj, bool bHookImmediate = true, bool bPostLoad = false); static FString getLuaFilePath(UObject* obj, class UClass* cls, bool bCDOLua, bool& bHookInstancedObj); #if WITH_EDITOR ULuaOverrider::ClassNativeMap cacheNativeFuncs; void removeOverrides(); - void removeOneOverride(UClass* cls); #endif + void removeOneOverride(UClass* cls, bool bObjectDeleted); protected: friend class LuaObject; @@ -115,7 +114,7 @@ namespace NS_SLUA void onAsyncLoadingFlushUpdate(); void onEngineGC(); - bool bindOverrideFuncs(const UObjectBase* objBase, UClass* cls, bool bHookInstancedObj); + bool bindOverrideFuncs(const UObjectBase* objBase, UClass* cls); void setmetatable(const LuaVar& luaSelfTable, void* objPtr); bool hookBpScript(UFunction* func, UClass* cls, FNativeFuncPtr hookFunc); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverriderInterface.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverriderInterface.h index a0abd01f..3a5928b1 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverriderInterface.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverriderInterface.h @@ -6,17 +6,6 @@ #include "Kismet/BlueprintFunctionLibrary.h" #include "LuaOverriderInterface.generated.h" -UINTERFACE() -class SLUA_UNREAL_API UInstancedLuaInterface : public UInterface -{ - GENERATED_UINTERFACE_BODY() -}; - -class SLUA_UNREAL_API IInstancedLuaInterface -{ - GENERATED_BODY() -}; - UINTERFACE() class SLUA_UNREAL_API ULuaOverriderInterface : public UInterface { @@ -33,19 +22,10 @@ class SLUA_UNREAL_API ILuaOverriderInterface NS_SLUA::LuaVar GetSelfTable() const; - virtual void PostLuaHook() - { - } - - /** - * Provide the ReceivePreRep interface before OnRep, Initialization sequence: bindOverrideFuncs->ReceivePreRep->OnRep->BeginPlay - */ - bool CallReceivePreRep(const FString& LuaFilePath); + virtual void PostLuaHook(); void TryHook(); - void TryHookActorComponents(); - NS_SLUA::LuaVar GetCachedLuaFunc(NS_SLUA::lua_State* L, const NS_SLUA::LuaVar selfTable, const FString& FunctionName) { #if WITH_EDITOR if (Cast(this)) { @@ -54,7 +34,7 @@ class SLUA_UNREAL_API ILuaOverriderInterface #endif if (!FuncMap.Contains(FunctionName)) { - FuncMap.Add(FunctionName, getFromTableIndex(L, selfTable, FunctionName)); + return FuncMap.Add(FunctionName, getFromTableIndex(L, selfTable, FunctionName)); } return FuncMap[FunctionName]; } @@ -163,8 +143,6 @@ class SLUA_UNREAL_API ILuaOverriderInterface return RET(); } - bool bTryHookSuccess = false; - public: TMap FuncMap; }; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerController.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerController.h index 2b32574b..2f805797 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerController.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerController.h @@ -12,8 +12,6 @@ class SLUA_UNREAL_API ALuaPlayerController : public APlayerController, public IL GENERATED_UCLASS_BODY() public: - virtual void PostInitializeComponents() override; - virtual FString GetLuaFilePath_Implementation() const override; virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerState.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerState.h index 3796cb8e..cfbe6ffd 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerState.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaPlayerState.h @@ -12,8 +12,6 @@ class SLUA_UNREAL_API ALuaPlayerState : public APlayerState, public ILuaOverride GENERATED_UCLASS_BODY() public: - virtual void PostInitializeComponents() override; - virtual FString GetLuaFilePath_Implementation() const override; virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaState.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaState.h index 56717aa6..4b926e6d 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaState.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaState.h @@ -237,7 +237,7 @@ namespace NS_SLUA { // call this function on script error void onError(const char* err); - static bool hookObject(LuaState* inState, const class UObjectBaseUtility* obj, bool bIsPostLoad = false, bool bCDOLua = true, bool bHookInstancedObj = false); + static bool hookObject(LuaState* inState, const class UObjectBaseUtility* obj, bool bHookImmediate = true, bool bPostHook = false); #if UE_BUILD_DEVELOPMENT void addRefTraceback(int ref); diff --git a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaUEObject.h b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaUEObject.h index 5db1f43f..dbb05d9e 100644 --- a/Plugins/slua_unreal/Source/slua_unreal/Public/LuaUEObject.h +++ b/Plugins/slua_unreal/Source/slua_unreal/Public/LuaUEObject.h @@ -5,16 +5,13 @@ #include "LuaUEObject.generated.h" UCLASS(BlueprintType, Blueprintable) -class SLUA_UNREAL_API ULuaObject : public UObject, public ILuaOverriderInterface, public IInstancedLuaInterface +class SLUA_UNREAL_API ULuaObject : public UObject, public ILuaOverriderInterface { GENERATED_UCLASS_BODY() public: FString GetLuaFilePath_Implementation() const override; - virtual void PostInitProperties() override; - void PostDuplicate(bool bDuplicateForPIE) override; - protected: UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "slua") FString LuaFilePath;