Skip to content

Commit a966709

Browse files
author
laishikai
committed
修改字符串引用(const 引用)
1 parent 5002f5c commit a966709

31 files changed

+181
-162
lines changed

Content/Script/FCUIEntrypoint.lua

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ function FCUIEntrypoint:ReceiveBeginPlay()
99
local objAvatar = obj.AvatarParams
1010
objAvatar.BoneAdjustItem.SlotName = "abc"
1111

12+
local v = FVector(1, 2, 3)
13+
local vecList = TArray("FVector")
14+
vecList:Add(v)
15+
vecList:Add(v)
16+
vecList:Add(v)
17+
18+
local v1 = vecList:Get(1)
19+
v1.X = 100
20+
print("[FCTestScript]v1=", v1, ",[1]:", vecList:Get(1))
21+
1222
local a = TArray("int")
1323
a:Add(1)
1424
a:Add(2)

Content/UnLua/Script/UI/UnluaEntryPointer.lua

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ function UnluaEntryPointer:ReceiveBeginPlay()
1616
UEPrint("[Unlua]UnluaEntryPointer:ReceiveBeginPlay")
1717
local AvatarClass = UClass.Load("UFCTest")
1818
local obj = NewObject(AvatarClass)
19+
obj.ID = 10
20+
local ID = obj.ID
1921

22+
-- local a = TArray(_G.FString)
23+
-- a:Add("aaa")
24+
-- a:Add("aaa")
25+
2026
local map = TMap(_G.int32, _G.int32)
2127
map:Add(1, 10)
2228
map:Add(2, 20)
23-
obj:SetIDMap(map)
24-
obj:SetIDMap(map)
29+
obj:GetIDList(map)
30+
-- obj:SetIDMap(a)
2531

2632
local world = self:GetWorld()
2733
local UIManager = require "UnLua.Script.UI.UIManager"

Plugins/FCScript/Source/FCScript/Private/FCCallScriptFunc.cpp

+43-15
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ void PushScriptFText(lua_State* L, const FCDynamicPropertyBase* DynamicProperty
6969
}
7070
void PushScriptFVector(lua_State* L, const FCDynamicPropertyBase *DynamicProperty, uint8 *ValueAddr, UObject *ThisObj, void* ObjRefPtr)
7171
{
72-
LuaPushValue(L, *((const FVector*)ValueAddr), false);
72+
PushScriptStruct(L, DynamicProperty, ValueAddr, ThisObj, ObjRefPtr);
7373
}
7474
void PushScriptFVector2D(lua_State* L, const FCDynamicPropertyBase *DynamicProperty, uint8 *ValueAddr, UObject *ThisObj, void* ObjRefPtr)
7575
{
76-
LuaPushValue(L, *((const FVector2D*)ValueAddr), false);
76+
PushScriptStruct(L, DynamicProperty, ValueAddr, ThisObj, ObjRefPtr);
7777
}
7878
void PushScriptFVector4D(lua_State* L, const FCDynamicPropertyBase *DynamicProperty, uint8 *ValueAddr, UObject *ThisObj, void* ObjRefPtr)
7979
{
80-
LuaPushValue(L, *((const FVector4*)ValueAddr), false);
80+
PushScriptStruct(L, DynamicProperty, ValueAddr, ThisObj, ObjRefPtr);
8181
}
8282

8383
// 功能:根据脚本变量得到UE对象的描述类
@@ -117,8 +117,8 @@ bool IsCanCastToScript(lua_State* L, int Idx, const FCDynamicPropertyBase *Dyna
117117
return true;
118118
}
119119

