Esempio n. 1
0
 public void SubscribeToEvent(StringHash eventType, Action <StringHash, VariantMap> function)
 {
     Urho3D_Object_SubscribeToEvent(__ToPInvoke(this), GCHandle.ToIntPtr(GCHandle.Alloc(function)),
                                    eventType.Hash, (gcHandle, type, args) =>
     {
         var callback = (Action <StringHash, VariantMap>)GCHandle.FromIntPtr(gcHandle).Target;
         callback.Invoke(StringHash.__FromPInvoke(type), VariantMap.__FromPInvoke(args));
     }, IntPtr.Zero);
 }
Esempio n. 2
0
 public void SubscribeToEvent(Object sender, StringHash eventType, Action <VariantMap> function)
 {
     Urho3D_Object_SubscribeToEvent(__ToPInvoke(this), GCHandle.ToIntPtr(GCHandle.Alloc(function)),
                                    eventType.Hash, (gcHandle, type, args) =>
     {
         var callback = (Action <VariantMap>)GCHandle.FromIntPtr(gcHandle).Target;
         callback.Invoke(VariantMap.__FromPInvoke(args, false));
     }, __ToPInvoke(sender));
 }
Esempio n. 3
0
        public T GetResource <T>(string name, bool sendEventOnFailure = true) where T : Resource
        {
            var typeHash          = new StringHash(typeof(T).Name);
            var componentInstance = Urho3D__ResourceCache__GetResource_Urho3D__StringHash_Urho3D__String_const__bool_(NativeInstance, typeHash.Hash, name, sendEventOnFailure);

            return(InstanceCache.GetOrAdd(componentInstance, ptr => {
                Type type = typeof(T);
                return (T)Activator.CreateInstance(type, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { ptr, false }, null);
            }));
        }
Esempio n. 4
0
        public void RegisterFactory <T>(string category = "") where T : Object
        {
            var type = typeof(T);

            _factoryTypes[StringHash.Calculate(type.Name)] = type;

            // Find a wrapper base type.
            var baseType = type.BaseType;

            while (baseType != null && baseType.Assembly != typeof(Context).Assembly)
            {
                baseType = baseType.BaseType;
            }

            if (baseType == null)
            {
                throw new InvalidOperationException("This type can not be registered as factory.");
            }

            Urho3D_Context_RegisterFactory(__ToPInvoke(this), type.Name, StringHash.Calculate("Wrappers::" + baseType.Name), category);
        }
Esempio n. 5
0
        public void RegisterFactory(Type type, string category = "")
        {
            if (!type.IsSubclassOf(typeof(Object)))
            {
                throw new ArgumentException("Type must be sublass of Object.");
            }

            _factoryTypes[StringHash.Calculate(type.Name)] = type;

            // Find a wrapper base type.
            var baseType = type.BaseType;

            while (baseType != null && baseType.Assembly != typeof(Context).Assembly)
            {
                baseType = baseType.BaseType;
            }

            if (baseType == null)
            {
                throw new InvalidOperationException("This type can not be registered as factory.");
            }

            Urho3D_Context_RegisterFactory(GetNativeInstance(this), type.Name, StringHash.Calculate("Wrappers::" + baseType.Name), category);
        }
Esempio n. 6
0
        public T GetComponent <T>(bool recursive = false) where T : Component
        {
            var componentInstance = Urho3D__Node__GetComponent_Urho3D__StringHash_bool__const(NativeInstance, StringHash.Calculate(typeof(T).Name), recursive);

            if (componentInstance == IntPtr.Zero)
            {
                return(default(T));
            }
            return(InstanceCache.GetOrAdd(componentInstance, ptr => (T)Activator.CreateInstance(typeof(T),
                                                                                                BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { ptr, false }, null)));
        }
Esempio n. 7
0
        public T CreateComponent <T>(CreateMode mode = CreateMode.REPLICATED, uint id = 0) where T : Component
        {
            var componentInstance = Urho3D__Node__CreateComponent_Urho3D__StringHash_Urho3D__CreateMode_unsigned_int_(NativeInstance, StringHash.Calculate(typeof(T).Name), mode, id);

            if (componentInstance == IntPtr.Zero)
            {
                return(default(T));
            }
            return(InstanceCache.GetOrAdd(componentInstance, ptr => (T)Activator.CreateInstance(typeof(T),
                                                                                                BindingFlags.NonPublic | BindingFlags.Instance,
                                                                                                null, new object[] { ptr, false }, null)));
        }
Esempio n. 8
0
 public T GetComponent <T>() where T : Component
 {
     return(GetManagedInstance <T>(
                Urho3D__Component__GetComponent_Urho3D__StringHash__const(
                    NativeInstance, StringHash.Calculate(typeof(T).Name)), false));
 }
Esempio n. 9
0
 public T GetOrCreateComponent <T>(Urho3D.CreateMode mode = Urho3D.CreateMode.Replicated, uint id = 0) where T : Component
 {
     return(GetManagedInstance <T>(
                Urho3D__Node__GetOrCreateComponent_Urho3D__StringHash_Urho3D__CreateMode_unsigned_int_(
                    NativeInstance, StringHash.Calculate(typeof(T).Name), mode, id), false));
 }
