Skip to content

Commit

Permalink
Merge pull request #522 from zjhongxian/master
Browse files Browse the repository at this point in the history
Fixes many bugs
  • Loading branch information
zjhongxian authored Jun 2, 2023
2 parents d8cfa38 + 5306365 commit 71dcb58
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Content/Lua/Test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
-- open profile
require 'TestProfile'

--- step gc 1ms in one frame, limit of 1000 times. Just do one gc cycle in 60 seconds.
slua.setGCParam(0.001, 1000, 60)

-- some.field come from c++
some.field.y = 103
EPropertyClass = import"EPropertyClass"
Expand Down
3 changes: 3 additions & 0 deletions Content/Lua/TestProfile.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
if slua_profile then
print"start slua profile"
slua_profile.start("127.0.0.1",8081)

local KismetSystemLibrary = import("KismetSystemLibrary")
KismetSystemLibrary.ExecuteConsoleCommand(slua.getGameInstance(), "slua.StartMemoryTrack")
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@
#include "SluaUtil.h"
#include "LuaProfiler.h"

FAutoConsoleVariableRef CVarSluaProfilerPort(
TEXT("slua.ProfilerPort"),
NS_SLUA::FProfileServer::Port,
TEXT("Slua profiler server port.\n"),
ECVF_Default);

