Skip to content

Commit

Permalink
Merge pull request #526 from zjhongxian/master
Browse files Browse the repository at this point in the history
fixed bugs
  • Loading branch information
zjhongxian authored Jun 9, 2023
2 parents 71dcb58 + 34be4e9 commit 762d64a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
62 changes: 50 additions & 12 deletions Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverrider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace NS_SLUA
{
const FString SUPER_CALL_FUNC_NAME_PREFIX("__overrider_");
LuaOverrider::OverridedClassMap LuaOverrider::overridedClasses;
LuaOverrider::ClassHookedFuncNames LuaOverrider::classHookedFuncNames;
}

TMap<NS_SLUA::lua_State*, ULuaOverrider::ObjectTableMap> ULuaOverrider::objectTableMap;
Expand Down Expand Up @@ -88,7 +89,11 @@ void ULuaOverrider::luaOverrideFunc(UObject* Context, FFrame& Stack, RESULT_DECL

returnProperty = func->GetReturnProperty();

#if (ENGINE_MINOR_VERSION<25) && (ENGINE_MAJOR_VERSION==4)
for (auto it = (FProperty*)func->Children; *Stack.Code != EX_EndFunctionParms; it = (FProperty*)it->Next)
#else
for (auto it = CastField<FProperty>(func->ChildProperties); *Stack.Code != EX_EndFunctionParms; it = CastField<FProperty>(it->Next))
#endif
{
Stack.Step(Stack.Object, it->ContainerPtrToValuePtr<uint8>(locals));

Expand Down Expand Up @@ -176,7 +181,11 @@ void ULuaOverrider::luaOverrideFunc(UObject* Context, FFrame& Stack, RESULT_DECL
if (lastOut)
{
func = superFunction;
#if (ENGINE_MINOR_VERSION<25) && (ENGINE_MAJOR_VERSION==4)
for (FProperty* prop = (FProperty*)superFunction->Children; prop && (prop->PropertyFlags & (CPF_Parm)) == CPF_Parm; prop = (FProperty*)prop->Next)
#else
for (FProperty* prop = CastField<FProperty>(superFunction->ChildProperties); prop && (prop->PropertyFlags & (CPF_Parm)) == CPF_Parm; prop = CastField<FProperty>(prop->Next))
#endif
{
if (prop->HasAnyPropertyFlags(CPF_OutParm))
{
Expand Down Expand Up @@ -471,6 +480,7 @@ namespace NS_SLUA
const char* LuaOverrider::CACHE_NAME = "__cache";
const uint8 LuaOverrider::Code[] = { (uint8)Ex_LuaOverride, EX_Return, EX_Nothing };
const int32 LuaOverrider::CodeSize = sizeof(Code);
FRWLock LuaOverrider::classHookMutex;
LuaOverrider::ClassHookLinker* LuaOverrider::currentHook = new LuaOverrider::ClassHookLinker();

const TCHAR* LuaOverrider::EInputEventNames[] = { TEXT("Pressed"), TEXT("Released"), TEXT("Repeat"), TEXT("DoubleClick"), TEXT("Axis"), TEXT("Max") };
Expand Down Expand Up @@ -625,6 +635,7 @@ namespace NS_SLUA
}

#if USE_CIRCULAR_DEPENDENCY_LOAD_DEFERRING
FRWScopeLock(classHookMutex, SLT_Write);
auto classHookerLink = new ClassHookLinker(this, (UObject*)obj, cls, currentHook);
currentHook = classHookerLink;
return true;
Expand Down Expand Up @@ -652,19 +663,41 @@ namespace NS_SLUA

void LuaOverrider::CustomClassConstructor(const FObjectInitializer& ObjectInitializer)
{
ensure(currentHook->pre != currentHook);

auto obj = ObjectInitializer.GetObj();
auto cls = obj->GetClass();
auto &current = *currentHook;

ensure(currentHook->obj == obj);
UClass::ClassConstructorType clsConstructor;

{
FRWScopeLock(classHookMutex, SLT_ReadOnly);
ensure(currentHook->pre != currentHook);
auto &current = *currentHook;

auto clsConstructor = current.clsConstructor;
// maybe in async thread!
if (currentHook->obj != obj)
{
auto tempHook = currentHook;
while (tempHook->cls != cls && tempHook->next != tempHook)
{
tempHook = tempHook->next;
}
check(tempHook->next != tempHook && !IsInGameThread());
clsConstructor = tempHook->clsConstructor;
}
else
{
clsConstructor = current.clsConstructor;
}
}

ensure(clsConstructor != CustomClassConstructor);
if (clsConstructor != CustomClassConstructor)
{
clsConstructor(ObjectInitializer);
if (!IsInGameThread())
{
return;
}
ObjectInitializer.~FObjectInitializer();

auto &ObjectInitializerProxy = *const_cast<FObjectInitializer*>(&ObjectInitializer);
Expand Down Expand Up @@ -714,6 +747,7 @@ namespace NS_SLUA
actorComponent->bWantsInitializeComponent = true;
}

FRWScopeLock(classHookMutex, SLT_Write);
auto tempClassHookLinker = currentHook;
while (tempClassHookLinker->obj == obj || tempClassHookLinker->cls == cls)
{
Expand Down Expand Up @@ -765,13 +799,13 @@ namespace NS_SLUA
if (!func->IsValidLowLevel()) return;
func->RemoveFromRoot();
}
else
#endif
{
if (duplicatedFuncs.Contains(func))
{
cls->RemoveFunctionFromFunctionMap(func);
func->ConditionalBeginDestroy();
duplicatedFuncs.Remove(func);
return;
}

Expand Down Expand Up @@ -874,18 +908,18 @@ namespace NS_SLUA
while (asyncLoadedObjects.IsValidIndex(curIndex))
{
AsyncLoadedObject& objInfo = asyncLoadedObjects[curIndex];
if (objInfo.obj && !objInfo.obj->HasAnyFlags(RF_NeedPostLoad))
auto obj = objInfo.obj.Get();
if (obj && !obj->HasAnyFlags(RF_NeedPostLoad))
{
// NS_SLUA::Log::Log("LuaOverrider::OnAsyncLoadingFlushUpdate %s", TCHAR_TO_UTF8(*actorInfo.obj->GetFName().ToString()));
auto obj = objInfo.obj;
UGameInstance* gameInstance = LuaState::getObjectGameInstance(obj);
if (!gameInstance || gameInstance == sluaState->getGameInstance())
{
UClass* cls = obj->GetClass();
bindOverrideFuncs(obj, cls);
}
}
else
else if (obj)
{
// need to handle next time
asyncLoadedObjects[newIndex] = objInfo;
Expand Down Expand Up @@ -1511,6 +1545,7 @@ namespace NS_SLUA
void LuaOverrider::overrideActionInputs(AActor* actor, UInputComponent* inputComponent, const TSet<FName>& luaFunctions)
{
UClass *actorClass = actor->GetClass();
auto &duplicatedFuncs = overridedClasses.FindOrAdd(actorClass);

TSet<FName> actionNames;
int32 numActionBindings = inputComponent->GetNumActionBindings();
Expand All @@ -1524,7 +1559,8 @@ namespace NS_SLUA
FName funcName = FName(*FString::Printf(TEXT("%s_%s"), *actionName, EInputEventNames[inputActionBinding.KeyEvent]));
if (luaFunctions.Find(funcName))
{
duplicateUFunction(inputActionFunc, actorClass, funcName, (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc);
auto inputFunc = duplicateUFunction(inputActionFunc, actorClass, funcName, (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc);
duplicatedFuncs.Add(inputFunc);
inputActionBinding.ActionDelegate.BindDelegate(actor, funcName);
}

Expand All @@ -1534,7 +1570,8 @@ namespace NS_SLUA
funcName = FName(*FString::Printf(TEXT("%s_%s"), *actionName, EInputEventNames[inputEvent]));
if (luaFunctions.Find(funcName))
{
duplicateUFunction(inputActionFunc, actorClass, funcName, (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc);
auto inputFunc = duplicateUFunction(inputActionFunc, actorClass, funcName, (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc);
duplicatedFuncs.Add(inputFunc);
FInputActionBinding AB(name, inputEvent);
AB.ActionDelegate.BindDelegate(actor, funcName);
inputComponent->AddActionBinding(AB);
Expand All @@ -1552,7 +1589,8 @@ namespace NS_SLUA
FName funcName = FName(*FString::Printf(TEXT("%s_%s"), *actionName.ToString(), EInputEventNames[InputEvents[i]]));
if (luaFunctions.Find(funcName))
{
duplicateUFunction(inputActionFunc, actorClass, funcName, (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc);
auto inputFunc = duplicateUFunction(inputActionFunc, actorClass, funcName, (FNativeFuncPtr)&ULuaOverrider::luaOverrideFunc);
duplicatedFuncs.Add(inputFunc);
FInputActionBinding inputActionBinding(actionName, InputEvents[i]);
inputActionBinding.ActionDelegate.BindDelegate(actor, funcName);
inputComponent->AddActionBinding(inputActionBinding);
Expand Down
5 changes: 3 additions & 2 deletions Plugins/slua_unreal/Source/slua_unreal/Public/LuaOverrider.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace NS_SLUA

struct AsyncLoadedObject
{
UObject* obj;
TWeakObjectPtr<UObject> obj;
};

class SLUA_UNREAL_API LuaOverrider
Expand Down Expand Up @@ -162,6 +162,7 @@ namespace NS_SLUA
ensure(clsConstructor != CustomClassConstructor);
}
};
static FRWLock classHookMutex;
static ClassHookLinker* currentHook;

static int __index(lua_State* L);
Expand All @@ -173,7 +174,7 @@ namespace NS_SLUA
class LuaNet* luaNet;

typedef TMap<UClass*, TSet<FName>> ClassHookedFuncNames;
ClassHookedFuncNames classHookedFuncNames;
static ClassHookedFuncNames classHookedFuncNames;
typedef TMap<UClass*, TSet<TWeakObjectPtr<UFunction>>> OverridedClassMap;
static OverridedClassMap overridedClasses;

Expand Down

0 comments on commit 762d64a

Please sign in to comment.