forked from Tencent/UnLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorialBlueprintFunctionLibrary.cpp
More file actions
102 lines (83 loc) · 3.27 KB
/
TutorialBlueprintFunctionLibrary.cpp
File metadata and controls
102 lines (83 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "TutorialBlueprintFunctionLibrary.h"
#include "Kismet/KismetSystemLibrary.h"
#include "LuaCore.h"
#include "UnLua.h"
#include "UnLuaDelegates.h"
static void PrintScreen(const FString& Msg)
{
UKismetSystemLibrary::PrintString(nullptr, Msg, true, false, FLinearColor(0, 0.66, 1), 100);
}
void UTutorialBlueprintFunctionLibrary::CallLuaByGlobalTable()
{
PrintScreen(TEXT("[C++]CallLuaByGlobalTable 开始"));
const auto L = UnLua::GetState();
const auto bSuccess = UnLua::RunChunk(L, "G_08_CppCallLua = require 'Tutorials.08_CppCallLua'");
check(bSuccess);
const auto RetValues = UnLua::CallTableFunc(L, "G_08_CppCallLua", "CallMe", 1.1f, 2.2f);
check(RetValues.Num() == 1);
const auto Msg = FString::Printf(TEXT("[C++]收到来自Lua的返回,结果=%f"), RetValues[0].Value<float>());
PrintScreen(Msg);
PrintScreen(TEXT("[C++]CallLuaByGlobalTable 结束"));
}
void UTutorialBlueprintFunctionLibrary::CallLuaByFLuaTable()
{
PrintScreen(TEXT("[C++]CallLuaByFLuaTable 开始"));
const auto L = UnLua::GetState();
const auto Require = UnLua::FLuaFunction("_G", "require");
const auto RetValues1 = Require.Call("Tutorials.08_CppCallLua");
check(RetValues1.Num() == 1);
const auto LuaTable = UnLua::FLuaTable(RetValues1[0]);
const auto RetValues2 = LuaTable.Call("CallMe", 3.3f, 4.4f);
check(RetValues2.Num() == 1);
const auto Msg = FString::Printf(TEXT("[C++]收到来自Lua的返回,结果=%f"), RetValues2[0].Value<float>());
PrintScreen(Msg);
PrintScreen(TEXT("[C++]CallLuaByFLuaTable 结束"));
}
bool CustomLoader1(const FString& RelativePath, TArray<uint8>& Data, FString& FullPath)
{
const auto SlashedRelativePath = RelativePath.Replace(TEXT("."), TEXT("/"));
FullPath = FString::Printf(TEXT("%s%s.lua"), *GLuaSrcFullPath, *SlashedRelativePath);
if (FFileHelper::LoadFileToArray(Data, *FullPath, FILEREAD_Silent))
return true;
FullPath.ReplaceInline(TEXT(".lua"), TEXT("/Index.lua"));
if (FFileHelper::LoadFileToArray(Data, *FullPath, FILEREAD_Silent))
return true;
return false;
}
bool CustomLoader2(const FString& RelativePath, TArray<uint8>& Data, FString& FullPath)
{
const auto SlashedRelativePath = RelativePath.Replace(TEXT("."), TEXT("/"));
const auto L = UnLua::GetState();
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
const char* Path = lua_tostring(L, -1);
lua_pop(L, 2);
if (!Path)
return false;
TArray<FString> Parts;
FString(Path).ParseIntoArray(Parts, TEXT(";"), false);
for (auto& Part : Parts)
{
Part.ReplaceInline(TEXT("?"), *SlashedRelativePath);
FPaths::CollapseRelativeDirectories(Part);
if (FPaths::IsRelative(Part))
FullPath = FPaths::ConvertRelativePathToFull(GLuaSrcFullPath, Part);
else
FullPath = Part;
if (FFileHelper::LoadFileToArray(Data, *FullPath, FILEREAD_Silent))
return true;
}
return false;
}
void UTutorialBlueprintFunctionLibrary::SetupCustomLoader(int Index)
{
switch (Index)
{
case 1:
FUnLuaDelegates::CustomLoadLuaFile.BindStatic(CustomLoader1);
break;
case 2:
FUnLuaDelegates::CustomLoadLuaFile.BindStatic(CustomLoader2);
break;
}
}