static private void initSubsystems()
 {
     registerSubsystem(NativeCore.WrapNative <Player> (csb_AtomicEngine_GetSubsystem("Player")));
     registerSubsystem(NativeCore.WrapNative <Graphics> (csb_AtomicEngine_GetSubsystem("Graphics")));
     registerSubsystem(NativeCore.WrapNative <Renderer> (csb_AtomicEngine_GetSubsystem("Renderer")));
     registerSubsystem(NativeCore.WrapNative <ResourceCache> (csb_AtomicEngine_GetSubsystem("ResourceCache")));
 }
Exemple #2
0
        /// <summary>
        ///  Release the handle, which will release the native instance immediately if in main thread
        ///  otherwise, will queue
        /// </summary>
        override protected bool ReleaseHandle()
        {
            if (handle == IntPtr.Zero)
            {
                throw new InvalidOperationException("RefCountedSafeFileHandle.ReleaseHandle - native == IntPtr.Zero");
            }

            // We can be called from Dispose in main thread or from finalizers, which aren't in the main thread
            if (AtomicNET.IsMainThread())
            {
                NativeCore.csi_AtomicEngine_ReleaseRef(handle);
            }
            else
            {
                // We're in a finalizer, need to add to queue to release when
                // back in main thread
                lock (RefCounted.refCountedFinalizerQueue)
                {
                    RefCounted.refCountedFinalizerQueue.Add(handle);
                }
            }

            handle = IntPtr.Zero;

            return(true);
        }
Exemple #3
0
        public static T GetSubsystem <T>() where T : AObject
        {
            AObject subsystem = null;
            var     type      = typeof(T);

            // See if already registered (or a managed subsystem which will only be in the dictionary)
            if (subSystems.TryGetValue(type, out subsystem))
            {
                return((T)subsystem);
            }

            // If we're a managed type, throw error
            if (!NativeCore.IsNativeType(type))
            {
                throw new System.InvalidOperationException($"AtomicNET.GetSubsystem<T> - Attempting to get null subsystem: {type.Name}");
            }

            // Look up possible native subsystem
            subsystem = AtomicNET.Context.GetSubsystem(type.Name);

            // If we didn't find one, this is an error
            if (subsystem == null)
            {
                throw new System.InvalidOperationException($"AtomicNET.GetSubsystem<T> - Attempting to get null subsystem: {type.Name}");
            }

            // register the subsystem
            RegisterSubsystem(subsystem);

            return((T)subsystem);
        }
Exemple #4
0
        void HandleCSComponentLoad(CSComponentLoadEvent e)
        {
            var scriptMap = e.scriptMap;

            // Get the NativeInstance as an IntPtr, otherwise we would be wrapping as a CSComponent
            IntPtr csnative = scriptMap.GetVoidPtr("NativeInstance");

            IntPtr fieldValues = IntPtr.Zero;

            if (scriptMap.Contains("FieldValues"))
            {
                fieldValues = scriptMap.GetVoidPtr("FieldValues");
            }

            CSComponentInfo csinfo;

            if (!CSComponentCore.componentCache.TryGetValue(e.ClassName, out csinfo))
            {
                return;
            }

            NativeCore.NativeContructorOverride = csnative;
            var component = (CSComponent)Activator.CreateInstance(csinfo.Type);

            NativeCore.VerifyNativeContructorOverrideConsumed();

            if (fieldValues != IntPtr.Zero)
            {
                csinfo.ApplyFieldValues(component, fieldValues);
            }

            AddCSComponent(component);
        }
Exemple #5
0
        // CSComponents can be instantiated from a serialized scene or from within managed code
        protected CSComponent()
        {
            if (nativeLoadOverrideValidate != IntPtr.Zero)
            {
                // When loading CSComponents from a scene, many issues circumvented by not allowing additional components
                // to be created (on the Node) during serialization, so this is an error state
                throw new InvalidOperationException($"CSComponent() - Recursive CSComponent instantiation in default constructor during load type: { GetType().Name} ");
            }

            if (nativeLoadOverride == IntPtr.Zero)
            {
                // We are being "new'd" in script
                nativeInstance = csi_Atomic_CSComponent_Constructor();
            }
            else
            {
                // We are loading from a serialized CSComponent
                nativeInstance = nativeLoadOverride;

                // validation bookkeeping
                nativeLoadOverrideValidate = nativeLoadOverride;
                nativeLoadOverride         = IntPtr.Zero;
            }

            NativeCore.RegisterNative(nativeInstance, this);
        }
