Skip to content

Commit

Permalink
fix weird behavior when calling RPC functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rowechien committed Mar 8, 2020
1 parent 9a44a59 commit cc10354
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 74 deletions.
6 changes: 5 additions & 1 deletion Plugins/UnLua/Source/UnLua/Private/LuaFunctionInjection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ void RemoveUFunction(UFunction *Function, UClass *OuterClass)
*/
void OverrideUFunction(UFunction *Function, FNativeFuncPtr NativeFunc, void *Userdata, bool bInsertOpcodes)
{
Function->SetNativeFunc(NativeFunc);
if (Function->HasAnyFunctionFlags(FUNC_Native))
{
Function->SetNativeFunc(NativeFunc);
}

if (Function->Script.Num() < 1)
{
#if UE_BUILD_SHIPPING || UE_BUILD_TEST
Expand Down
19 changes: 10 additions & 9 deletions Plugins/UnLua/Source/UnLua/Private/UEReflectionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,15 @@ int32 FFunctionDesc::CallUE(lua_State *L, int32 NumParams, void *Userdata)
return 0;
}

#if SUPPORTS_RPC_CALL
int32 Callspace = Object->GetFunctionCallspace(Function, nullptr);
bool bRemote = Callspace & FunctionCallspace::Remote;
#else
bool bRemote = false;
#endif

void *Params = PreCall(L, NumParams, FirstParamIndex, Userdata, bRemote); // prepare values of properties

UFunction *FinalFunction = Function;
if (bInterfaceFunc)
{
Expand All @@ -1167,7 +1176,7 @@ int32 FFunctionDesc::CallUE(lua_State *L, int32 NumParams, void *Userdata)
#if ENABLE_CALL_OVERRIDDEN_FUNCTION
else
{
if (Function->HasAnyFunctionFlags(FUNC_BlueprintEvent))
if (Function->HasAnyFunctionFlags(FUNC_BlueprintEvent) && !bRemote)
{
UFunction *OverriddenFunc = GReflectionRegistry.FindOverriddenFunction(Function);
if (OverriddenFunc)
Expand All @@ -1178,14 +1187,6 @@ int32 FFunctionDesc::CallUE(lua_State *L, int32 NumParams, void *Userdata)
}
#endif

bool bRemote = false;
#if SUPPORTS_RPC_CALL
int32 Callspace = Object->GetFunctionCallspace(FinalFunction, nullptr);
bRemote = Callspace & FunctionCallspace::Remote;
#endif

void *Params = PreCall(L, NumParams, FirstParamIndex, Userdata, bRemote); // prepare values of properties

// call the UFuncton...
#if !SUPPORTS_RPC_CALL && !WITH_EDITOR
if (FinalFunction->HasAnyFunctionFlags(FUNC_Native))
Expand Down
62 changes: 2 additions & 60 deletions Plugins/UnLua/Source/UnLua/Private/UnLuaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ void UUnLuaManager::Cleanup(UWorld *InWorld, bool bFullCleanup)
AttachedObjects.Empty();
}
AttachedActors.Empty();
ActorsWithoutWorld.Empty();
}

ModuleNames.Empty();
Expand Down Expand Up @@ -494,29 +493,6 @@ void UUnLuaManager::OnMapLoaded(UWorld *World)
}

