コード例 #1
0
        public static T GetWrapper <T>(ObjectPointerDescription ptrDesc) where T : NativeWrapper
        {
            if (ptrDesc.Pointer == IntPtr.Zero)
            {
                return(null);
            }

            if (_wrappers.TryGetValue(ptrDesc.Pointer, out var result))
            {
                return(result as T);
            }

            var cppClass = Marshal.PtrToStringUTF8(ptrDesc.TypeName, ptrDesc.TypeNameLen);

            var asm  = typeof(NativeWrapper).Assembly;
            var type = asm.GetType("UnrealEngine." + cppClass);

            if (type == null)
            {
                Ue.LogWarning($"Manage type {cppClass} not found, use {typeof(T).FullName}");
                type = typeof(T);
            }

            var ctor = type.GetConstructor(new[] { typeof(IntPtr) });

            if (ctor == null)
            {
                Ue.LogError($"Type {type.FullName} not contains constructor {type.Name}(IntPtr)");
                return(null);
            }

            var obj = ctor.Invoke(new[] { (object)ptrDesc.Pointer }) as T;

            return(obj);
        }
コード例 #2
0
 public static void RemoveWrapper(IntPtr adress)
 {
     if (_wrappers.Remove(adress))
     {
         Ue.LogDebug($"Free object {adress}");
     }
     else
     {
         Ue.LogWarning($"Failed free object, {adress} not found");
     }
 }
コード例 #3
0
        public static bool AddWrapper(IntPtr adress, string dotnetTypeName)
        {
            var typeName  = JsonConvert.DeserializeObject <FDotnetTypeName>(dotnetTypeName);
            var className = _gameLogicAssembly.GetName().Name + "." + typeName.FullName;

            if (_wrappers.ContainsKey(adress))
            {
                Ue.LogWarning($"Object is already registered. Type:{className}, Adress: 0x{(long) adress:X}");
                return(false);
            }

            var type = _gameLogicAssembly.GetType(className);

            if (type == null)
            {
                Ue.LogError($"Failed create object, type {className} not found");
                return(false);
            }

            var constructor = type.GetConstructor(new[] { typeof(IntPtr) });

            if (constructor == null)
            {
                Ue.LogError($"Failed create object, type {className} not have IntPtr constructor");
                return(false);
            }

            try
            {
                var obj = constructor.Invoke(new object[] { adress });

                foreach (var prop in type.GetProperties())
                {
                    var attr = prop.GetCustomAttribute(typeof(DefaultValueAttribute)) as DefaultValueAttribute;

                    if (attr != null)
                    {
                        prop.SetValue(obj, attr.Value, null);
                    }
                }

                foreach (var prop in typeName.PropertyValue)
                {
                    var pi = obj.GetType().GetProperty(prop.Name);

                    if (pi == null)
                    {
                        Ue.LogError($"Type {className} have not {prop.Name}");
                        continue;
                    }

                    try
                    {
                        pi.SetValue(obj, Convert.ChangeType(prop.Value, pi.PropertyType), null);
                    }
                    catch
                    {
                        Ue.LogError($"Failed convert '{prop.Value}' to {pi.PropertyType.FullName} (In {className}.{prop.Name})");
                    }
                }

                _wrappers.Add(adress, obj);
            }
            catch (Exception e)
            {
                Ue.LogError($"Failed create object, exception:{e}\n{e.StackTrace}");
                return(false);
            }

            Ue.LogDebug($"Create object, Type:{className}, Adress: 0x{(long) adress:X}");
            return(true);
        }