120-
const char* Name = GetUEClassName(ClassDesc->m_UEClassName.c_str());
121-
if (DynamicProperty->Name == Name)
120+
const char* Name = GetUEClassName(ClassDesc->m_UEClassName);
121+
if (DynamicProperty->Name == Name || strcmp(DynamicProperty->Name, Name) == 0)
122122
{
123123
return true;
124124
}
@@ -320,13 +320,13 @@ void InitDynamicPropertyWriteFunc(FCDynamicProperty *DynamicProperty, FCPropert
320320
DynamicProperty->m_WriteScriptFunc = PushScriptStruct;
321321
break;
322322
case FCPROPERTY_Vector2:
323-
DynamicProperty->m_WriteScriptFunc = PushScriptFVector2D;
323+
DynamicProperty->m_WriteScriptFunc = PushScriptStruct;
324324
break;
325325
case FCPROPERTY_Vector3:
326-
DynamicProperty->m_WriteScriptFunc = PushScriptFVector;
326+
DynamicProperty->m_WriteScriptFunc = PushScriptStruct;
327327
break;
328328
case FCPROPERTY_Vector4:
329-
DynamicProperty->m_WriteScriptFunc = PushScriptFVector4D;
329+
DynamicProperty->m_WriteScriptFunc = PushScriptStruct;
330330
break;
331331
case FCPROPERTY_Array:
332332
DynamicProperty->m_WriteScriptFunc = PushScriptTArray;
@@ -410,6 +410,34 @@ void ReadScriptFText(lua_State* L, int ValueIdx, const FCDynamicPropertyBase* D
410410
*value = FText::FromString(InStr);
411411
}
412412
// 将脚本对象写入到UE对象
413+
void ReadScriptFVector2D(lua_State* L, int ValueIdx, const FCDynamicPropertyBase* DynamicProperty, uint8* ValueAddr, UObject* ThisObj, void* ObjRefPtr)
414+
{
415+
FStructProperty* StructProperty = (FStructProperty*)DynamicProperty->Property;
416+
FCObjRef* ObjRef = (FCObjRef*)FCScript::GetObjRefPtr(L, ValueIdx);
417+
if (ObjRef && ObjRef->GetPropertyType() == FCPropertyType::FCPROPERTY_Vector2)
418+
{
419+
*((FVector2D*)ValueAddr) = *((const FVector2D*)ObjRef->GetPropertyAddr());
420+
}
421+
}
422+
void ReadScriptFVector(lua_State* L, int ValueIdx, const FCDynamicPropertyBase* DynamicProperty, uint8* ValueAddr, UObject* ThisObj, void* ObjRefPtr)
423+
{
424+
FStructProperty* StructProperty = (FStructProperty*)DynamicProperty->Property;
425+
FCObjRef* ObjRef = (FCObjRef*)FCScript::GetObjRefPtr(L, ValueIdx);
426+
if (ObjRef && ObjRef->GetPropertyType() == FCPropertyType::FCPROPERTY_Vector3)
427+
{
428+
*((FVector*)ValueAddr) = *((const FVector*)ObjRef->GetPropertyAddr());
429+
}
430+
}
431+
void ReadScriptFVector4D(lua_State* L, int ValueIdx, const FCDynamicPropertyBase* DynamicProperty, uint8* ValueAddr, UObject* ThisObj, void* ObjRefPtr)
432+
{
433+
FStructProperty* StructProperty = (FStructProperty*)DynamicProperty->Property;
434+
FCObjRef* ObjRef = (FCObjRef*)FCScript::GetObjRefPtr(L, ValueIdx);
435+
if (ObjRef && ObjRef->GetPropertyType() == FCPropertyType::FCPROPERTY_Vector4)
436+
{
437+
*((FVector4*)ValueAddr) = *((const FVector4*)ObjRef->GetPropertyAddr());
438+
}
439+
}
440+
// 将脚本对象写入到UE对象
413441
void ReadScriptStruct(lua_State* L, int ValueIdx, const FCDynamicPropertyBase *DynamicProperty, uint8 *ValueAddr, UObject *ThisObj, void* ObjRefPtr)
414442
{
415443
FStructProperty* StructProperty = (FStructProperty*)DynamicProperty->Property;
@@ -635,14 +663,14 @@ void InitDynamicPropertyReadFunc(FCDynamicProperty *DynamicProperty, FCProperty
635663
DynamicProperty->m_ReadScriptFunc = ReadScriptStruct;
636664
break;
637665
case FCPROPERTY_Vector2:
638-
DynamicProperty->m_ReadScriptFunc = ReadScriptStruct;
639-
break;
666+
DynamicProperty->m_ReadScriptFunc = ReadScriptFVector2D;
667+
break;
640668
case FCPROPERTY_Vector3:
641-
DynamicProperty->m_ReadScriptFunc = ReadScriptStruct;
642-
break;
669+
DynamicProperty->m_ReadScriptFunc = ReadScriptFVector;
670+
break;
643671
case FCPROPERTY_Vector4:
644-
DynamicProperty->m_ReadScriptFunc = ReadScriptStruct;
645-
break;
672+
DynamicProperty->m_ReadScriptFunc = ReadScriptFVector4D;
673+
break;
646674
case FCPROPERTY_Array:
647675
DynamicProperty->m_ReadScriptFunc = ReadScriptTArray;
648676
break;
@@ -782,7 +810,7 @@ void FCCallScriptDelegate(FCScriptContext *Context, UObject *Object, int64 Scri
782810
FCInnerCallScriptFunc(Context, Object, ScriptIns, DynamicFunction, TheStack, ScriptDelegatePrepareCall, &DelegateInfo);
783811
else
784812
{
785-
UE_LOG(LogFCScript, Error, TEXT("Invalid Script call, FunctionRef = %d : %s"), DelegateInfo.FunctionRef, UTF8_TO_TCHAR(DynamicFunction->Name.c_str()));
813+
UE_LOG(LogFCScript, Error, TEXT("Invalid Script call, FunctionRef = %d : %s"), DelegateInfo.FunctionRef, UTF8_TO_TCHAR(DynamicFunction->Name));
786814
}
787815
int CurIdx = lua_gettop(L);
788816
if (CurIdx > StartIdx)

Plugins/FCScript/Source/FCScript/Private/FCDelegateModule.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ void FFCDelegateModule::TryBindScript(const class UObjectBaseUtility *Object)
322322
}
323323
}
324324

325-
#if (ENGINE_MAJOR_VERSION > 4) || (ENGINE_MINOR_VERSION > 23)
325+
#if OLD_UE_ENGINE == 0
326326
void FFCDelegateModule::OnWorldTickStart(UWorld *World, ELevelTick TickType, float DeltaTime)
327327
#else
328328
void FFCDelegateModule::OnWorldTickStart(ELevelTick TickType, float DeltaTime)

Plugins/FCScript/Source/FCScript/Private/FCDelegateModule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class FFCDelegateModule : public FUObjectArray::FUObjectCreateListener, public F
5454
void Shutdown();
5555
void TryBindScript(const class UObjectBaseUtility *Object);
5656

57-
#if (ENGINE_MAJOR_VERSION > 4) || (ENGINE_MINOR_VERSION > 23)
57+
#if OLD_UE_ENGINE == 0
5858
void OnWorldTickStart(UWorld *World, ELevelTick TickType, float DeltaTime);
5959
#else
6060
void OnWorldTickStart(ELevelTick TickType, float DeltaTime);

Plugins/FCScript/Source/FCScript/Private/FCDynamicClassDesc.cpp

+25-20
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
void FCDynamicProperty::InitProperty(const FProperty *InProperty)
88
{
99
Name = TCHAR_TO_UTF8(*(InProperty->GetName()));
10+
Name = GetConstName(Name);
1011
ElementSize = InProperty->ElementSize;
1112
Offset_Internal = InProperty->GetOffset_ForInternal();
1213
Flags = InProperty->GetPropertyFlags();
1314
Property = InProperty;
1415
bOuter = InProperty->HasAnyPropertyFlags(CPF_OutParm);
1516

1617
Type = GetScriptPropertyType(Property);
17-
GetScriptPropertyClassName(ClassName, Type, InProperty);
18+
ClassName = GetScriptPropertyClassName(Type, InProperty);
1819

1920
InitDynamicPropertyWriteFunc(this, Type);
2021
InitDynamicPropertyReadFunc(this, Type);
@@ -23,6 +24,7 @@ void FCDynamicProperty::InitProperty(const FProperty *InProperty)
2324
void FCDynamicFunction::InitParam(UFunction *InFunction)
2425
{
2526
Name = TCHAR_TO_UTF8(*(InFunction->GetName()));
27+
Name = GetConstName(Name);
2628
Function = InFunction;
2729
ParmsSize = InFunction->ParmsSize;
2830
m_Property.resize(InFunction->NumParms);
@@ -50,7 +52,7 @@ void FCDynamicFunction::InitParam(UFunction *InFunction)
5052
}
5153
if(LatentPropertyIndex == -1)
5254
{
53-
if (FCProperty->Name == "LatentInfo")
55+
if(strcmp(FCProperty->Name, "LatentInfo") == 0)
5456
{
5557
LatentPropertyIndex = Index;
5658
}
@@ -77,7 +79,7 @@ void FCDynamicFunction::InitParam(UFunction *InFunction)
7779
}
7880

7981

80-
#if ENGINE_MINOR_VERSION < 25
82+
#if OLD_UE_ENGINE
8183
struct FFakeProperty : public UField
8284
#else
8385
struct FFakeProperty : public FField
@@ -170,15 +172,15 @@ FCDynamicClassDesc &FCDynamicClassDesc::CopyDesc(const FCDynamicClassDesc &other
170172
{
171173
FCDynamicProperty *FCProperty = new FCDynamicProperty(*(other.m_Property[i]));
172174
m_Property[i] = FCProperty;
173-
m_Name2Property[FCProperty->Name.c_str()] = FCProperty;
174-
m_Fileds[FCProperty->Name.c_str()] = FCProperty;
175+
m_Name2Property[FCProperty->Name] = FCProperty;
176+
m_Fileds[FCProperty->Name] = FCProperty;
175177
}
176178
m_Functions.clear();
177179
for(CDynamicFunctionNameMap::const_iterator it = other.m_Functions.begin(); it != other.m_Functions.end(); ++it)
178180
{
179181
FCDynamicFunction *Func = new FCDynamicFunction(*(it->second));
180-
m_Functions[Func->Name.c_str()] = Func;
181-
m_Fileds[Func->Name.c_str()] = Func;
182+
m_Functions[Func->Name] = Func;
183+
m_Fileds[Func->Name] = Func;
182184
}
183185
m_LibFields.clear();
184186
for (CDynamicFieldNameMap::const_iterator it = other.m_LibFields.begin(); it != other.m_LibFields.end(); ++it)
@@ -198,6 +200,7 @@ void FCDynamicClassDesc::OnRegisterStruct(UStruct *Struct, void *Context)
198200
if(Super)
199201
{
200202
m_SuperName = TCHAR_TO_UTF8(*(Super->GetName()));
203+
m_SuperName = GetConstName(m_SuperName);
201204
//OnRegisterStruct(Super);
202205
}
203206
m_Class = Cast<UClass>(Struct);
@@ -227,7 +230,7 @@ void FCDynamicClassDesc::OnAddStructMember(UStruct* Struct, void* Context)
227230
FCProperty->PropertyIndex = m_Property.size();
228231
FCProperty->bOuter = false;
229232

230-
const char* FieldName = FCProperty->Name.c_str();
233+
const char* FieldName = FCProperty->Name;
231234
m_Property.push_back(FCProperty);
232235
m_Name2Property[FieldName] = FCProperty;
233236
m_Fileds[FieldName] = FCProperty;
@@ -241,7 +244,7 @@ void FCDynamicClassDesc::OnAddStructMember(UStruct* Struct, void* Context)
241244
{
242245
FCDynamicFunction* DynamicFunction = new FCDynamicFunction();
243246
DynamicFunction->InitParam(Function);
244-
const char* FieldName = DynamicFunction->Name.c_str();
247+
const char* FieldName = DynamicFunction->Name;
245248
m_Functions[FieldName] = DynamicFunction;
246249
m_Fileds[FieldName] = DynamicFunction;
247250
}
@@ -278,8 +281,8 @@ FCDynamicFunction* FCDynamicClassDesc::RegisterUEFunc(const char *pcsFuncName)
278281
FCDynamicFunction* DynamicFunction = new FCDynamicFunction();
279282
DynamicFunction->InitParam(Function);
280283
//DynamicFunction->Name = pcsFuncName;
281-
m_Functions[DynamicFunction->Name.c_str()] = DynamicFunction;
282-
m_Fileds[DynamicFunction->Name.c_str()] = DynamicFunction;
284+
m_Functions[DynamicFunction->Name] = DynamicFunction;
285+
m_Fileds[DynamicFunction->Name] = DynamicFunction;
283286

284287
return DynamicFunction;
285288
}
@@ -296,7 +299,7 @@ FCDynamicFunction* FCDynamicClassDesc::RegisterFunc(const char *pcsFuncName)
296299
FCDynamicFunction *DynamicFunction = RegisterUEFunc(pcsFuncName);
297300
if (!DynamicFunction)
298301
{
299-
UE_LOG(LogFCScript, Warning, TEXT("failed register function: %s, class name: %s"), UTF8_TO_TCHAR(pcsFuncName), UTF8_TO_TCHAR(m_UEClassName.c_str()));
302+
UE_LOG(LogFCScript, Warning, TEXT("failed register function: %s, class name: %s"), UTF8_TO_TCHAR(pcsFuncName), UTF8_TO_TCHAR(m_UEClassName));
300303
}
301304
return DynamicFunction;
302305
}
@@ -309,9 +312,9 @@ FCDynamicField* FCDynamicClassDesc::RegisterWrapLibFunction(const char* pcsFuncN
309312
return itFiled->second;
310313
}
311314
FCDynamicWrapLibFunction* LibField = new FCDynamicWrapLibFunction(InGetFunc, InSetFunc);
312-
LibField->Name = pcsFuncName;
313-
m_LibFields[LibField->Name.c_str()] = LibField;
314-
m_Fileds[LibField->Name.c_str()] = LibField;
315+
LibField->Name = GetConstName(pcsFuncName);
316+
m_LibFields[LibField->Name] = LibField;
317+
m_Fileds[LibField->Name] = LibField;
315318
return LibField;
316319
}
317320

@@ -323,9 +326,9 @@ FCDynamicField* FCDynamicClassDesc::RegisterWrapLibAttrib(const char* pcsFuncNam
323326
return itFiled->second;
324327
}
325328
FCDynamicWrapLibAttrib* LibField = new FCDynamicWrapLibAttrib(InGetFunc, InSetFunc);
326-
LibField->Name = pcsFuncName;
327-
m_LibFields[LibField->Name.c_str()] = LibField;
328-
m_Fileds[LibField->Name.c_str()] = LibField;
329+
LibField->Name = GetConstName(pcsFuncName);
330+
m_LibFields[LibField->Name] = LibField;
331+
m_Fileds[LibField->Name] = LibField;
329332
return LibField;
330333
}
331334
//---------------------------------------------------------------------------
@@ -422,10 +425,11 @@ FCDynamicClassDesc* FCScriptContext::RegisterUClass(const char *UEClassName)
422425
NameOffset = 0;
423426
}
424427