namespace NS_SLUA
{
int32 FProfileServer::Port = 8081;

FProfileServer::FProfileServer()
: Thread(nullptr)
, bStop(true)
Expand All @@ -46,7 +54,7 @@ namespace NS_SLUA
bStop = false;

ListenEndpoint.Address = FIPv4Address(0, 0, 0, 0);
ListenEndpoint.Port = 8081;
ListenEndpoint.Port = (std::numeric_limits<uint16>::min() < Port) && (Port < std::numeric_limits<uint16>::max()) ? Port : 8081;
Listener = new FTcpListener(ListenEndpoint);
Listener->OnConnectionAccepted().BindRaw(this, &FProfileServer::HandleConnectionAccepted);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace NS_SLUA {
FOnProfileMessageDelegate& OnProfileMessageRecv();

TArray<TSharedPtr<FProfileConnection>> GetConnections();

static int32 Port;

protected:
bool Init() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ALuaLevelScriptActor::onLuaStateInit(NS_SLUA::lua_State* L)
{
if (!HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
{
NS_SLUA::LuaState::hookObject(nullptr, this, true);
TryHook();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,17 @@ bool FLuaNetSerialization::Read(FNetDeltaSerializeInfo& deltaParms, FLuaNetSeria
ClassLuaReplicated::ReplicateIndexType index = *It;
if (SerializeVersion)
{
if (preIndex == index)
{
continue;
}

if (!flatProperties.IsValidIndex(index))
{
UE_LOG(Slua, Error, TEXT("FLuaNetSerialization::Read Error: Object[%s]'s flatProperties index[%d] is out of range[%d]!"), *object->GetFullName(), index, flatProperties.Num());
break;
}

index = flatProperties[index].propIndex;
if (preIndex == index)
{
continue;
}
preIndex = index;

proxy->dirtyMark.Add(index);
Expand Down
49 changes: 33 additions & 16 deletions Plugins/slua_unreal/Source/slua_unreal/Private/LuaOverrider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,35 +168,48 @@ void ULuaOverrider::luaOverrideFunc(UObject* Context, FFrame& Stack, RESULT_DECL
if (superFunction)
{
uint8* savedCode = Stack.Code;
auto savedNode = Stack.Node;
auto savedPropertyChain = Stack.PropertyChainForCompiledIn;
uint8* newCode = superFunction->Script.GetData();
if (superFunction->GetNativeFunc() == (NS_SLUA::FNativeFuncPtr)&UObject::ProcessInternal)

FOutParmRec* lastOut = Stack.OutParms;
if (lastOut)
{
if (newCode)
func = superFunction;
for (FProperty* prop = (FProperty*)superFunction->Children; prop && (prop->PropertyFlags & (CPF_Parm)) == CPF_Parm; prop = (FProperty*)prop->Next)
{
FOutParmRec* lastOut = Stack.OutParms;
if (lastOut)
if (prop->HasAnyPropertyFlags(CPF_OutParm))
{
func = superFunction;
for ( FProperty* prop = (FProperty*)superFunction->Children; prop && (prop->PropertyFlags&(CPF_Parm)) == CPF_Parm; prop = (FProperty*)prop->Next )
{
if ( prop->HasAnyPropertyFlags(CPF_OutParm) )
{
// Fixed OutParam Property in Script
lastOut->Property = prop;
lastOut = lastOut->NextOutParm;
}
}
// Fixed OutParam Property in Script
lastOut->Property = prop;
lastOut = lastOut->NextOutParm;
}
}
}
#if (ENGINE_MINOR_VERSION<25) && (ENGINE_MAJOR_VERSION==4)
Stack.PropertyChainForCompiledIn = superFunction->Children;
#else
Stack.PropertyChainForCompiledIn = superFunction->ChildProperties;
#endif

if (superFunction->GetNativeFunc() == (NS_SLUA::FNativeFuncPtr)&UObject::ProcessInternal)
{
if (newCode)
{
Stack.Code = newCode;
Stack.Node = superFunction;
superFunction->Invoke(obj, Stack, RESULT_PARAM);
}
}
else
{
Stack.Code = newCode;
Stack.Node = superFunction;
superFunction->Invoke(obj, Stack, RESULT_PARAM);
}

Stack.PropertyChainForCompiledIn = savedPropertyChain;
Stack.Node = savedNode;
Stack.Code = savedCode;
}
else
Expand Down Expand Up @@ -695,15 +708,19 @@ namespace NS_SLUA
#endif
}

bool bActorComponent = Cast<UActorComponent>(obj) != nullptr;
auto actorComponent = Cast<UActorComponent>(obj);
if (actorComponent)
{
actorComponent->bWantsInitializeComponent = true;
}

auto tempClassHookLinker = currentHook;
while (tempClassHookLinker->obj == obj || tempClassHookLinker->cls == cls)
{
ensure(tempClassHookLinker->cls == cls);
auto overrider = tempClassHookLinker->overrider;
auto currentLinker = tempClassHookLinker;
if (!bActorComponent)
if (!actorComponent)
{
overrider->bindOverrideFuncs(obj, cls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ void ILuaOverriderInterface::TryHook()
{
return;
}
if (obj->HasAnyFlags(RF_ArchetypeObject))
{
return;
}

NS_SLUA::LuaState::hookObject(nullptr, obj, true);
}
7 changes: 6 additions & 1 deletion Plugins/slua_unreal/Source/slua_unreal/Private/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ namespace NS_SLUA {
}

// check lua top , this function can omit
void LuaState::tick(float dtime) {
void LuaState::Tick(float dtime) {
ensure(IsInGameThread());
if (!L) return;
int top = lua_gettop(L);
Expand Down Expand Up @@ -338,6 +338,11 @@ namespace NS_SLUA {
tickLuaActors(dtime);
}

TStatId LuaState::GetStatId() const
{
RETURN_QUICK_DECLARE_CYCLE_STAT(LuaState, STATGROUP_Game);
}

void LuaState::tickGC(float dtime) {
if (stepGCTimeLimit > 0.0) {
QUICK_SCOPE_CYCLE_COUNTER(Lua_StepGC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void FProfileDataProcessRunnable::ClearData()

void FProfileDataProcessRunnable::SaveData()
{
SaveDataWithData(cpuViewBeginIndex, memViewBeginIndex, allProfileData, allLuaMemNodeList, "ModuleSave");
SaveDataWithData(cpuViewBeginIndex, memViewBeginIndex, allProfileData, allLuaMemNodeList, "");
}


Expand All @@ -378,10 +378,10 @@ void FProfileDataProcessRunnable::SaveDataWithData(int CpuViewBeginIndex, int Me
bool tempRecording = bIsRecording;
bIsRecording = false;
FDateTime Now = FDateTime::Now();
FString FilePath = FPaths::ProjectSavedDir()
FString FilePath = FPaths::ProfilingDir() + "/Sluastats/"
/ FString::FromInt(Now.GetYear()) + FString::FromInt(Now.GetMonth()) + FString::FromInt(Now.GetDay())
+ FString::FromInt(Now.GetHour()) + FString::FromInt(Now.GetMinute()) + FString::FromInt(Now.GetSecond())
+ FString::FromInt(Now.GetMillisecond()) + SavePath + "DtataModule.sluastat";
+ FString::FromInt(Now.GetMillisecond()) + SavePath + ".sluastat";
FBufferArchive* BufferArchive = new FBufferArchive();

SerializeSave(BufferArchive, CpuViewBeginIndex, MemViewBeginIndex, ProfileData, LuaMemNodeList);
Expand Down Expand Up @@ -413,7 +413,7 @@ FArchiveSaveCompressedProxy CompressProxyLZ4 = FArchiveSaveCompressedProxy(Compr
BufferArchive->Close();
CompressProxyLZ4.Close();
bIsRecording = tempRecording;
UE_LOG(Slua, Log, TEXT("END SAVE DATA "));
UE_LOG(Slua, Log, TEXT("END SAVE DATA %s"), *FilePath);

}

Expand Down Expand Up @@ -481,15 +481,16 @@ void FProfileDataProcessRunnable::SerializeSave(FBufferArchive* BufferArchive, i
for (auto InnerIter = InnerMap.CreateIterator(); InnerIter; ++InnerIter)
{
*BufferArchive << InnerIter->Key;
InnerIter->Value->Serialize(*BufferArchive);
}
}
TMap<FString, TSharedPtr<FileMemInfo>> ParentFileMap = MemNode->parentFileMap;
int32 ParentFileMapNum = InfoMap.Num();;
*BufferArchive << ParentFileMapNum;
for (auto Iter = ParentFileMap.CreateIterator(); Iter; ++Iter)
{
*BufferArchive << Iter->Key; // FString
//FileMemInfo::StaticStruct()->SerializeBin(*BufferArchive, Iter->Value.Get());
*BufferArchive << Iter->Key;
Iter->Value->Serialize(*BufferArchive);
}
}
int64 BufferSize = BufferArchive->TotalSize();
Expand Down Expand Up @@ -562,7 +563,7 @@ void FProfileDataProcessRunnable::DeserializeCompressedSave(FBufferArchive* BufA
*MemoryReader << InnerMapKey;

FileMemInfo* Info = new FileMemInfo();
//FileMemInfo::StaticStruct()->SerializeBin(*MemoryReader, Info);
Info->Serialize(*MemoryReader);
InnerMap.Add(InnerMapKey, MakeShareable(Info));
}
InfoMap.Add(InfoMapKey, InnerMap);
Expand All @@ -577,7 +578,7 @@ void FProfileDataProcessRunnable::DeserializeCompressedSave(FBufferArchive* BufA
FString ParentFileMapKey;
*MemoryReader << ParentFileMapKey;
FileMemInfo* Info = new FileMemInfo();
//FileMemInfo::StaticStruct()->SerializeBin(*MemoryReader, Info);
Info->Serialize(*MemoryReader);
ParentFileMap.Add(ParentFileMapKey, MakeShareable(Info));
}

Expand Down Expand Up @@ -716,7 +717,6 @@ void FProfileDataProcessRunnable::CollectMemoryNode(TMap<int64, NS_SLUA::LuaMemI
else if (lastLuaMemNode.IsValid())
{
//copy lastLuaMemNode to memNode
// *(memNode.Get()) = *(lastLuaMemNode.Get());
FProflierMemNode& last = *lastLuaMemNode;
FProflierMemNode& current = *memNode;
current.totalSize = last.totalSize;
Expand Down
2 changes: 1 addition & 1 deletion Plugins/slua_unreal/Source/slua_unreal/Public/LuaNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace NS_SLUA
static TMap<FString, EFunctionFlags> luaRPCTypeMap;
static TSet<TWeakObjectPtr<UClass>> addedRPCClasses;
#if WITH_EDITOR
TSet<UFunction*> luaRPCFuncs;
TSet<TWeakObjectPtr<UFunction>> luaRPCFuncs;
#endif
};
}
8 changes: 7 additions & 1 deletion Plugins/slua_unreal/Source/slua_unreal/Public/LuaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "UObject/WeakFieldPtr.h"
#endif
#include "HAL/Runnable.h"
#include "Tickable.h"

#define SLUA_LUACODE "[sluacode]"
#define SLUA_CPPINST "__cppinst"
Expand Down Expand Up @@ -92,6 +93,7 @@ namespace NS_SLUA {
: public FUObjectArray::FUObjectDeleteListener
, public FUObjectArray::FUObjectCreateListener
, public FGCObject
, public FTickableGameObject
{
public:
LuaState(const char* name=nullptr,UGameInstance* pGI=nullptr);
Expand Down Expand Up @@ -143,7 +145,11 @@ namespace NS_SLUA {
void unRegistLuaTick(const UObject* obj);
FORCEINLINE void callLuaTick(UObject* obj, NS_SLUA::LuaVar& tickFunc, float dtime);
// tick function
virtual void tick(float dtime);
virtual void Tick(float dtime);
virtual TStatId GetStatId() const;
#if (ENGINE_MINOR_VERSION<=18) && (ENGINE_MAJOR_VERSION==4)
virtual bool IsTickable() const override { return true; }
#endif
void tickGC(float dtime);
void tickLuaActors(float dtime);

Expand Down

0 comments on commit 71dcb58

Please sign in to comment.