MCPcopy Create free account
hub / github.com/StudioCherno/Coral / main

Function main

Example/Example.Native/Source/Main.cpp:54–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52}
53
54int main([[maybe_unused]] int argc, char** argv)
55{
56 auto exeDir = std::filesystem::path(argv[0]).parent_path();
57 auto coralDir = exeDir.string();
58 Coral::HostSettings settings;
59 settings.CoralDirectory = coralDir;
60 settings.ExceptionCallback = ExceptionCallback;
61 Coral::HostInstance hostInstance;
62 hostInstance.Initialize(settings);
63
64 auto loadContext = hostInstance.CreateAssemblyLoadContext("ExampleContext");
65
66 auto assemblyPath = exeDir / "Example.Managed.dll";
67 auto& assembly = loadContext.LoadAssembly(assemblyPath.string());
68
69 assembly.AddInternalCall("Example.Managed.ExampleClass", "VectorAddIcall", reinterpret_cast<void*>(&VectorAddIcall));
70 assembly.AddInternalCall("Example.Managed.ExampleClass", "PrintStringIcall", reinterpret_cast<void*>(&PrintStringIcall));
71 assembly.AddInternalCall("Example.Managed.ExampleClass", "NativeArrayIcall", reinterpret_cast<void*>(&NativeArrayIcall));
72 assembly.AddInternalCall("Example.Managed.ExampleClass", "ArrayReturnIcall", reinterpret_cast<void*>(&ArrayReturnIcall));
73 assembly.UploadInternalCalls();
74
75 // Get a reference to the ExampleClass type
76 auto& exampleType = assembly.GetLocalType("Example.Managed.ExampleClass");
77
78 // Call the static method "StaticMethod" with value 50
79 exampleType.InvokeStaticMethod("StaticMethod", 50.0f);
80
81 // Get a reference to the CustomAttribute type
82 auto& customAttributeType = assembly.GetLocalType("Example.Managed.CustomAttribute");
83
84 // Get a list of all attributes on the exampleType class
85 auto exampleTypeAttribs = exampleType.GetAttributes();
86 for (auto& attribute : exampleTypeAttribs)
87 {
88 if (attribute.GetType() == customAttributeType)
89 {
90 // Get the value of "Value" from the CustomAttribute attribute
91 std::cout << "CustomAttribute: " << attribute.GetFieldValue<float>("Value") << std::endl;
92 }
93 }
94
95 // Create an instance of type Example.Managed.ExampleClass and pass 50 to the constructor
96 auto exampleInstance = exampleType.CreateInstance(50);
97
98 // Invoke the method named "MemberMethod" with a MyVec3 argument (doesn't return anything)
99 exampleInstance.InvokeMethod("Void MemberMethod(MyVec3)", MyVec3 { 10.0f, 10.0f, 10.0f });
100
101 // Invokes the setter on PublicProp with the value 10 (will be multiplied by 2 in C#)
102 exampleInstance.SetPropertyValue("PublicProp", 10);
103
104 // Get the value of PublicProp as an int
105 std::cout << exampleInstance.GetPropertyValue<int32_t>("PublicProp") << std::endl;
106
107 // Sets the value of the private field "myPrivateValue" with the value 10 (will NOT be multiplied by 2 in C#)
108 exampleInstance.SetFieldValue("myPrivateValue", 10);
109
110 // Get the value of myPrivateValue as an int
111 std::cout << exampleInstance.GetFieldValue<int32_t>("myPrivateValue") << std::endl;

Callers

nothing calls this directly

Calls 11

LoadAssemblyMethod · 0.80
AddInternalCallMethod · 0.80
UploadInternalCallsMethod · 0.80
InitializeMethod · 0.45
InvokeStaticMethodMethod · 0.45
GetAttributesMethod · 0.45
CreateInstanceMethod · 0.45
InvokeMethodMethod · 0.45
SetPropertyValueMethod · 0.45
SetFieldValueMethod · 0.45

Tested by

no test coverage detected