428+
UEClassName = GetConstName(UEClassName);
429+
425430
FCDynamicClassDesc *ScriptClassDesc = new FCDynamicClassDesc();
426431
ScriptClassDesc->m_UEClassName = UEClassName;
427432
ScriptClassDesc->OnRegisterStruct(Struct, this);
428-
UEClassName = ScriptClassDesc->m_UEClassName.c_str();
429433

430434
m_ClassNameMap[UEClassName] = ScriptClassDesc;
431435
m_StructMap[Struct] = ScriptClassDesc;
@@ -450,9 +454,10 @@ FCDynamicClassDesc* FCScriptContext::RegisterUStruct(UStruct *Struct)
450454
UEClassName += Struct->GetName();
451455
FCDynamicClassDesc *ScriptClassDesc = new FCDynamicClassDesc();
452456
ScriptClassDesc->m_UEClassName = TCHAR_TO_UTF8(*UEClassName);
457+
ScriptClassDesc->m_UEClassName = GetConstName(ScriptClassDesc->m_UEClassName);
453458
ScriptClassDesc->OnRegisterStruct(Struct, this);
454459

455-
const char* ClassName = ScriptClassDesc->m_UEClassName.c_str();
460+
const char* ClassName = ScriptClassDesc->m_UEClassName;
456461
m_ClassNameMap[ClassName] = ScriptClassDesc;
457462
m_StructMap[Struct] = ScriptClassDesc;
458463

0 commit comments

Comments
 (0)