ENetMode NetMode = World->GetNetMode();
#if SUPPORTS_RPC_CALL
for (AActor *Actor : ActorsWithoutWorld)
{
UClass *Class = GetTargetClass(Actor->GetClass());
FString *ModuleName = ModuleNames.Find(Class);
check(ModuleName);
TSet<FName> *LuaFunctionsPtr = ModuleFunctions.Find(*ModuleName);
TMap<FName, UFunction*> *UEFunctionsPtr = OverridableFunctions.Find(Class);
check(LuaFunctionsPtr && UEFunctionsPtr);
for (const FName &LuaFuncName : (*LuaFunctionsPtr))
{
UFunction **Func = UEFunctionsPtr->Find(LuaFuncName);
if (Func)
{
UFunction *Function = *Func;
if ((Function->HasAnyFunctionFlags(FUNC_NetClient) && NetMode == NM_Client) || (Function->HasAnyFunctionFlags(FUNC_NetServer) && (NetMode == NM_DedicatedServer || NetMode == NM_ListenServer)))
{
OverrideFunction(Function, Class, LuaFuncName);
}
}
}
}
#endif
if (NetMode == NM_DedicatedServer)
{
return;
Expand Down Expand Up @@ -659,8 +635,7 @@ bool UUnLuaManager::BindInternal(UObjectBaseUtility *Object, UClass *Class, cons
TMap<FName, UFunction*> &UEFunctions = OverridableFunctions.Add(Class);
GetOverridableFunctions(Class, UEFunctions); // get all overridable UFunctions

ENetMode NetMode = CheckObjectNetMode(Object, Class, bNewCreated);
OverrideFunctions(LuaFunctions, UEFunctions, Class, bNewCreated, NetMode); // try to override UFunctions
OverrideFunctions(LuaFunctions, UEFunctions, Class, bNewCreated); // try to override UFunctions

return ConditionalUpdateClass(Class, LuaFunctions, UEFunctions);
}
Expand Down Expand Up @@ -726,50 +701,17 @@ bool UUnLuaManager::ConditionalUpdateClass(UClass *Class, const TSet<FName> &Lua
return true;
}

/**
* Check net mode of the UObject
*/
ENetMode UUnLuaManager::CheckObjectNetMode(UObjectBaseUtility *Object, UClass *Class, bool bNewCreated)
{
ENetMode NetMode = NM_Standalone;
#if SUPPORTS_RPC_CALL
if (bNewCreated)
{
if (Class->IsChildOf<AActor>() && !Object->HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject) && !Object->GetOuter()->HasAnyFlags(RF_BeginDestroyed) && !Object->GetOuter()->IsUnreachable())
{
ULevel *Level = Object->GetTypedOuter<ULevel>();
NetMode = Level && Level->OwningWorld ? Level->OwningWorld->GetNetMode() : NM_MAX;
}
if (NetMode == NM_MAX)
{
ActorsWithoutWorld.Add((AActor*)Object);
}
}
#endif
return NetMode;
}

/**
* Override candidate UFunctions
*/
void UUnLuaManager::OverrideFunctions(const TSet<FName> &LuaFunctions, TMap<FName, UFunction*> &UEFunctions, UClass *OuterClass, bool bCheckFuncNetMode, ENetMode NetMode)
void UUnLuaManager::OverrideFunctions(const TSet<FName> &LuaFunctions, TMap<FName, UFunction*> &UEFunctions, UClass *OuterClass, bool bCheckFuncNetMode)
{
for (const FName &LuaFuncName : LuaFunctions)
{
UFunction **Func = UEFunctions.Find(LuaFuncName);
if (Func)
{
UFunction *Function = *Func;
#if SUPPORTS_RPC_CALL
if (bCheckFuncNetMode)
{
if ((Function->HasAnyFunctionFlags(FUNC_NetClient) && (NetMode == NM_DedicatedServer || NetMode == NM_ListenServer || NetMode == NM_MAX)) ||
(Function->HasAnyFunctionFlags(FUNC_NetServer) && (NetMode == NM_Client || NetMode == NM_MAX)))
{
continue;
}
}
#endif
OverrideFunction(Function, OuterClass, LuaFuncName);
}
}
Expand Down
5 changes: 1 addition & 4 deletions Plugins/UnLua/Source/UnLua/Private/UnLuaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ class UUnLuaManager : public UObject
bool BindSurvivalObject(struct lua_State *L, UObjectBaseUtility *Object, UClass *Class, const char *ModuleName);
bool ConditionalUpdateClass(UClass *Class, const TSet<FName> &LuaFunctions, TMap<FName, UFunction*> &UEFunctions);

ENetMode CheckObjectNetMode(UObjectBaseUtility *Object, UClass *Class, bool bNewCreated);

void OverrideFunctions(const TSet<FName> &LuaFunctions, TMap<FName, UFunction*> &UEFunctions, UClass *OuterClass, bool bCheckFuncNetMode = false, ENetMode NetMode = NM_Standalone);
void OverrideFunctions(const TSet<FName> &LuaFunctions, TMap<FName, UFunction*> &UEFunctions, UClass *OuterClass, bool bCheckFuncNetMode = false);
void OverrideFunction(UFunction *TemplateFunction, UClass *OuterClass, FName NewFuncName);
void AddFunction(UFunction *TemplateFunction, UClass *OuterClass, FName NewFuncName);
void ReplaceFunction(UFunction *TemplateFunction, UClass *OuterClass);
Expand Down Expand Up @@ -124,7 +122,6 @@ class UUnLuaManager : public UObject

TMap<UObjectBaseUtility*, int32> AttachedObjects;
TSet<AActor*> AttachedActors;
TSet<AActor*> ActorsWithoutWorld;

UFunction *InputActionFunc;
UFunction *InputAxisFunc;
Expand Down

0 comments on commit cc10354

Please sign in to comment.