-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplePlugin.cpp
More file actions
121 lines (91 loc) · 5.42 KB
/
examplePlugin.cpp
File metadata and controls
121 lines (91 loc) · 5.42 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import InterceptCommon;
#include <cstdint>
#include <span>
#include <string_view>
// On Linux you currently can't use modules (because GCC constantly crashes and I can't figure it out) so you have to use this instead
/*
#include "lib/InterceptAR/src/common/interceptCommon.ixx"
#include <cstdint>
#include <span>
*/
/*
sealed class ExampleClass
{
static proto float TestFunction(string arg);
static proto float GetMeAVector(out vector arg);
proto external void TestFunc(out vector arg); // external gets instance as first argument and can access instance variables
static proto string StringFunc(string inputString, out string arg);
static proto void ArrayTest(notnull array<string> stringArray, notnull array<vector3> vectorArray, notnull array<float> floatArray);
int testIntMemberVariable = 5;
string testStringMember = "stringtest";
float testFloatArray[3] = {1,2,3};
// "InterceptLoaded" is a "magic" variable, when your plugin class DoSetup method gets executed, it will be set to true.
// You can use this to detect if your plugin has actually been loaded.
static bool InterceptLoaded = false;
};
*/
class ExampleClass : public ScriptClassBaseSimple<"ExampleClass"> {
public:
// static proto float TestFunction(string arg);
static void TestFunction(FunctionArgumentsHandler& args, FunctionResultHandler& result) {
//if (!args.VerifyType<std::string_view>(0))
// __debugbreak(); // wrong type inside script declaration
auto myString = args.GetAs<std::string_view>(0);
result.SetAs<float>(133.7f);
}
// static proto void GetMeAVector(out vector arg);
static void GetMeAVector(FunctionArgumentsHandler& args, FunctionResultHandler& result) {
// set via out var
args.SetAs<Vector3>(0, Vector3{1, 2, 3});
}
// static proto external void TestFunc(out vector arg);
static void TestFunc(FunctionArgumentsHandler& args, FunctionResultHandler& result) {
// For "external" functions, first argument is always pointer to a class instance which holds member variables
auto classInstance = args.GetAs<ClassInstance*>(0);
auto instanceName = classInstance->GetClassName();
auto instanceType = classInstance->GetClassType().GetType();
auto testIntMemberVariable = classInstance->GetVariable("testIntMemberVariable")->GetAs<int>();
auto testStringMember = classInstance->GetVariable("testStringMember")->GetAs<std::string_view>();
auto testFloatArray = classInstance->GetVariable("testFloatArray")->GetAs<std::span<float>>();
// set via out var
args.SetAs<Vector3>(0, Vector3{(float)testIntMemberVariable, 2, 3});
}
// static proto string StringFunc(string inputString, out string arg);
static void StringFunc(FunctionArgumentsHandler& args, FunctionResultHandler& result) {
// Strings can also be read as string_view, but currently this cannot be used in Set, that will be fixed in the future
auto inputString = args.GetAs<std::string_view>(0);
// Set a string via out var, this will copy the string into Enfusion's own memory
args.SetAs<const char*>(1, "testabc");
result.SetAs<const char*>(inputString.data());
}
// static proto void ArrayTest(notnull array<string> stringArray, notnull array<vector3> vectorArray, notnull array<float> floatArray);
static void ArrayTest(FunctionArgumentsHandler& args, FunctionResultHandler& result) {
// Arrays can currently only be read, via std::span. Writing to arrays is not possible for now.
// Technically the Vector3/float/int/bool, all values that don't need memory allocation by themselves, could be in-place overwritten to change them in the array.
// But element adding/removing is not supported yet.
// This is not checking for null, thats why we have notnull in the function definition
auto stringArray = args.GetAs<std::span<const char*>>(0);
auto vectorArray = args.GetAs<std::span<Vector3>>(1);
auto floatArray = args.GetAs<std::span<float>>(2);
/*
auto vectorArray = new array<vector>();
points.Insert( Vector( 0, 0, 0) );
points.Insert( Vector( 5, 0, 0) );
points.Insert( Vector( 8, 3, 0) );
points.Insert( Vector( 6, 1, 0) );
autoptr array<string> stringArray = {"jedna", "dva", "tri"};
autoptr array<float> floatArray = {0.1, 0.2, 0.3};
DedmensSecondClass.ArrayTest(stringArray, vectorArray, floatArray);
*/
}
void DoSetup(ScriptClassBaseSimple::RegisterFuncHandler registerFunction, Intercept::ClassType* selfType) override {
// We need to assign Enscript function name, to our function implementation here
registerFunction("TestFunction", &TestFunction);
registerFunction("GetMeAVector", &GetMeAVector);
registerFunction("TestFunc", &TestFunc);
registerFunction("StringFunc", &StringFunc);
registerFunction("ArrayTest", &ArrayTest);
Intercept::Print(LogLevel::Verbose, "ExamplePlugin was loaded!"); // Example on how to print a log message (to workbench log, or game server logfile)
}
};
ExampleClass GExampleClass; // Register the class, it needs to be a global that never gets deleted