Ejemplo n.º 1
0
 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")));
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 7
0
        public void UnsubscribeFromEvent(AObject sender, uint eventType)
        {
            NativeCore.UnsubscribeFromEvent(this, sender, eventType);
            var key = new SenderEventKey(eventType, sender.nativeInstance);

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

            NativeCore.RegisterNativeType(this);
        }
Ejemplo n.º 9
0
        /// <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);
        }
Ejemplo n.º 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
        }
Ejemplo n.º 11
0
        public static bool RunFrame()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            NativeCore.ReleaseExpiredNativeReferences();

            return(atomicsharp_runframe());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 13
0
        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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 18
0
        public void UnsubscribeFromAllEvents()
        {
            if (eventHandlers != null)
            {
                eventHandlers.Clear();
            }

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

            NativeCore.UnsubscribeFromAllEvents(this);
            csi_Atomic_AObject_UnsubscribeFromAllEvents(nativeInstance);
        }
Ejemplo n.º 19
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();
            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();
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 21
0
 public ScriptObject()
 {
     nativeInstance = NativeCore.RegisterNative(csb_Atomic_CSScriptObject_Constructor(), this);
 }
Ejemplo n.º 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;
                    }
                }
            }
        }
Ejemplo n.º 23
0
 public void SubscribeToEvent(uint eventType, EventDelegate eventDelegate)
 {
     NETCore.RegisterNETEventType(eventType);
     EventHandlers[eventType] = eventDelegate;
     NativeCore.SubscribeToEvent(this, eventType);
 }
Ejemplo n.º 24
0
 public void UnsubscribeFromEvent(uint eventType)
 {
     NativeCore.UnsubscribeFromEvent(this, eventType);
     EventHandlers.Remove(eventType);
     NativeEventHandlers.Remove(eventType);
 }
Ejemplo n.º 25
0
 /// <summary>
 ///  Runs a GC collection and waits for any finalizers
 /// </summary>
 public static void RunGC()
 {
     NativeCore.RunGC();
 }
Ejemplo n.º 26
0
 public static string GetCacheStatus() => NativeCore.GetCacheStatus();