Exemple #6
0
        public void SubscribeToEvent <T>(AObject sender, NativeEventDelegate <T> eventDelegate) where T : NativeEventData
        {
            uint eventType = NativeEvents.GetEventID <T>();

            if (eventType == 0)
            {
                throw new InvalidOperationException("AObject.SubscribeToEvent<T>(EventDelegate<T> eventDelegate) - Unknown native event id");
            }

            // Move this
            NETCore.RegisterNETEventType(eventType);

            NativeEventHandlers[eventType] = (eventData) =>
            {
                eventDelegate((T)eventData);
            };

            if (sender != null)
            {
                NativeCore.SubscribeToEvent(this, sender, eventType);
            }
            else
            {
                NativeCore.SubscribeToEvent(this, eventType);
            }
        }
Exemple #7
0
        public void UnsubscribeFromEvent(AObject sender, uint eventType)
        {
            NativeCore.UnsubscribeFromEvent(this, sender, eventType);
            var key = new SenderEventKey(eventType, sender.nativeInstance);

            SenderEventHandlers.Remove(key);
        }
        public NativeType(IntPtr nativeClassID, Type type, Func <IntPtr, RefCounted> managedConstructor)
        {
            this.nativeClassID      = nativeClassID;
            this.type               = type;
            this.managedConstructor = managedConstructor;

            NativeCore.RegisterNativeType(this);
        }
        /// <summary>
        ///  Release all resources. When called with the force flag false, releases all currently unused resources.
        /// </summary>
        public void ReleaseAllResources(bool force = false)
        {
            // We need to GC before calling native ResourceCache::ReleaseAllResources, to ensure all managed resource references are down
            // otherwise, the cache will hold onto the resource
            NativeCore.RunGC();

            csi_Atomic_ResourceCache_ReleaseAllResources(nativeInstance, force);
        }
Exemple #10
0
        public static void Initialize()
        {
            // Atomic Modules
            CoreModule.Initialize();
            MathModule.Initialize();
            EngineModule.Initialize();
            InputModule.Initialize();
            IOModule.Initialize();
            ResourceModule.Initialize();
            AudioModule.Initialize();
            GraphicsModule.Initialize();
            SceneModule.Initialize();
            Atomic2DModule.Initialize();
            NavigationModule.Initialize();
            NetworkModule.Initialize();
            PhysicsModule.Initialize();
            EnvironmentModule.Initialize();
            UIModule.Initialize();

#if ATOMIC_DESKTOP
            IPCModule.Initialize();
#endif

            AtomicAppModule.Initialize();
            ScriptModule.Initialize();

            AtomicNETScriptModule.Initialize();
            AtomicNETNativeModule.Initialize();

            PlayerModule.Initialize();

            coreDelegates = new CoreDelegates();
            coreDelegates.eventDispatch  = NativeCore.EventDispatch;
            coreDelegates.updateDispatch = NativeCore.UpdateDispatch;

            IntPtr coreptr = csi_Atomic_NETCore_Initialize(ref coreDelegates);

            NETCore core = (coreptr == IntPtr.Zero ? null : NativeCore.WrapNative <NETCore>(coreptr));

            if (core != null)
            {
                AtomicNET.RegisterSubsystem("NETCore", core);
            }

            context = core.Context;

            NativeCore.Initialize();
            CSComponentCore.Initialize();

#if ATOMIC_DESKTOP
            string[] arguments = Environment.GetCommandLineArgs();
            foreach (string arg in arguments)
            {
                AppBase.AddArgument(arg);
            }
#endif
        }
        public static bool RunFrame()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            NativeCore.ReleaseExpiredNativeReferences();

            return(atomicsharp_runframe());
        }