Esempio n. 10
0
 public T GetComponent <T>(bool recursive = false) where T : Component
 {
     return(GetManagedInstance <T>(
                Urho3D__Node__GetComponent_Urho3D__StringHash_bool__const(
                    NativeInstance, StringHash.Calculate(typeof(T).Name), recursive), false));
 }
Esempio n. 11
0
 internal static uint GetNativeInstance(StringHash source)
 {
     return(source.Hash);
 }
Esempio n. 12
0
 internal static uint __ToPInvoke(StringHash source)
 {
     return(source.Hash);
 }
 public T GetResource <T>(string name, bool sendEventOnFailure = true) where T : Resource
 {
     return(GetManagedInstance <T>(
                Urho3D__ResourceCache__GetResource_Urho3D__StringHash_Urho3D__String_const__bool_(
                    NativeInstance, StringHash.Calculate(typeof(T).Name), name, sendEventOnFailure), true));
 }
Esempio n. 14
0
        private void HandleEventWithType(IntPtr gcHandle, uint type, IntPtr args)
        {
            var callback = (Action <StringHash, VariantMap>)GCHandle.FromIntPtr(gcHandle).Target;

            callback.Invoke(StringHash.__FromPInvoke(type), VariantMap.__FromPInvoke(args, false));
        }
Esempio n. 15
0
 public void SubscribeToEvent(Object sender, StringHash eventType, Action <VariantMap> function)
 {
     Urho3D_Object_SubscribeToEvent(__ToPInvoke(this), GCHandle.ToIntPtr(GCHandle.Alloc(function)),
                                    eventType.Hash, Marshal.GetFunctionPointerForDelegate((EventHandler)HandleEventWithoutType), __ToPInvoke(sender));
 }
Esempio n. 16
0
        internal static void RegisterSerializableAttributes(Context context)
        {
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (!type.IsSubclassOf(typeof(Serializable)))
                    {
                        continue;
                    }

                    foreach (var fieldInfo in type.GetFields())
                    {
                        var attributes = (AttributeAttribute[])fieldInfo.GetCustomAttributes(typeof(AttributeAttribute), true);
                        foreach (var attribute in attributes)
                        {
                            AttributeGetterDelegate attributeGetter = serializable =>
                            {
                                var value = fieldInfo.GetValue(GetManagedInstance(serializable));
                                return(Variant.GetNativeInstance(Variant.FromObject(value)));
                            };

                            AttributeSetterDelegate attributeSetter = (serializable, variant) =>
                            {
                                fieldInfo.SetValue(GetManagedInstance(serializable),
                                                   Variant.GetManagedInstance(variant).Object);
                            };

                            _attributeAccessorReferences.Add(GCHandle.Alloc(attributeGetter));
                            _attributeAccessorReferences.Add(GCHandle.Alloc(attributeSetter));

                            var typeHash = new StringHash(type);
                            Urho3D_Serializable_RegisterAttribute(Context.GetNativeInstance(context), typeHash.Hash,
                                                                  Variant.GetVariantType(fieldInfo.FieldType), attribute.Name ?? fieldInfo.Name,
                                                                  Variant.GetNativeInstance(Variant.FromObject(attribute.DefaultValue)), attribute.Mode,
                                                                  fieldInfo.FieldType.IsEnum ? fieldInfo.FieldType.GetEnumNames() : null,
                                                                  Marshal.GetFunctionPointerForDelegate(attributeGetter),
                                                                  Marshal.GetFunctionPointerForDelegate(attributeSetter));
                        }
                    }

                    foreach (var propertyInfo in type.GetProperties())
                    {
                        var attributes = (AttributeAttribute[])propertyInfo.GetCustomAttributes(typeof(AttributeAttribute), true);
                        foreach (var attribute in attributes)
                        {
                            AttributeGetterDelegate attributeGetter = serializable =>
                            {
                                var value = propertyInfo.GetValue(GetManagedInstance(serializable));
                                return(Variant.GetNativeInstance(Variant.FromObject(value)));
                            };

                            AttributeSetterDelegate attributeSetter = (serializable, variant) =>
                            {
                                propertyInfo.SetValue(GetManagedInstance(serializable),
                                                      Variant.GetManagedInstance(variant).Object);
                            };

                            _attributeAccessorReferences.Add(GCHandle.Alloc(attributeGetter));
                            _attributeAccessorReferences.Add(GCHandle.Alloc(attributeSetter));

                            var typeHash = new StringHash(type);
                            Urho3D_Serializable_RegisterAttribute(Context.GetNativeInstance(context), typeHash.Hash,
                                                                  Variant.GetVariantType(propertyInfo.PropertyType), attribute.Name ?? propertyInfo.Name,
                                                                  Variant.GetNativeInstance(Variant.FromObject(attribute.DefaultValue)), attribute.Mode,
                                                                  propertyInfo.PropertyType.IsEnum ? propertyInfo.PropertyType.GetEnumNames() : null,
                                                                  Marshal.GetFunctionPointerForDelegate(attributeGetter),
                                                                  Marshal.GetFunctionPointerForDelegate(attributeSetter));
                        }
                    }
                }
            }
        }