コード例 #1
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");
     }
 }
コード例 #2
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);
        }