Exemple #12
0
        public RefCountedSafeFileHandle(IntPtr handle, bool ownsHandle = true)
            : base(handle, ownsHandle)
        {
            if (handle == IntPtr.Zero)
            {
                throw new InvalidOperationException("RefCountedSafeFileHandle - native == IntPtr.Zero");
            }

            NativeCore.csi_AtomicEngine_AddRef(handle);
        }
        public T Get <T>(string key) where T : RefCounted
        {
            checkValid();

            // TODO: safe case

            IntPtr r = csb_Atomic_VariantMap_GetInstance(native, key);

            return(r == IntPtr.Zero ? null :  NativeCore.WrapNative <T> (r));
        }
Exemple #14
0
        public void SubscribeToEvent(AObject sender, uint eventType, EventDelegate eventDelegate)
        {
            if (sender == null)
            {
                throw new InvalidOperationException("AObject.SubscribeToEvent - trying to subscribe to events from a null object");
            }

            // Move this
            NETCore.RegisterNETEventType(eventType);
            NativeCore.SubscribeToEvent(this, sender, eventType);
        }
Exemple #15
0
        /// <summary>
        /// Releases RefCounted instances which were finalized (which can happen on any thread)
        /// </summary>
        static internal void ReleaseFinalized()
        {
            lock (refCountedFinalizerQueue)
            {
                foreach (var native in refCountedFinalizerQueue)
                {
                    NativeCore.RemoveNative(native);
                    NativeCore.csi_AtomicEngine_ReleaseRef(native);
                }

                refCountedFinalizerQueue.Clear();
            }
        }
Exemple #16
0
        public void SubscribeToEvent(AObject sender, uint eventType, SenderEventDelegate eventDelegate)
        {
            if (sender == null)
            {
                throw new InvalidOperationException("AObject.SubscribeToEvent - trying to subscribe to events from a null object");
            }

            NETCore.RegisterNETEventType(eventType);
            var key = new SenderEventKey(eventType, sender.nativeInstance);

            SenderEventHandlers[key] = eventDelegate;
            NativeCore.SubscribeToEvent(this, sender, eventType);
        }
Exemple #17
0
        protected virtual void Dispose(bool disposing)
        {
            disposed = true;

            if (refHandle != null && !refHandle.IsInvalid)
            {
                NativeCore.RemoveNative(nativeInstance);

                // Free the handle
                refHandle.Dispose();
            }

            nativeInstance = IntPtr.Zero;
        }
Exemple #18
0
        public void UnsubscribeFromAllEvents()
        {
            if (eventHandlers != null)
            {
                eventHandlers.Clear();
            }

            if (nativeHandlers != null)
            {
                nativeHandlers.Clear();
            }

            NativeCore.UnsubscribeFromAllEvents(this);
            csi_Atomic_AObject_UnsubscribeFromAllEvents(nativeInstance);
        }
        public static void Initialize()
        {
            // Atomic Modules
            CoreModule.Initialize();
            MathModule.Initialize();
            EngineModule.Initialize();
            InputModule.Initialize();
            IOModule.Initialize();
            ResourceModule.Initialize();
            AudioModule.Initialize();
            GraphicsModule.Initialize();
            SceneModule.Initialize();
            Atomic2DModule.Initialize();
            Atomic3DModule.Initialize();
            NavigationModule.Initialize();
            NetworkModule.Initialize();
            PhysicsModule.Initialize();
            EnvironmentModule.Initialize();
            UIModule.Initialize();
            IPCModule.Initialize();
            AtomicAppModule.Initialize();
            ScriptModule.Initialize();

            AtomicNETScriptModule.Initialize();
            AtomicNETNativeModule.Initialize();

            PlayerModule.Initialize();

            coreDelegates = new CoreDelegates();
            coreDelegates.eventDispatch  = NativeCore.EventDispatch;
            coreDelegates.updateDispatch = NativeCore.UpdateDispatch;

            IntPtr coreptr = csb_Atomic_NETCore_Initialize(ref coreDelegates);

            NETCore core = (coreptr == IntPtr.Zero ? null : NativeCore.WrapNative <NETCore>(coreptr));

            if (core != null)
            {
                AtomicNET.RegisterSubsystem("NETCore", core);
            }

            context = core.Context;

            NativeCore.Initialize();
            CSComponentCore.Initialize();
        }
