public static IComponentMetaclass GetMetaclass(uint componentId)
        {
            if (!Metaclasses.TryGetValue(componentId, out var metaclass))
            {
                throw new ArgumentException($"Can not find Metaclass for SpatialOS component ID {componentId}.");
            }

            return(metaclass);
        }
Exemple #2
0
        static ComponentDatabase()
        {
            Metaclasses = ReflectionUtility.GetNonAbstractTypes(typeof(IComponentMetaclass))
                          .Select(type => (IComponentMetaclass)Activator.CreateInstance(type))
                          .ToDictionary(metaclass => metaclass.ComponentId, metaclass => metaclass);

            ComponentsToIds = Metaclasses.ToDictionary(pair => pair.Value.Data, pair => pair.Key);
            SnapshotsToIds  = Metaclasses.ToDictionary(pair => pair.Value.Snapshot, pair => pair.Key);
        }
Exemple #3
0
        internal void GetOrdinal()
        {
            System.Diagnostics.Debug.Assert(this.Parent.RelationOrdinalBase > -1, "RelationOrdinalBase for type " + Parent.FullName + " not computed");
            Type        t  = Type.GetType(this.Parent.FullName + ", " + this.Parent.AssemblyName);
            IMetaClass2 mc = Metaclasses.GetClass(t);

            Ordinal = this.Parent.RelationOrdinalBase + mc.GetRelationOrdinal(this.FieldName);
            if (Ordinal > 63)
            {
                throw new NDOException(18, "Class " + Parent.FullName + " has too much relations.");
            }
        }
        static ComponentDatabase()
        {
            Metaclasses = ReflectionUtility.GetNonAbstractTypes(typeof(IComponentMetaclass))
                          .Select(type => (IComponentMetaclass)Activator.CreateInstance(type))
                          .ToDictionary(metaclass => metaclass.ComponentId, metaclass => metaclass);

            ComponentsToIds            = Metaclasses.ToDictionary(pair => pair.Value.Data, pair => pair.Key);
            SnapshotsToIds             = Metaclasses.ToDictionary(pair => pair.Value.Snapshot, pair => pair.Key);
            RequestsToCommandMetaclass = Metaclasses
                                         .SelectMany(pair => pair.Value.Commands.Select(cmd => (cmd.Request, cmd)))
                                         .ToDictionary(pair => pair.Request, pair => pair.cmd);
            ReceivedRequestsToCommandMetaclass = Metaclasses
                                                 .SelectMany(pair => pair.Value.Commands.Select(cmd => (cmd.ReceivedRequest, cmd)))
                                                 .ToDictionary(pair => pair.ReceivedRequest, pair => pair.cmd);
        }
Exemple #5
0
        void FixRelations(JToken jObj, IPersistenceCapable e, List <IPersistenceCapable> rootObjects, List <IPersistenceCapable> additionalObjects)
        {
            var      t  = e.GetType();
            FieldMap fm = new FieldMap(t);
            var      mc = Metaclasses.GetClass(t);

            foreach (var fi in fm.Relations)
            {
                var token = jObj[fi.Name];
                if (token == null)
                {
                    continue;
                }
                bool isArray = typeof(IList).IsAssignableFrom(fi.FieldType);
                if (token is JArray jarray)
                {
                    if (isArray)
                    {
                        IList container = (IList)fi.GetValue(e);
                        if (container == null)
                        {
                            throw new NDOException(20002, $"Container object of relation {t.Name}.{fi.Name} is not initialized. Please initialize the field in your class constructor.");
                        }

                        container.Clear();
                        foreach (var relJObj in jarray)
                        {
                            container.Add(DeserializeObject(relJObj));
                        }
                    }
                }
                else
                {
                    if (!isArray)
                    {
                        if (token.Type == JTokenType.Null)
                        {
                            fi.SetValue(e, null);
                        }
                        else
                        {
                            fi.SetValue(e, DeserializeObject(token));
                        }
                    }
                }
            }
        }
Exemple #6
0
        IDictionary <string, object> MakeDict(IPersistenceCapable pc)
        {
            var      dict    = pc.ToDictionary(pm);
            var      shortId = ((IPersistenceCapable)pc).ShortId();
            var      t       = pc.GetType();
            FieldMap fm      = new FieldMap(t);
            var      mc      = Metaclasses.GetClass(t);

            foreach (var fi in fm.Relations)
            {
                var fiName = fi.Name;
                if (((IPersistenceCapable)pc).NDOGetLoadState(mc.GetRelationOrdinal(fiName)))
                {
                    object relationObj = fi.GetValue(pc);
                    if (relationObj is IList list)
                    {
                        List <object> dictList = new List <object>();
                        foreach (IPersistenceCapable relObject in list)
                        {
                            shortId = ((IPersistenceCapable)relObject).ShortId();
                            dictList.Add(new { _oid = shortId });
                        }
                        dict.Add(fiName, dictList);
                    }
                    else
                    {
                        // Hollow object means, that we don't want to transfer the object to the other side.
                        if (relationObj == null || ((IPersistenceCapable)relationObj).NDOObjectState == NDOObjectState.Hollow)
                        {
                            dict.Add(fiName, null);
                        }
                        else
                        {
                            IPersistenceCapable relIPersistenceCapable = (IPersistenceCapable)relationObj;
                            shortId = ((IPersistenceCapable)relIPersistenceCapable).ShortId();
                            dict.Add(fiName, new { _oid = shortId });
                        }
                    }
                }
            }

            return(dict);
        }
Exemple #7
0
        void RecursivelyAddAdditionalObjects(IPersistenceCapable e, List <IPersistenceCapable> rootObjects, List <IPersistenceCapable> additionalObjects)
        {
            var      t  = e.GetType();
            FieldMap fm = new FieldMap(t);
            var      mc = Metaclasses.GetClass(t);

            foreach (var fi in fm.Relations)
            {
                if (((IPersistenceCapable)e).NDOGetLoadState(mc.GetRelationOrdinal(fi.Name)))
                {
                    object relationObj = fi.GetValue(e);
                    if (relationObj is IList list)
                    {
                        List <object> dictList = new List <object>();
                        foreach (IPersistenceCapable relIPersistenceCapable in list)
                        {
                            if (!rootObjects.Contains(relIPersistenceCapable) && !additionalObjects.Contains(relIPersistenceCapable))
                            {
                                additionalObjects.Add(relIPersistenceCapable);
                                RecursivelyAddAdditionalObjects(relIPersistenceCapable, rootObjects, additionalObjects);
                            }
                        }
                    }
                    else
                    {
                        // Hollow object means, that we don't want to transfer the object to the other side.
                        if (relationObj != null && ((IPersistenceCapable)relationObj).NDOObjectState != NDOObjectState.Hollow)
                        {
                            IPersistenceCapable relIPersistenceCapable = (IPersistenceCapable)relationObj;
                            if (!rootObjects.Contains(relIPersistenceCapable) && !additionalObjects.Contains(relIPersistenceCapable))
                            {
                                additionalObjects.Add(relIPersistenceCapable);
                                RecursivelyAddAdditionalObjects(relIPersistenceCapable, rootObjects, additionalObjects);
                            }
                        }
                    }
                }
            }
        }