public static void Call() { IntPtr classAddress = NativeReflection.GetClass("/Script/UnrealEngine_Runtime.MyClass"); StringTest_FunctionAddress = NativeReflection.GetFunction(classAddress, "MyTestFunc2"); StringTest_ParamsSize = NativeReflection.GetFunctionParamsSize(StringTest_FunctionAddress); StringTest_inVal_Offset = NativeReflection.GetPropertyOffset(StringTest_FunctionAddress, "inVal"); StringTest_stringTest_Offset = NativeReflection.GetPropertyOffset(StringTest_FunctionAddress, "stringTest"); StringTest_ParamResult_Offset = NativeReflection.GetPropertyOffset(StringTest_FunctionAddress, "__return"); UMyClass myClass = UObject.NewObject <UMyClass>(); string refVal = "inref"; string arg = "hello world"; unsafe { byte * ParamsBufferAllocation = stackalloc byte[StringTest_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); FStringMarshaler.ToNative(IntPtr.Add(ParamsBuffer, StringTest_inVal_Offset), refVal); //FStringMarshaler.ToNative(IntPtr.Add(ParamsBuffer, StringTest_stringTest_Offset), arg); NativeReflection.InvokeFunction(myClass.Address, StringTest_FunctionAddress, ParamsBuffer, StringTest_ParamsSize); refVal = FStringMarshaler.FromNative(IntPtr.Add(ParamsBuffer, StringTest_inVal_Offset)); arg = FStringMarshaler.FromNative(IntPtr.Add(ParamsBuffer, StringTest_stringTest_Offset)); string toReturn = FStringMarshaler.FromNative(IntPtr.Add(ParamsBuffer, StringTest_ParamResult_Offset)); NativeReflection.InvokeFunction_DestroyAll(StringTest_FunctionAddress, ParamsBuffer); } }
protected virtual void CallMe_Implementation() { CheckDestroyed(); if (!CallMe_IsValid) { NativeReflection.LogInvalidFunctionAccessed("/Script/USharp.BPTest1:CallMe"); return; } unsafe { byte * ParamsBufferAllocation = stackalloc byte[CallMe_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); NativeReflection.InvokeFunction(Address, CallMe_FunctionAddress, ParamsBuffer, CallMe_ParamsSize); NativeReflection.InvokeFunction_DestroyAll(CallMe_FunctionAddress, ParamsBuffer); } }
public void CallMe6(FMyStructCustomCtor test, ref FMyStructCustomCtor test2) { CheckDestroyed(); if (!CallMe6_IsValid) { NativeReflection.LogInvalidFunctionAccessed("/Script/USharp.BPTest1:CallMe6"); return; } unsafe { byte * ParamsBufferAllocation = stackalloc byte[CallMe6_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); FMyStructCustomCtor.ToNative(IntPtr.Add(ParamsBuffer, CallMe6_test_Offset), test); FMyStructCustomCtor.ToNative(IntPtr.Add(ParamsBuffer, CallMe6_test2_Offset), test2); NativeReflection.InvokeFunction(Address, CallMe6_FunctionAddress, ParamsBuffer, CallMe6_ParamsSize); test2 = FMyStructCustomCtor.FromNative(IntPtr.Add(ParamsBuffer, CallMe6_test2_Offset)); NativeReflection.InvokeFunction_DestroyAll(CallMe6_FunctionAddress, ParamsBuffer); } }
public void CallMe() { CheckDestroyed(); if (!CallMe_IsValid) { NativeReflection.LogInvalidFunctionAccessed("/Script/USharp.BPTest1:CallMe"); return; } if (CallMe_InstanceFunctionAddress == IntPtr.Zero) { CallMe_InstanceFunctionAddress = NativeReflection.GetFunctionFromInstance(Address, "CallMe"); } unsafe { byte * ParamsBufferAllocation = stackalloc byte[CallMe_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); NativeReflection.InvokeFunction(Address, CallMe_InstanceFunctionAddress, ParamsBuffer, CallMe_ParamsSize); NativeReflection.InvokeFunction_DestroyAll(CallMe_InstanceFunctionAddress, ParamsBuffer); } }
public List <string> CallMe3() { CheckDestroyed(); if (!CallMe3_IsValid) { NativeReflection.LogInvalidFunctionAccessed("/Script/USharp.BPTest1:CallMe3"); return(default(List <string>)); } unsafe { byte * ParamsBufferAllocation = stackalloc byte[CallMe3_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); NativeReflection.InvokeFunction(Address, CallMe3_FunctionAddress, ParamsBuffer, CallMe3_ParamsSize); TArrayCopyMarshaler <string> CallMe3_ReturnValue_Marshaler = new TArrayCopyMarshaler <string>(1, CallMe3_ReturnValue_PropertyAddress, CachedMarshalingDelegates <string, FStringMarshaler> .FromNative, CachedMarshalingDelegates <string, FStringMarshaler> .ToNative); List <string> toReturn = CallMe3_ReturnValue_Marshaler.FromNative(IntPtr.Add(ParamsBuffer, CallMe3_ReturnValue_Offset)); NativeReflection.InvokeFunction_DestroyAll(CallMe3_FunctionAddress, ParamsBuffer); return(toReturn); } }
public static void Call2() { IntPtr classAddress = NativeReflection.GetClass("/Script/UnrealEngine_Runtime.MyClass"); Test2_FunctionAddress = NativeReflection.GetFunction(classAddress, "MyTestFunc3"); Test2_ParamsSize = NativeReflection.GetFunctionParamsSize(Test2_FunctionAddress); Test2_Param1_Offset = NativeReflection.GetPropertyOffset(Test2_FunctionAddress, "myStruct"); UMyClass myClass = UObject.NewObject <UMyClass>(); FMyStructCustomCtor val = StructDefault <FMyStructCustomCtor> .Value; unsafe { byte * ParamsBufferAllocation = stackalloc byte[Test2_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); FMyStructCustomCtor.ToNative(IntPtr.Add(ParamsBuffer, Test2_Param1_Offset), val); NativeReflection.InvokeFunction(myClass.Address, Test2_FunctionAddress, ParamsBuffer, Test2_ParamsSize); NativeReflection.InvokeFunction_DestroyAll(Test2_FunctionAddress, ParamsBuffer); } }
public void CallMe5(ref List <string> a1, List <string> a2) { CheckDestroyed(); if (!CallMe5_IsValid) { NativeReflection.LogInvalidFunctionAccessed("/Script/USharp.BPTest1:CallMe5"); return; } unsafe { byte * ParamsBufferAllocation = stackalloc byte[CallMe5_ParamsSize]; IntPtr ParamsBuffer = new IntPtr(ParamsBufferAllocation); TArrayCopyMarshaler <string> CallMe5_a1_Marshaler = new TArrayCopyMarshaler <string>(1, CallMe5_a1_PropertyAddress, CachedMarshalingDelegates <string, FStringMarshaler> .FromNative, CachedMarshalingDelegates <string, FStringMarshaler> .ToNative); CallMe5_a1_Marshaler.ToNative(IntPtr.Add(ParamsBuffer, CallMe5_a1_Offset), a1); TArrayCopyMarshaler <string> CallMe5_a2_Marshaler = new TArrayCopyMarshaler <string>(1, CallMe5_a2_PropertyAddress, CachedMarshalingDelegates <string, FStringMarshaler> .FromNative, CachedMarshalingDelegates <string, FStringMarshaler> .ToNative); CallMe5_a2_Marshaler.ToNative(IntPtr.Add(ParamsBuffer, CallMe5_a2_Offset), a2); NativeReflection.InvokeFunction(Address, CallMe5_FunctionAddress, ParamsBuffer, CallMe5_ParamsSize); a1 = CallMe5_a1_Marshaler.FromNative(IntPtr.Add(ParamsBuffer, CallMe5_a1_Offset)); NativeReflection.InvokeFunction_DestroyAll(CallMe5_FunctionAddress, ParamsBuffer); } }
/// <summary> /// Dynamically invokes the function /// </summary> /// <param name="obj"></param> /// <param name="parameters"></param> /// <returns></returns> public object DynamicInvoke(UObject obj, params object[] parameters) { if (parameters == null) { parameters = new object[0]; } bool validParams = true; Dictionary <UProperty, Delegate> fromNativeParams = new Dictionary <UProperty, Delegate>(); Dictionary <UProperty, Delegate> toNativeParams = new Dictionary <UProperty, Delegate>(); UProperty returnValueProp = null; List <UProperty> paramProps = new List <UProperty>(); foreach (UProperty prop in GetProperties <UProperty>()) { if (prop.HasAnyPropertyFlags(EPropertyFlags.Parm)) { if (prop.HasAnyPropertyFlags(EPropertyFlags.ReturnParm)) { returnValueProp = prop; } else { paramProps.Add(prop); } Type paramType = UProperty.GetTypeFromProperty(prop); if (paramType == null) { validParams = false; break; } Delegate fromNative = MarshalingDelegateResolverSlow.GetFromNative(paramType); Delegate toNative = MarshalingDelegateResolverSlow.GetToNative(paramType); if (fromNative == null || toNative == null) { validParams = false; break; } fromNativeParams.Add(prop, fromNative); toNativeParams.Add(prop, toNative); } } if (parameters.Length != paramProps.Count) { validParams = false; } if (!validParams) { return(null); } // Sort the parameters by offset, this is assumingly the correct thing to do? // - Otherwise we need to take the param names into this function. Or just not sort at all? //paramProps.Sort((x, y) => x.GetOffset_ForUFunction().CompareTo(y.GetOffset_ForUFunction())); object result = null; unsafe { int paramsSize = ParmsSize; byte * paramsBufferAllocation = stackalloc byte[ParmsSize]; IntPtr paramsBuffer = new IntPtr(paramsBufferAllocation); FMemory.Memzero(paramsBuffer, paramsSize); // Initialize default values for all parameters foreach (UProperty prop in GetProperties <UProperty>()) { if (prop.HasAnyPropertyFlags(EPropertyFlags.Parm)) { Native.Native_UProperty.InitializeValue_InContainer(prop.Address, paramsBuffer); } } // Copy the managed parameters to the buffer for (int i = 0; i < parameters.Length; i++) { UProperty paramProp = paramProps[i]; object paramValue = parameters[i]; if (paramValue != null && (!paramProp.HasAnyPropertyFlags(EPropertyFlags.OutParm) || paramProp.HasAnyPropertyFlags(EPropertyFlags.ReferenceParm))) { toNativeParams[paramProp].DynamicInvoke( paramsBuffer + paramProp.GetOffset_ForUFunction(), (int)0, paramProp.Address, paramValue); } } // Invoke the function NativeReflection.InvokeFunction(obj.Address, Address, paramsBuffer, paramsSize); // Copy parameters / return value from the buffer for (int i = 0; i < parameters.Length; i++) { UProperty paramProp = paramProps[i]; if (paramProp.HasAnyPropertyFlags(EPropertyFlags.OutParm)) { parameters[i] = fromNativeParams[paramProp].DynamicInvoke( paramsBuffer + paramProp.GetOffset_ForUFunction(), (int)0, paramProp.Address); } } if (returnValueProp != null) { result = fromNativeParams[returnValueProp].DynamicInvoke( paramsBuffer + returnValueProp.GetOffset_ForUFunction(), (int)0, returnValueProp.Address); } // Destroy the memory for all of the parameters foreach (UProperty prop in GetProperties <UProperty>()) { if (prop.HasAnyPropertyFlags(EPropertyFlags.Parm)) { Native.Native_UProperty.DestroyValue_InContainer(prop.Address, paramsBuffer); } } } return(result); }