Exemple #20
0
        void HandleComponentLoad(uint eventType, ScriptVariantMap eventData)
        {
            var assemblyPath = eventData["AssemblyPath"];

            var    className   = eventData["ClassName"];
            IntPtr csnative    = eventData.GetVoidPtr("NativeInstance");
            IntPtr fieldValues = IntPtr.Zero;

            if (eventData.Contains("FieldValues"))
            {
                fieldValues = eventData.GetVoidPtr("FieldValues");
            }

            Dictionary <string, CSComponentInfo> assemblyTypes = null;

            if (!componentCache.TryGetValue(assemblyPath, out assemblyTypes))
            {
                return;
            }

            CSComponentInfo csinfo;

            if (!assemblyTypes.TryGetValue(className, out csinfo))
            {
                return;
            }

            NativeCore.NativeContructorOverride = csnative;
            var component = (CSComponent)Activator.CreateInstance(csinfo.Type);

            NativeCore.VerifyNativeContructorOverrideConsumed();

            if (fieldValues != IntPtr.Zero)
            {
                csinfo.ApplyFieldValues(component, fieldValues);
            }

            csinfo.RegisterInstance(component);
        }
 public ScriptObject()
 {
     nativeInstance = NativeCore.RegisterNative(csb_Atomic_CSScriptObject_Constructor(), this);
 }
Exemple #22
0
        // This will need to be optimized
        public static void CSComponentApplyFields(IntPtr componentPtr, IntPtr fieldMapPtr)
        {
            NETVariantMap fieldMap  = NativeCore.WrapNative <NETVariantMap>(fieldMapPtr);;
            CSComponent   component = NativeCore.WrapNative <CSComponent>(componentPtr);;

            if (fieldMap == null || component == null)
            {
                return;
            }

            FieldInfo[] fields = componentClassFields[component.GetType()];

            foreach (var field in fields)
            {
                if (fieldMap.Contains(field.Name))
                {
                    //Console.WriteLine("Applying: {0} {1}", field.Name, field.FieldType.Name);

                    var fieldType = field.FieldType;

                    if (fieldType.IsEnum)
                    {
                        field.SetValue(component, fieldMap.GetInt(field.Name));
                        continue;
                    }

                    switch (Type.GetTypeCode(fieldType))
                    {
                    case TypeCode.Boolean:
                        field.SetValue(component, fieldMap.GetBool(field.Name));
                        break;

                    case TypeCode.Int32:
                        field.SetValue(component, fieldMap.GetInt(field.Name));
                        break;

                    case TypeCode.Single:
                        field.SetValue(component, fieldMap.GetFloat(field.Name));
                        break;

                    case TypeCode.String:
                        field.SetValue(component, fieldMap.GetString(field.Name));
                        break;

                    default:

                        if (fieldType == typeof(Vector3))
                        {
                            field.SetValue(component, fieldMap.GetVector3(field.Name));
                        }
                        else if (fieldType == typeof(Quaternion))
                        {
                            field.SetValue(component, fieldMap.GetQuaternion(field.Name));
                        }
                        else if (fieldType.IsSubclassOf(typeof(Resource)))
                        {
                            field.SetValue(component, fieldMap.GetResourceFromRef(field.Name));
                        }
                        else if (fieldType.IsSubclassOf(typeof(RefCounted)))
                        {
                            field.SetValue(component, fieldMap.GetPtr(field.Name));
                        }

                        break;
                    }
                }
            }
        }
Exemple #23
0
 public void SubscribeToEvent(uint eventType, EventDelegate eventDelegate)
 {
     NETCore.RegisterNETEventType(eventType);
     EventHandlers[eventType] = eventDelegate;
     NativeCore.SubscribeToEvent(this, eventType);
 }
Exemple #24
0
 public void UnsubscribeFromEvent(uint eventType)
 {
     NativeCore.UnsubscribeFromEvent(this, eventType);
     EventHandlers.Remove(eventType);
     NativeEventHandlers.Remove(eventType);
 }
Exemple #25
0
 /// <summary>
 ///  Runs a GC collection and waits for any finalizers
 /// </summary>
 public static void RunGC()
 {
     NativeCore.RunGC();
 }
Exemple #26
0
 public static string GetCacheStatus() => NativeCore.GetCacheStatus();